1 |
|
---|
2 | /*
|
---|
3 | * Paletted texture demo. Written by Brian Paul.
|
---|
4 | * This program is in the public domain.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include <stdio.h>
|
---|
8 | #include <stdlib.h>
|
---|
9 | #include <math.h>
|
---|
10 | #include <string.h>
|
---|
11 | #ifdef _WIN32
|
---|
12 | #include <windows.h>
|
---|
13 | #endif
|
---|
14 | #include <GL/glew.h>
|
---|
15 | #include "glut_wrap.h"
|
---|
16 |
|
---|
17 |
|
---|
18 | static float Rot = 0.0;
|
---|
19 | static GLboolean Anim = 1;
|
---|
20 |
|
---|
21 |
|
---|
22 | static void Idle( void )
|
---|
23 | {
|
---|
24 | float t = glutGet(GLUT_ELAPSED_TIME) * 0.001; /* in seconds */
|
---|
25 | Rot = t * 360 / 4; /* 1 rotation per 4 seconds */
|
---|
26 | glutPostRedisplay();
|
---|
27 | }
|
---|
28 |
|
---|
29 |
|
---|
30 | static void Display( void )
|
---|
31 | {
|
---|
32 | /* 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();
|
---|
40 |
|
---|
41 | glPushMatrix();
|
---|
42 | glRotatef(Rot, 0, 0, 1);
|
---|
43 |
|
---|
44 | glEnable(GL_TEXTURE_2D);
|
---|
45 | glBegin(GL_POLYGON);
|
---|
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);
|
---|
50 | glEnd();
|
---|
51 |
|
---|
52 | glPopMatrix();
|
---|
53 |
|
---|
54 | glutSwapBuffers();
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | static void Reshape( int width, int height )
|
---|
59 | {
|
---|
60 | glViewport( 0, 0, width, height );
|
---|
61 | glMatrixMode( GL_PROJECTION );
|
---|
62 | glLoadIdentity();
|
---|
63 | glOrtho( -1.5, 1.5, -1.0, 1.0, -1.0, 1.0 );
|
---|
64 | glMatrixMode( GL_MODELVIEW );
|
---|
65 | glLoadIdentity();
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | static 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;
|
---|
77 | 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;
|
---|
87 | }
|
---|
88 | glutPostRedisplay();
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | static void Init( void )
|
---|
93 | {
|
---|
94 | #define HEIGHT 8
|
---|
95 | #define WIDTH 32
|
---|
96 | /* 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 | "
|
---|
106 | };
|
---|
107 | GLubyte table[256][4];
|
---|
108 |
|
---|
109 | if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
|
---|
110 | printf("Sorry, GL_EXT_paletted_texture not supported\n");
|
---|
111 | exit(0);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /* load the color table for each texel-index */
|
---|
115 | memset(table, 0, 256*4);
|
---|
116 | table[' '][0] = 255;
|
---|
117 | table[' '][1] = 255;
|
---|
118 | table[' '][2] = 255;
|
---|
119 | table[' '][3] = 64;
|
---|
120 | table['M'][0] = 255;
|
---|
121 | table['M'][1] = 0;
|
---|
122 | table['M'][2] = 0;
|
---|
123 | table['M'][3] = 255;
|
---|
124 | table['E'][0] = 0;
|
---|
125 | table['E'][1] = 255;
|
---|
126 | table['E'][2] = 0;
|
---|
127 | table['E'][3] = 255;
|
---|
128 | table['S'][0] = 0;
|
---|
129 | table['S'][1] = 0;
|
---|
130 | table['S'][2] = 255;
|
---|
131 | table['S'][3] = 255;
|
---|
132 | table['A'][0] = 255;
|
---|
133 | table['A'][1] = 255;
|
---|
134 | table['A'][2] = 0;
|
---|
135 | table['A'][3] = 255;
|
---|
136 |
|
---|
137 | #ifdef GL_EXT_paletted_texture
|
---|
138 |
|
---|
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);
|
---|
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 */
|
---|
160 | WIDTH, HEIGHT, /* width, height */
|
---|
161 | 0, /* border */
|
---|
162 | GL_COLOR_INDEX, /* texture format */
|
---|
163 | GL_UNSIGNED_BYTE, /* texture type */
|
---|
164 | texture); /* the texture */
|
---|
165 | #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);
|
---|
171 |
|
---|
172 | glEnable(GL_BLEND);
|
---|
173 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
174 | #undef HEIGHT
|
---|
175 | #undef WIDTH
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 |
|
---|
180 | /*
|
---|
181 | * Color ramp test
|
---|
182 | */
|
---|
183 | static 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);
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | int main( int argc, char *argv[] )
|
---|
250 | {
|
---|
251 | glutInitWindowSize( 400, 300 );
|
---|
252 | glutInit( &argc, argv );
|
---|
253 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
|
---|
254 | glutCreateWindow(argv[0]);
|
---|
255 | glewInit();
|
---|
256 |
|
---|
257 | Init();
|
---|
258 | (void) Init2; /* silence warning */
|
---|
259 |
|
---|
260 | glutReshapeFunc( Reshape );
|
---|
261 | glutKeyboardFunc( Key );
|
---|
262 | glutDisplayFunc( Display );
|
---|
263 | if (Anim)
|
---|
264 | glutIdleFunc( Idle );
|
---|
265 |
|
---|
266 | glutMainLoop();
|
---|
267 | return 0;
|
---|
268 | }
|
---|