blob: 0ebd3987f5816a4eb87031a727c1b91cdd07f915 [file] [log] [blame]
Brian Paul8743d002006-04-21 01:01:58 +00001/* Test glGenProgramsARB(), glIsProgramARB(), glLoadProgramARB() */
Karl Rasche4814d792003-11-23 17:44:02 +00002
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
11static void Display( void )
12{
13 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
14
15 glPushMatrix();
16
17 glBegin(GL_POLYGON);
Brian Paul8743d002006-04-21 01:01:58 +000018 glVertexAttrib2fARB(0, -1, -1);
19 glVertexAttrib2fARB(0, 1, -1);
20 glVertexAttrib2fARB(0, 0, 1);
Karl Rasche4814d792003-11-23 17:44:02 +000021 glEnd();
22
23 glPopMatrix();
24
25 glutSwapBuffers();
26}
27
28
29static void Reshape( int width, int height )
30{
31 glViewport( 0, 0, width, height );
32 glMatrixMode( GL_PROJECTION );
33 glLoadIdentity();
34 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
35 glMatrixMode( GL_MODELVIEW );
36 glLoadIdentity();
37 glTranslatef( 0.0, 0.0, -15.0 );
38}
39
40
41static void Key( unsigned char key, int x, int y )
42{
43 (void) x;
44 (void) y;
45 switch (key) {
46 case 27:
47 exit(0);
48 break;
49 }
50 glutPostRedisplay();
51}
52
53static void load_program(const char *prog, GLuint prognum)
54{
55 int a;
56 GLint errorpos, errno;
57
58 glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prognum);
59 glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
60 strlen(prog), (const GLubyte *) prog);
61
62 assert(glIsProgramARB(prognum));
63 errno = glGetError();
64 printf("glGetError = %d\n", errno);
65 if (errno != GL_NO_ERROR)
66 {
67 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
68 printf("errorpos: %d\n", errorpos);
Alan Hourihane29b2ced2004-01-28 16:28:53 +000069 printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB));
Karl Rasche4814d792003-11-23 17:44:02 +000070
71 for (a=-10; a<10; a++)
72 {
73 if ((errorpos+a < 0) || (errorpos+a >= strlen(prog))) continue;
74 printf("%c", prog[errorpos+a]);
75 }
76 printf("\n");
77 exit(1);
78 }
79}
80
81static void Init( void )
82{
83 GLuint prognum[4];
84
85 static const char *prog1 =
86 "!!ARBvp1.0\n"
87 "TEMP R0;\n"
88 "MUL result.color.primary.xyz, R0, program.local[35]; \n"
89 "END\n";
90 static const char *prog2 =
91 "!!ARBvp1.0\n"
92 "#\n"
93 "# c[0-3] = modelview projection (composite) matrix\n"
94 "# c[32] = normalized light direction in object-space\n"
95 "# c[35] = yellow diffuse material, (1.0, 1.0, 0.0, 1.0)\n"
96 "# c[64].x = 0.0\n"
97 "# c[64].z = 0.125, a scaling factor\n"
98 "TEMP R0, R1;\n"
99 "#\n"
100 "# outputs diffuse illumination for color and perturbed position\n"
101 "#\n"
102 "DP3 R0, program.local[32], vertex.normal; # light direction DOT normal\n"
103 "MUL result.color.primary.xyz, R0, program.local[35]; \n"
104 "MAX R0, program.local[64].x, R0; \n"
105 "MUL R0, R0, vertex.normal; \n"
106 "MUL R0, R0, program.local[64].z; \n"
107 "ADD R1, vertex.position, -R0; # perturb object space position\n"
Karl Raschea8a4ad42003-12-09 18:17:04 +0000108 "DP4 result.position.x, state.matrix.mvp.row[3], R1; \n"
Karl Rasche4814d792003-11-23 17:44:02 +0000109 "DP4 result.position.y, state.matrix.mvp.row[1], R1; \n"
110 "DP4 result.position.z, state.matrix.mvp.row[2], R1; \n"
111 "DP4 result.position.w, state.matrix.mvp.row[3], R1; \n"
112 "END\n";
113 static const char *prog3 =
114 "!!ARBvp1.0\n"
115 "TEMP R0, R1, R2, R3;\n"
116 "DP4 result.position.x, state.matrix.mvp.row[0], vertex.position;\n"
117 "DP4 result.position.y, state.matrix.mvp.row[1], vertex.position;\n"
118 "DP4 result.position.z, state.matrix.mvp.row[2], vertex.position;\n"
119 "DP4 result.position.w, state.matrix.mvp.row[3], vertex.position;\n"
120 "DP3 R0.x, state.matrix.modelview.inverse.row[0], vertex.normal;\n"
121 "DP3 R0.y, state.matrix.modelview.inverse.row[1], vertex.normal;\n"
122 "DP3 R0.z, state.matrix.modelview.inverse.row[2], vertex.normal;\n"
123 "DP3 R1.x, program.env[32], R0; # R1.x = Lpos DOT n'\n"
124 "DP3 R1.y, program.env[33], R0; # R1.y = hHat DOT n'\n"
125 "MOV R1.w, program.local[38].x; # R1.w = specular power\n"
126 "LIT R2, R1; # Compute lighting values\n"
127 "MAD R3, program.env[35].x, R2.y, program.env[35].y; # diffuse + emissive\n"
128 "MAD result.color.primary.xyz, program.env[36], R2.z, R3; # + specular\n"
129 "END\n";
130 static const char *prog4 =
131 "!!ARBvp1.0\n"
Karl Raschea8a4ad42003-12-09 18:17:04 +0000132 "TEMP R2, R3;\n"
133 "PARAM foo = {0., 0., 0., 1.};\n"
134 "PARAM blah[] = { program.local[0..8] };\n"
135 "ADDRESS A0;\n"
136 "ARL A0.x, foo.x;\n"
137 "DP4 R2, R3, blah[A0.x].x;\n"
138 "DP4 R2, R3, blah[A0.x + 5];\n"
139 "DP4 result.position, R3, blah[A0.x - 4];\n"
Karl Rasche4814d792003-11-23 17:44:02 +0000140 "END\n";
Karl Rasche4814d792003-11-23 17:44:02 +0000141
Brian Paul8743d002006-04-21 01:01:58 +0000142 glGenProgramsARB(4, prognum);
Karl Rasche4814d792003-11-23 17:44:02 +0000143
144 load_program(prog1, prognum[0]);
145 load_program(prog2, prognum[1]);
146 load_program(prog3, prognum[2]);
Karl Rasche4814d792003-11-23 17:44:02 +0000147 load_program(prog4, prognum[3]);
Karl Rasche4814d792003-11-23 17:44:02 +0000148}
149
150
151int main( int argc, char *argv[] )
152{
153 glutInit( &argc, argv );
154 glutInitWindowPosition( 0, 0 );
155 glutInitWindowSize( 250, 250 );
156 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
157 glutCreateWindow(argv[0]);
158 glutReshapeFunc( Reshape );
159 glutKeyboardFunc( Key );
160 glutDisplayFunc( Display );
161 Init();
162 glutMainLoop();
163 return 0;
164}