Convert libGL and DRI drivers to require libdrm.
[mesa-demos.git] / src / tests / arbvptorus.c
blobc1e2d0b76a8c38d8f767227b21fb18399280fd95
1 /*
2 * A lit, rotating torus via vertex program
3 */
5 #include <assert.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <math.h>
10 #define GL_GLEXT_PROTOTYPES
11 #include <GL/glut.h>
13 static float Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
14 static GLboolean Anim = GL_TRUE;
17 static void Idle( void )
19 Xrot += .3;
20 Yrot += .4;
21 Zrot += .2;
22 glutPostRedisplay();
26 static void Display( void )
28 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
30 glPushMatrix();
31 glRotatef(Xrot, 1, 0, 0);
32 glRotatef(Yrot, 0, 1, 0);
33 glRotatef(Zrot, 0, 0, 1);
34 glutSolidTorus(0.75, 2.0, 10, 20);
35 glPopMatrix();
37 glutSwapBuffers();
41 static void Reshape( int width, int height )
43 glViewport( 0, 0, width, height );
44 glMatrixMode( GL_PROJECTION );
45 glLoadIdentity();
46 glFrustum( -2.0, 2.0, -2.0, 2.0, 5.0, 25.0 );
47 glMatrixMode( GL_MODELVIEW );
48 glLoadIdentity();
49 glTranslatef( 0.0, 0.0, -12.0 );
53 static void Key( unsigned char key, int x, int y )
55 (void) x;
56 (void) y;
57 switch (key) {
58 case ' ':
59 Xrot = Yrot = Zrot = 0;
60 break;
61 case 'a':
62 Anim = !Anim;
63 if (Anim)
64 glutIdleFunc(Idle);
65 else
66 glutIdleFunc(NULL);
67 break;
68 case 'z':
69 Zrot -= 5.0;
70 break;
71 case 'Z':
72 Zrot += 5.0;
73 break;
74 case 27:
75 exit(0);
76 break;
78 glutPostRedisplay();
82 static void SpecialKey( int key, int x, int y )
84 const GLfloat step = 3.0;
85 (void) x;
86 (void) y;
87 switch (key) {
88 case GLUT_KEY_UP:
89 Xrot -= step;
90 break;
91 case GLUT_KEY_DOWN:
92 Xrot += step;
93 break;
94 case GLUT_KEY_LEFT:
95 Yrot -= step;
96 break;
97 case GLUT_KEY_RIGHT:
98 Yrot += step;
99 break;
101 glutPostRedisplay();
105 static void Init( void )
107 GLint errno;
108 GLuint prognum;
110 /* borrowed from an nvidia demo:
111 * c[0..3] = modelview matrix
112 * c[4..7] = invtrans modelview matrix
113 * c[32] = light pos
114 * c[35] = diffuse color
116 static const char prog[] =
117 "!!ARBvp1.0\n"
118 "TEMP R0, R1; \n"
119 "#Simple transform and diffuse lighting\n"
120 "# object x MVP -> clip\n"
121 "DP4 result.position.x, state.matrix.mvp.row[0], vertex.position ;\n"
122 "DP4 result.position.y, state.matrix.mvp.row[1], vertex.position ;\n"
123 "DP4 result.position.z, state.matrix.mvp.row[2], vertex.position ;\n"
124 "DP4 result.position.w, state.matrix.mvp.row[3], vertex.position ;\n"
126 "# normal x MV-1T -> lighting normal\n"
127 "DP3 R1.x, state.matrix.modelview.invtrans.row[0], vertex.normal ;\n"
128 "DP3 R1.y, state.matrix.modelview.invtrans.row[1], vertex.normal;\n"
129 "DP3 R1.z, state.matrix.modelview.invtrans.row[2], vertex.normal;\n"
131 "DP3 R0, program.local[32], R1; # L.N\n"
132 #if 0
133 "MUL result.color.xyz, R0, program.local[35] ; # col = L.N * diffuse\n"
134 #else
135 "MUL result.color.primary.xyz, R0, program.local[35] ; # col = L.N * diffuse\n"
136 #endif
137 "MOV result.texcoord, vertex.texcoord;\n"
138 "END";
140 if (!glutExtensionSupported("GL_ARB_vertex_program")) {
141 printf("Sorry, this program requires GL_ARB_vertex_program");
142 exit(1);
146 glGenProgramsARB(1, &prognum);
148 glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prognum);
149 glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
150 strlen(prog), (const GLubyte *) prog);
152 assert(glIsProgramARB(prognum));
153 errno = glGetError();
154 printf("glGetError = %d\n", errno);
155 if (errno != GL_NO_ERROR)
157 GLint errorpos;
159 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
160 printf("errorpos: %d\n", errorpos);
161 printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB));
164 /* Light position */
165 glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB, 32, 2, 2, 4, 1);
166 /* Diffuse material color */
167 glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB, 35, 0.25, 0, 0.25, 1);
169 glEnable(GL_VERTEX_PROGRAM_ARB);
170 glEnable(GL_DEPTH_TEST);
171 glClearColor(0.3, 0.3, 0.3, 1);
175 int main( int argc, char *argv[] )
177 glutInit( &argc, argv );
178 glutInitWindowPosition( 0, 0 );
179 glutInitWindowSize( 250, 250 );
180 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
181 glutCreateWindow(argv[0]);
182 glutReshapeFunc( Reshape );
183 glutKeyboardFunc( Key );
184 glutSpecialFunc( SpecialKey );
185 glutDisplayFunc( Display );
186 if (Anim)
187 glutIdleFunc(Idle);
188 Init();
189 glutMainLoop();
190 return 0;