blob: e98b3ed046900e06ab067756d473722712e65c46 [file] [log] [blame]
Ian Romanicke5700a12005-11-14 23:31:05 +00001/*
2 * (C) Copyright IBM Corporation 2005
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25/**
26 * \file interleave.c
27 *
28 * Simple test of glInterleavedArrays functionality. For each mode, two
29 * meshes are drawn. One is drawn using interleaved arrays and the othe is
30 * drawn using immediate mode. Both should look identical.
31 *
32 * \author Ian Romanick <idr@us.ibm.com>
33 */
34
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
Ian Romanicka1632562005-11-15 01:46:49 +000038#include <time.h>
Ian Romanicke5700a12005-11-14 23:31:05 +000039#include <GL/glut.h>
40
41static int Width = 400;
42static int Height = 300;
43static const GLfloat Near = 5.0, Far = 25.0;
44
45static const GLfloat t[][4] = {
46 { 0.5, 0.0, 0.0, 1.0 },
47
48 { 0.25, 0.5, 0.0, 1.0 },
49 { 0.75, 0.5, 0.0, 1.0 },
50
51 { 0.0, 1.0, 0.0, 1.0 },
52 { 0.5, 1.0, 0.0, 1.0 },
53 { 1.0, 1.0, 0.0, 1.0 },
54};
55
56static const GLfloat c_f[][4] = {
57 { 1.0, 0.0, 0.0, 1.0 },
58
59 { 0.0, 1.0, 0.0, 1.0 },
60 { 0.0, 1.0, 0.0, 1.0 },
61
62 { 0.0, 0.0, 1.0, 1.0 },
63 { 1.0, 0.0, 1.0, 1.0 },
64 { 0.0, 0.0, 1.0, 1.0 },
65};
66
67static const GLubyte c_ub[][4] = {
68 { 0xff, 0x00, 0x00, 0xff },
69
70 { 0x00, 0xff, 0x00, 0xff },
71 { 0x00, 0xff, 0x00, 0xff },
72
73 { 0x00, 0x00, 0xff, 0xff },
74 { 0xff, 0x00, 0xff, 0xff },
75 { 0x00, 0x00, 0xff, 0xff },
76};
77
78static const GLfloat n[][3] = {
79 { 0.0, 0.0, -1.0 },
80
81 { 0.0, 0.0, -1.0 },
82 { 0.0, 0.0, -1.0 },
83
84 { 0.0, 0.0, -1.0 },
85 { 0.0, 0.0, -1.0 },
86 { 0.0, 0.0, -1.0 },
87};
88
89static const GLfloat v[][4] = {
90 { 0.0, 1.0, 0.0, 1.0, },
91
92 { -0.5, 0.0, 0.0, 1.0, },
93 { 0.5, 0.0, 0.0, 1.0, },
94
95 { -1.0, -1.0, 0.0, 1.0, },
96 { 0.0, -1.0, 0.0, 1.0, },
97 { 1.0, -1.0, 0.0, 1.0, },
98};
99
100static const unsigned indicies[12] = {
101 0, 1, 2,
102 1, 3, 4,
103 2, 4, 5,
104 1, 4, 2
105};
106
107#define NONE { NULL, 0, 0, 0 }
108#define V2F { v, 2, 2 * sizeof( GLfloat ), GL_FLOAT, sizeof( v[0] ) }
109#define V3F { v, 3, 3 * sizeof( GLfloat ), GL_FLOAT, sizeof( v[0] ) }
110#define V4F { v, 4, 4 * sizeof( GLfloat ), GL_FLOAT, sizeof( v[0] ) }
111
112#define C4UB { c_ub, 4, 4 * sizeof( GLubyte ), GL_UNSIGNED_BYTE, sizeof( c_ub[0] ) }
113#define C3F { c_f, 3, 3 * sizeof( GLfloat ), GL_FLOAT, sizeof( c_f[0] ) }
114#define C4F { c_f, 4, 4 * sizeof( GLfloat ), GL_FLOAT, sizeof( c_f[0] ) }
115
116#define T2F { t, 2, 2 * sizeof( GLfloat ), GL_FLOAT, sizeof( t[0] ) }
117#define T4F { t, 4, 4 * sizeof( GLfloat ), GL_FLOAT, sizeof( t[0] ) }
118
119#define N3F { n, 3, 3 * sizeof( GLfloat ), GL_FLOAT, sizeof( n[0] ) }
120
121struct interleave_info {
122 const void * data;
123 unsigned count;
124 unsigned size;
125 GLenum type;
126 unsigned stride;
127};
128
Ian Romanicka1632562005-11-15 01:46:49 +0000129#define NUM_MODES 14
130#define INVALID_MODE 14
131#define INVALID_STRIDE 15
Ian Romanicke5700a12005-11-14 23:31:05 +0000132
133struct interleave_info info[ NUM_MODES ][4] = {
134 { NONE, NONE, NONE, V2F },
135 { NONE, NONE, NONE, V3F },
136 { NONE, C4UB, NONE, V2F },
137 { NONE, C4UB, NONE, V3F },
138 { NONE, C3F, NONE, V3F },
139
140 { NONE, NONE, N3F, V3F },
141 { NONE, C4F, N3F, V3F },
142
143 { T2F, NONE, NONE, V3F },
144 { T4F, NONE, NONE, V4F },
145
146 { T2F, C4UB, NONE, V3F },
147 { T2F, C3F, NONE, V3F },
148 { T2F, NONE, N3F, V3F },
149 { T2F, C4F, N3F, V3F },
150 { T4F, C4F, N3F, V4F },
151};
152
153const char * const mode_names[ NUM_MODES ] = {
154 "GL_V2F",
155 "GL_V3F",
156 "GL_C4UB_V2F",
157 "GL_C4UB_V3F",
158 "GL_C3F_V3F",
159 "GL_N3F_V3F",
160 "GL_C4F_N3F_V3F",
161 "GL_T2F_V3F",
162 "GL_T4F_V4F",
163 "GL_T2F_C4UB_V3F",
164 "GL_T2F_C3F_V3F",
165 "GL_T2F_N3F_V3F",
166 "GL_T2F_C4F_N3F_V3F",
167 "GL_T4F_C4F_N3F_V4F",
168};
169
Ian Romanicka1632562005-11-15 01:46:49 +0000170static unsigned interleave_mode = 0;
171static GLboolean use_invalid_mode = GL_FALSE;
172static GLboolean use_invalid_stride = GL_FALSE;
Ian Romanicke5700a12005-11-14 23:31:05 +0000173
174#define DEREF(item,idx) (void *) & ((char *)curr_info[item].data)[idx * curr_info[item].stride]
175
176static void Display( void )
177{
178 const struct interleave_info * const curr_info = info[ interleave_mode ];
179
180 /* 4 floats for 12 verticies for 4 data elements.
181 */
182 char data[ (sizeof( GLfloat ) * 4) * 12 * 4 ];
183
184 unsigned i;
185 unsigned offset;
Ian Romanicka1632562005-11-15 01:46:49 +0000186 GLenum err;
187 GLenum format;
188 GLsizei stride;
Ian Romanicke5700a12005-11-14 23:31:05 +0000189
190
191 glClearColor(0.2, 0.2, 0.8, 0);
192 glClear( GL_COLOR_BUFFER_BIT );
193
194 glPushMatrix();
195
196 glTranslatef(-1.5, 0, 0);
197
198 glColor3fv( c_f[0] );
199
200 if ( curr_info[0].data != NULL ) {
201 glEnable( GL_TEXTURE_2D );
202 }
203 else {
204 glDisable( GL_TEXTURE_2D );
205 }
206
207
208 offset = 0;
209 glBegin(GL_TRIANGLES);
210 for ( i = 0 ; i < 12 ; i++ ) {
211 const unsigned index = indicies[i];
212
213
214 /* Handle the vertex texture coordinate.
215 */
216 if ( curr_info[0].data != NULL ) {
217 if ( curr_info[0].count == 2 ) {
218 glTexCoord2fv( DEREF(0, index) );
219 }
220 else {
221 glTexCoord4fv( DEREF(0, index) );
222 }
223
224 (void) memcpy( & data[ offset ], DEREF(0, index),
225 curr_info[0].size );
226 offset += curr_info[0].size;
227 }
228
229
230 /* Handle the vertex color.
231 */
232 if ( curr_info[1].data != NULL ) {
233 if ( curr_info[1].type == GL_FLOAT ) {
234 if ( curr_info[1].count == 3 ) {
235 glColor3fv( DEREF(1, index) );
236 }
237 else {
238 glColor4fv( DEREF(1, index) );
239 }
240 }
241 else {
242 glColor4ubv( DEREF(1, index) );
243 }
244
245 (void) memcpy( & data[ offset ], DEREF(1, index),
246 curr_info[1].size );
247 offset += curr_info[1].size;
248 }
249
250
251 /* Handle the vertex normal.
252 */
253 if ( curr_info[2].data != NULL ) {
254 glNormal3fv( DEREF(2, index) );
255
256 (void) memcpy( & data[ offset ], DEREF(2, index),
257 curr_info[2].size );
258 offset += curr_info[2].size;
259 }
260
261
262 switch( curr_info[3].count ) {
263 case 2:
264 glVertex2fv( DEREF(3, index) );
265 break;
266 case 3:
267 glVertex3fv( DEREF(3, index) );
268 break;
269 case 4:
270 glVertex4fv( DEREF(3, index) );
271 break;
272 }
273
274 (void) memcpy( & data[ offset ], DEREF(3, index),
275 curr_info[3].size );
276 offset += curr_info[3].size;
277 }
278 glEnd();
279
280
281 glTranslatef(3.0, 0, 0);
Ian Romanicka1632562005-11-15 01:46:49 +0000282
Ian Romanick5ef524c2005-11-15 01:55:46 +0000283 /* The masking with ~0x2A00 is a bit of a hack to make sure that format
284 * ends up with an invalid value no matter what rand() returns.
285 */
Ian Romanicka1632562005-11-15 01:46:49 +0000286 format = (use_invalid_mode)
287 ? (rand() & ~0x2A00) : GL_V2F + interleave_mode;
288 stride = (use_invalid_stride) ? -abs(rand()) : 0;
289
290 (void) glGetError();
Ian Romanick5ef524c2005-11-15 01:55:46 +0000291 glInterleavedArrays( format, stride, data );
Ian Romanicka1632562005-11-15 01:46:49 +0000292 err = glGetError();
293 if ( err ) {
294 printf("glInterleavedArrays(0x%04x, %d, %p) generated the error 0x%04x\n",
295 format, stride, data, err );
296 }
297 else {
298 glDrawArrays( GL_TRIANGLES, 0, 12 );
299 }
Ian Romanicke5700a12005-11-14 23:31:05 +0000300
301 glPopMatrix();
302
303 glutSwapBuffers();
304}
305
306
307static void Reshape( int width, int height )
308{
309 GLfloat ar = (float) width / (float) height;
310 Width = width;
311 Height = height;
312 glViewport( 0, 0, width, height );
313 glMatrixMode( GL_PROJECTION );
314 glLoadIdentity();
315 glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
316 glMatrixMode( GL_MODELVIEW );
317 glLoadIdentity();
318 glTranslatef( 0.0, 0.0, -15.0 );
319}
320
321
322static void Key( unsigned char key, int x, int y )
323{
324 (void) x;
325 (void) y;
326 switch (key) {
327 case 27:
328 exit(0);
329 break;
330 }
331 glutPostRedisplay();
332}
333
334
335static void ModeMenu( int entry )
336{
Ian Romanicka1632562005-11-15 01:46:49 +0000337 if ( entry == INVALID_MODE ) {
338 use_invalid_mode = GL_TRUE;
339 use_invalid_stride = GL_FALSE;
340 }
341 else if ( entry == INVALID_STRIDE ) {
342 use_invalid_mode = GL_FALSE;
343 use_invalid_stride = GL_TRUE;
344 }
345 else {
346 use_invalid_mode = GL_FALSE;
347 use_invalid_stride = GL_FALSE;
348 interleave_mode = entry;
349 }
Ian Romanicke5700a12005-11-14 23:31:05 +0000350}
351
352static void Init( void )
353{
354 const char * const ver_string = (const char * const)
355 glGetString( GL_VERSION );
356 const GLubyte tex[16] = {
357 0xff, 0x00, 0xff, 0x00,
358 0x00, 0xff, 0x00, 0xff,
359 0xff, 0x00, 0xff, 0x00,
360 0x00, 0xff, 0x00, 0xff,
361 };
362
363 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
364 printf("GL_VERSION = %s\n", ver_string);
365
366 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
367 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
368 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
369 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0,
370 GL_LUMINANCE, GL_UNSIGNED_BYTE, tex );
371
372 printf("Use the context menu (right click) to select the interleaved array mode.\n");
373 printf("Press ESCAPE to exit.\n\n");
374 printf("NOTE: This is *NOT* a very good test of the modes that use normals.\n");
375}
376
377
378int main( int argc, char *argv[] )
379{
380 unsigned i;
381
Ian Romanicka1632562005-11-15 01:46:49 +0000382 srand( time( NULL ) );
383
Ian Romanicke5700a12005-11-14 23:31:05 +0000384 glutInit( &argc, argv );
385 glutInitWindowPosition( 0, 0 );
386 glutInitWindowSize( Width, Height );
387 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
388 glutCreateWindow( "glInterleavedArrays test" );
389 glutReshapeFunc( Reshape );
390 glutKeyboardFunc( Key );
391 glutDisplayFunc( Display );
392
393 glutCreateMenu( ModeMenu );
394 for ( i = 0 ; i < NUM_MODES ; i++ ) {
395 glutAddMenuEntry( mode_names[i], i);
396 }
397
Ian Romanicka1632562005-11-15 01:46:49 +0000398 glutAddMenuEntry( "Random invalid mode", INVALID_MODE);
399 glutAddMenuEntry( "Random invalid stride", INVALID_STRIDE);
400
Ian Romanicke5700a12005-11-14 23:31:05 +0000401 glutAttachMenu(GLUT_RIGHT_BUTTON);
402
403 Init();
404 glutMainLoop();
405 return 0;
406}