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