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.

texwrap.c 8.2KB

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