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.

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