vptest1.c revision 32001f49
1/* Test glGenProgramsNV(), glIsProgramNV(), glLoadProgramNV() */
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
17   glPushMatrix();
18
19   glBegin(GL_POLYGON);
20   glVertexAttrib2fNV(0, -1, -1);
21   glVertexAttrib2fNV(0, 1, -1);
22   glVertexAttrib2fNV(0, 0,  1);
23   glEnd();
24
25   glPopMatrix();
26
27   glutSwapBuffers();
28}
29
30
31static void Reshape( int width, int height )
32{
33   glViewport( 0, 0, width, height );
34   glMatrixMode( GL_PROJECTION );
35   glLoadIdentity();
36   glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
37   glMatrixMode( GL_MODELVIEW );
38   glLoadIdentity();
39   glTranslatef( 0.0, 0.0, -15.0 );
40}
41
42
43static void Key( unsigned char key, int x, int y )
44{
45   (void) x;
46   (void) y;
47   switch (key) {
48      case 27:
49         exit(0);
50         break;
51   }
52   glutPostRedisplay();
53}
54
55
56static void Init( void )
57{
58   static const char *prog1 =
59      "!!VP1.0\n"
60      "MUL   o[COL0].xyz, R0, c[35]; \n"
61      "END\n";
62   static const char *prog2 =
63      "!!VP1.0\n"
64      "#\n"
65      "# c[0-3]  = modelview projection (composite) matrix\n"
66      "# c[32]   = normalized light direction in object-space\n"
67      "# c[35]   = yellow diffuse material, (1.0, 1.0, 0.0, 1.0)\n"
68      "# c[64].x = 0.0\n"
69      "# c[64].z = 0.125, a scaling factor\n"
70      "#\n"
71      "# outputs diffuse illumination for color and perturbed position\n"
72      "#\n"
73      "DP3   R0, c[32], v[NRML];     # light direction DOT normal\n"
74      "MUL   o[COL0].xyz, R0, c[35]; \n"
75      "MAX   R0, c[64].x, R0; \n"
76      "MUL   R0, R0, v[NRML]; \n"
77      "MUL   R0, R0, c[64].z;  \n"
78      "ADD   R1, v[OPOS], -R0;       # perturb object space position\n"
79      "DP4   o[HPOS].x, c[0], R1; \n"
80      "DP4   o[HPOS].y, c[1], R1; \n"
81      "DP4   o[HPOS].z, c[2], R1; \n"
82      "DP4   o[HPOS].w, c[3], R1; \n"
83      "END\n";
84   static const char *prog3 =
85      "!!VP1.0\n"
86      "DP4   o[HPOS].x, c[0], v[OPOS];\n"
87      "DP4   o[HPOS].y, c[1], v[OPOS];\n"
88      "DP4   o[HPOS].z, c[2], v[OPOS];\n"
89      "DP4   o[HPOS].w, c[3], v[OPOS];\n"
90      "DP3   R0.x, c[4], v[NRML];\n"
91      "DP3   R0.y, c[5], v[NRML]; \n"
92      "DP3   R0.z, c[6], v[NRML];           # R0 = n' = transformed normal\n"
93      "DP3   R1.x, c[32], R0;               # R1.x = Lpos DOT n'\n"
94      "DP3   R1.y, c[33], R0;               # R1.y = hHat DOT n'\n"
95      "MOV   R1.w, c[38].x;                 # R1.w = specular power\n"
96      "LIT   R2, R1;                        # Compute lighting values\n"
97      "MAD   R3, c[35].x, R2.y, c[35].y;    # diffuse + emissive\n"
98      "MAD   o[COL0].xyz, c[36], R2.z, R3;  # + specular\n"
99      "END\n";
100   static const char *prog4 =
101      "!!VP1.0\n"
102      "DP4   R2, R3, c[A0.x];\n"
103      "DP4   R2, R3, c[A0.x + 5];\n"
104      "DP4   o[HPOS], R3, c[A0.x - 4];\n"
105      "END\n";
106   static const char *prog5 =
107      "!!VSP1.0\n"
108      "DP4   R2, R3, c[A0.x];\n"
109      "DP4   R2, R3, v[0];\n"
110      "DP4   c[3], R3, R2;\n"
111      "END\n";
112
113
114   GLuint progs[5];
115
116   if (!glutExtensionSupported("GL_NV_vertex_program")) {
117      printf("Sorry, this program requires GL_NV_vertex_program\n");
118      exit(1);
119   }
120
121   glGenProgramsNV(2, progs);
122   assert(progs[0]);
123   assert(progs[1]);
124   assert(progs[0] != progs[1]);
125
126   glGenProgramsNV(3, progs + 2);
127   assert(progs[2]);
128   assert(progs[3]);
129   assert(progs[2] != progs[3]);
130   assert(progs[0] != progs[2]);
131
132
133   glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 1,
134                   strlen(prog1),
135                   (const GLubyte *) prog1);
136   assert(glIsProgramNV(1));
137
138   glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 2,
139                   strlen(prog2),
140                   (const GLubyte *) prog2);
141   assert(glIsProgramNV(2));
142
143   glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 3,
144                   strlen(prog3),
145                   (const GLubyte *) prog3);
146   assert(glIsProgramNV(3));
147
148   glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 4,
149                   strlen(prog4),
150                   (const GLubyte *) prog4);
151   assert(glIsProgramNV(4));
152
153   glLoadProgramNV(GL_VERTEX_STATE_PROGRAM_NV, 5,
154                   strlen(prog5),
155                   (const GLubyte *) prog5);
156   assert(glIsProgramNV(5));
157
158   printf("glGetError = %d\n", (int) glGetError());
159}
160
161
162int main( int argc, char *argv[] )
163{
164   glutInit( &argc, argv );
165   glutInitWindowPosition( 0, 0 );
166   glutInitWindowSize( 250, 250 );
167   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
168   glutCreateWindow(argv[0]);
169   glewInit();
170   glutReshapeFunc( Reshape );
171   glutKeyboardFunc( Key );
172   glutDisplayFunc( Display );
173   Init();
174   glutMainLoop();
175   return 0;
176}
177