2 * Test vertex arrays and multitexture.
3 * Press 'a' to toggle vertex arrays on/off.
4 * When you run this program you should see a square with four colors:
21 static GLuint Window
= 0;
23 static GLuint TexObj
[2];
24 static GLfloat Angle
= 0.0f
;
25 static GLboolean UseArrays
= 1, Anim
= 0;
27 static GLfloat VertArray
[4][2] = {
28 {-1.2, -1.2}, {1.2, -1.2}, {1.2, 1.2}, {-1.2, 1.2}
31 static GLfloat Tex0Array
[4][2] = {
32 {0, 0}, {1, 0}, {1, 1}, {0, 1}
35 static GLfloat Tex1Array
[4][2] = {
36 {0, 0}, {1, 0}, {1, 1}, {0, 1}
40 static void init_arrays(void)
42 glVertexPointer(2, GL_FLOAT
, 0, VertArray
);
43 glEnableClientState(GL_VERTEX_ARRAY
);
45 glClientActiveTextureARB(GL_TEXTURE0_ARB
);
46 glTexCoordPointer(2, GL_FLOAT
, 0, Tex0Array
);
47 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
49 glClientActiveTextureARB(GL_TEXTURE1_ARB
);
50 glTexCoordPointer(2, GL_FLOAT
, 0, Tex1Array
);
51 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
55 static void draw( void )
57 glClear( GL_COLOR_BUFFER_BIT
);
59 glColor3f( 0.0, 0.0, 0.0 );
61 /* draw first polygon */
63 glRotatef( Angle
, 0.0, 0.0, 1.0 );
66 glDrawArrays(GL_POLYGON
, 0, 4);
69 glBegin( GL_POLYGON
);
70 glTexCoord2f( 0.0, 0.0 );
71 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 0.0, 0.0);
72 glVertex2f( -1.0, -1.0 );
74 glTexCoord2f( 1.0, 0.0 );
75 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 1.0, 0.0);
76 glVertex2f( 1.0, -1.0 );
78 glTexCoord2f( 1.0, 1.0 );
79 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 1.0, 1.0);
80 glVertex2f( 1.0, 1.0 );
82 glTexCoord2f( 0.0, 1.0 );
83 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 0.0, 1.0);
84 glVertex2f( -1.0, 1.0 );
95 static void idle( void )
103 /* change view Angle, exit upon ESC */
104 static void key(unsigned char k
, int x
, int y
)
110 UseArrays
= !UseArrays
;
111 printf("UseArrays: %d\n", UseArrays
);
121 glDeleteTextures( 2, TexObj
);
122 glutDestroyWindow(Window
);
130 /* new window size or exposure */
131 static void reshape( int width
, int height
)
133 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
134 glMatrixMode(GL_PROJECTION
);
136 /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
137 glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
138 glMatrixMode(GL_MODELVIEW
);
140 glTranslatef( 0.0, 0.0, -8.0 );
144 static void init( void )
146 static int width
=8, height
=8;
150 /* generate texture object IDs */
151 glGenTextures( 2, TexObj
);
154 * setup first texture object
156 glActiveTextureARB(GL_TEXTURE0_ARB
);
157 glEnable( GL_TEXTURE_2D
);
158 glTexEnvi( GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_ADD
);
160 glBindTexture( GL_TEXTURE_2D
, TexObj
[0] );
161 assert(glIsTexture(TexObj
[0]));
164 for (i
=0;i
<height
;i
++) {
165 for (j
=0;j
<width
;j
++) {
167 if (i
< height
/ 2) {
168 tex
[p
][0] = 0; tex
[p
][1] = 0; tex
[p
][2] = 0;
171 tex
[p
][0] = 255; tex
[p
][1] = 0; tex
[p
][2] = 0;
176 glTexImage2D( GL_TEXTURE_2D
, 0, 3, width
, height
, 0,
177 GL_RGB
, GL_UNSIGNED_BYTE
, tex
);
178 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
179 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
180 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
181 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
185 * setup second texture object
187 glActiveTextureARB(GL_TEXTURE1_ARB
);
188 glEnable( GL_TEXTURE_2D
);
189 glTexEnvi( GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_ADD
);
191 glBindTexture( GL_TEXTURE_2D
, TexObj
[1] );
192 assert(glIsTexture(TexObj
[1]));
194 /* left=green, right = blue */
195 for (i
=0;i
<height
;i
++) {
196 for (j
=0;j
<width
;j
++) {
199 tex
[p
][0] = 0; tex
[p
][1] = 255; tex
[p
][2] = 0;
202 tex
[p
][0] = 0; tex
[p
][1] = 0; tex
[p
][2] = 255;
206 glTexImage2D( GL_TEXTURE_2D
, 0, 3, width
, height
, 0,
207 GL_RGB
, GL_UNSIGNED_BYTE
, tex
);
208 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
209 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
210 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
211 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
216 int main( int argc
, char *argv
[] )
218 glutInit(&argc
, argv
);
219 glutInitWindowPosition(0, 0);
220 glutInitWindowSize(300, 300);
221 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
223 Window
= glutCreateWindow("Texture Objects");
231 glutReshapeFunc( reshape
);
232 glutKeyboardFunc( key
);
234 glutIdleFunc( idle
);
235 glutDisplayFunc( draw
);