vptest2.c revision 32001f49
1/* Test vertex state program execution */
2
3#include <assert.h>
4#include <string.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <math.h>
8#include <GL/glew.h>
9#include "glut_wrap.h"
10
11
12
13static void Display( void )
14{
15   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
16   glPushMatrix();
17   glutSolidCube(2.0);
18   glPopMatrix();
19   glutSwapBuffers();
20}
21
22
23static void Reshape( int width, int height )
24{
25   glViewport( 0, 0, width, height );
26   glMatrixMode( GL_PROJECTION );
27   glLoadIdentity();
28   glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
29   glMatrixMode( GL_MODELVIEW );
30   glLoadIdentity();
31   glTranslatef( 0.0, 0.0, -15.0 );
32}
33
34
35static void Key( unsigned char key, int x, int y )
36{
37   (void) x;
38   (void) y;
39   switch (key) {
40      case 27:
41         exit(0);
42         break;
43   }
44   glutPostRedisplay();
45}
46
47
48static void Test1( void )
49{
50   static const GLfloat p[4] = {9, 8, 7, 6};
51   GLfloat q[4];
52   /* test addition */
53   static const char *prog =
54      "!!VSP1.0\n"
55      "MOV R0, c[0];\n"
56      "MOV R1, c[1];\n"
57      "ADD  c[2], R0, R1;\n"
58      "END\n";
59
60   glLoadProgramNV(GL_VERTEX_STATE_PROGRAM_NV, 1,
61                   strlen(prog),
62                   (const GLubyte *) prog);
63   assert(glIsProgramNV(1));
64
65   glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 0, 1, 2, 3, 4);
66   glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 1, 10, 20, 30, 40);
67
68   glExecuteProgramNV(GL_VERTEX_STATE_PROGRAM_NV, 1, p);
69
70   glGetProgramParameterfvNV(GL_VERTEX_PROGRAM_NV, 2, GL_PROGRAM_PARAMETER_NV, q);
71   printf("Result c[2] = %g %g %g %g  (should be 11 22 33 44)\n",
72          q[0], q[1], q[2], q[3]);
73}
74
75
76static void Test2( void )
77{
78   static const GLfloat p[4] = {9, 8, 7, 6};
79   GLfloat q[4];
80   /* test swizzling */
81   static const char *prog =
82      "!!VSP1.0\n"
83      "MOV R0, c[0].wzyx;\n"
84      "MOV R1, c[1].wzyx;\n"
85      "ADD c[2], R0, R1;\n"
86      "END\n";
87
88   glLoadProgramNV(GL_VERTEX_STATE_PROGRAM_NV, 1,
89                   strlen(prog),
90                   (const GLubyte *) prog);
91   assert(glIsProgramNV(1));
92
93   glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 0, 1, 2, 3, 4);
94   glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 1, 10, 20, 30, 40);
95
96   glExecuteProgramNV(GL_VERTEX_STATE_PROGRAM_NV, 1, p);
97
98   glGetProgramParameterfvNV(GL_VERTEX_PROGRAM_NV, 2, GL_PROGRAM_PARAMETER_NV, q);
99   printf("Result c[2] = %g %g %g %g  (should be 44 33 22 11)\n",
100          q[0], q[1], q[2], q[3]);
101}
102
103
104static void Test3( void )
105{
106   static const GLfloat p[4] = {0, 0, 0, 0};
107   GLfloat q[4];
108   /* normalize vector */
109   static const char *prog =
110      "!!VSP1.0\n"
111      "# c[0] = (nx,ny,nz)\n"
112      "# R0.xyz = normalize(R1)\n"
113      "# R0.w   = 1/sqrt(nx*nx + ny*ny + nz*nz)\n"
114      "# c[2] = R0\n"
115      "DP3 R0.w, c[0], c[0];\n"
116      "RSQ R0.w, R0.w;\n"
117      "MUL R0.xyz, c[0], R0.w;\n"
118      "MOV c[2], R0;\n"
119      "END\n";
120
121   glLoadProgramNV(GL_VERTEX_STATE_PROGRAM_NV, 1,
122                   strlen(prog),
123                   (const GLubyte *) prog);
124   assert(glIsProgramNV(1));
125
126   glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 0, 0, 10, 0, 0);
127
128   glExecuteProgramNV(GL_VERTEX_STATE_PROGRAM_NV, 1, p);
129
130   glGetProgramParameterfvNV(GL_VERTEX_PROGRAM_NV, 2, GL_PROGRAM_PARAMETER_NV, q);
131   printf("Result c[2] = %g %g %g %g  (should be 0, 1, 0, 0.1)\n",
132          q[0], q[1], q[2], q[3]);
133}
134
135
136int main( int argc, char *argv[] )
137{
138   glutInit( &argc, argv );
139   glutInitWindowPosition( 0, 0 );
140   glutInitWindowSize( 50, 50 );
141   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
142   glutCreateWindow(argv[0]);
143   glewInit();
144   glutReshapeFunc( Reshape );
145   glutKeyboardFunc( Key );
146   glutDisplayFunc( Display );
147
148   if (!glutExtensionSupported("GL_NV_vertex_program")) {
149      printf("Sorry, this program requires GL_NV_vertex_program\n");
150      exit(1);
151   }
152
153   Test1();
154   Test2();
155   Test3();
156   glutMainLoop();
157   return 0;
158}
159