Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

paltex.c 7.1KB

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