Clone of mesa.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

paltex.c 7.1KB

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