1 /* $Id: paltex.c,v 1.6 2000/10/05 07:17:43 joukj Exp $ */
4 * Paletted texture demo. Written by Brian Paul.
5 * This program is in the public domain.
12 #define GL_GLEXT_LEGACY
16 static float Rot
= 0.0;
17 static GLboolean Anim
= 1;
20 static void Idle( void )
22 float t
= glutGet(GLUT_ELAPSED_TIME
) * 0.001; /* in seconds */
23 Rot
= t
* 360 / 4; /* 1 rotation per 4 seconds */
28 static void Display( void )
30 /* draw background gradient */
31 glDisable(GL_TEXTURE_2D
);
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);
40 glRotatef(Rot
, 0, 0, 1);
42 glEnable(GL_TEXTURE_2D
);
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);
56 static void Reshape( int width
, int height
)
58 glViewport( 0, 0, width
, height
);
59 glMatrixMode( GL_PROJECTION
);
61 glOrtho( -1.5, 1.5, -1.0, 1.0, -1.0, 1.0 );
62 glMatrixMode( GL_MODELVIEW
);
67 static void Key( unsigned char key
, int x
, int y
)
90 static void Init( void )
94 static char texture
[HEIGHT
][WIDTH
] = {
98 " M M M EEEE SS A A ",
101 " M M EEEE SSS A A ",
104 GLubyte table
[256][4];
106 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
107 printf("Sorry, GL_EXT_paletted_texture not supported\n");
111 /* load the color table for each texel-index */
112 memset(table
, 0, 256*4);
134 #ifdef GL_EXT_paletted_texture
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
);
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 */
154 glTexImage2D(GL_TEXTURE_2D
, /* target */
156 GL_COLOR_INDEX8_EXT
, /* internal format */
157 WIDTH
, HEIGHT
, /* width, height */
159 GL_COLOR_INDEX
, /* texture format */
160 GL_UNSIGNED_BYTE
, /* texture type */
161 texture
); /* teh texture */
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
);
170 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
180 static void Init2( void )
184 static char texture
[HEIGHT
][WIDTH
];
185 GLubyte table
[256][4];
188 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
189 printf("Sorry, GL_EXT_paletted_texture not supported\n");
193 for (j
= 0; j
< HEIGHT
; j
++) {
194 for (i
= 0; i
< WIDTH
; i
++) {
199 for (i
= 0; i
< 255; i
++) {
206 #ifdef GL_EXT_paletted_texture
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
);
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 */
226 glTexImage2D(GL_TEXTURE_2D
, /* target */
228 GL_COLOR_INDEX8_EXT
, /* internal format */
229 WIDTH
, HEIGHT
, /* width, height */
231 GL_COLOR_INDEX
, /* texture format */
232 GL_UNSIGNED_BYTE
, /* texture type */
233 texture
); /* teh texture */
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
);
242 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
246 int main( int argc
, char *argv
[] )
248 glutInit( &argc
, argv
);
249 glutInitWindowPosition( 0, 0 );
250 glutInitWindowSize( 400, 300 );
252 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
254 glutCreateWindow(argv
[0]);
258 glutReshapeFunc( Reshape
);
259 glutKeyboardFunc( Key
);
260 glutDisplayFunc( Display
);
262 glutIdleFunc( Idle
);