vptest1.c revision 32001f49
132001f49Smrg/* Test glGenProgramsNV(), glIsProgramNV(), glLoadProgramNV() */
232001f49Smrg
332001f49Smrg#include <assert.h>
432001f49Smrg#include <string.h>
532001f49Smrg#include <stdio.h>
632001f49Smrg#include <stdlib.h>
732001f49Smrg#include <math.h>
832001f49Smrg#include <GL/glew.h>
932001f49Smrg#include "glut_wrap.h"
1032001f49Smrg
1132001f49Smrg
1232001f49Smrg
1332001f49Smrgstatic void Display( void )
1432001f49Smrg{
1532001f49Smrg   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
1632001f49Smrg
1732001f49Smrg   glPushMatrix();
1832001f49Smrg
1932001f49Smrg   glBegin(GL_POLYGON);
2032001f49Smrg   glVertexAttrib2fNV(0, -1, -1);
2132001f49Smrg   glVertexAttrib2fNV(0, 1, -1);
2232001f49Smrg   glVertexAttrib2fNV(0, 0,  1);
2332001f49Smrg   glEnd();
2432001f49Smrg
2532001f49Smrg   glPopMatrix();
2632001f49Smrg
2732001f49Smrg   glutSwapBuffers();
2832001f49Smrg}
2932001f49Smrg
3032001f49Smrg
3132001f49Smrgstatic void Reshape( int width, int height )
3232001f49Smrg{
3332001f49Smrg   glViewport( 0, 0, width, height );
3432001f49Smrg   glMatrixMode( GL_PROJECTION );
3532001f49Smrg   glLoadIdentity();
3632001f49Smrg   glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
3732001f49Smrg   glMatrixMode( GL_MODELVIEW );
3832001f49Smrg   glLoadIdentity();
3932001f49Smrg   glTranslatef( 0.0, 0.0, -15.0 );
4032001f49Smrg}
4132001f49Smrg
4232001f49Smrg
4332001f49Smrgstatic void Key( unsigned char key, int x, int y )
4432001f49Smrg{
4532001f49Smrg   (void) x;
4632001f49Smrg   (void) y;
4732001f49Smrg   switch (key) {
4832001f49Smrg      case 27:
4932001f49Smrg         exit(0);
5032001f49Smrg         break;
5132001f49Smrg   }
5232001f49Smrg   glutPostRedisplay();
5332001f49Smrg}
5432001f49Smrg
5532001f49Smrg
5632001f49Smrgstatic void Init( void )
5732001f49Smrg{
5832001f49Smrg   static const char *prog1 =
5932001f49Smrg      "!!VP1.0\n"
6032001f49Smrg      "MUL   o[COL0].xyz, R0, c[35]; \n"
6132001f49Smrg      "END\n";
6232001f49Smrg   static const char *prog2 =
6332001f49Smrg      "!!VP1.0\n"
6432001f49Smrg      "#\n"
6532001f49Smrg      "# c[0-3]  = modelview projection (composite) matrix\n"
6632001f49Smrg      "# c[32]   = normalized light direction in object-space\n"
6732001f49Smrg      "# c[35]   = yellow diffuse material, (1.0, 1.0, 0.0, 1.0)\n"
6832001f49Smrg      "# c[64].x = 0.0\n"
6932001f49Smrg      "# c[64].z = 0.125, a scaling factor\n"
7032001f49Smrg      "#\n"
7132001f49Smrg      "# outputs diffuse illumination for color and perturbed position\n"
7232001f49Smrg      "#\n"
7332001f49Smrg      "DP3   R0, c[32], v[NRML];     # light direction DOT normal\n"
7432001f49Smrg      "MUL   o[COL0].xyz, R0, c[35]; \n"
7532001f49Smrg      "MAX   R0, c[64].x, R0; \n"
7632001f49Smrg      "MUL   R0, R0, v[NRML]; \n"
7732001f49Smrg      "MUL   R0, R0, c[64].z;  \n"
7832001f49Smrg      "ADD   R1, v[OPOS], -R0;       # perturb object space position\n"
7932001f49Smrg      "DP4   o[HPOS].x, c[0], R1; \n"
8032001f49Smrg      "DP4   o[HPOS].y, c[1], R1; \n"
8132001f49Smrg      "DP4   o[HPOS].z, c[2], R1; \n"
8232001f49Smrg      "DP4   o[HPOS].w, c[3], R1; \n"
8332001f49Smrg      "END\n";
8432001f49Smrg   static const char *prog3 =
8532001f49Smrg      "!!VP1.0\n"
8632001f49Smrg      "DP4   o[HPOS].x, c[0], v[OPOS];\n"
8732001f49Smrg      "DP4   o[HPOS].y, c[1], v[OPOS];\n"
8832001f49Smrg      "DP4   o[HPOS].z, c[2], v[OPOS];\n"
8932001f49Smrg      "DP4   o[HPOS].w, c[3], v[OPOS];\n"
9032001f49Smrg      "DP3   R0.x, c[4], v[NRML];\n"
9132001f49Smrg      "DP3   R0.y, c[5], v[NRML]; \n"
9232001f49Smrg      "DP3   R0.z, c[6], v[NRML];           # R0 = n' = transformed normal\n"
9332001f49Smrg      "DP3   R1.x, c[32], R0;               # R1.x = Lpos DOT n'\n"
9432001f49Smrg      "DP3   R1.y, c[33], R0;               # R1.y = hHat DOT n'\n"
9532001f49Smrg      "MOV   R1.w, c[38].x;                 # R1.w = specular power\n"
9632001f49Smrg      "LIT   R2, R1;                        # Compute lighting values\n"
9732001f49Smrg      "MAD   R3, c[35].x, R2.y, c[35].y;    # diffuse + emissive\n"
9832001f49Smrg      "MAD   o[COL0].xyz, c[36], R2.z, R3;  # + specular\n"
9932001f49Smrg      "END\n";
10032001f49Smrg   static const char *prog4 =
10132001f49Smrg      "!!VP1.0\n"
10232001f49Smrg      "DP4   R2, R3, c[A0.x];\n"
10332001f49Smrg      "DP4   R2, R3, c[A0.x + 5];\n"
10432001f49Smrg      "DP4   o[HPOS], R3, c[A0.x - 4];\n"
10532001f49Smrg      "END\n";
10632001f49Smrg   static const char *prog5 =
10732001f49Smrg      "!!VSP1.0\n"
10832001f49Smrg      "DP4   R2, R3, c[A0.x];\n"
10932001f49Smrg      "DP4   R2, R3, v[0];\n"
11032001f49Smrg      "DP4   c[3], R3, R2;\n"
11132001f49Smrg      "END\n";
11232001f49Smrg
11332001f49Smrg
11432001f49Smrg   GLuint progs[5];
11532001f49Smrg
11632001f49Smrg   if (!glutExtensionSupported("GL_NV_vertex_program")) {
11732001f49Smrg      printf("Sorry, this program requires GL_NV_vertex_program\n");
11832001f49Smrg      exit(1);
11932001f49Smrg   }
12032001f49Smrg
12132001f49Smrg   glGenProgramsNV(2, progs);
12232001f49Smrg   assert(progs[0]);
12332001f49Smrg   assert(progs[1]);
12432001f49Smrg   assert(progs[0] != progs[1]);
12532001f49Smrg
12632001f49Smrg   glGenProgramsNV(3, progs + 2);
12732001f49Smrg   assert(progs[2]);
12832001f49Smrg   assert(progs[3]);
12932001f49Smrg   assert(progs[2] != progs[3]);
13032001f49Smrg   assert(progs[0] != progs[2]);
13132001f49Smrg
13232001f49Smrg
13332001f49Smrg   glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 1,
13432001f49Smrg                   strlen(prog1),
13532001f49Smrg                   (const GLubyte *) prog1);
13632001f49Smrg   assert(glIsProgramNV(1));
13732001f49Smrg
13832001f49Smrg   glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 2,
13932001f49Smrg                   strlen(prog2),
14032001f49Smrg                   (const GLubyte *) prog2);
14132001f49Smrg   assert(glIsProgramNV(2));
14232001f49Smrg
14332001f49Smrg   glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 3,
14432001f49Smrg                   strlen(prog3),
14532001f49Smrg                   (const GLubyte *) prog3);
14632001f49Smrg   assert(glIsProgramNV(3));
14732001f49Smrg
14832001f49Smrg   glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 4,
14932001f49Smrg                   strlen(prog4),
15032001f49Smrg                   (const GLubyte *) prog4);
15132001f49Smrg   assert(glIsProgramNV(4));
15232001f49Smrg
15332001f49Smrg   glLoadProgramNV(GL_VERTEX_STATE_PROGRAM_NV, 5,
15432001f49Smrg                   strlen(prog5),
15532001f49Smrg                   (const GLubyte *) prog5);
15632001f49Smrg   assert(glIsProgramNV(5));
15732001f49Smrg
15832001f49Smrg   printf("glGetError = %d\n", (int) glGetError());
15932001f49Smrg}
16032001f49Smrg
16132001f49Smrg
16232001f49Smrgint main( int argc, char *argv[] )
16332001f49Smrg{
16432001f49Smrg   glutInit( &argc, argv );
16532001f49Smrg   glutInitWindowPosition( 0, 0 );
16632001f49Smrg   glutInitWindowSize( 250, 250 );
16732001f49Smrg   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
16832001f49Smrg   glutCreateWindow(argv[0]);
16932001f49Smrg   glewInit();
17032001f49Smrg   glutReshapeFunc( Reshape );
17132001f49Smrg   glutKeyboardFunc( Key );
17232001f49Smrg   glutDisplayFunc( Display );
17332001f49Smrg   Init();
17432001f49Smrg   glutMainLoop();
17532001f49Smrg   return 0;
17632001f49Smrg}
177