summaryrefslogtreecommitdiffstats
path: root/progs/tests/cva.c
blob: 80483900cb7d077bb80aed85de9c9438533546f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

/*
 * Trivial CVA test, good for testing driver fastpaths (especially
 * indexed vertex buffers if they are supported).
 *
 * Gareth Hughes
 * November 2000
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>	/* for ptrdiff_t, referenced by GL.h when GL_GLEXT_LEGACY defined */
#ifdef _WIN32
#include <windows.h>
#endif
#define GL_GLEXT_LEGACY
#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/glext.h>

GLfloat verts[][4] = {
   { -0.5, -0.5, -2.0, 0.0 },
   {  0.5, -0.5, -2.0, 0.0 },
   { -0.5,  0.5, -2.0, 0.0 },
   {  0.5,  0.5, -2.0, 0.0 },
};

GLubyte color[][4] = {
   { 0xff, 0x00, 0x00, 0x00 },
   { 0x00, 0xff, 0x00, 0x00 },
   { 0x00, 0x00, 0xff, 0x00 },
   { 0xff, 0xff, 0xff, 0x00 },
};

GLuint indices[] = { 0, 1, 2, 3 };

GLboolean compiled = GL_TRUE;
GLboolean doubleBuffer = GL_TRUE;


void init( void )
{
   glClearColor( 0.0, 0.0, 0.0, 0.0 );
   glShadeModel( GL_SMOOTH );

   glFrontFace( GL_CCW );
   glCullFace( GL_BACK );
   glEnable( GL_CULL_FACE );

   glEnable( GL_DEPTH_TEST );

   glEnableClientState( GL_VERTEX_ARRAY );
   glEnableClientState( GL_COLOR_ARRAY );

   glMatrixMode( GL_PROJECTION );
   glLoadIdentity();
   glFrustum( -1.0, 1.0, -1.0, 1.0, 2.0, 10.0 );
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity();

   glVertexPointer( 3, GL_FLOAT, sizeof(verts[0]), verts );
   glColorPointer( 4, GL_UNSIGNED_BYTE, 0, color );

#ifdef GL_EXT_compiled_vertex_array
   if ( compiled ) {
      glLockArraysEXT( 0, 4 );
   }
#endif
}

void display( void )
{
   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

   glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices );

   glFlush();
   if ( doubleBuffer ) {
      glutSwapBuffers();
   }
}

void keyboard( unsigned char key, int x, int y )
{
   switch ( key ) {
      case 27:
         exit( 0 );
         break;
   }

   glutPostRedisplay();
}

GLboolean args( int argc, char **argv )
{
    GLint i;

    doubleBuffer = GL_TRUE;

    for ( i = 1 ; i < argc ; i++ ) {
	if ( strcmp( argv[i], "-sb" ) == 0 ) {
	    doubleBuffer = GL_FALSE;
	} else if ( strcmp( argv[i], "-db" ) == 0 ) {
	    doubleBuffer = GL_TRUE;
	} else {
	    fprintf( stderr, "%s (Bad option).\n", argv[i] );
	    return GL_FALSE;
	}
    }
    return GL_TRUE;
}

int main( int argc, char **argv )
{
   GLenum type;
   char *string;
   double version;

   glutInit( &argc, argv );

   if ( args( argc, argv ) == GL_FALSE ) {
      exit( 1 );
   }

   type = GLUT_RGB | GLUT_DEPTH;
   type |= ( doubleBuffer ) ? GLUT_DOUBLE : GLUT_SINGLE;

   glutInitDisplayMode( type );
   glutInitWindowSize( 250, 250 );
   glutInitWindowPosition( 100, 100 );
   glutCreateWindow( "CVA Test" );
   glewInit();

   /* Make sure the server supports GL 1.2 vertex arrays.
    */
   string = (char *) glGetString( GL_VERSION );

   version = atof(string);
   if ( version < 1.2 ) {
      fprintf( stderr, "This program requires OpenGL 1.2 vertex arrays.\n" );
      exit( -1 );
   }

   /* See if the server supports compiled vertex arrays.
    */
   string = (char *) glGetString( GL_EXTENSIONS );

   if ( !strstr( string, "GL_EXT_compiled_vertex_array" ) ) {
      fprintf( stderr, "Compiled vertex arrays not supported by this renderer.\n" );
      compiled = GL_FALSE;
   }

   init();

   glutDisplayFunc( display );
   glutKeyboardFunc( keyboard );
   glutMainLoop();

   return 0;
}