1 /* Test glGenProgramsNV(), glIsProgramNV(), glLoadProgramNV() */
17 static const char *filename
= NULL
;
18 static GLuint nr_steps
= 4;
20 static void usage( char *name
)
22 fprintf( stderr
, "usage: %s [ options ] shader_filename\n", name
);
23 fprintf( stderr
, "\n" );
24 fprintf( stderr
, "options:\n" );
25 fprintf( stderr
, " -f flat shaded\n" );
26 fprintf( stderr
, " -nNr subdivision steps\n" );
27 fprintf( stderr
, " -fps show frames per second\n" );
30 unsigned show_fps
= 0;
31 unsigned int frame_cnt
= 0;
35 void alarmhandler(int);
37 void alarmhandler (int sig
)
40 printf("%d frames in 5.0 seconds = %.3f FPS\n", frame_cnt
,
45 signal(SIGALRM
, alarmhandler
);
51 static void args(int argc
, char *argv
[])
55 for (i
= 1; i
< argc
; i
++) {
56 if (strncmp(argv
[i
], "-n", 2) == 0) {
57 nr_steps
= atoi((argv
[i
]) + 2);
59 else if (strcmp(argv
[i
], "-f") == 0) {
60 glShadeModel(GL_FLAT
);
62 else if (strcmp(argv
[i
], "-fps") == 0) {
65 else if (i
== argc
- 1) {
82 static void Init( void )
90 if ((f
= fopen(filename
, "r")) == NULL
) {
91 fprintf(stderr
, "couldn't open %s\n", filename
);
95 sz
= (GLuint
) fread(buf
, 1, sizeof(buf
), f
);
97 fprintf(stderr
, "file too long\n");
101 fprintf(stderr
, "%.*s\n", sz
, buf
);
103 if (strncmp( buf
, "!!VP", 4 ) == 0) {
104 glEnable( GL_VERTEX_PROGRAM_NV
);
105 glGenProgramsNV( 1, &prognum
);
106 glBindProgramNV( GL_VERTEX_PROGRAM_NV
, prognum
);
107 glLoadProgramNV( GL_VERTEX_PROGRAM_NV
, prognum
, sz
, (const GLubyte
*) buf
);
108 assert( glIsProgramNV( prognum
) );
111 glEnable(GL_VERTEX_PROGRAM_ARB
);
113 glGenProgramsARB(1, &prognum
);
115 glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, prognum
);
116 glProgramStringARB(GL_VERTEX_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
117 sz
, (const GLubyte
*) buf
);
118 assert(glIsProgramARB(prognum
));
121 errno
= glGetError();
122 printf("glGetError = %d\n", errno
);
123 if (errno
!= GL_NO_ERROR
)
127 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errorpos
);
128 printf("errorpos: %d\n", errorpos
);
129 printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
));
133 const float Ambient
[4] = { 0.0, 1.0, 0.0, 0.0 };
134 const float Diffuse
[4] = { 1.0, 0.0, 0.0, 0.0 };
135 const float Specular
[4] = { 0.0, 0.0, 1.0, 0.0 };
136 const float Emission
[4] = { 0.0, 0.0, 0.0, 1.0 };
137 glMaterialfv(GL_FRONT_AND_BACK
, GL_AMBIENT
, Ambient
);
138 glMaterialfv(GL_FRONT_AND_BACK
, GL_DIFFUSE
, Diffuse
);
139 glMaterialfv(GL_FRONT_AND_BACK
, GL_SPECULAR
, Specular
);
140 glMaterialfv(GL_FRONT_AND_BACK
, GL_EMISSION
, Emission
);
153 static void make_midpoint( union vert
*out
,
154 const union vert
*v0
,
155 const union vert
*v1
)
158 for (i
= 0; i
< 6; i
++)
159 out
->f
[i
] = v0
->f
[i
] + .5 * (v1
->f
[i
] - v0
->f
[i
]);
162 static void subdiv( union vert
*v0
,
168 glColor3fv(v0
->v
.color
);
169 glVertex3fv(v0
->v
.pos
);
170 glColor3fv(v1
->v
.color
);
171 glVertex3fv(v1
->v
.pos
);
172 glColor3fv(v2
->v
.color
);
173 glVertex3fv(v2
->v
.pos
);
178 make_midpoint(&m
[0], v0
, v1
);
179 make_midpoint(&m
[1], v1
, v2
);
180 make_midpoint(&m
[2], v2
, v0
);
182 subdiv(&m
[0], &m
[2], v0
, depth
-1);
183 subdiv(&m
[1], &m
[0], v1
, depth
-1);
184 subdiv(&m
[2], &m
[1], v2
, depth
-1);
185 subdiv(&m
[0], &m
[1], &m
[2], depth
-1);
190 #define ASSIGN_3V( V, V0, V1, V2 ) \
197 static void Display( void )
199 glClearColor(0.3, 0.3, 0.3, 1);
200 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
202 glBegin(GL_TRIANGLES
);
208 ASSIGN_3V(v
[0].v
.color
, 0,0,1);
209 ASSIGN_3V(v
[0].v
.pos
, 0.9, -0.9, 0.0);
210 ASSIGN_3V(v
[1].v
.color
, 1,0,0);
211 ASSIGN_3V(v
[1].v
.pos
, 0.9, 0.9, 0.0);
212 ASSIGN_3V(v
[2].v
.color
, 0,1,0);
213 ASSIGN_3V(v
[2].v
.pos
, -0.9, 0, 0.0);
215 subdiv(&v
[0], &v
[1], &v
[2], nr_steps
);
229 static void Reshape( int width
, int height
)
231 glViewport( 0, 0, width
, height
);
232 glMatrixMode( GL_PROJECTION
);
234 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
235 glMatrixMode( GL_MODELVIEW
);
237 /*glTranslatef( 0.0, 0.0, -15.0 );*/
241 static void Key( unsigned char key
, int x
, int y
)
256 int main( int argc
, char *argv
[] )
258 glutInit( &argc
, argv
);
259 glutInitWindowPosition( 0, 0 );
260 glutInitWindowSize( 250, 250 );
261 glutInitDisplayMode( GLUT_RGB
| GLUT_SINGLE
| GLUT_DEPTH
);
262 glutCreateWindow(argv
[argc
-1]);
264 glutReshapeFunc( Reshape
);
265 glutKeyboardFunc( Key
);
266 glutDisplayFunc( Display
);
271 signal(SIGALRM
, alarmhandler
);