3 * Paletted texture demo. Written by Brian Paul.
4 * This program is in the public domain.
17 static float Rot
= 0.0;
18 static GLboolean Anim
= 1;
21 static void Idle( void )
23 float t
= glutGet(GLUT_ELAPSED_TIME
) * 0.001; /* in seconds */
24 Rot
= t
* 360 / 4; /* 1 rotation per 4 seconds */
29 static void Display( void )
31 /* draw background gradient */
32 glDisable(GL_TEXTURE_2D
);
34 glColor3f(1.0, 0.0, 0.2); glVertex2f(-1.5, -1.0);
35 glColor3f(1.0, 0.0, 0.2); glVertex2f( 1.5, -1.0);
36 glColor3f(0.0, 0.0, 1.0); glVertex2f( 1.5, 1.0);
37 glColor3f(0.0, 0.0, 1.0); glVertex2f(-1.5, 1.0);
41 glRotatef(Rot
, 0, 0, 1);
43 glEnable(GL_TEXTURE_2D
);
45 glTexCoord2f(0, 1); glVertex2f(-1, -0.5);
46 glTexCoord2f(1, 1); glVertex2f( 1, -0.5);
47 glTexCoord2f(1, 0); glVertex2f( 1, 0.5);
48 glTexCoord2f(0, 0); glVertex2f(-1, 0.5);
57 static void Reshape( int width
, int height
)
59 glViewport( 0, 0, width
, height
);
60 glMatrixMode( GL_PROJECTION
);
62 glOrtho( -1.5, 1.5, -1.0, 1.0, -1.0, 1.0 );
63 glMatrixMode( GL_MODELVIEW
);
68 static void Key( unsigned char key
, int x
, int y
)
91 static void Init( void )
95 /* 257 = HEIGHT * WIDTH + 1 (for trailing '\0') */
96 static char texture
[257] = {"\
106 GLubyte table
[256][4];
108 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
109 printf("Sorry, GL_EXT_paletted_texture not supported\n");
113 /* load the color table for each texel-index */
114 memset(table
, 0, 256*4);
136 #ifdef GL_EXT_paletted_texture
138 #if defined(GL_EXT_shared_texture_palette) && defined(USE_SHARED_PALETTE)
139 printf("Using shared palette\n");
140 glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT
, /* target */
141 GL_RGBA
, /* internal format */
142 256, /* table size */
143 GL_RGBA
, /* table format */
144 GL_UNSIGNED_BYTE
, /* table type */
145 table
); /* the color table */
146 glEnable(GL_SHARED_TEXTURE_PALETTE_EXT
);
148 glColorTableEXT(GL_TEXTURE_2D
, /* target */
149 GL_RGBA
, /* internal format */
150 256, /* table size */
151 GL_RGBA
, /* table format */
152 GL_UNSIGNED_BYTE
, /* table type */
153 table
); /* the color table */
156 glTexImage2D(GL_TEXTURE_2D
, /* target */
158 GL_COLOR_INDEX8_EXT
, /* internal format */
159 WIDTH
, HEIGHT
, /* width, height */
161 GL_COLOR_INDEX
, /* texture format */
162 GL_UNSIGNED_BYTE
, /* texture type */
163 texture
); /* the texture */
166 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
167 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
168 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
169 glEnable(GL_TEXTURE_2D
);
172 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
182 static void Init2( void )
186 static char texture
[HEIGHT
][WIDTH
];
187 GLubyte table
[256][4];
190 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
191 printf("Sorry, GL_EXT_paletted_texture not supported\n");
195 for (j
= 0; j
< HEIGHT
; j
++) {
196 for (i
= 0; i
< WIDTH
; i
++) {
201 for (i
= 0; i
< 255; i
++) {
208 #ifdef GL_EXT_paletted_texture
210 #if defined(GL_EXT_shared_texture_palette) && defined(USE_SHARED_PALETTE)
211 printf("Using shared palette\n");
212 glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT
, /* target */
213 GL_RGBA
, /* internal format */
214 256, /* table size */
215 GL_RGBA
, /* table format */
216 GL_UNSIGNED_BYTE
, /* table type */
217 table
); /* the color table */
218 glEnable(GL_SHARED_TEXTURE_PALETTE_EXT
);
220 glColorTableEXT(GL_TEXTURE_2D
, /* target */
221 GL_RGBA
, /* internal format */
222 256, /* table size */
223 GL_RGBA
, /* table format */
224 GL_UNSIGNED_BYTE
, /* table type */
225 table
); /* the color table */
228 glTexImage2D(GL_TEXTURE_2D
, /* target */
230 GL_COLOR_INDEX8_EXT
, /* internal format */
231 WIDTH
, HEIGHT
, /* width, height */
233 GL_COLOR_INDEX
, /* texture format */
234 GL_UNSIGNED_BYTE
, /* texture type */
235 texture
); /* teh texture */
238 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
239 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
240 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
241 glEnable(GL_TEXTURE_2D
);
244 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
248 int main( int argc
, char *argv
[] )
250 glutInit( &argc
, argv
);
251 glutInitWindowPosition( 0, 0 );
252 glutInitWindowSize( 400, 300 );
254 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
256 glutCreateWindow(argv
[0]);
260 glutReshapeFunc( Reshape
);
261 glutKeyboardFunc( Key
);
262 glutDisplayFunc( Display
);
264 glutIdleFunc( Idle
);