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