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