1 /* $Id: paltex.c,v 1.9 2002/07/22 13:45:31 brianp Exp $ */
4 * Paletted texture demo. Written by Brian Paul.
5 * This program is in the public domain.
15 #define GL_GLEXT_LEGACY
19 static float Rot
= 0.0;
20 static GLboolean Anim
= 1;
23 static void Idle( void )
25 float t
= glutGet(GLUT_ELAPSED_TIME
) * 0.001; /* in seconds */
26 Rot
= t
* 360 / 4; /* 1 rotation per 4 seconds */
31 static void Display( void )
33 /* draw background gradient */
34 glDisable(GL_TEXTURE_2D
);
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);
43 glRotatef(Rot
, 0, 0, 1);
45 glEnable(GL_TEXTURE_2D
);
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);
59 static void Reshape( int width
, int height
)
61 glViewport( 0, 0, width
, height
);
62 glMatrixMode( GL_PROJECTION
);
64 glOrtho( -1.5, 1.5, -1.0, 1.0, -1.0, 1.0 );
65 glMatrixMode( GL_MODELVIEW
);
70 static void Key( unsigned char key
, int x
, int y
)
93 static void Init( void )
97 /* 257 = HEIGHT * WIDTH + 1 (for trailing '\0') */
98 static char texture
[257] = {"\
108 GLubyte table
[256][4];
110 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
111 printf("Sorry, GL_EXT_paletted_texture not supported\n");
115 /* load the color table for each texel-index */
116 memset(table
, 0, 256*4);
138 #ifdef GL_EXT_paletted_texture
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
);
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 */
158 glTexImage2D(GL_TEXTURE_2D
, /* target */
160 GL_COLOR_INDEX8_EXT
, /* internal format */
161 WIDTH
, HEIGHT
, /* width, height */
163 GL_COLOR_INDEX
, /* texture format */
164 GL_UNSIGNED_BYTE
, /* texture type */
165 texture
); /* the texture */
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
);
174 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
184 static void Init2( void )
188 static char texture
[HEIGHT
][WIDTH
];
189 GLubyte table
[256][4];
192 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
193 printf("Sorry, GL_EXT_paletted_texture not supported\n");
197 for (j
= 0; j
< HEIGHT
; j
++) {
198 for (i
= 0; i
< WIDTH
; i
++) {
203 for (i
= 0; i
< 255; i
++) {
210 #ifdef GL_EXT_paletted_texture
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
);
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 */
230 glTexImage2D(GL_TEXTURE_2D
, /* target */
232 GL_COLOR_INDEX8_EXT
, /* internal format */
233 WIDTH
, HEIGHT
, /* width, height */
235 GL_COLOR_INDEX
, /* texture format */
236 GL_UNSIGNED_BYTE
, /* texture type */
237 texture
); /* teh texture */
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
);
246 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
250 int main( int argc
, char *argv
[] )
252 glutInit( &argc
, argv
);
253 glutInitWindowPosition( 0, 0 );
254 glutInitWindowSize( 400, 300 );
256 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
258 glutCreateWindow(argv
[0]);
262 glutReshapeFunc( Reshape
);
263 glutKeyboardFunc( Key
);
264 glutDisplayFunc( Display
);
266 glutIdleFunc( Idle
);