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

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