jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
Brian Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 3 | * Paletted texture demo. Written by Brian Paul. |
| 4 | * This program is in the public domain. |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <math.h> |
Jouk Jansen | 55590ee | 2000-10-05 07:17:43 +0000 | [diff] [blame] | 10 | #include <string.h> |
Karl Schultz | 164ce12 | 2002-01-16 00:48:43 +0000 | [diff] [blame] | 11 | #ifdef _WIN32 |
| 12 | #include <windows.h> |
| 13 | #endif |
José Fonseca | 2e61d13 | 2009-01-24 16:39:49 +0000 | [diff] [blame] | 14 | #include <GL/glew.h> |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 15 | #include <GL/glut.h> |
| 16 | |
| 17 | |
| 18 | static float Rot = 0.0; |
Brian Paul | 476609c | 2000-10-04 18:15:39 +0000 | [diff] [blame] | 19 | static GLboolean Anim = 1; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 20 | |
| 21 | |
| 22 | static void Idle( void ) |
| 23 | { |
Brian Paul | 476609c | 2000-10-04 18:15:39 +0000 | [diff] [blame] | 24 | float t = glutGet(GLUT_ELAPSED_TIME) * 0.001; /* in seconds */ |
| 25 | Rot = t * 360 / 4; /* 1 rotation per 4 seconds */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 26 | glutPostRedisplay(); |
| 27 | } |
| 28 | |
| 29 | |
| 30 | static void Display( void ) |
| 31 | { |
Brian Paul | 476609c | 2000-10-04 18:15:39 +0000 | [diff] [blame] | 32 | /* draw background gradient */ |
| 33 | glDisable(GL_TEXTURE_2D); |
| 34 | glBegin(GL_POLYGON); |
| 35 | glColor3f(1.0, 0.0, 0.2); glVertex2f(-1.5, -1.0); |
| 36 | glColor3f(1.0, 0.0, 0.2); glVertex2f( 1.5, -1.0); |
| 37 | glColor3f(0.0, 0.0, 1.0); glVertex2f( 1.5, 1.0); |
| 38 | glColor3f(0.0, 0.0, 1.0); glVertex2f(-1.5, 1.0); |
| 39 | glEnd(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 40 | |
| 41 | glPushMatrix(); |
| 42 | glRotatef(Rot, 0, 0, 1); |
| 43 | |
Brian Paul | 476609c | 2000-10-04 18:15:39 +0000 | [diff] [blame] | 44 | glEnable(GL_TEXTURE_2D); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 45 | glBegin(GL_POLYGON); |
Brian Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 46 | glTexCoord2f(0, 1); glVertex2f(-1, -0.5); |
| 47 | glTexCoord2f(1, 1); glVertex2f( 1, -0.5); |
| 48 | glTexCoord2f(1, 0); glVertex2f( 1, 0.5); |
| 49 | glTexCoord2f(0, 0); glVertex2f(-1, 0.5); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 50 | glEnd(); |
| 51 | |
| 52 | glPopMatrix(); |
| 53 | |
| 54 | glutSwapBuffers(); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | static void Reshape( int width, int height ) |
| 59 | { |
| 60 | glViewport( 0, 0, width, height ); |
| 61 | glMatrixMode( GL_PROJECTION ); |
| 62 | glLoadIdentity(); |
Brian Paul | 476609c | 2000-10-04 18:15:39 +0000 | [diff] [blame] | 63 | glOrtho( -1.5, 1.5, -1.0, 1.0, -1.0, 1.0 ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 64 | glMatrixMode( GL_MODELVIEW ); |
| 65 | glLoadIdentity(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | |
| 69 | static void Key( unsigned char key, int x, int y ) |
| 70 | { |
| 71 | (void) x; |
| 72 | (void) y; |
| 73 | switch (key) { |
| 74 | case 27: |
| 75 | exit(0); |
| 76 | break; |
Brian Paul | 476609c | 2000-10-04 18:15:39 +0000 | [diff] [blame] | 77 | case 's': |
| 78 | Rot += 0.5; |
| 79 | break; |
| 80 | case ' ': |
| 81 | Anim = !Anim; |
| 82 | if (Anim) |
| 83 | glutIdleFunc( Idle ); |
| 84 | else |
| 85 | glutIdleFunc( NULL ); |
| 86 | break; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 87 | } |
| 88 | glutPostRedisplay(); |
| 89 | } |
| 90 | |
| 91 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 92 | static void Init( void ) |
| 93 | { |
Brian Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 94 | #define HEIGHT 8 |
| 95 | #define WIDTH 32 |
Brian Paul | c712425 | 2002-07-22 13:45:31 +0000 | [diff] [blame] | 96 | /* 257 = HEIGHT * WIDTH + 1 (for trailing '\0') */ |
| 97 | static char texture[257] = {"\ |
| 98 | \ |
| 99 | MMM EEEE SSS AAA \ |
| 100 | M M M E S S A A \ |
| 101 | M M M EEEE SS A A \ |
| 102 | M M M E SS AAAAA \ |
| 103 | M M E S S A A \ |
| 104 | M M EEEE SSS A A \ |
| 105 | " |
Brian Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 106 | }; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 107 | GLubyte table[256][4]; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 108 | |
| 109 | if (!glutExtensionSupported("GL_EXT_paletted_texture")) { |
| 110 | printf("Sorry, GL_EXT_paletted_texture not supported\n"); |
| 111 | exit(0); |
| 112 | } |
| 113 | |
Brian Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 114 | /* load the color table for each texel-index */ |
Brian Paul | 476609c | 2000-10-04 18:15:39 +0000 | [diff] [blame] | 115 | memset(table, 0, 256*4); |
| 116 | table[' '][0] = 255; |
| 117 | table[' '][1] = 255; |
| 118 | table[' '][2] = 255; |
| 119 | table[' '][3] = 64; |
Brian Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 120 | table['M'][0] = 255; |
| 121 | table['M'][1] = 0; |
| 122 | table['M'][2] = 0; |
Brian Paul | 476609c | 2000-10-04 18:15:39 +0000 | [diff] [blame] | 123 | table['M'][3] = 255; |
Brian Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 124 | table['E'][0] = 0; |
| 125 | table['E'][1] = 255; |
| 126 | table['E'][2] = 0; |
Brian Paul | 476609c | 2000-10-04 18:15:39 +0000 | [diff] [blame] | 127 | table['E'][3] = 255; |
| 128 | table['S'][0] = 0; |
| 129 | table['S'][1] = 0; |
Brian Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 130 | table['S'][2] = 255; |
Brian Paul | 476609c | 2000-10-04 18:15:39 +0000 | [diff] [blame] | 131 | table['S'][3] = 255; |
Brian Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 132 | table['A'][0] = 255; |
| 133 | table['A'][1] = 255; |
| 134 | table['A'][2] = 0; |
Brian Paul | 476609c | 2000-10-04 18:15:39 +0000 | [diff] [blame] | 135 | table['A'][3] = 255; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 136 | |
| 137 | #ifdef GL_EXT_paletted_texture |
| 138 | |
Brian Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 139 | #if defined(GL_EXT_shared_texture_palette) && defined(USE_SHARED_PALETTE) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 140 | printf("Using shared palette\n"); |
| 141 | glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, /* target */ |
| 142 | GL_RGBA, /* internal format */ |
| 143 | 256, /* table size */ |
| 144 | GL_RGBA, /* table format */ |
| 145 | GL_UNSIGNED_BYTE, /* table type */ |
| 146 | table); /* the color table */ |
| 147 | glEnable(GL_SHARED_TEXTURE_PALETTE_EXT); |
| 148 | #else |
| 149 | glColorTableEXT(GL_TEXTURE_2D, /* target */ |
| 150 | GL_RGBA, /* internal format */ |
| 151 | 256, /* table size */ |
| 152 | GL_RGBA, /* table format */ |
| 153 | GL_UNSIGNED_BYTE, /* table type */ |
| 154 | table); /* the color table */ |
| 155 | #endif |
| 156 | |
| 157 | glTexImage2D(GL_TEXTURE_2D, /* target */ |
| 158 | 0, /* level */ |
| 159 | GL_COLOR_INDEX8_EXT, /* internal format */ |
Brian Paul | d51b2c9 | 1999-11-02 15:09:04 +0000 | [diff] [blame] | 160 | WIDTH, HEIGHT, /* width, height */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 161 | 0, /* border */ |
| 162 | GL_COLOR_INDEX, /* texture format */ |
| 163 | GL_UNSIGNED_BYTE, /* texture type */ |
Brian Paul | f02a5f6 | 2002-07-12 15:54:01 +0000 | [diff] [blame] | 164 | texture); /* the texture */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 165 | #endif |
| 166 | |
| 167 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 168 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 169 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 170 | glEnable(GL_TEXTURE_2D); |
Brian Paul | 476609c | 2000-10-04 18:15:39 +0000 | [diff] [blame] | 171 | |
| 172 | glEnable(GL_BLEND); |
| 173 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
Jouk Jansen | 55590ee | 2000-10-05 07:17:43 +0000 | [diff] [blame] | 174 | #undef HEIGHT |
| 175 | #undef WIDTH |
Brian Paul | 476609c | 2000-10-04 18:15:39 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | |
| 179 | |
| 180 | /* |
| 181 | * Color ramp test |
| 182 | */ |
| 183 | static void Init2( void ) |
| 184 | { |
| 185 | #define HEIGHT 32 |
| 186 | #define WIDTH 256 |
| 187 | static char texture[HEIGHT][WIDTH]; |
| 188 | GLubyte table[256][4]; |
| 189 | int i, j; |
| 190 | |
| 191 | if (!glutExtensionSupported("GL_EXT_paletted_texture")) { |
| 192 | printf("Sorry, GL_EXT_paletted_texture not supported\n"); |
| 193 | exit(0); |
| 194 | } |
| 195 | |
| 196 | for (j = 0; j < HEIGHT; j++) { |
| 197 | for (i = 0; i < WIDTH; i++) { |
| 198 | texture[j][i] = i; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | for (i = 0; i < 255; i++) { |
| 203 | table[i][0] = i; |
| 204 | table[i][1] = 0; |
| 205 | table[i][2] = 0; |
| 206 | table[i][3] = 255; |
| 207 | } |
| 208 | |
| 209 | #ifdef GL_EXT_paletted_texture |
| 210 | |
| 211 | #if defined(GL_EXT_shared_texture_palette) && defined(USE_SHARED_PALETTE) |
| 212 | printf("Using shared palette\n"); |
| 213 | glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, /* target */ |
| 214 | GL_RGBA, /* internal format */ |
| 215 | 256, /* table size */ |
| 216 | GL_RGBA, /* table format */ |
| 217 | GL_UNSIGNED_BYTE, /* table type */ |
| 218 | table); /* the color table */ |
| 219 | glEnable(GL_SHARED_TEXTURE_PALETTE_EXT); |
| 220 | #else |
| 221 | glColorTableEXT(GL_TEXTURE_2D, /* target */ |
| 222 | GL_RGBA, /* internal format */ |
| 223 | 256, /* table size */ |
| 224 | GL_RGBA, /* table format */ |
| 225 | GL_UNSIGNED_BYTE, /* table type */ |
| 226 | table); /* the color table */ |
| 227 | #endif |
| 228 | |
| 229 | glTexImage2D(GL_TEXTURE_2D, /* target */ |
| 230 | 0, /* level */ |
| 231 | GL_COLOR_INDEX8_EXT, /* internal format */ |
| 232 | WIDTH, HEIGHT, /* width, height */ |
| 233 | 0, /* border */ |
| 234 | GL_COLOR_INDEX, /* texture format */ |
| 235 | GL_UNSIGNED_BYTE, /* texture type */ |
| 236 | texture); /* teh texture */ |
| 237 | #endif |
| 238 | |
| 239 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 240 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 241 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 242 | glEnable(GL_TEXTURE_2D); |
| 243 | |
| 244 | glEnable(GL_BLEND); |
| 245 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | |
| 249 | int main( int argc, char *argv[] ) |
| 250 | { |
| 251 | glutInit( &argc, argv ); |
| 252 | glutInitWindowPosition( 0, 0 ); |
Brian Paul | 476609c | 2000-10-04 18:15:39 +0000 | [diff] [blame] | 253 | glutInitWindowSize( 400, 300 ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 254 | |
| 255 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); |
| 256 | |
| 257 | glutCreateWindow(argv[0]); |
José Fonseca | 2e61d13 | 2009-01-24 16:39:49 +0000 | [diff] [blame] | 258 | glewInit(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 259 | |
| 260 | Init(); |
Brian Paul | 5222662 | 2004-08-10 15:36:46 +0000 | [diff] [blame] | 261 | (void) Init2; /* silence warning */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 262 | |
| 263 | glutReshapeFunc( Reshape ); |
| 264 | glutKeyboardFunc( Key ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 265 | glutDisplayFunc( Display ); |
Brian Paul | 476609c | 2000-10-04 18:15:39 +0000 | [diff] [blame] | 266 | if (Anim) |
| 267 | glutIdleFunc( Idle ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 268 | |
| 269 | glutMainLoop(); |
| 270 | return 0; |
| 271 | } |