blob: 73da4341c2e8f6e4345bff4034f667ce8321b035 [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>
38#include <GL/glut.h>
39
40static int Width = 400;
41static int Height = 300;
42static const GLfloat Near = 5.0, Far = 25.0;
43
44static const GLfloat t[][4] = {
45 { 0.5, 0.0, 0.0, 1.0 },
46
47 { 0.25, 0.5, 0.0, 1.0 },
48 { 0.75, 0.5, 0.0, 1.0 },
49
50 { 0.0, 1.0, 0.0, 1.0 },
51 { 0.5, 1.0, 0.0, 1.0 },
52 { 1.0, 1.0, 0.0, 1.0 },
53};
54
55static const GLfloat c_f[][4] = {
56 { 1.0, 0.0, 0.0, 1.0 },
57
58 { 0.0, 1.0, 0.0, 1.0 },
59 { 0.0, 1.0, 0.0, 1.0 },
60
61 { 0.0, 0.0, 1.0, 1.0 },
62 { 1.0, 0.0, 1.0, 1.0 },
63 { 0.0, 0.0, 1.0, 1.0 },
64};
65
66static const GLubyte c_ub[][4] = {
67 { 0xff, 0x00, 0x00, 0xff },
68
69 { 0x00, 0xff, 0x00, 0xff },
70 { 0x00, 0xff, 0x00, 0xff },
71
72 { 0x00, 0x00, 0xff, 0xff },
73 { 0xff, 0x00, 0xff, 0xff },
74 { 0x00, 0x00, 0xff, 0xff },
75};
76
77static const GLfloat n[][3] = {
78 { 0.0, 0.0, -1.0 },
79
80 { 0.0, 0.0, -1.0 },
81 { 0.0, 0.0, -1.0 },
82
83 { 0.0, 0.0, -1.0 },
84 { 0.0, 0.0, -1.0 },
85 { 0.0, 0.0, -1.0 },
86};
87
88static const GLfloat v[][4] = {
89 { 0.0, 1.0, 0.0, 1.0, },
90
91 { -0.5, 0.0, 0.0, 1.0, },
92 { 0.5, 0.0, 0.0, 1.0, },
93
94 { -1.0, -1.0, 0.0, 1.0, },
95 { 0.0, -1.0, 0.0, 1.0, },
96 { 1.0, -1.0, 0.0, 1.0, },
97};
98
99static const unsigned indicies[12] = {
100 0, 1, 2,
101 1, 3, 4,
102 2, 4, 5,
103 1, 4, 2
104};
105
106#define NONE { NULL, 0, 0, 0 }
107#define V2F { v, 2, 2 * sizeof( GLfloat ), GL_FLOAT, sizeof( v[0] ) }
108#define V3F { v, 3, 3 * sizeof( GLfloat ), GL_FLOAT, sizeof( v[0] ) }
109#define V4F { v, 4, 4 * sizeof( GLfloat ), GL_FLOAT, sizeof( v[0] ) }
110
111#define C4UB { c_ub, 4, 4 * sizeof( GLubyte ), GL_UNSIGNED_BYTE, sizeof( c_ub[0] ) }
112#define C3F { c_f, 3, 3 * sizeof( GLfloat ), GL_FLOAT, sizeof( c_f[0] ) }
113#define C4F { c_f, 4, 4 * sizeof( GLfloat ), GL_FLOAT, sizeof( c_f[0] ) }
114
115#define T2F { t, 2, 2 * sizeof( GLfloat ), GL_FLOAT, sizeof( t[0] ) }
116#define T4F { t, 4, 4 * sizeof( GLfloat ), GL_FLOAT, sizeof( t[0] ) }
117
118#define N3F { n, 3, 3 * sizeof( GLfloat ), GL_FLOAT, sizeof( n[0] ) }
119
120struct interleave_info {
121 const void * data;
122 unsigned count;
123 unsigned size;
124 GLenum type;
125 unsigned stride;
126};
127
128#define NUM_MODES 14
129
130struct interleave_info info[ NUM_MODES ][4] = {
131 { NONE, NONE, NONE, V2F },
132 { NONE, NONE, NONE, V3F },
133 { NONE, C4UB, NONE, V2F },
134 { NONE, C4UB, NONE, V3F },
135 { NONE, C3F, NONE, V3F },
136
137 { NONE, NONE, N3F, V3F },
138 { NONE, C4F, N3F, V3F },
139
140 { T2F, NONE, NONE, V3F },
141 { T4F, NONE, NONE, V4F },
142
143 { T2F, C4UB, NONE, V3F },
144 { T2F, C3F, NONE, V3F },
145 { T2F, NONE, N3F, V3F },
146 { T2F, C4F, N3F, V3F },
147 { T4F, C4F, N3F, V4F },
148};
149
150const char * const mode_names[ NUM_MODES ] = {
151 "GL_V2F",
152 "GL_V3F",
153 "GL_C4UB_V2F",
154 "GL_C4UB_V3F",
155 "GL_C3F_V3F",
156 "GL_N3F_V3F",
157 "GL_C4F_N3F_V3F",
158 "GL_T2F_V3F",
159 "GL_T4F_V4F",
160 "GL_T2F_C4UB_V3F",
161 "GL_T2F_C3F_V3F",
162 "GL_T2F_N3F_V3F",
163 "GL_T2F_C4F_N3F_V3F",
164 "GL_T4F_C4F_N3F_V4F",
165};
166
167unsigned interleave_mode = 0;
168
169#define DEREF(item,idx) (void *) & ((char *)curr_info[item].data)[idx * curr_info[item].stride]
170
171static void Display( void )
172{
173 const struct interleave_info * const curr_info = info[ interleave_mode ];
174
175 /* 4 floats for 12 verticies for 4 data elements.
176 */
177 char data[ (sizeof( GLfloat ) * 4) * 12 * 4 ];
178
179 unsigned i;
180 unsigned offset;
181
182
183 glClearColor(0.2, 0.2, 0.8, 0);
184 glClear( GL_COLOR_BUFFER_BIT );
185
186 glPushMatrix();
187
188 glTranslatef(-1.5, 0, 0);
189
190 glColor3fv( c_f[0] );
191
192 if ( curr_info[0].data != NULL ) {
193 glEnable( GL_TEXTURE_2D );
194 }
195 else {
196 glDisable( GL_TEXTURE_2D );
197 }
198
199
200 offset = 0;
201 glBegin(GL_TRIANGLES);
202 for ( i = 0 ; i < 12 ; i++ ) {
203 const unsigned index = indicies[i];
204
205
206 /* Handle the vertex texture coordinate.
207 */
208 if ( curr_info[0].data != NULL ) {
209 if ( curr_info[0].count == 2 ) {
210 glTexCoord2fv( DEREF(0, index) );
211 }
212 else {
213 glTexCoord4fv( DEREF(0, index) );
214 }
215
216 (void) memcpy( & data[ offset ], DEREF(0, index),
217 curr_info[0].size );
218 offset += curr_info[0].size;
219 }
220
221
222 /* Handle the vertex color.
223 */
224 if ( curr_info[1].data != NULL ) {
225 if ( curr_info[1].type == GL_FLOAT ) {
226 if ( curr_info[1].count == 3 ) {
227 glColor3fv( DEREF(1, index) );
228 }
229 else {
230 glColor4fv( DEREF(1, index) );
231 }
232 }
233 else {
234 glColor4ubv( DEREF(1, index) );
235 }
236
237 (void) memcpy( & data[ offset ], DEREF(1, index),
238 curr_info[1].size );
239 offset += curr_info[1].size;
240 }
241
242
243 /* Handle the vertex normal.
244 */
245 if ( curr_info[2].data != NULL ) {
246 glNormal3fv( DEREF(2, index) );
247
248 (void) memcpy( & data[ offset ], DEREF(2, index),
249 curr_info[2].size );
250 offset += curr_info[2].size;
251 }
252
253
254 switch( curr_info[3].count ) {
255 case 2:
256 glVertex2fv( DEREF(3, index) );
257 break;
258 case 3:
259 glVertex3fv( DEREF(3, index) );
260 break;
261 case 4:
262 glVertex4fv( DEREF(3, index) );
263 break;
264 }
265
266 (void) memcpy( & data[ offset ], DEREF(3, index),
267 curr_info[3].size );
268 offset += curr_info[3].size;
269 }
270 glEnd();
271
272
273 glTranslatef(3.0, 0, 0);
274 glInterleavedArrays( GL_V2F + interleave_mode, 0, data );
275 glDrawArrays( GL_TRIANGLES, 0, 12 );
276
277 glPopMatrix();
278
279 glutSwapBuffers();
280}
281
282
283static void Reshape( int width, int height )
284{
285 GLfloat ar = (float) width / (float) height;
286 Width = width;
287 Height = height;
288 glViewport( 0, 0, width, height );
289 glMatrixMode( GL_PROJECTION );
290 glLoadIdentity();
291 glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
292 glMatrixMode( GL_MODELVIEW );
293 glLoadIdentity();
294 glTranslatef( 0.0, 0.0, -15.0 );
295}
296
297
298static void Key( unsigned char key, int x, int y )
299{
300 (void) x;
301 (void) y;
302 switch (key) {
303 case 27:
304 exit(0);
305 break;
306 }
307 glutPostRedisplay();
308}
309
310
311static void ModeMenu( int entry )
312{
313 interleave_mode = entry;
314}
315
316static void Init( void )
317{
318 const char * const ver_string = (const char * const)
319 glGetString( GL_VERSION );
320 const GLubyte tex[16] = {
321 0xff, 0x00, 0xff, 0x00,
322 0x00, 0xff, 0x00, 0xff,
323 0xff, 0x00, 0xff, 0x00,
324 0x00, 0xff, 0x00, 0xff,
325 };
326
327 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
328 printf("GL_VERSION = %s\n", ver_string);
329
330 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
331 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
332 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
333 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0,
334 GL_LUMINANCE, GL_UNSIGNED_BYTE, tex );
335
336 printf("Use the context menu (right click) to select the interleaved array mode.\n");
337 printf("Press ESCAPE to exit.\n\n");
338 printf("NOTE: This is *NOT* a very good test of the modes that use normals.\n");
339}
340
341
342int main( int argc, char *argv[] )
343{
344 unsigned i;
345
346 glutInit( &argc, argv );
347 glutInitWindowPosition( 0, 0 );
348 glutInitWindowSize( Width, Height );
349 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
350 glutCreateWindow( "glInterleavedArrays test" );
351 glutReshapeFunc( Reshape );
352 glutKeyboardFunc( Key );
353 glutDisplayFunc( Display );
354
355 glutCreateMenu( ModeMenu );
356 for ( i = 0 ; i < NUM_MODES ; i++ ) {
357 glutAddMenuEntry( mode_names[i], i);
358 }
359
360 glutAttachMenu(GLUT_RIGHT_BUTTON);
361
362 Init();
363 glutMainLoop();
364 return 0;
365}