3 * Paletted texture demo. Written by Brian Paul.
4 * This program is in the public domain.
15 #include "glut_wrap.h"
18 static float Rot
= 0.0;
19 static GLboolean Anim
= 1;
22 static void Idle( void )
24 float t
= glutGet(GLUT_ELAPSED_TIME
) * 0.001; /* in seconds */
25 Rot
= t
* 360 / 4; /* 1 rotation per 4 seconds */
30 static void Display( void )
32 /* draw background gradient */
33 glDisable(GL_TEXTURE_2D
);
35 glColor3f(1.0, 0.0, 0.2); glVertex2f(-1.5, -1.0);
36 glColor3f(1.0, 0.0, 0.2); glVertex2f( 1.5, -1.0);
37 glColor3f(0.0, 0.0, 1.0); glVertex2f( 1.5, 1.0);
38 glColor3f(0.0, 0.0, 1.0); glVertex2f(-1.5, 1.0);
42 glRotatef(Rot
, 0, 0, 1);
44 glEnable(GL_TEXTURE_2D
);
46 glTexCoord2f(0, 1); glVertex2f(-1, -0.5);
47 glTexCoord2f(1, 1); glVertex2f( 1, -0.5);
48 glTexCoord2f(1, 0); glVertex2f( 1, 0.5);
49 glTexCoord2f(0, 0); glVertex2f(-1, 0.5);
58 static void Reshape( int width
, int height
)
60 glViewport( 0, 0, width
, height
);
61 glMatrixMode( GL_PROJECTION
);
63 glOrtho( -1.5, 1.5, -1.0, 1.0, -1.0, 1.0 );
64 glMatrixMode( GL_MODELVIEW
);
69 static void Key( unsigned char key
, int x
, int y
)
92 static void Init( void )
96 /* 257 = HEIGHT * WIDTH + 1 (for trailing '\0') */
97 static char texture
[257] = {"\
107 GLubyte table
[256][4];
109 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
110 printf("Sorry, GL_EXT_paletted_texture not supported\n");
114 /* load the color table for each texel-index */
115 memset(table
, 0, 256*4);
137 #ifdef GL_EXT_paletted_texture
139 #if defined(GL_EXT_shared_texture_palette) && defined(USE_SHARED_PALETTE)
140 printf("Using shared palette\n");
141 glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT
, /* target */
142 GL_RGBA
, /* internal format */
143 256, /* table size */
144 GL_RGBA
, /* table format */
145 GL_UNSIGNED_BYTE
, /* table type */
146 table
); /* the color table */
147 glEnable(GL_SHARED_TEXTURE_PALETTE_EXT
);
149 glColorTableEXT(GL_TEXTURE_2D
, /* target */
150 GL_RGBA
, /* internal format */
151 256, /* table size */
152 GL_RGBA
, /* table format */
153 GL_UNSIGNED_BYTE
, /* table type */
154 table
); /* the color table */
157 glTexImage2D(GL_TEXTURE_2D
, /* target */
159 GL_COLOR_INDEX8_EXT
, /* internal format */
160 WIDTH
, HEIGHT
, /* width, height */
162 GL_COLOR_INDEX
, /* texture format */
163 GL_UNSIGNED_BYTE
, /* texture type */
164 texture
); /* the texture */
167 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
168 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
169 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
170 glEnable(GL_TEXTURE_2D
);
173 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
183 static void Init2( void )
187 static char texture
[HEIGHT
][WIDTH
];
188 GLubyte table
[256][4];
191 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
192 printf("Sorry, GL_EXT_paletted_texture not supported\n");
196 for (j
= 0; j
< HEIGHT
; j
++) {
197 for (i
= 0; i
< WIDTH
; i
++) {
202 for (i
= 0; i
< 255; i
++) {
209 #ifdef GL_EXT_paletted_texture
211 #if defined(GL_EXT_shared_texture_palette) && defined(USE_SHARED_PALETTE)
212 printf("Using shared palette\n");
213 glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT
, /* target */
214 GL_RGBA
, /* internal format */
215 256, /* table size */
216 GL_RGBA
, /* table format */
217 GL_UNSIGNED_BYTE
, /* table type */
218 table
); /* the color table */
219 glEnable(GL_SHARED_TEXTURE_PALETTE_EXT
);
221 glColorTableEXT(GL_TEXTURE_2D
, /* target */
222 GL_RGBA
, /* internal format */
223 256, /* table size */
224 GL_RGBA
, /* table format */
225 GL_UNSIGNED_BYTE
, /* table type */
226 table
); /* the color table */
229 glTexImage2D(GL_TEXTURE_2D
, /* target */
231 GL_COLOR_INDEX8_EXT
, /* internal format */
232 WIDTH
, HEIGHT
, /* width, height */
234 GL_COLOR_INDEX
, /* texture format */
235 GL_UNSIGNED_BYTE
, /* texture type */
236 texture
); /* teh texture */
239 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
240 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
241 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
242 glEnable(GL_TEXTURE_2D
);
245 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
249 int main( int argc
, char *argv
[] )
251 glutInitWindowSize( 400, 300 );
252 glutInit( &argc
, argv
);
253 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
254 glutCreateWindow(argv
[0]);
258 (void) Init2
; /* silence warning */
260 glutReshapeFunc( Reshape
);
261 glutKeyboardFunc( Key
);
262 glutDisplayFunc( Display
);
264 glutIdleFunc( Idle
);