Clone of mesa.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

texwrap.c 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* $Id: texwrap.c,v 1.2 2001/04/12 20:50:26 brianp Exp $ */
  2. /*
  3. * Test texture wrap modes.
  4. * Press 'b' to toggle texture image borders. You should see the same
  5. * rendering whether or not you're using borders.
  6. *
  7. * Brian Paul March 2001
  8. */
  9. #define GL_GLEXT_PROTOTYPES
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <math.h>
  13. #include <GL/glut.h>
  14. #ifndef GL_CLAMP_TO_BORDER_ARB
  15. #define GL_CLAMP_TO_BORDER_ARB 0x812D
  16. #endif
  17. #define BORDER_TEXTURE 1
  18. #define NO_BORDER_TEXTURE 2
  19. #define SIZE 8
  20. static GLubyte BorderImage[SIZE+2][SIZE+2][4];
  21. static GLubyte NoBorderImage[SIZE][SIZE][4];
  22. static GLuint Border = 1;
  23. static void
  24. PrintString(const char *s)
  25. {
  26. while (*s) {
  27. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  28. s++;
  29. }
  30. }
  31. static void Display( void )
  32. {
  33. static const GLenum modes[] = {
  34. GL_REPEAT,
  35. GL_CLAMP,
  36. GL_CLAMP_TO_EDGE,
  37. GL_CLAMP_TO_BORDER_ARB
  38. };
  39. static const char *names[] = {
  40. "GL_REPEAT",
  41. "GL_CLAMP",
  42. "GL_CLAMP_TO_EDGE",
  43. "GL_CLAMP_TO_BORDER_ARB"
  44. };
  45. GLint i, j;
  46. GLint numModes;
  47. numModes = glutExtensionSupported("GL_ARB_texture_border_clamp") ? 4 : 3;
  48. glClearColor(0.5, 0.5, 0.5, 1.0);
  49. glClear( GL_COLOR_BUFFER_BIT );
  50. #if 0
  51. /* draw texture as image */
  52. glDisable(GL_TEXTURE_2D);
  53. glWindowPos2iMESA(1, 1);
  54. glDrawPixels(6, 6, GL_RGBA, GL_UNSIGNED_BYTE, (void *) TexImage);
  55. #endif
  56. glBindTexture(GL_TEXTURE_2D, Border ? BORDER_TEXTURE : NO_BORDER_TEXTURE);
  57. /* loop over min/mag filters */
  58. for (i = 0; i < 2; i++) {
  59. if (i) {
  60. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  61. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  62. }
  63. else {
  64. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  65. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  66. }
  67. /* loop over border modes */
  68. for (j = 0; j < numModes; j++) {
  69. const GLfloat x0 = 0, y0 = 0, x1 = 140, y1 = 140;
  70. const GLfloat b = 0.2;
  71. const GLfloat s0 = -b, t0 = -b, s1 = 1.0+b, t1 = 1.0+b;
  72. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, modes[j]);
  73. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, modes[j]);
  74. glPushMatrix();
  75. glTranslatef(j * 150 + 10, i * 150 + 25, 0);
  76. glEnable(GL_TEXTURE_2D);
  77. glColor3f(1, 1, 1);
  78. glBegin(GL_POLYGON);
  79. glTexCoord2f(s0, t0); glVertex2f(x0, y0);
  80. glTexCoord2f(s1, t0); glVertex2f(x1, y0);
  81. glTexCoord2f(s1, t1); glVertex2f(x1, y1);
  82. glTexCoord2f(s0, t1); glVertex2f(x0, y1);
  83. glEnd();
  84. /* draw red outline showing bounds of texture at s=0,1 and t=0,1 */
  85. glDisable(GL_TEXTURE_2D);
  86. glColor3f(1, 0, 0);
  87. glBegin(GL_LINE_LOOP);
  88. glVertex2f(x0 + b * (x1-x0) / (s1-s0), y0 + b * (y1-y0) / (t1-t0));
  89. glVertex2f(x1 - b * (x1-x0) / (s1-s0), y0 + b * (y1-y0) / (t1-t0));
  90. glVertex2f(x1 - b * (x1-x0) / (s1-s0), y1 - b * (y1-y0) / (t1-t0));
  91. glVertex2f(x0 + b * (x1-x0) / (s1-s0), y1 - b * (y1-y0) / (t1-t0));
  92. glEnd();
  93. glPopMatrix();
  94. }
  95. }
  96. glDisable(GL_TEXTURE_2D);
  97. glColor3f(1, 1, 1);
  98. for (i = 0; i < numModes; i++) {
  99. glWindowPos2iMESA( i * 150 + 10, 5);
  100. PrintString(names[i]);
  101. }
  102. glutSwapBuffers();
  103. }
  104. static void Reshape( int width, int height )
  105. {
  106. glViewport( 0, 0, width, height );
  107. glMatrixMode( GL_PROJECTION );
  108. glLoadIdentity();
  109. glOrtho(0, width, 0, height, -1, 1);
  110. glMatrixMode( GL_MODELVIEW );
  111. glLoadIdentity();
  112. }
  113. static void Key( unsigned char key, int x, int y )
  114. {
  115. (void) x;
  116. (void) y;
  117. switch (key) {
  118. case 'b':
  119. Border = !Border;
  120. printf("Texture Border Size = %d\n", Border);
  121. break;
  122. case 27:
  123. exit(0);
  124. break;
  125. }
  126. glutPostRedisplay();
  127. }
  128. static void Init( void )
  129. {
  130. static const GLubyte border[4] = { 0, 255, 0, 255 };
  131. static const GLfloat borderf[4] = { 0, 1.0, 0, 1.0 };
  132. GLint i, j;
  133. for (i = 0; i < SIZE+2; i++) {
  134. for (j = 0; j < SIZE+2; j++) {
  135. if (i == 0 || j == 0 || i == SIZE+1 || j == SIZE+1) {
  136. /* border color */
  137. BorderImage[i][j][0] = border[0];
  138. BorderImage[i][j][1] = border[1];
  139. BorderImage[i][j][2] = border[2];
  140. BorderImage[i][j][3] = border[3];
  141. }
  142. else if ((i + j) & 1) {
  143. /* white */
  144. BorderImage[i][j][0] = 255;
  145. BorderImage[i][j][1] = 255;
  146. BorderImage[i][j][2] = 255;
  147. BorderImage[i][j][3] = 255;
  148. }
  149. else {
  150. /* black */
  151. BorderImage[i][j][0] = 0;
  152. BorderImage[i][j][1] = 0;
  153. BorderImage[i][j][2] = 0;
  154. BorderImage[i][j][3] = 0;
  155. }
  156. }
  157. }
  158. glBindTexture(GL_TEXTURE_2D, BORDER_TEXTURE);
  159. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SIZE+2, SIZE+2, 1,
  160. GL_RGBA, GL_UNSIGNED_BYTE, (void *) BorderImage);
  161. for (i = 0; i < SIZE; i++) {
  162. for (j = 0; j < SIZE; j++) {
  163. if ((i + j) & 1) {
  164. /* white */
  165. NoBorderImage[i][j][0] = 255;
  166. NoBorderImage[i][j][1] = 255;
  167. NoBorderImage[i][j][2] = 255;
  168. NoBorderImage[i][j][3] = 255;
  169. }
  170. else {
  171. /* black */
  172. NoBorderImage[i][j][0] = 0;
  173. NoBorderImage[i][j][1] = 0;
  174. NoBorderImage[i][j][2] = 0;
  175. NoBorderImage[i][j][3] = 0;
  176. }
  177. }
  178. }
  179. glBindTexture(GL_TEXTURE_2D, NO_BORDER_TEXTURE);
  180. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SIZE, SIZE, 0,
  181. GL_RGBA, GL_UNSIGNED_BYTE, (void *) NoBorderImage);
  182. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderf);
  183. }
  184. int main( int argc, char *argv[] )
  185. {
  186. glutInit( &argc, argv );
  187. glutInitWindowPosition( 0, 0 );
  188. glutInitWindowSize( 650, 340 );
  189. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  190. glutCreateWindow(argv[0]);
  191. glutReshapeFunc( Reshape );
  192. glutKeyboardFunc( Key );
  193. glutDisplayFunc( Display );
  194. Init();
  195. glutMainLoop();
  196. return 0;
  197. }