blob: 5d4b8662b5cb8d050f9522be2ec9bfc9943eca02 [file] [log] [blame]
jtgafb833d1999-08-19 00:55:39 +00001
2/*
Brian Pauld51b2c91999-11-02 15:09:04 +00003 * Paletted texture demo. Written by Brian Paul.
4 * This program is in the public domain.
jtgafb833d1999-08-19 00:55:39 +00005 */
6
jtgafb833d1999-08-19 00:55:39 +00007#include <stdio.h>
8#include <stdlib.h>
9#include <math.h>
Jouk Jansen55590ee2000-10-05 07:17:43 +000010#include <string.h>
Karl Schultz164ce122002-01-16 00:48:43 +000011#ifdef _WIN32
12#include <windows.h>
13#endif
José Fonseca2e61d132009-01-24 16:39:49 +000014#include <GL/glew.h>
jtgafb833d1999-08-19 00:55:39 +000015#include <GL/glut.h>
16
17
18static float Rot = 0.0;
Brian Paul476609c2000-10-04 18:15:39 +000019static GLboolean Anim = 1;
jtgafb833d1999-08-19 00:55:39 +000020
21
22static void Idle( void )
23{
Brian Paul476609c2000-10-04 18:15:39 +000024 float t = glutGet(GLUT_ELAPSED_TIME) * 0.001; /* in seconds */
25 Rot = t * 360 / 4; /* 1 rotation per 4 seconds */
jtgafb833d1999-08-19 00:55:39 +000026 glutPostRedisplay();
27}
28
29
30static void Display( void )
31{
Brian Paul476609c2000-10-04 18:15:39 +000032 /* draw background gradient */
33 glDisable(GL_TEXTURE_2D);
34 glBegin(GL_POLYGON);
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);
39 glEnd();
jtgafb833d1999-08-19 00:55:39 +000040
41 glPushMatrix();
42 glRotatef(Rot, 0, 0, 1);
43
Brian Paul476609c2000-10-04 18:15:39 +000044 glEnable(GL_TEXTURE_2D);
jtgafb833d1999-08-19 00:55:39 +000045 glBegin(GL_POLYGON);
Brian Pauld51b2c91999-11-02 15:09:04 +000046 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);
jtgafb833d1999-08-19 00:55:39 +000050 glEnd();
51
52 glPopMatrix();
53
54 glutSwapBuffers();
55}
56
57
58static void Reshape( int width, int height )
59{
60 glViewport( 0, 0, width, height );
61 glMatrixMode( GL_PROJECTION );
62 glLoadIdentity();
Brian Paul476609c2000-10-04 18:15:39 +000063 glOrtho( -1.5, 1.5, -1.0, 1.0, -1.0, 1.0 );
jtgafb833d1999-08-19 00:55:39 +000064 glMatrixMode( GL_MODELVIEW );
65 glLoadIdentity();
jtgafb833d1999-08-19 00:55:39 +000066}
67
68
69static void Key( unsigned char key, int x, int y )
70{
71 (void) x;
72 (void) y;
73 switch (key) {
74 case 27:
75 exit(0);
76 break;
Brian Paul476609c2000-10-04 18:15:39 +000077 case 's':
78 Rot += 0.5;
79 break;
80 case ' ':
81 Anim = !Anim;
82 if (Anim)
83 glutIdleFunc( Idle );
84 else
85 glutIdleFunc( NULL );
86 break;
jtgafb833d1999-08-19 00:55:39 +000087 }
88 glutPostRedisplay();
89}
90
91
jtgafb833d1999-08-19 00:55:39 +000092static void Init( void )
93{
Brian Pauld51b2c91999-11-02 15:09:04 +000094#define HEIGHT 8
95#define WIDTH 32
Brian Paulc7124252002-07-22 13:45:31 +000096 /* 257 = HEIGHT * WIDTH + 1 (for trailing '\0') */
97 static char texture[257] = {"\
98 \
99 MMM EEEE SSS AAA \
100 M M M E S S A A \
101 M M M EEEE SS A A \
102 M M M E SS AAAAA \
103 M M E S S A A \
104 M M EEEE SSS A A \
105 "
Brian Pauld51b2c91999-11-02 15:09:04 +0000106 };
jtgafb833d1999-08-19 00:55:39 +0000107 GLubyte table[256][4];
jtgafb833d1999-08-19 00:55:39 +0000108
109 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
110 printf("Sorry, GL_EXT_paletted_texture not supported\n");
111 exit(0);
112 }
113
Brian Pauld51b2c91999-11-02 15:09:04 +0000114 /* load the color table for each texel-index */
Brian Paul476609c2000-10-04 18:15:39 +0000115 memset(table, 0, 256*4);
116 table[' '][0] = 255;
117 table[' '][1] = 255;
118 table[' '][2] = 255;
119 table[' '][3] = 64;
Brian Pauld51b2c91999-11-02 15:09:04 +0000120 table['M'][0] = 255;
121 table['M'][1] = 0;
122 table['M'][2] = 0;
Brian Paul476609c2000-10-04 18:15:39 +0000123 table['M'][3] = 255;
Brian Pauld51b2c91999-11-02 15:09:04 +0000124 table['E'][0] = 0;
125 table['E'][1] = 255;
126 table['E'][2] = 0;
Brian Paul476609c2000-10-04 18:15:39 +0000127 table['E'][3] = 255;
128 table['S'][0] = 0;
129 table['S'][1] = 0;
Brian Pauld51b2c91999-11-02 15:09:04 +0000130 table['S'][2] = 255;
Brian Paul476609c2000-10-04 18:15:39 +0000131 table['S'][3] = 255;
Brian Pauld51b2c91999-11-02 15:09:04 +0000132 table['A'][0] = 255;
133 table['A'][1] = 255;
134 table['A'][2] = 0;
Brian Paul476609c2000-10-04 18:15:39 +0000135 table['A'][3] = 255;
jtgafb833d1999-08-19 00:55:39 +0000136
137#ifdef GL_EXT_paletted_texture
138
Brian Pauld51b2c91999-11-02 15:09:04 +0000139#if defined(GL_EXT_shared_texture_palette) && defined(USE_SHARED_PALETTE)
jtgafb833d1999-08-19 00:55:39 +0000140 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);
148#else
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 */
155#endif
156
157 glTexImage2D(GL_TEXTURE_2D, /* target */
158 0, /* level */
159 GL_COLOR_INDEX8_EXT, /* internal format */
Brian Pauld51b2c91999-11-02 15:09:04 +0000160 WIDTH, HEIGHT, /* width, height */
jtgafb833d1999-08-19 00:55:39 +0000161 0, /* border */
162 GL_COLOR_INDEX, /* texture format */
163 GL_UNSIGNED_BYTE, /* texture type */
Brian Paulf02a5f62002-07-12 15:54:01 +0000164 texture); /* the texture */
jtgafb833d1999-08-19 00:55:39 +0000165#endif
166
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);
Brian Paul476609c2000-10-04 18:15:39 +0000171
172 glEnable(GL_BLEND);
173 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Jouk Jansen55590ee2000-10-05 07:17:43 +0000174#undef HEIGHT
175#undef WIDTH
Brian Paul476609c2000-10-04 18:15:39 +0000176}
177
178
179
180/*
181 * Color ramp test
182 */
183static void Init2( void )
184{
185#define HEIGHT 32
186#define WIDTH 256
187 static char texture[HEIGHT][WIDTH];
188 GLubyte table[256][4];
189 int i, j;
190
191 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
192 printf("Sorry, GL_EXT_paletted_texture not supported\n");
193 exit(0);
194 }
195
196 for (j = 0; j < HEIGHT; j++) {
197 for (i = 0; i < WIDTH; i++) {
198 texture[j][i] = i;
199 }
200 }
201
202 for (i = 0; i < 255; i++) {
203 table[i][0] = i;
204 table[i][1] = 0;
205 table[i][2] = 0;
206 table[i][3] = 255;
207 }
208
209#ifdef GL_EXT_paletted_texture
210
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);
220#else
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 */
227#endif
228
229 glTexImage2D(GL_TEXTURE_2D, /* target */
230 0, /* level */
231 GL_COLOR_INDEX8_EXT, /* internal format */
232 WIDTH, HEIGHT, /* width, height */
233 0, /* border */
234 GL_COLOR_INDEX, /* texture format */
235 GL_UNSIGNED_BYTE, /* texture type */
236 texture); /* teh texture */
237#endif
238
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);
243
244 glEnable(GL_BLEND);
245 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
jtgafb833d1999-08-19 00:55:39 +0000246}
247
248
249int main( int argc, char *argv[] )
250{
Brian Paul476609c2000-10-04 18:15:39 +0000251 glutInitWindowSize( 400, 300 );
Brian Paul263f4322009-12-18 08:12:55 -0700252 glutInit( &argc, argv );
jtgafb833d1999-08-19 00:55:39 +0000253 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
jtgafb833d1999-08-19 00:55:39 +0000254 glutCreateWindow(argv[0]);
José Fonseca2e61d132009-01-24 16:39:49 +0000255 glewInit();
jtgafb833d1999-08-19 00:55:39 +0000256
257 Init();
Brian Paul52226622004-08-10 15:36:46 +0000258 (void) Init2; /* silence warning */
jtgafb833d1999-08-19 00:55:39 +0000259
260 glutReshapeFunc( Reshape );
261 glutKeyboardFunc( Key );
jtgafb833d1999-08-19 00:55:39 +0000262 glutDisplayFunc( Display );
Brian Paul476609c2000-10-04 18:15:39 +0000263 if (Anim)
264 glutIdleFunc( Idle );
jtgafb833d1999-08-19 00:55:39 +0000265
266 glutMainLoop();
267 return 0;
268}