added readpix program
[mesa.git] / progs / demos / paltex.c
1 /* $Id: paltex.c,v 1.2 1999/11/02 15:09:04 brianp Exp $ */
2
3 /*
4 * Paletted texture demo. Written by Brian Paul.
5 * This program is in the public domain.
6 */
7
8 /*
9 * $Log: paltex.c,v $
10 * Revision 1.2 1999/11/02 15:09:04 brianp
11 * new texture image, cleaned-up code
12 *
13 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
14 * Imported sources
15 *
16 * Revision 3.1 1999/03/28 18:20:49 brianp
17 * minor clean-up
18 *
19 * Revision 3.0 1998/02/14 18:42:29 brianp
20 * initial rev
21 *
22 */
23
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <math.h>
28 #include <GL/glut.h>
29
30
31 static float Rot = 0.0;
32
33
34 static void Idle( void )
35 {
36 Rot += 5.0;
37 glutPostRedisplay();
38 }
39
40
41 static void Display( void )
42 {
43 glClear( GL_COLOR_BUFFER_BIT );
44
45 glPushMatrix();
46 glRotatef(Rot, 0, 0, 1);
47
48 glBegin(GL_POLYGON);
49 glTexCoord2f(0, 1); glVertex2f(-1, -0.5);
50 glTexCoord2f(1, 1); glVertex2f( 1, -0.5);
51 glTexCoord2f(1, 0); glVertex2f( 1, 0.5);
52 glTexCoord2f(0, 0); glVertex2f(-1, 0.5);
53 glEnd();
54
55 glPopMatrix();
56
57 glutSwapBuffers();
58 }
59
60
61 static void Reshape( int width, int height )
62 {
63 glViewport( 0, 0, width, height );
64 glMatrixMode( GL_PROJECTION );
65 glLoadIdentity();
66 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
67 glMatrixMode( GL_MODELVIEW );
68 glLoadIdentity();
69 glTranslatef( 0.0, 0.0, -7.0 );
70 }
71
72
73 static void Key( unsigned char key, int x, int y )
74 {
75 (void) x;
76 (void) y;
77 switch (key) {
78 case 27:
79 exit(0);
80 break;
81 }
82 glutPostRedisplay();
83 }
84
85
86 static void Init( void )
87 {
88 #define HEIGHT 8
89 #define WIDTH 32
90 static char texture[HEIGHT][WIDTH] = {
91 " ",
92 " MMM EEEE SSS AAA ",
93 " M M M E S S A A ",
94 " M M M EEEE SS A A ",
95 " M M M E SS AAAAA ",
96 " M M E S S A A ",
97 " M M EEEE SSS A A ",
98 " "
99 };
100 GLubyte table[256][4];
101 int i;
102
103 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
104 printf("Sorry, GL_EXT_paletted_texture not supported\n");
105 exit(0);
106 }
107
108 /* load the color table for each texel-index */
109 table[' '][0] = 50;
110 table[' '][1] = 50;
111 table[' '][2] = 50;
112 table[' '][3] = 50;
113 table['M'][0] = 255;
114 table['M'][1] = 0;
115 table['M'][2] = 0;
116 table['M'][3] = 0;
117 table['E'][0] = 0;
118 table['E'][1] = 255;
119 table['E'][2] = 0;
120 table['E'][3] = 0;
121 table['S'][0] = 40;
122 table['S'][1] = 40;
123 table['S'][2] = 255;
124 table['S'][3] = 0;
125 table['A'][0] = 255;
126 table['A'][1] = 255;
127 table['A'][2] = 0;
128 table['A'][3] = 0;
129
130 #ifdef GL_EXT_paletted_texture
131
132 #if defined(GL_EXT_shared_texture_palette) && defined(USE_SHARED_PALETTE)
133 printf("Using shared palette\n");
134 glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, /* target */
135 GL_RGBA, /* internal format */
136 256, /* table size */
137 GL_RGBA, /* table format */
138 GL_UNSIGNED_BYTE, /* table type */
139 table); /* the color table */
140 glEnable(GL_SHARED_TEXTURE_PALETTE_EXT);
141 #else
142 glColorTableEXT(GL_TEXTURE_2D, /* target */
143 GL_RGBA, /* internal format */
144 256, /* table size */
145 GL_RGBA, /* table format */
146 GL_UNSIGNED_BYTE, /* table type */
147 table); /* the color table */
148 #endif
149
150 glTexImage2D(GL_TEXTURE_2D, /* target */
151 0, /* level */
152 GL_COLOR_INDEX8_EXT, /* internal format */
153 WIDTH, HEIGHT, /* width, height */
154 0, /* border */
155 GL_COLOR_INDEX, /* texture format */
156 GL_UNSIGNED_BYTE, /* texture type */
157 texture); /* teh texture */
158 #endif
159
160 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
162 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
163 glEnable(GL_TEXTURE_2D);
164 }
165
166
167 int main( int argc, char *argv[] )
168 {
169 glutInit( &argc, argv );
170 glutInitWindowPosition( 0, 0 );
171 glutInitWindowSize( 400, 400 );
172
173 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
174
175 glutCreateWindow(argv[0]);
176
177 Init();
178
179 glutReshapeFunc( Reshape );
180 glutKeyboardFunc( Key );
181 glutDisplayFunc( Display );
182 glutIdleFunc( Idle );
183
184 glutMainLoop();
185 return 0;
186 }