1/* Test glGenProgramsNV(), glIsProgramNV(), glLoadProgramNV() */ 2 3#include <stdio.h> 4#include <assert.h> 5#include <string.h> 6#include <stdlib.h> 7#include <math.h> 8#include <GL/glew.h> 9#include "glut_wrap.h" 10 11 12 13GLenum doubleBuffer; 14 15static void Init(void) 16{ 17 GLint errnum; 18 GLuint prognum; 19 20 static const char *prog1 = 21 "!!ARBvp1.0\n" 22 "PARAM Emission = state.material.emission; \n" 23 "PARAM Ambient = state.material.ambient; \n" 24 "PARAM Diffuse = state.material.diffuse; \n" 25 "PARAM Specular = state.material.specular; \n" 26 "DP4 result.position.x, Ambient, vertex.position;\n" 27 "DP4 result.position.y, Diffuse, vertex.position;\n" 28 "DP4 result.position.z, Specular, vertex.position;\n" 29 "DP4 result.position.w, Emission, vertex.position;\n" 30 "MOV result.color, vertex.color;\n" 31 "END\n"; 32 33 const float Ambient[4] = { 0.0, 1.0, 0.0, 0.0 }; 34 const float Diffuse[4] = { 1.0, 0.0, 0.0, 0.0 }; 35 const float Specular[4] = { 0.0, 0.0, 1.0, 0.0 }; 36 const float Emission[4] = { 0.0, 0.0, 0.0, 1.0 }; 37 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, Ambient); 38 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Diffuse); 39 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Specular); 40 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, Emission); 41 42 43 glGenProgramsARB(1, &prognum); 44 45 glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prognum); 46 glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, 47 strlen(prog1), (const GLubyte *) prog1); 48 49 assert(glIsProgramARB(prognum)); 50 errnum = glGetError(); 51 printf("glGetError = %d\n", errnum); 52 if (errnum != GL_NO_ERROR) 53 { 54 GLint errorpos; 55 56 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos); 57 printf("errorpos: %d\n", errorpos); 58 printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)); 59 } 60 61 glEnable(GL_VERTEX_PROGRAM_NV); 62 63} 64 65 66static void Reshape(int width, int height) 67{ 68 69 glViewport(0, 0, (GLint)width, (GLint)height); 70 71 glMatrixMode(GL_PROJECTION); 72 glLoadIdentity(); 73/* glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); */ 74 glMatrixMode(GL_MODELVIEW); 75} 76 77static void Key(unsigned char key, int x, int y) 78{ 79 80 switch (key) { 81 case 27: 82 exit(1); 83 default: 84 break; 85 } 86 87 glutPostRedisplay(); 88} 89 90static void Draw(void) 91{ 92 glClear(GL_COLOR_BUFFER_BIT); 93 94 glBegin(GL_TRIANGLES); 95 glColor3f(0,0,.7); 96 glVertex3f( 0.9, -0.9, -0.0); 97 glColor3f(.8,0,0); 98 glVertex3f( 0.9, 0.9, -0.0); 99 glColor3f(0,.9,0); 100 glVertex3f(-0.9, 0.0, -0.0); 101 glEnd(); 102 103 glFlush(); 104 105 if (doubleBuffer) { 106 glutSwapBuffers(); 107 } 108} 109 110static GLenum Args(int argc, char **argv) 111{ 112 GLint i; 113 114 doubleBuffer = GL_FALSE; 115 116 for (i = 1; i < argc; i++) { 117 if (strcmp(argv[i], "-sb") == 0) { 118 doubleBuffer = GL_FALSE; 119 } else if (strcmp(argv[i], "-db") == 0) { 120 doubleBuffer = GL_TRUE; 121 } else { 122 fprintf(stderr, "%s (Bad option).\n", argv[i]); 123 return GL_FALSE; 124 } 125 } 126 return GL_TRUE; 127} 128 129int main(int argc, char **argv) 130{ 131 GLenum type; 132 133 glutInit(&argc, argv); 134 135 if (Args(argc, argv) == GL_FALSE) { 136 exit(1); 137 } 138 139 glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250); 140 141 type = GLUT_RGB | GLUT_ALPHA; 142 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE; 143 glutInitDisplayMode(type); 144 145 if (glutCreateWindow(*argv) == GL_FALSE) { 146 exit(1); 147 } 148 149 glewInit(); 150 151 Init(); 152 153 glutReshapeFunc(Reshape); 154 glutKeyboardFunc(Key); 155 glutDisplayFunc(Draw); 156 glutMainLoop(); 157 return 0; 158} 159