blob: aabb47bbc0d3dc945af6727d8453ddee3851a943 [file] [log] [blame]
Jouk Jansen55590ee2000-10-05 07:17:43 +00001/* $Id: paltex.c,v 1.6 2000/10/05 07:17:43 joukj Exp $ */
jtgafb833d1999-08-19 00:55:39 +00002
3/*
Brian Pauld51b2c91999-11-02 15:09:04 +00004 * Paletted texture demo. Written by Brian Paul.
5 * This program is in the public domain.
jtgafb833d1999-08-19 00:55:39 +00006 */
7
jtgafb833d1999-08-19 00:55:39 +00008#include <stdio.h>
9#include <stdlib.h>
10#include <math.h>
Jouk Jansen55590ee2000-10-05 07:17:43 +000011#include <string.h>
Brian Paul02e8a032000-06-27 17:04:43 +000012#define GL_GLEXT_LEGACY
jtgafb833d1999-08-19 00:55:39 +000013#include <GL/glut.h>
14
15
16static float Rot = 0.0;
Brian Paul476609c2000-10-04 18:15:39 +000017static GLboolean Anim = 1;
jtgafb833d1999-08-19 00:55:39 +000018
19
20static void Idle( void )
21{
Brian Paul476609c2000-10-04 18:15:39 +000022 float t = glutGet(GLUT_ELAPSED_TIME) * 0.001; /* in seconds */
23 Rot = t * 360 / 4; /* 1 rotation per 4 seconds */
jtgafb833d1999-08-19 00:55:39 +000024 glutPostRedisplay();
25}
26
27
28static void Display( void )
29{
Brian Paul476609c2000-10-04 18:15:39 +000030 /* draw background gradient */
31 glDisable(GL_TEXTURE_2D);
32 glBegin(GL_POLYGON);
33 glColor3f(1.0, 0.0, 0.2); glVertex2f(-1.5, -1.0);
34 glColor3f(1.0, 0.0, 0.2); glVertex2f( 1.5, -1.0);
35 glColor3f(0.0, 0.0, 1.0); glVertex2f( 1.5, 1.0);
36 glColor3f(0.0, 0.0, 1.0); glVertex2f(-1.5, 1.0);
37 glEnd();
jtgafb833d1999-08-19 00:55:39 +000038
39 glPushMatrix();
40 glRotatef(Rot, 0, 0, 1);
41
Brian Paul476609c2000-10-04 18:15:39 +000042 glEnable(GL_TEXTURE_2D);
jtgafb833d1999-08-19 00:55:39 +000043 glBegin(GL_POLYGON);
Brian Pauld51b2c91999-11-02 15:09:04 +000044 glTexCoord2f(0, 1); glVertex2f(-1, -0.5);
45 glTexCoord2f(1, 1); glVertex2f( 1, -0.5);
46 glTexCoord2f(1, 0); glVertex2f( 1, 0.5);
47 glTexCoord2f(0, 0); glVertex2f(-1, 0.5);
jtgafb833d1999-08-19 00:55:39 +000048 glEnd();
49
50 glPopMatrix();
51
52 glutSwapBuffers();
53}
54
55
56static void Reshape( int width, int height )
57{
58 glViewport( 0, 0, width, height );
59 glMatrixMode( GL_PROJECTION );
60 glLoadIdentity();
Brian Paul476609c2000-10-04 18:15:39 +000061 glOrtho( -1.5, 1.5, -1.0, 1.0, -1.0, 1.0 );
jtgafb833d1999-08-19 00:55:39 +000062 glMatrixMode( GL_MODELVIEW );
63 glLoadIdentity();
jtgafb833d1999-08-19 00:55:39 +000064}
65
66
67static void Key( unsigned char key, int x, int y )
68{
69 (void) x;
70 (void) y;
71 switch (key) {
72 case 27:
73 exit(0);
74 break;
Brian Paul476609c2000-10-04 18:15:39 +000075 case 's':
76 Rot += 0.5;
77 break;
78 case ' ':
79 Anim = !Anim;
80 if (Anim)
81 glutIdleFunc( Idle );
82 else
83 glutIdleFunc( NULL );
84 break;
jtgafb833d1999-08-19 00:55:39 +000085 }
86 glutPostRedisplay();
87}
88
89
jtgafb833d1999-08-19 00:55:39 +000090static void Init( void )
91{
Brian Pauld51b2c91999-11-02 15:09:04 +000092#define HEIGHT 8
93#define WIDTH 32
94 static char texture[HEIGHT][WIDTH] = {
95 " ",
96 " MMM EEEE SSS AAA ",
97 " M M M E S S A A ",
98 " M M M EEEE SS A A ",
99 " M M M E SS AAAAA ",
100 " M M E S S A A ",
101 " M M EEEE SSS A A ",
102 " "
103 };
jtgafb833d1999-08-19 00:55:39 +0000104 GLubyte table[256][4];
jtgafb833d1999-08-19 00:55:39 +0000105
106 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
107 printf("Sorry, GL_EXT_paletted_texture not supported\n");
108 exit(0);
109 }
110
Brian Pauld51b2c91999-11-02 15:09:04 +0000111 /* load the color table for each texel-index */
Brian Paul476609c2000-10-04 18:15:39 +0000112 memset(table, 0, 256*4);
113 table[' '][0] = 255;
114 table[' '][1] = 255;
115 table[' '][2] = 255;
116 table[' '][3] = 64;
Brian Pauld51b2c91999-11-02 15:09:04 +0000117 table['M'][0] = 255;
118 table['M'][1] = 0;
119 table['M'][2] = 0;
Brian Paul476609c2000-10-04 18:15:39 +0000120 table['M'][3] = 255;
Brian Pauld51b2c91999-11-02 15:09:04 +0000121 table['E'][0] = 0;
122 table['E'][1] = 255;
123 table['E'][2] = 0;
Brian Paul476609c2000-10-04 18:15:39 +0000124 table['E'][3] = 255;
125 table['S'][0] = 0;
126 table['S'][1] = 0;
Brian Pauld51b2c91999-11-02 15:09:04 +0000127 table['S'][2] = 255;
Brian Paul476609c2000-10-04 18:15:39 +0000128 table['S'][3] = 255;
Brian Pauld51b2c91999-11-02 15:09:04 +0000129 table['A'][0] = 255;
130 table['A'][1] = 255;
131 table['A'][2] = 0;
Brian Paul476609c2000-10-04 18:15:39 +0000132 table['A'][3] = 255;
jtgafb833d1999-08-19 00:55:39 +0000133
134#ifdef GL_EXT_paletted_texture
135
Brian Pauld51b2c91999-11-02 15:09:04 +0000136#if defined(GL_EXT_shared_texture_palette) && defined(USE_SHARED_PALETTE)
jtgafb833d1999-08-19 00:55:39 +0000137 printf("Using shared palette\n");
138 glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, /* target */
139 GL_RGBA, /* internal format */
140 256, /* table size */
141 GL_RGBA, /* table format */
142 GL_UNSIGNED_BYTE, /* table type */
143 table); /* the color table */
144 glEnable(GL_SHARED_TEXTURE_PALETTE_EXT);
145#else
146 glColorTableEXT(GL_TEXTURE_2D, /* target */
147 GL_RGBA, /* internal format */
148 256, /* table size */
149 GL_RGBA, /* table format */
150 GL_UNSIGNED_BYTE, /* table type */
151 table); /* the color table */
152#endif
153
154 glTexImage2D(GL_TEXTURE_2D, /* target */
155 0, /* level */
156 GL_COLOR_INDEX8_EXT, /* internal format */
Brian Pauld51b2c91999-11-02 15:09:04 +0000157 WIDTH, HEIGHT, /* width, height */
jtgafb833d1999-08-19 00:55:39 +0000158 0, /* border */
159 GL_COLOR_INDEX, /* texture format */
160 GL_UNSIGNED_BYTE, /* texture type */
161 texture); /* teh texture */
162#endif
163
164 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
165 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
166 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
167 glEnable(GL_TEXTURE_2D);
Brian Paul476609c2000-10-04 18:15:39 +0000168
169 glEnable(GL_BLEND);
170 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Jouk Jansen55590ee2000-10-05 07:17:43 +0000171#undef HEIGHT
172#undef WIDTH
Brian Paul476609c2000-10-04 18:15:39 +0000173}
174
175
176
177/*
178 * Color ramp test
179 */
180static void Init2( void )
181{
182#define HEIGHT 32
183#define WIDTH 256
184 static char texture[HEIGHT][WIDTH];
185 GLubyte table[256][4];
186 int i, j;
187
188 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
189 printf("Sorry, GL_EXT_paletted_texture not supported\n");
190 exit(0);
191 }
192
193 for (j = 0; j < HEIGHT; j++) {
194 for (i = 0; i < WIDTH; i++) {
195 texture[j][i] = i;
196 }
197 }
198
199 for (i = 0; i < 255; i++) {
200 table[i][0] = i;
201 table[i][1] = 0;
202 table[i][2] = 0;
203 table[i][3] = 255;
204 }
205
206#ifdef GL_EXT_paletted_texture
207
208#if defined(GL_EXT_shared_texture_palette) && defined(USE_SHARED_PALETTE)
209 printf("Using shared palette\n");
210 glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, /* target */
211 GL_RGBA, /* internal format */
212 256, /* table size */
213 GL_RGBA, /* table format */
214 GL_UNSIGNED_BYTE, /* table type */
215 table); /* the color table */
216 glEnable(GL_SHARED_TEXTURE_PALETTE_EXT);
217#else
218 glColorTableEXT(GL_TEXTURE_2D, /* target */
219 GL_RGBA, /* internal format */
220 256, /* table size */
221 GL_RGBA, /* table format */
222 GL_UNSIGNED_BYTE, /* table type */
223 table); /* the color table */
224#endif
225
226 glTexImage2D(GL_TEXTURE_2D, /* target */
227 0, /* level */
228 GL_COLOR_INDEX8_EXT, /* internal format */
229 WIDTH, HEIGHT, /* width, height */
230 0, /* border */
231 GL_COLOR_INDEX, /* texture format */
232 GL_UNSIGNED_BYTE, /* texture type */
233 texture); /* teh texture */
234#endif
235
236 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
237 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
238 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
239 glEnable(GL_TEXTURE_2D);
240
241 glEnable(GL_BLEND);
242 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
jtgafb833d1999-08-19 00:55:39 +0000243}
244
245
246int main( int argc, char *argv[] )
247{
248 glutInit( &argc, argv );
249 glutInitWindowPosition( 0, 0 );
Brian Paul476609c2000-10-04 18:15:39 +0000250 glutInitWindowSize( 400, 300 );
jtgafb833d1999-08-19 00:55:39 +0000251
252 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
253
254 glutCreateWindow(argv[0]);
255
256 Init();
257
258 glutReshapeFunc( Reshape );
259 glutKeyboardFunc( Key );
jtgafb833d1999-08-19 00:55:39 +0000260 glutDisplayFunc( Display );
Brian Paul476609c2000-10-04 18:15:39 +0000261 if (Anim)
262 glutIdleFunc( Idle );
jtgafb833d1999-08-19 00:55:39 +0000263
264 glutMainLoop();
265 return 0;
266}