1 /* $Id: paltex.c,v 1.5 2000/10/04 18:15:39 brianp Exp $ */
4 * Paletted texture demo. Written by Brian Paul.
5 * This program is in the public domain.
11 #define GL_GLEXT_LEGACY
15 static float Rot
= 0.0;
16 static GLboolean Anim
= 1;
19 static void Idle( void )
21 float t
= glutGet(GLUT_ELAPSED_TIME
) * 0.001; /* in seconds */
22 Rot
= t
* 360 / 4; /* 1 rotation per 4 seconds */
27 static void Display( void )
29 /* draw background gradient */
30 glDisable(GL_TEXTURE_2D
);
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);
39 glRotatef(Rot
, 0, 0, 1);
41 glEnable(GL_TEXTURE_2D
);
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);
55 static void Reshape( int width
, int height
)
57 glViewport( 0, 0, width
, height
);
58 glMatrixMode( GL_PROJECTION
);
60 glOrtho( -1.5, 1.5, -1.0, 1.0, -1.0, 1.0 );
61 glMatrixMode( GL_MODELVIEW
);
66 static void Key( unsigned char key
, int x
, int y
)
89 static void Init( void )
93 static char texture
[HEIGHT
][WIDTH
] = {
97 " M M M EEEE SS A A ",
100 " M M EEEE SSS A A ",
103 GLubyte table
[256][4];
105 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
106 printf("Sorry, GL_EXT_paletted_texture not supported\n");
110 /* load the color table for each texel-index */
111 memset(table
, 0, 256*4);
133 #ifdef GL_EXT_paletted_texture
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
);
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 */
153 glTexImage2D(GL_TEXTURE_2D
, /* target */
155 GL_COLOR_INDEX8_EXT
, /* internal format */
156 WIDTH
, HEIGHT
, /* width, height */
158 GL_COLOR_INDEX
, /* texture format */
159 GL_UNSIGNED_BYTE
, /* texture type */
160 texture
); /* teh texture */
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
);
169 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
179 static void Init2( void )
183 static char texture
[HEIGHT
][WIDTH
];
184 GLubyte table
[256][4];
187 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
188 printf("Sorry, GL_EXT_paletted_texture not supported\n");
192 for (j
= 0; j
< HEIGHT
; j
++) {
193 for (i
= 0; i
< WIDTH
; i
++) {
198 for (i
= 0; i
< 255; i
++) {
205 #ifdef GL_EXT_paletted_texture
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
);
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 */
225 glTexImage2D(GL_TEXTURE_2D
, /* target */
227 GL_COLOR_INDEX8_EXT
, /* internal format */
228 WIDTH
, HEIGHT
, /* width, height */
230 GL_COLOR_INDEX
, /* texture format */
231 GL_UNSIGNED_BYTE
, /* texture type */
232 texture
); /* teh texture */
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
);
241 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
245 int main( int argc
, char *argv
[] )
247 glutInit( &argc
, argv
);
248 glutInitWindowPosition( 0, 0 );
249 glutInitWindowSize( 400, 300 );
251 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
253 glutCreateWindow(argv
[0]);
257 glutReshapeFunc( Reshape
);
258 glutKeyboardFunc( Key
);
259 glutDisplayFunc( Display
);
261 glutIdleFunc( Idle
);