Merge remote branch 'origin/master' into lp-binning
[mesa.git] / progs / tests / mapvbo.c
1 /*
2 * Test glMapBuffer() call immediately after glDrawArrays().
3 * See details below.
4 *
5 * NOTE: Do not use freeglut with this test! It calls the Display()
6 * callback twice right away instead of just once.
7 *
8 * Brian Paul
9 * 27 Feb 2009
10 */
11
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <GL/glew.h>
16 #include <GL/glut.h>
17
18 static GLuint BufferID;
19
20
21 static GLuint Win;
22
23
24
25
26 /*
27 * Create VBO (position and color) and load with data.
28 */
29 static void
30 SetupBuffers(void)
31 {
32 static const GLfloat data[] = {
33 /* vertex */ /* color */
34 0, -1, 0, 1, 1, 0,
35 1, 0, 0, 1, 1, 0,
36 0, 1, 0, 1, 1, 0,
37 -1, 0, 0, 1, 1, 0
38 };
39
40 glGenBuffersARB(1, &BufferID);
41 glBindBufferARB(GL_ARRAY_BUFFER_ARB, BufferID);
42 glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(data), data,
43 GL_STATIC_DRAW_ARB);
44 }
45
46
47 static void
48 Draw(void)
49 {
50 static int count = 1;
51
52 printf("Draw Frame %d\n", count);
53 count++;
54
55 glBindBufferARB(GL_ARRAY_BUFFER_ARB, BufferID);
56 glVertexPointer(3, GL_FLOAT, 24, 0);
57 glEnableClientState(GL_VERTEX_ARRAY);
58
59 glColorPointer(3, GL_FLOAT, 24, (void*) 12);
60 glEnableClientState(GL_COLOR_ARRAY);
61
62 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
63
64 if (0)
65 glFinish();
66
67 /* Immediately map the color buffer and change something.
68 * This should not effect the first glDrawArrays above, but the
69 * next time we draw we should see a black vertex.
70 */
71 if (1) {
72 GLfloat *m = (GLfloat *) glMapBufferARB(GL_ARRAY_BUFFER_ARB,
73 GL_WRITE_ONLY_ARB);
74 m[3] = m[4] = m[5] = 0.0f; /* black vertex */
75 glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
76 }
77 }
78
79
80 static void Display( void )
81 {
82 glClear( GL_COLOR_BUFFER_BIT );
83 Draw();
84 glutSwapBuffers();
85 }
86
87
88 static void Reshape( int width, int height )
89 {
90 float ar = (float) width / (float) height;
91 glViewport( 0, 0, width, height );
92 glMatrixMode( GL_PROJECTION );
93 glLoadIdentity();
94 glFrustum( -ar, ar, -1.0, 1.0, 5.0, 25.0 );
95 glMatrixMode( GL_MODELVIEW );
96 glLoadIdentity();
97 glTranslatef( 0.0, 0.0, -15.0 );
98 }
99
100
101 static void Key( unsigned char key, int x, int y )
102 {
103 (void) x;
104 (void) y;
105 if (key == 27) {
106 glutDestroyWindow(Win);
107 exit(0);
108 }
109 glutPostRedisplay();
110 }
111
112
113 static void Init( void )
114 {
115 if (!glutExtensionSupported("GL_ARB_vertex_buffer_object")) {
116 printf("GL_ARB_vertex_buffer_object not found!\n");
117 exit(0);
118 }
119 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
120
121 SetupBuffers();
122 }
123
124
125 int main( int argc, char *argv[] )
126 {
127 glutInit( &argc, argv );
128 glutInitWindowPosition( 0, 0 );
129 glutInitWindowSize( 300, 300 );
130 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
131 Win = glutCreateWindow(argv[0]);
132 glewInit();
133 glutReshapeFunc( Reshape );
134 glutKeyboardFunc( Key );
135 glutDisplayFunc( Display );
136 Init();
137 glutMainLoop();
138 return 0;
139 }