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.

subtex.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Test glTexSubImage mid-way through a frame.
  3. *
  4. * The same texture is used for both quads but it gets redefined
  5. * with glTexSubImage (or glTexImage) after the first quad.
  6. */
  7. #include <assert.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "GL/glut.h"
  11. static GLuint Window = 0;
  12. static GLboolean Anim = GL_FALSE;
  13. static GLfloat Angle = 0.0f;
  14. static void
  15. first_texture(void)
  16. {
  17. static int width=8, height=8;
  18. static GLubyte tex1[] = {
  19. 0, 0, 0, 0, 0, 0, 0, 0,
  20. 0, 0, 0, 0, 1, 0, 0, 0,
  21. 0, 0, 0, 1, 1, 0, 0, 0,
  22. 0, 0, 0, 0, 1, 0, 0, 0,
  23. 0, 0, 0, 0, 1, 0, 0, 0,
  24. 0, 0, 0, 0, 1, 0, 0, 0,
  25. 0, 0, 0, 1, 1, 1, 0, 0,
  26. 0, 0, 0, 0, 0, 0, 0, 0 };
  27. GLubyte tex[64][3];
  28. GLint i, j;
  29. /* red on white */
  30. for (i=0;i<height;i++) {
  31. for (j=0;j<width;j++) {
  32. int p = i*width+j;
  33. if (tex1[(height-i-1)*width+j]) {
  34. tex[p][0] = 255; tex[p][1] = 0; tex[p][2] = 0;
  35. }
  36. else {
  37. tex[p][0] = 255; tex[p][1] = 255; tex[p][2] = 255;
  38. }
  39. }
  40. }
  41. glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
  42. GL_RGB, GL_UNSIGNED_BYTE, tex );
  43. }
  44. static void
  45. second_texture(void)
  46. {
  47. static int width=8, height=8;
  48. static GLubyte tex2[] = {
  49. 0, 0, 0, 0, 0, 0, 0, 0,
  50. 0, 0, 0, 2, 2, 0, 0, 0,
  51. 0, 0, 2, 0, 0, 2, 0, 0,
  52. 0, 0, 0, 0, 0, 2, 0, 0,
  53. 0, 0, 0, 0, 2, 0, 0, 0,
  54. 0, 0, 0, 2, 0, 0, 0, 0,
  55. 0, 0, 2, 2, 2, 2, 0, 0,
  56. 0, 0, 0, 0, 0, 0, 0, 0 };
  57. GLubyte tex[64][3];
  58. GLint i, j;
  59. /* green on blue */
  60. for (i=0;i<height;i++) {
  61. for (j=0;j<width;j++) {
  62. int p = i*width+j;
  63. if (tex2[(height-i-1)*width+j]) {
  64. tex[p][0] = 0; tex[p][1] = 255; tex[p][2] = 0;
  65. }
  66. else {
  67. tex[p][0] = 0; tex[p][1] = 0; tex[p][2] = 255;
  68. }
  69. }
  70. }
  71. #if 0
  72. glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
  73. GL_RGB, GL_UNSIGNED_BYTE, tex );
  74. #else
  75. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
  76. GL_RGB, GL_UNSIGNED_BYTE, tex );
  77. #endif
  78. }
  79. static void draw( void )
  80. {
  81. glClear( GL_COLOR_BUFFER_BIT );
  82. glColor3f( 1.0, 1.0, 1.0 );
  83. /* draw first polygon */
  84. glPushMatrix();
  85. glTranslatef( -1.0, 0.0, 0.0 );
  86. glRotatef( Angle, 0.0, 0.0, 1.0 );
  87. first_texture();
  88. glBegin( GL_POLYGON );
  89. glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
  90. glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
  91. glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
  92. glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
  93. glEnd();
  94. glPopMatrix();
  95. /* draw second polygon */
  96. glPushMatrix();
  97. glTranslatef( 1.0, 0.0, 0.0 );
  98. glRotatef( Angle-90.0, 0.0, 1.0, 0.0 );
  99. second_texture();
  100. glBegin( GL_POLYGON );
  101. glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
  102. glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
  103. glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
  104. glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
  105. glEnd();
  106. glPopMatrix();
  107. glutSwapBuffers();
  108. }
  109. static void idle( void )
  110. {
  111. static double t0 = -1.;
  112. double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  113. if (t0 < 0.0)
  114. t0 = t;
  115. dt = t - t0;
  116. t0 = t;
  117. Angle += 120.0*dt;
  118. glutPostRedisplay();
  119. }
  120. /* change view Angle, exit upon ESC */
  121. static void key(unsigned char k, int x, int y)
  122. {
  123. (void) x;
  124. (void) y;
  125. switch (k) {
  126. case 'a':
  127. Anim = !Anim;
  128. if (Anim)
  129. glutIdleFunc( idle );
  130. else
  131. glutIdleFunc( NULL );
  132. break;
  133. case 27:
  134. glutDestroyWindow(Window);
  135. exit(0);
  136. }
  137. }
  138. /* new window size or exposure */
  139. static void reshape( int width, int height )
  140. {
  141. glViewport(0, 0, (GLint)width, (GLint)height);
  142. glMatrixMode(GL_PROJECTION);
  143. glLoadIdentity();
  144. /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
  145. glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
  146. glMatrixMode(GL_MODELVIEW);
  147. glLoadIdentity();
  148. glTranslatef( 0.0, 0.0, -8.0 );
  149. }
  150. static void init( void )
  151. {
  152. /* Setup texturing */
  153. glEnable( GL_TEXTURE_2D );
  154. glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
  155. glBindTexture( GL_TEXTURE_2D, 0 );
  156. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  157. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  158. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  159. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  160. }
  161. int main( int argc, char *argv[] )
  162. {
  163. glutInit(&argc, argv);
  164. glutInitWindowPosition(0, 0);
  165. glutInitWindowSize(300, 300);
  166. glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
  167. Window = glutCreateWindow("Texture Objects");
  168. if (!Window) {
  169. exit(1);
  170. }
  171. init();
  172. glutReshapeFunc( reshape );
  173. glutKeyboardFunc( key );
  174. if (Anim)
  175. glutIdleFunc( idle );
  176. glutDisplayFunc( draw );
  177. glutMainLoop();
  178. return 0;
  179. }