Use correct PV when clipping.
[mesa.git] / progs / demos / paltex.c
1 /* $Id: paltex.c,v 1.6 2000/10/05 07:17:43 joukj Exp $ */
2
3 /*
4 * Paletted texture demo. Written by Brian Paul.
5 * This program is in the public domain.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <math.h>
11 #include <string.h>
12 #define GL_GLEXT_LEGACY
13 #include <GL/glut.h>
14
15
16 static float Rot = 0.0;
17 static GLboolean Anim = 1;
18
19
20 static void Idle( void )
21 {
22 float t = glutGet(GLUT_ELAPSED_TIME) * 0.001; /* in seconds */
23 Rot = t * 360 / 4; /* 1 rotation per 4 seconds */
24 glutPostRedisplay();
25 }
26
27
28 static void Display( void )
29 {
30 /* draw background gradient */
31 glDisable(GL_TEXTURE_2D);
32 glBegin(GL_POLYGON);
33 glColor3f(1.0, 0.0, 0.2); glVertex2f(-1.5, -1.0);
34 glColor3f(1.0, 0.0, 0.2); glVertex2f( 1.5, -1.0);
35 glColor3f(0.0, 0.0, 1.0); glVertex2f( 1.5, 1.0);
36 glColor3f(0.0, 0.0, 1.0); glVertex2f(-1.5, 1.0);
37 glEnd();
38
39 glPushMatrix();
40 glRotatef(Rot, 0, 0, 1);
41
42 glEnable(GL_TEXTURE_2D);
43 glBegin(GL_POLYGON);
44 glTexCoord2f(0, 1); glVertex2f(-1, -0.5);
45 glTexCoord2f(1, 1); glVertex2f( 1, -0.5);
46 glTexCoord2f(1, 0); glVertex2f( 1, 0.5);
47 glTexCoord2f(0, 0); glVertex2f(-1, 0.5);
48 glEnd();
49
50 glPopMatrix();
51
52 glutSwapBuffers();
53 }
54
55
56 static void Reshape( int width, int height )
57 {
58 glViewport( 0, 0, width, height );
59 glMatrixMode( GL_PROJECTION );
60 glLoadIdentity();
61 glOrtho( -1.5, 1.5, -1.0, 1.0, -1.0, 1.0 );
62 glMatrixMode( GL_MODELVIEW );
63 glLoadIdentity();
64 }
65
66
67 static void Key( unsigned char key, int x, int y )
68 {
69 (void) x;
70 (void) y;
71 switch (key) {
72 case 27:
73 exit(0);
74 break;
75 case 's':
76 Rot += 0.5;
77 break;
78 case ' ':
79 Anim = !Anim;
80 if (Anim)
81 glutIdleFunc( Idle );
82 else
83 glutIdleFunc( NULL );
84 break;
85 }
86 glutPostRedisplay();
87 }
88
89
90 static void Init( void )
91 {
92 #define HEIGHT 8
93 #define WIDTH 32
94 static char texture[HEIGHT][WIDTH] = {
95 " ",
96 " MMM EEEE SSS AAA ",
97 " M M M E S S A A ",
98 " M M M EEEE SS A A ",
99 " M M M E SS AAAAA ",
100 " M M E S S A A ",
101 " M M EEEE SSS A A ",
102 " "
103 };
104 GLubyte table[256][4];
105
106 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
107 printf("Sorry, GL_EXT_paletted_texture not supported\n");
108 exit(0);
109 }
110
111 /* load the color table for each texel-index */
112 memset(table, 0, 256*4);
113 table[' '][0] = 255;
114 table[' '][1] = 255;
115 table[' '][2] = 255;
116 table[' '][3] = 64;
117 table['M'][0] = 255;
118 table['M'][1] = 0;
119 table['M'][2] = 0;
120 table['M'][3] = 255;
121 table['E'][0] = 0;
122 table['E'][1] = 255;
123 table['E'][2] = 0;
124 table['E'][3] = 255;
125 table['S'][0] = 0;
126 table['S'][1] = 0;
127 table['S'][2] = 255;
128 table['S'][3] = 255;
129 table['A'][0] = 255;
130 table['A'][1] = 255;
131 table['A'][2] = 0;
132 table['A'][3] = 255;
133
134 #ifdef GL_EXT_paletted_texture
135
136 #if defined(GL_EXT_shared_texture_palette) && defined(USE_SHARED_PALETTE)
137 printf("Using shared palette\n");
138 glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, /* target */
139 GL_RGBA, /* internal format */
140 256, /* table size */
141 GL_RGBA, /* table format */
142 GL_UNSIGNED_BYTE, /* table type */
143 table); /* the color table */
144 glEnable(GL_SHARED_TEXTURE_PALETTE_EXT);
145 #else
146 glColorTableEXT(GL_TEXTURE_2D, /* target */
147 GL_RGBA, /* internal format */
148 256, /* table size */
149 GL_RGBA, /* table format */
150 GL_UNSIGNED_BYTE, /* table type */
151 table); /* the color table */
152 #endif
153
154 glTexImage2D(GL_TEXTURE_2D, /* target */
155 0, /* level */
156 GL_COLOR_INDEX8_EXT, /* internal format */
157 WIDTH, HEIGHT, /* width, height */
158 0, /* border */
159 GL_COLOR_INDEX, /* texture format */
160 GL_UNSIGNED_BYTE, /* texture type */
161 texture); /* teh texture */
162 #endif
163
164 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
165 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
166 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
167 glEnable(GL_TEXTURE_2D);
168
169 glEnable(GL_BLEND);
170 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
171 #undef HEIGHT
172 #undef WIDTH
173 }
174
175
176
177 /*
178 * Color ramp test
179 */
180 static void Init2( void )
181 {
182 #define HEIGHT 32
183 #define WIDTH 256
184 static char texture[HEIGHT][WIDTH];
185 GLubyte table[256][4];
186 int i, j;
187
188 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
189 printf("Sorry, GL_EXT_paletted_texture not supported\n");
190 exit(0);
191 }
192
193 for (j = 0; j < HEIGHT; j++) {
194 for (i = 0; i < WIDTH; i++) {
195 texture[j][i] = i;
196 }
197 }
198
199 for (i = 0; i < 255; i++) {
200 table[i][0] = i;
201 table[i][1] = 0;
202 table[i][2] = 0;
203 table[i][3] = 255;
204 }
205
206 #ifdef GL_EXT_paletted_texture
207
208 #if defined(GL_EXT_shared_texture_palette) && defined(USE_SHARED_PALETTE)
209 printf("Using shared palette\n");
210 glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, /* target */
211 GL_RGBA, /* internal format */
212 256, /* table size */
213 GL_RGBA, /* table format */
214 GL_UNSIGNED_BYTE, /* table type */
215 table); /* the color table */
216 glEnable(GL_SHARED_TEXTURE_PALETTE_EXT);
217 #else
218 glColorTableEXT(GL_TEXTURE_2D, /* target */
219 GL_RGBA, /* internal format */
220 256, /* table size */
221 GL_RGBA, /* table format */
222 GL_UNSIGNED_BYTE, /* table type */
223 table); /* the color table */
224 #endif
225
226 glTexImage2D(GL_TEXTURE_2D, /* target */
227 0, /* level */
228 GL_COLOR_INDEX8_EXT, /* internal format */
229 WIDTH, HEIGHT, /* width, height */
230 0, /* border */
231 GL_COLOR_INDEX, /* texture format */
232 GL_UNSIGNED_BYTE, /* texture type */
233 texture); /* teh texture */
234 #endif
235
236 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
237 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
238 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
239 glEnable(GL_TEXTURE_2D);
240
241 glEnable(GL_BLEND);
242 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
243 }
244
245
246 int main( int argc, char *argv[] )
247 {
248 glutInit( &argc, argv );
249 glutInitWindowPosition( 0, 0 );
250 glutInitWindowSize( 400, 300 );
251
252 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
253
254 glutCreateWindow(argv[0]);
255
256 Init();
257
258 glutReshapeFunc( Reshape );
259 glutKeyboardFunc( Key );
260 glutDisplayFunc( Display );
261 if (Anim)
262 glutIdleFunc( Idle );
263
264 glutMainLoop();
265 return 0;
266 }