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