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