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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Test texture wrap modes.
  3. * Press 'b' to toggle texture image borders. You should see the same
  4. * rendering whether or not you're using borders.
  5. *
  6. * Brian Paul March 2001
  7. */
  8. #define GL_GLEXT_PROTOTYPES
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <GL/glut.h>
  13. #ifndef GL_CLAMP_TO_BORDER
  14. #define GL_CLAMP_TO_BORDER 0x812D
  15. #endif
  16. #ifndef GL_MIRRORED_REPEAT
  17. #define GL_MIRRORED_REPEAT 0x8370
  18. #endif
  19. #ifndef GL_EXT_texture_mirror_clamp
  20. #define GL_MIRROR_CLAMP_EXT 0x8742
  21. #define GL_MIRROR_CLAMP_TO_EDGE_EXT 0x8743
  22. #define GL_MIRROR_CLAMP_TO_BORDER_EXT 0x8912
  23. #endif
  24. #define BORDER_TEXTURE 1
  25. #define NO_BORDER_TEXTURE 2
  26. #define SIZE 8
  27. static GLubyte BorderImage[SIZE+2][SIZE+2][4];
  28. static GLubyte NoBorderImage[SIZE][SIZE][4];
  29. static GLuint Border = 0;
  30. #define TILE_SIZE 110
  31. #define WRAP_MODE(m) { m , # m, GL_TRUE, 1.0, { NULL, NULL } }
  32. #define WRAP_EXT(m,e1,e2,v) { m , # m, GL_FALSE, v, { e1, e2 } }
  33. struct wrap_mode {
  34. GLenum mode;
  35. const char * name;
  36. GLboolean supported;
  37. GLfloat version;
  38. const char * extension_names[2];
  39. };
  40. static struct wrap_mode modes[] = {
  41. WRAP_MODE( GL_REPEAT ),
  42. WRAP_MODE( GL_CLAMP ),
  43. WRAP_EXT ( GL_CLAMP_TO_EDGE, "GL_EXT_texture_edge_clamp",
  44. "GL_SGIS_texture_edge_clamp",
  45. 1.2 ),
  46. WRAP_EXT ( GL_CLAMP_TO_BORDER, "GL_ARB_texture_border_clamp",
  47. "GL_SGIS_texture_border_clamp",
  48. 1.3 ),
  49. WRAP_EXT ( GL_MIRRORED_REPEAT, "GL_ARB_texture_mirrored_repeat",
  50. "GL_IBM_texture_mirrored_repeat",
  51. 1.4 ),
  52. WRAP_EXT ( GL_MIRROR_CLAMP_EXT, "GL_ATI_texture_mirror_once",
  53. "GL_EXT_texture_mirror_clamp",
  54. 999.0 ),
  55. WRAP_EXT ( GL_MIRROR_CLAMP_TO_BORDER_EXT, "GL_EXT_texture_mirror_clamp",
  56. NULL,
  57. 999.0 ),
  58. WRAP_EXT ( GL_MIRROR_CLAMP_TO_EDGE_EXT, "GL_ATI_texture_mirror_once",
  59. "GL_EXT_texture_mirror_clamp",
  60. 999.0 ),
  61. { 0 }
  62. };
  63. static void
  64. PrintString(const char *s)
  65. {
  66. while (*s) {
  67. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  68. s++;
  69. }
  70. }
  71. static void Display( void )
  72. {
  73. GLenum i, j;
  74. GLint offset;
  75. GLfloat version;
  76. /* Fill in the extensions that are supported.
  77. */
  78. version = atof( (char *) glGetString( GL_VERSION ) );
  79. for ( i = 0 ; modes[i].mode != 0 ; i++ ) {
  80. if ( ((modes[i].extension_names[0] != NULL)
  81. && glutExtensionSupported(modes[i].extension_names[0]))
  82. || ((modes[i].extension_names[1] != NULL)
  83. && glutExtensionSupported(modes[i].extension_names[1])) ) {
  84. modes[i].supported = GL_TRUE;
  85. }
  86. else if ( !modes[i].supported && (modes[i].version <= version) ) {
  87. fprintf( stderr, "WARNING: OpenGL library meets minimum version\n"
  88. " requirement for %s, but the\n"
  89. " extension string is not advertised.\n"
  90. " (%s%s%s)\n",
  91. modes[i].name,
  92. modes[i].extension_names[0],
  93. (modes[i].extension_names[1] != NULL)
  94. ? " or " : "",
  95. (modes[i].extension_names[1] != NULL)
  96. ? modes[i].extension_names[1] : "" );
  97. modes[i].supported = GL_TRUE;
  98. }
  99. }
  100. glClearColor(0.5, 0.5, 0.5, 1.0);
  101. glClear( GL_COLOR_BUFFER_BIT );
  102. #if 0
  103. /* draw texture as image */
  104. glDisable(GL_TEXTURE_2D);
  105. glWindowPos2iARB(1, 1);
  106. glDrawPixels(6, 6, GL_RGBA, GL_UNSIGNED_BYTE, (void *) TexImage);
  107. #endif
  108. glBindTexture(GL_TEXTURE_2D, Border ? BORDER_TEXTURE : NO_BORDER_TEXTURE);
  109. /* loop over min/mag filters */
  110. for (i = 0; i < 2; i++) {
  111. offset = 0;
  112. if (i) {
  113. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  114. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  115. }
  116. else {
  117. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  118. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  119. }
  120. /* loop over border modes */
  121. for (j = 0; modes[j].mode != 0; j++) {
  122. const GLfloat x0 = 0, y0 = 0, x1 = (TILE_SIZE - 10), y1 = (TILE_SIZE - 10);
  123. const GLfloat b = 1.2;
  124. const GLfloat s0 = -b, t0 = -b, s1 = 1.0+b, t1 = 1.0+b;
  125. if ( modes[j].supported != GL_TRUE )
  126. continue;
  127. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, modes[j].mode);
  128. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, modes[j].mode);
  129. glPushMatrix();
  130. glTranslatef(offset * TILE_SIZE + 10, i * TILE_SIZE + 40, 0);
  131. offset++;
  132. glEnable(GL_TEXTURE_2D);
  133. glColor3f(1, 1, 1);
  134. glBegin(GL_POLYGON);
  135. glTexCoord2f(s0, t0); glVertex2f(x0, y0);
  136. glTexCoord2f(s1, t0); glVertex2f(x1, y0);
  137. glTexCoord2f(s1, t1); glVertex2f(x1, y1);
  138. glTexCoord2f(s0, t1); glVertex2f(x0, y1);
  139. glEnd();
  140. /* draw red outline showing bounds of texture at s=0,1 and t=0,1 */
  141. glDisable(GL_TEXTURE_2D);
  142. glColor3f(1, 0, 0);
  143. glBegin(GL_LINE_LOOP);
  144. glVertex2f(x0 + b * (x1-x0) / (s1-s0), y0 + b * (y1-y0) / (t1-t0));
  145. glVertex2f(x1 - b * (x1-x0) / (s1-s0), y0 + b * (y1-y0) / (t1-t0));
  146. glVertex2f(x1 - b * (x1-x0) / (s1-s0), y1 - b * (y1-y0) / (t1-t0));
  147. glVertex2f(x0 + b * (x1-x0) / (s1-s0), y1 - b * (y1-y0) / (t1-t0));
  148. glEnd();
  149. glPopMatrix();
  150. }
  151. }
  152. glDisable(GL_TEXTURE_2D);
  153. glColor3f(1, 1, 1);
  154. offset = 0;
  155. for (i = 0; modes[i].mode != 0; i++) {
  156. if ( modes[i].supported ) {
  157. glWindowPos2iARB( offset * TILE_SIZE + 10, 5 + ((offset & 1) * 15) );
  158. PrintString(modes[i].name);
  159. offset++;
  160. }
  161. }
  162. glutSwapBuffers();
  163. }
  164. static void Reshape( int width, int height )
  165. {
  166. glViewport( 0, 0, width, height );
  167. glMatrixMode( GL_PROJECTION );
  168. glLoadIdentity();
  169. glOrtho(0, width, 0, height, -1, 1);
  170. glMatrixMode( GL_MODELVIEW );
  171. glLoadIdentity();
  172. }
  173. static void Key( unsigned char key, int x, int y )
  174. {
  175. (void) x;
  176. (void) y;
  177. switch (key) {
  178. case 'b':
  179. Border = !Border;
  180. printf("Texture Border Size = %d\n", Border);
  181. break;
  182. case 27:
  183. exit(0);
  184. break;
  185. }
  186. glutPostRedisplay();
  187. }
  188. static void Init( void )
  189. {
  190. static const GLubyte border[4] = { 0, 255, 0, 255 };
  191. static const GLfloat borderf[4] = { 0, 1.0, 0, 1.0 };
  192. GLint i, j;
  193. for (i = 0; i < SIZE+2; i++) {
  194. for (j = 0; j < SIZE+2; j++) {
  195. if (i == 0 || j == 0 || i == SIZE+1 || j == SIZE+1) {
  196. /* border color */
  197. BorderImage[i][j][0] = border[0];
  198. BorderImage[i][j][1] = border[1];
  199. BorderImage[i][j][2] = border[2];
  200. BorderImage[i][j][3] = border[3];
  201. }
  202. else if ((i + j) & 1) {
  203. /* white */
  204. BorderImage[i][j][0] = 255;
  205. BorderImage[i][j][1] = 255;
  206. BorderImage[i][j][2] = 255;
  207. BorderImage[i][j][3] = 255;
  208. }
  209. else {
  210. /* black */
  211. BorderImage[i][j][0] = 0;
  212. BorderImage[i][j][1] = 0;
  213. BorderImage[i][j][2] = 0;
  214. BorderImage[i][j][3] = 0;
  215. }
  216. }
  217. }
  218. glBindTexture(GL_TEXTURE_2D, BORDER_TEXTURE);
  219. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SIZE+2, SIZE+2, 1,
  220. GL_RGBA, GL_UNSIGNED_BYTE, (void *) BorderImage);
  221. for (i = 0; i < SIZE; i++) {
  222. for (j = 0; j < SIZE; j++) {
  223. if ((i + j) & 1) {
  224. /* white */
  225. NoBorderImage[i][j][0] = 255;
  226. NoBorderImage[i][j][1] = 255;
  227. NoBorderImage[i][j][2] = 255;
  228. NoBorderImage[i][j][3] = 255;
  229. }
  230. else {
  231. /* black */
  232. NoBorderImage[i][j][0] = 0;
  233. NoBorderImage[i][j][1] = 0;
  234. NoBorderImage[i][j][2] = 0;
  235. NoBorderImage[i][j][3] = 0;
  236. }
  237. }
  238. }
  239. glBindTexture(GL_TEXTURE_2D, NO_BORDER_TEXTURE);
  240. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SIZE, SIZE, 0,
  241. GL_RGBA, GL_UNSIGNED_BYTE, (void *) NoBorderImage);
  242. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderf);
  243. }
  244. int main( int argc, char *argv[] )
  245. {
  246. glutInit( &argc, argv );
  247. glutInitWindowPosition( 0, 0 );
  248. glutInitWindowSize( 1000, 270 );
  249. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  250. glutCreateWindow(argv[0]);
  251. glutReshapeFunc( Reshape );
  252. glutKeyboardFunc( Key );
  253. glutDisplayFunc( Display );
  254. Init();
  255. glutMainLoop();
  256. return 0;
  257. }