Brian Paul | 02e8a03 | 2000-06-27 17:04:43 +0000 | [diff] [blame^] | 1 | /* $Id: paltex.c,v 1.3 2000/06/27 17:04:43 brianp Exp $ */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
Brian Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 4 | * Paletted texture demo. Written by Brian Paul. |
| 5 | * This program is in the public domain. |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <math.h> |
Brian Paul | 02e8a03 | 2000-06-27 17:04:43 +0000 | [diff] [blame^] | 11 | #define GL_GLEXT_LEGACY |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 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); |
Brian Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 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); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 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(); |
Brian Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 53 | glTranslatef( 0.0, 0.0, -7.0 ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 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 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 70 | static void Init( void ) |
| 71 | { |
Brian Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 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 | }; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 84 | 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 Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 92 | /* 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; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 113 | |
| 114 | #ifdef GL_EXT_paletted_texture |
| 115 | |
Brian Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 116 | #if defined(GL_EXT_shared_texture_palette) && defined(USE_SHARED_PALETTE) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 117 | 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 Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 137 | WIDTH, HEIGHT, /* width, height */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 138 | 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 | |
| 151 | int 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 ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 165 | glutDisplayFunc( Display ); |
| 166 | glutIdleFunc( Idle ); |
| 167 | |
| 168 | glutMainLoop(); |
| 169 | return 0; |
| 170 | } |