Merge branch 'nouveau-import'
[platform/upstream/mesa.git] / progs / tests / vptest1.c
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 #define GL_GLEXT_PROTOTYPES
9 #include <GL/glut.h>
10
11
12
13 static 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
31 static 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
43 static 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
56 static 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    glGenProgramsNV(2, progs);
117    assert(progs[0]);
118    assert(progs[1]);
119    assert(progs[0] != progs[1]);
120
121    glGenProgramsNV(3, progs + 2);
122    assert(progs[2]);
123    assert(progs[3]);
124    assert(progs[2] != progs[3]);
125    assert(progs[0] != progs[2]);
126
127
128    glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 1,
129                    strlen(prog1),
130                    (const GLubyte *) prog1);
131    assert(!glIsProgramNV(1));
132
133    glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 2,
134                    strlen(prog2),
135                    (const GLubyte *) prog2);
136    assert(glIsProgramNV(2));
137
138    glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 3,
139                    strlen(prog3),
140                    (const GLubyte *) prog3);
141    assert(glIsProgramNV(3));
142
143    glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 4,
144                    strlen(prog4),
145                    (const GLubyte *) prog4);
146    assert(glIsProgramNV(4));
147
148    glLoadProgramNV(GL_VERTEX_STATE_PROGRAM_NV, 5,
149                    strlen(prog5),
150                    (const GLubyte *) prog5);
151    assert(glIsProgramNV(5));
152
153    printf("glGetError = %d\n", (int) glGetError());
154 }
155
156
157 int main( int argc, char *argv[] )
158 {
159    glutInit( &argc, argv );
160    glutInitWindowPosition( 0, 0 );
161    glutInitWindowSize( 250, 250 );
162    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
163    glutCreateWindow(argv[0]);
164    glutReshapeFunc( Reshape );
165    glutKeyboardFunc( Key );
166    glutDisplayFunc( Display );
167    Init();
168    glutMainLoop();
169    return 0;
170 }