Keith Whitwell | d9f362a | 2005-11-01 06:28:39 +0000 | [diff] [blame] | 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 | static const char *filename = NULL; |
| 12 | static GLuint nr_steps = 4; |
| 13 | |
| 14 | static void usage( char *name ) |
| 15 | { |
| 16 | fprintf( stderr, "usage: %s [ options ] shader_filename\n", name ); |
| 17 | fprintf( stderr, "\n" ); |
| 18 | fprintf( stderr, "options:\n" ); |
| 19 | fprintf( stderr, " -f flat shaded\n" ); |
| 20 | fprintf( stderr, " -nNr subdivision steps\n" ); |
| 21 | } |
| 22 | |
| 23 | |
| 24 | static void args(int argc, char *argv[]) |
| 25 | { |
| 26 | GLint i; |
Keith Whitwell | d9f362a | 2005-11-01 06:28:39 +0000 | [diff] [blame] | 27 | |
| 28 | for (i = 1; i < argc; i++) { |
| 29 | if (strncmp(argv[i], "-n", 2) == 0) { |
| 30 | nr_steps = atoi((argv[i]) + 2); |
| 31 | } |
| 32 | else if (strcmp(argv[i], "-f") == 0) { |
| 33 | glShadeModel(GL_FLAT); |
| 34 | } |
| 35 | else if (i == argc - 1) { |
| 36 | filename = argv[i]; |
| 37 | } |
| 38 | else { |
| 39 | usage(argv[0]); |
| 40 | exit(1); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | if (!filename) { |
| 45 | usage(argv[0]); |
| 46 | exit(1); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | static void Init( void ) |
| 53 | { |
| 54 | GLint errno; |
| 55 | GLuint prognum; |
| 56 | char buf[4096]; |
| 57 | GLuint sz; |
| 58 | FILE *f; |
| 59 | |
| 60 | if ((f = fopen(filename, "r")) == NULL) { |
| 61 | fprintf(stderr, "couldn't open %s\n", filename); |
| 62 | exit(1); |
| 63 | } |
| 64 | |
| 65 | sz = fread(buf, 1, sizeof(buf), f); |
| 66 | if (!feof(f)) { |
| 67 | fprintf(stderr, "file too long\n"); |
| 68 | exit(1); |
| 69 | } |
| 70 | |
Brian Paul | 9cd1cc0 | 2006-04-24 16:32:05 +0000 | [diff] [blame] | 71 | fprintf(stderr, "%.*s\n", sz, buf); |
Keith Whitwell | d9f362a | 2005-11-01 06:28:39 +0000 | [diff] [blame] | 72 | |
| 73 | glGenProgramsARB(1, &prognum); |
| 74 | |
| 75 | glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prognum); |
| 76 | glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, |
| 77 | sz, (const GLubyte *) buf); |
| 78 | |
Keith Whitwell | d9f362a | 2005-11-01 06:28:39 +0000 | [diff] [blame] | 79 | errno = glGetError(); |
| 80 | printf("glGetError = %d\n", errno); |
| 81 | if (errno != GL_NO_ERROR) |
| 82 | { |
| 83 | GLint errorpos; |
| 84 | |
| 85 | glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos); |
| 86 | printf("errorpos: %d\n", errorpos); |
| 87 | printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)); |
| 88 | } |
Keith Whitwell | a9b927c | 2005-11-01 20:29:59 +0000 | [diff] [blame] | 89 | assert(glIsProgramARB(prognum)); |
Keith Whitwell | d9f362a | 2005-11-01 06:28:39 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | |
| 93 | union vert { |
| 94 | struct { |
| 95 | GLfloat color[3]; |
| 96 | GLfloat pos[3]; |
| 97 | } v; |
| 98 | GLfloat f[6]; |
| 99 | }; |
| 100 | |
| 101 | static void make_midpoint( union vert *out, |
| 102 | const union vert *v0, |
| 103 | const union vert *v1) |
| 104 | { |
| 105 | int i; |
| 106 | for (i = 0; i < 6; i++) |
| 107 | out->f[i] = v0->f[i] + .5 * (v1->f[i] - v0->f[i]); |
| 108 | } |
| 109 | |
| 110 | static void subdiv( union vert *v0, |
| 111 | union vert *v1, |
| 112 | union vert *v2, |
| 113 | GLuint depth ) |
| 114 | { |
| 115 | if (depth == 0) { |
| 116 | glColor3fv(v0->v.color); |
| 117 | glVertex3fv(v0->v.pos); |
| 118 | glColor3fv(v1->v.color); |
| 119 | glVertex3fv(v1->v.pos); |
| 120 | glColor3fv(v2->v.color); |
| 121 | glVertex3fv(v2->v.pos); |
| 122 | } |
| 123 | else { |
| 124 | union vert m[3]; |
| 125 | |
| 126 | make_midpoint(&m[0], v0, v1); |
| 127 | make_midpoint(&m[1], v1, v2); |
| 128 | make_midpoint(&m[2], v2, v0); |
| 129 | |
| 130 | subdiv(&m[0], &m[2], v0, depth-1); |
| 131 | subdiv(&m[1], &m[0], v1, depth-1); |
| 132 | subdiv(&m[2], &m[1], v2, depth-1); |
| 133 | subdiv(&m[0], &m[1], &m[2], depth-1); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** Assignment */ |
| 138 | #define ASSIGN_3V( V, V0, V1, V2 ) \ |
| 139 | do { \ |
| 140 | V[0] = V0; \ |
| 141 | V[1] = V1; \ |
| 142 | V[2] = V2; \ |
| 143 | } while(0) |
| 144 | |
| 145 | static void Display( void ) |
| 146 | { |
| 147 | glClearColor(0.3, 0.3, 0.3, 1); |
| 148 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
| 149 | |
| 150 | glEnable(GL_VERTEX_PROGRAM_NV); |
| 151 | |
| 152 | glBegin(GL_TRIANGLES); |
| 153 | |
| 154 | |
| 155 | { |
| 156 | union vert v[3]; |
| 157 | |
| 158 | ASSIGN_3V(v[0].v.color, 0,0,1); |
| 159 | ASSIGN_3V(v[0].v.pos, 0.9, -0.9, 0.0); |
Keith Whitwell | da70bc6 | 2005-11-01 12:22:48 +0000 | [diff] [blame] | 160 | ASSIGN_3V(v[1].v.color, 1,0,0); |
Keith Whitwell | a605d9c | 2005-11-01 12:20:13 +0000 | [diff] [blame] | 161 | ASSIGN_3V(v[1].v.pos, 0.9, 0.9, 0.0); |
Keith Whitwell | da70bc6 | 2005-11-01 12:22:48 +0000 | [diff] [blame] | 162 | ASSIGN_3V(v[2].v.color, 0,1,0); |
Keith Whitwell | d9f362a | 2005-11-01 06:28:39 +0000 | [diff] [blame] | 163 | ASSIGN_3V(v[2].v.pos, -0.9, 0, 0.0); |
| 164 | |
| 165 | subdiv(&v[0], &v[1], &v[2], nr_steps); |
| 166 | } |
| 167 | |
| 168 | glEnd(); |
| 169 | |
| 170 | |
| 171 | glFlush(); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | static void Reshape( int width, int height ) |
| 176 | { |
| 177 | glViewport( 0, 0, width, height ); |
| 178 | glMatrixMode( GL_PROJECTION ); |
| 179 | glLoadIdentity(); |
| 180 | glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); |
| 181 | glMatrixMode( GL_MODELVIEW ); |
| 182 | glLoadIdentity(); |
| 183 | /*glTranslatef( 0.0, 0.0, -15.0 );*/ |
| 184 | } |
| 185 | |
| 186 | |
| 187 | static void Key( unsigned char key, int x, int y ) |
| 188 | { |
| 189 | (void) x; |
| 190 | (void) y; |
| 191 | switch (key) { |
| 192 | case 27: |
| 193 | exit(0); |
| 194 | break; |
| 195 | } |
| 196 | glutPostRedisplay(); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | int main( int argc, char *argv[] ) |
| 203 | { |
| 204 | glutInit( &argc, argv ); |
| 205 | glutInitWindowPosition( 0, 0 ); |
| 206 | glutInitWindowSize( 250, 250 ); |
| 207 | glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH ); |
| 208 | glutCreateWindow(argv[0]); |
| 209 | glutReshapeFunc( Reshape ); |
| 210 | glutKeyboardFunc( Key ); |
| 211 | glutDisplayFunc( Display ); |
| 212 | args( argc, argv ); |
| 213 | Init(); |
| 214 | glutMainLoop(); |
| 215 | return 0; |
| 216 | } |