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