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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* $Id: paltex.c,v 1.2 1999/11/02 15:09:04 brianp Exp $ */
  2. /*
  3. * Paletted texture demo. Written by Brian Paul.
  4. * This program is in the public domain.
  5. */
  6. /*
  7. * $Log: paltex.c,v $
  8. * Revision 1.2 1999/11/02 15:09:04 brianp
  9. * new texture image, cleaned-up code
  10. *
  11. * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
  12. * Imported sources
  13. *
  14. * Revision 3.1 1999/03/28 18:20:49 brianp
  15. * minor clean-up
  16. *
  17. * Revision 3.0 1998/02/14 18:42:29 brianp
  18. * initial rev
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <math.h>
  24. #include <GL/glut.h>
  25. static float Rot = 0.0;
  26. static void Idle( void )
  27. {
  28. Rot += 5.0;
  29. glutPostRedisplay();
  30. }
  31. static void Display( void )
  32. {
  33. glClear( GL_COLOR_BUFFER_BIT );
  34. glPushMatrix();
  35. glRotatef(Rot, 0, 0, 1);
  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. glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
  51. glMatrixMode( GL_MODELVIEW );
  52. glLoadIdentity();
  53. glTranslatef( 0.0, 0.0, -7.0 );
  54. }
  55. static void Key( unsigned char key, int x, int y )
  56. {
  57. (void) x;
  58. (void) y;
  59. switch (key) {
  60. case 27:
  61. exit(0);
  62. break;
  63. }
  64. glutPostRedisplay();
  65. }
  66. static void Init( void )
  67. {
  68. #define HEIGHT 8
  69. #define WIDTH 32
  70. static char texture[HEIGHT][WIDTH] = {
  71. " ",
  72. " MMM EEEE SSS AAA ",
  73. " M M M E S S A A ",
  74. " M M M EEEE SS A A ",
  75. " M M M E SS AAAAA ",
  76. " M M E S S A A ",
  77. " M M EEEE SSS A A ",
  78. " "
  79. };
  80. GLubyte table[256][4];
  81. int i;
  82. if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
  83. printf("Sorry, GL_EXT_paletted_texture not supported\n");
  84. exit(0);
  85. }
  86. /* load the color table for each texel-index */
  87. table[' '][0] = 50;
  88. table[' '][1] = 50;
  89. table[' '][2] = 50;
  90. table[' '][3] = 50;
  91. table['M'][0] = 255;
  92. table['M'][1] = 0;
  93. table['M'][2] = 0;
  94. table['M'][3] = 0;
  95. table['E'][0] = 0;
  96. table['E'][1] = 255;
  97. table['E'][2] = 0;
  98. table['E'][3] = 0;
  99. table['S'][0] = 40;
  100. table['S'][1] = 40;
  101. table['S'][2] = 255;
  102. table['S'][3] = 0;
  103. table['A'][0] = 255;
  104. table['A'][1] = 255;
  105. table['A'][2] = 0;
  106. table['A'][3] = 0;
  107. #ifdef GL_EXT_paletted_texture
  108. #if defined(GL_EXT_shared_texture_palette) && defined(USE_SHARED_PALETTE)
  109. printf("Using shared palette\n");
  110. glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, /* target */
  111. GL_RGBA, /* internal format */
  112. 256, /* table size */
  113. GL_RGBA, /* table format */
  114. GL_UNSIGNED_BYTE, /* table type */
  115. table); /* the color table */
  116. glEnable(GL_SHARED_TEXTURE_PALETTE_EXT);
  117. #else
  118. glColorTableEXT(GL_TEXTURE_2D, /* target */
  119. GL_RGBA, /* internal format */
  120. 256, /* table size */
  121. GL_RGBA, /* table format */
  122. GL_UNSIGNED_BYTE, /* table type */
  123. table); /* the color table */
  124. #endif
  125. glTexImage2D(GL_TEXTURE_2D, /* target */
  126. 0, /* level */
  127. GL_COLOR_INDEX8_EXT, /* internal format */
  128. WIDTH, HEIGHT, /* width, height */
  129. 0, /* border */
  130. GL_COLOR_INDEX, /* texture format */
  131. GL_UNSIGNED_BYTE, /* texture type */
  132. texture); /* teh texture */
  133. #endif
  134. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  135. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  136. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  137. glEnable(GL_TEXTURE_2D);
  138. }
  139. int main( int argc, char *argv[] )
  140. {
  141. glutInit( &argc, argv );
  142. glutInitWindowPosition( 0, 0 );
  143. glutInitWindowSize( 400, 400 );
  144. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  145. glutCreateWindow(argv[0]);
  146. Init();
  147. glutReshapeFunc( Reshape );
  148. glutKeyboardFunc( Key );
  149. glutDisplayFunc( Display );
  150. glutIdleFunc( Idle );
  151. glutMainLoop();
  152. return 0;
  153. }