132001f49Smrg/* Test glGenProgramsARB(), glIsProgramARB(), glLoadProgramARB() */ 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 1132001f49Smrgstatic void Display( void ) 1232001f49Smrg{ 1332001f49Smrg glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 1432001f49Smrg 1532001f49Smrg glPushMatrix(); 1632001f49Smrg 1732001f49Smrg glBegin(GL_POLYGON); 1832001f49Smrg glVertexAttrib2fARB(0, -1, -1); 1932001f49Smrg glVertexAttrib2fARB(0, 1, -1); 2032001f49Smrg glVertexAttrib2fARB(0, 0, 1); 2132001f49Smrg glEnd(); 2232001f49Smrg 2332001f49Smrg glPopMatrix(); 2432001f49Smrg 2532001f49Smrg glutSwapBuffers(); 2632001f49Smrg} 2732001f49Smrg 2832001f49Smrg 2932001f49Smrgstatic void Reshape( int width, int height ) 3032001f49Smrg{ 3132001f49Smrg glViewport( 0, 0, width, height ); 3232001f49Smrg glMatrixMode( GL_PROJECTION ); 3332001f49Smrg glLoadIdentity(); 3432001f49Smrg glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 ); 3532001f49Smrg glMatrixMode( GL_MODELVIEW ); 3632001f49Smrg glLoadIdentity(); 3732001f49Smrg glTranslatef( 0.0, 0.0, -15.0 ); 3832001f49Smrg} 3932001f49Smrg 4032001f49Smrg 4132001f49Smrgstatic void Key( unsigned char key, int x, int y ) 4232001f49Smrg{ 4332001f49Smrg (void) x; 4432001f49Smrg (void) y; 4532001f49Smrg switch (key) { 4632001f49Smrg case 27: 4732001f49Smrg exit(0); 4832001f49Smrg break; 4932001f49Smrg } 5032001f49Smrg glutPostRedisplay(); 5132001f49Smrg} 5232001f49Smrg 5332001f49Smrgstatic void load_program(const char *prog, GLuint prognum) 5432001f49Smrg{ 5532001f49Smrg int a; 567ec3b29aSmrg GLint errorpos, errnum; 5732001f49Smrg 5832001f49Smrg glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prognum); 5932001f49Smrg glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, 6032001f49Smrg strlen(prog), (const GLubyte *) prog); 6132001f49Smrg 6232001f49Smrg assert(glIsProgramARB(prognum)); 637ec3b29aSmrg errnum = glGetError(); 647ec3b29aSmrg printf("glGetError = %d\n", errnum); 657ec3b29aSmrg if (errnum != GL_NO_ERROR) 6632001f49Smrg { 6732001f49Smrg glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos); 6832001f49Smrg printf("errorpos: %d\n", errorpos); 6932001f49Smrg printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)); 7032001f49Smrg 7132001f49Smrg for (a=-10; a<10; a++) 7232001f49Smrg { 7332001f49Smrg if ((errorpos+a < 0) || (errorpos+a >= strlen(prog))) continue; 7432001f49Smrg printf("%c", prog[errorpos+a]); 7532001f49Smrg } 7632001f49Smrg printf("\n"); 7732001f49Smrg exit(1); 7832001f49Smrg } 7932001f49Smrg} 8032001f49Smrg 8132001f49Smrgstatic void Init( void ) 8232001f49Smrg{ 8332001f49Smrg GLuint prognum[4]; 8432001f49Smrg 8532001f49Smrg static const char *prog1 = 8632001f49Smrg "!!ARBvp1.0\n" 8732001f49Smrg "TEMP R0;\n" 8832001f49Smrg "MUL result.color.primary.xyz, R0, program.local[35]; \n" 8932001f49Smrg "END\n"; 9032001f49Smrg static const char *prog2 = 9132001f49Smrg "!!ARBvp1.0\n" 9232001f49Smrg "#\n" 9332001f49Smrg "# c[0-3] = modelview projection (composite) matrix\n" 9432001f49Smrg "# c[32] = normalized light direction in object-space\n" 9532001f49Smrg "# c[35] = yellow diffuse material, (1.0, 1.0, 0.0, 1.0)\n" 9632001f49Smrg "# c[64].x = 0.0\n" 9732001f49Smrg "# c[64].z = 0.125, a scaling factor\n" 9832001f49Smrg "TEMP R0, R1;\n" 9932001f49Smrg "#\n" 10032001f49Smrg "# outputs diffuse illumination for color and perturbed position\n" 10132001f49Smrg "#\n" 10232001f49Smrg "DP3 R0, program.local[32], vertex.normal; # light direction DOT normal\n" 10332001f49Smrg "MUL result.color.primary.xyz, R0, program.local[35]; \n" 10432001f49Smrg "MAX R0, program.local[64].x, R0; \n" 10532001f49Smrg "MUL R0, R0, vertex.normal; \n" 10632001f49Smrg "MUL R0, R0, program.local[64].z; \n" 10732001f49Smrg "ADD R1, vertex.position, -R0; # perturb object space position\n" 10832001f49Smrg "DP4 result.position.x, state.matrix.mvp.row[3], R1; \n" 10932001f49Smrg "DP4 result.position.y, state.matrix.mvp.row[1], R1; \n" 11032001f49Smrg "DP4 result.position.z, state.matrix.mvp.row[2], R1; \n" 11132001f49Smrg "DP4 result.position.w, state.matrix.mvp.row[3], R1; \n" 11232001f49Smrg "END\n"; 11332001f49Smrg static const char *prog3 = 11432001f49Smrg "!!ARBvp1.0\n" 11532001f49Smrg "TEMP R0, R1, R2, R3;\n" 11632001f49Smrg "DP4 result.position.x, state.matrix.mvp.row[0], vertex.position;\n" 11732001f49Smrg "DP4 result.position.y, state.matrix.mvp.row[1], vertex.position;\n" 11832001f49Smrg "DP4 result.position.z, state.matrix.mvp.row[2], vertex.position;\n" 11932001f49Smrg "DP4 result.position.w, state.matrix.mvp.row[3], vertex.position;\n" 12032001f49Smrg "DP3 R0.x, state.matrix.modelview.inverse.row[0], vertex.normal;\n" 12132001f49Smrg "DP3 R0.y, state.matrix.modelview.inverse.row[1], vertex.normal;\n" 12232001f49Smrg "DP3 R0.z, state.matrix.modelview.inverse.row[2], vertex.normal;\n" 12332001f49Smrg "DP3 R1.x, program.env[32], R0; # R1.x = Lpos DOT n'\n" 12432001f49Smrg "DP3 R1.y, program.env[33], R0; # R1.y = hHat DOT n'\n" 12532001f49Smrg "MOV R1.w, program.local[38].x; # R1.w = specular power\n" 12632001f49Smrg "LIT R2, R1; # Compute lighting values\n" 12732001f49Smrg "MAD R3, program.env[35].x, R2.y, program.env[35].y; # diffuse + emissive\n" 12832001f49Smrg "MAD result.color.primary.xyz, program.env[36], R2.z, R3; # + specular\n" 12932001f49Smrg "END\n"; 13032001f49Smrg static const char *prog4 = 13132001f49Smrg "!!ARBvp1.0\n" 13232001f49Smrg "TEMP R2, R3;\n" 13332001f49Smrg "PARAM foo = {0., 0., 0., 1.};\n" 13432001f49Smrg "PARAM blah[] = { program.local[0..8] };\n" 13532001f49Smrg "ADDRESS A0;\n" 13632001f49Smrg "ARL A0.x, foo.x;\n" 13732001f49Smrg "DP4 R2, R3, blah[A0.x].x;\n" 13832001f49Smrg "DP4 R2, R3, blah[A0.x + 5];\n" 13932001f49Smrg "DP4 result.position, R3, blah[A0.x - 4];\n" 14032001f49Smrg "END\n"; 14132001f49Smrg 14232001f49Smrg glGenProgramsARB(4, prognum); 14332001f49Smrg 14432001f49Smrg load_program(prog1, prognum[0]); 14532001f49Smrg load_program(prog2, prognum[1]); 14632001f49Smrg load_program(prog3, prognum[2]); 14732001f49Smrg load_program(prog4, prognum[3]); 14832001f49Smrg} 14932001f49Smrg 15032001f49Smrg 15132001f49Smrgint main( int argc, char *argv[] ) 15232001f49Smrg{ 15332001f49Smrg glutInit( &argc, argv ); 15432001f49Smrg glutInitWindowPosition( 0, 0 ); 15532001f49Smrg glutInitWindowSize( 250, 250 ); 15632001f49Smrg glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); 15732001f49Smrg glutCreateWindow(argv[0]); 15832001f49Smrg glewInit(); 15932001f49Smrg glutReshapeFunc( Reshape ); 16032001f49Smrg glutKeyboardFunc( Key ); 16132001f49Smrg glutDisplayFunc( Display ); 16232001f49Smrg Init(); 16332001f49Smrg glutMainLoop(); 16432001f49Smrg return 0; 16532001f49Smrg} 166