Merge branch 'mesa_7_7_branch'
[mesa.git] / progs / trivial / vbo-tri.c
1 /* Even simpler for many drivers than trivial/tri -- pass-through
2 * vertex shader and vertex data in a VBO.
3 */
4
5 #include <assert.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <math.h>
10 #include <GL/glew.h>
11 #include <GL/glut.h>
12
13
14 struct {
15 GLfloat pos[4];
16 GLfloat color[4];
17 } verts[] =
18 {
19 { { -0.9, -0.9, 0.0, 1.0 },
20 {.8,0,0, 1},
21 },
22
23 { { 0.9, -0.9, 0.0, 1.0 },
24 { 0, .9, 0, 1 },
25 },
26
27 { { 0, 0.9, 0.0, 1.0 },
28 {0,0,.7, 1},
29 },
30 };
31
32 GLuint arrayObj;
33
34 static void Init( void )
35 {
36 GLint errno;
37 GLuint prognum;
38
39 static const char *prog1 =
40 "!!ARBvp1.0\n"
41 "MOV result.color, vertex.color;\n"
42 "MOV result.position, vertex.position;\n"
43 "END\n";
44
45
46 glGenProgramsARB(1, &prognum);
47
48 glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prognum);
49 glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
50 strlen(prog1), (const GLubyte *) prog1);
51
52 assert(glIsProgramARB(prognum));
53 errno = glGetError();
54 printf("glGetError = %d\n", errno);
55 if (errno != GL_NO_ERROR)
56 {
57 GLint errorpos;
58
59 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
60 printf("errorpos: %d\n", errorpos);
61 printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB));
62 }
63
64
65 glEnableClientState( GL_VERTEX_ARRAY );
66 glEnableClientState( GL_COLOR_ARRAY );
67
68 glGenBuffersARB(1, &arrayObj);
69 glBindBufferARB(GL_ARRAY_BUFFER_ARB, arrayObj);
70 glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts, GL_STATIC_DRAW_ARB);
71
72 glVertexPointer( 4, GL_FLOAT, sizeof(verts[0]), 0 );
73 glColorPointer( 4, GL_FLOAT, sizeof(verts[0]), (void *)(4*sizeof(float)) );
74 }
75
76
77
78 static void Display( void )
79 {
80 glClearColor(0.3, 0.3, 0.3, 1);
81 glClear( GL_COLOR_BUFFER_BIT );
82
83 glEnable(GL_VERTEX_PROGRAM_NV);
84 glDrawArrays( GL_TRIANGLES, 0, 3 );
85
86 glutSwapBuffers();
87 }
88
89
90 static void Reshape( int width, int height )
91 {
92 glViewport( 0, 0, width, height );
93 glMatrixMode( GL_PROJECTION );
94 glLoadIdentity();
95 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
96 glMatrixMode( GL_MODELVIEW );
97 glLoadIdentity();
98 /*glTranslatef( 0.0, 0.0, -15.0 );*/
99 }
100
101
102 static void Key( unsigned char key, int x, int y )
103 {
104 (void) x;
105 (void) y;
106 switch (key) {
107 case 27:
108 exit(0);
109 break;
110 }
111 glutPostRedisplay();
112 }
113
114
115
116
117 int main( int argc, char *argv[] )
118 {
119 glutInit( &argc, argv );
120 glutInitWindowPosition( 0, 0 );
121 glutInitWindowSize( 250, 250 );
122 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
123 glutCreateWindow(argv[0]);
124 glewInit();
125 glutReshapeFunc( Reshape );
126 glutKeyboardFunc( Key );
127 glutDisplayFunc( Display );
128 Init();
129 glutMainLoop();
130 return 0;
131 }