blob: 6ffac28c1eda62a7b281b701633e1615e8b0bed6 [file] [log] [blame]
Brian Paul02e8a032000-06-27 17:04:43 +00001/* $Id: paltex.c,v 1.3 2000/06/27 17:04:43 brianp 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>
Brian Paul02e8a032000-06-27 17:04:43 +000011#define GL_GLEXT_LEGACY
jtgafb833d1999-08-19 00:55:39 +000012#include <GL/glut.h>
13
14
15static float Rot = 0.0;
16
17
18static void Idle( void )
19{
20 Rot += 5.0;
21 glutPostRedisplay();
22}
23
24
25static void Display( void )
26{
27 glClear( GL_COLOR_BUFFER_BIT );
28
29 glPushMatrix();
30 glRotatef(Rot, 0, 0, 1);
31
32 glBegin(GL_POLYGON);
Brian Pauld51b2c91999-11-02 15:09:04 +000033 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);
jtgafb833d1999-08-19 00:55:39 +000037 glEnd();
38
39 glPopMatrix();
40
41 glutSwapBuffers();
42}
43
44
45static 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();
Brian Pauld51b2c91999-11-02 15:09:04 +000053 glTranslatef( 0.0, 0.0, -7.0 );
jtgafb833d1999-08-19 00:55:39 +000054}
55
56
57static 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
jtgafb833d1999-08-19 00:55:39 +000070static void Init( void )
71{
Brian Pauld51b2c91999-11-02 15:09:04 +000072#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 };
jtgafb833d1999-08-19 00:55:39 +000084 GLubyte table[256][4];
85 int i;
86
87 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
88 printf("Sorry, GL_EXT_paletted_texture not supported\n");
89 exit(0);
90 }
91
Brian Pauld51b2c91999-11-02 15:09:04 +000092 /* load the color table for each texel-index */
93 table[' '][0] = 50;
94 table[' '][1] = 50;
95 table[' '][2] = 50;
96 table[' '][3] = 50;
97 table['M'][0] = 255;
98 table['M'][1] = 0;
99 table['M'][2] = 0;
100 table['M'][3] = 0;
101 table['E'][0] = 0;
102 table['E'][1] = 255;
103 table['E'][2] = 0;
104 table['E'][3] = 0;
105 table['S'][0] = 40;
106 table['S'][1] = 40;
107 table['S'][2] = 255;
108 table['S'][3] = 0;
109 table['A'][0] = 255;
110 table['A'][1] = 255;
111 table['A'][2] = 0;
112 table['A'][3] = 0;
jtgafb833d1999-08-19 00:55:39 +0000113
114#ifdef GL_EXT_paletted_texture
115
Brian Pauld51b2c91999-11-02 15:09:04 +0000116#if defined(GL_EXT_shared_texture_palette) && defined(USE_SHARED_PALETTE)
jtgafb833d1999-08-19 00:55:39 +0000117 printf("Using shared palette\n");
118 glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, /* target */
119 GL_RGBA, /* internal format */
120 256, /* table size */
121 GL_RGBA, /* table format */
122 GL_UNSIGNED_BYTE, /* table type */
123 table); /* the color table */
124 glEnable(GL_SHARED_TEXTURE_PALETTE_EXT);
125#else
126 glColorTableEXT(GL_TEXTURE_2D, /* target */
127 GL_RGBA, /* internal format */
128 256, /* table size */
129 GL_RGBA, /* table format */
130 GL_UNSIGNED_BYTE, /* table type */
131 table); /* the color table */
132#endif
133
134 glTexImage2D(GL_TEXTURE_2D, /* target */
135 0, /* level */
136 GL_COLOR_INDEX8_EXT, /* internal format */
Brian Pauld51b2c91999-11-02 15:09:04 +0000137 WIDTH, HEIGHT, /* width, height */
jtgafb833d1999-08-19 00:55:39 +0000138 0, /* border */
139 GL_COLOR_INDEX, /* texture format */
140 GL_UNSIGNED_BYTE, /* texture type */
141 texture); /* teh texture */
142#endif
143
144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
146 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
147 glEnable(GL_TEXTURE_2D);
148}
149
150
151int main( int argc, char *argv[] )
152{
153 glutInit( &argc, argv );
154 glutInitWindowPosition( 0, 0 );
155 glutInitWindowSize( 400, 400 );
156
157 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
158
159 glutCreateWindow(argv[0]);
160
161 Init();
162
163 glutReshapeFunc( Reshape );
164 glutKeyboardFunc( Key );
jtgafb833d1999-08-19 00:55:39 +0000165 glutDisplayFunc( Display );
166 glutIdleFunc( Idle );
167
168 glutMainLoop();
169 return 0;
170}