arbfpspec.c revision 7ec3b29a
1/*
2 * To demo that specular color gets lost someplace after vertex
3 * program completion and fragment program startup
4 */
5
6#include <assert.h>
7#include <string.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <math.h>
11#include <GL/glew.h>
12#include "glut_wrap.h"
13
14static float Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
15static GLboolean Anim = GL_TRUE;
16
17
18static void Idle( void )
19{
20   Xrot += .3;
21   Yrot += .4;
22   Zrot += .2;
23   glutPostRedisplay();
24}
25
26
27static void Display( void )
28{
29   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
30
31   glPushMatrix();
32      glRotatef(Xrot, 1, 0, 0);
33      glRotatef(Yrot, 0, 1, 0);
34      glRotatef(Zrot, 0, 0, 1);
35      glutSolidTorus(0.75, 2.0, 10, 20);
36   glPopMatrix();
37
38   glutSwapBuffers();
39}
40
41
42static void Reshape( int width, int height )
43{
44   glViewport( 0, 0, width, height );
45   glMatrixMode( GL_PROJECTION );
46   glLoadIdentity();
47   glFrustum( -2.0, 2.0, -2.0, 2.0, 5.0, 25.0 );
48   glMatrixMode( GL_MODELVIEW );
49   glLoadIdentity();
50   glTranslatef( 0.0, 0.0, -12.0 );
51}
52
53
54static void Key( unsigned char key, int x, int y )
55{
56   (void) x;
57   (void) y;
58   switch (key) {
59      case ' ':
60         Xrot = Yrot = Zrot = 0;
61         break;
62      case 'a':
63         Anim = !Anim;
64         if (Anim)
65            glutIdleFunc(Idle);
66         else
67            glutIdleFunc(NULL);
68         break;
69      case 'z':
70         Zrot -= 5.0;
71         break;
72      case 'Z':
73         Zrot += 5.0;
74         break;
75      case 27:
76         exit(0);
77         break;
78   }
79   glutPostRedisplay();
80}
81
82
83static void SpecialKey( int key, int x, int y )
84{
85   const GLfloat step = 3.0;
86   (void) x;
87   (void) y;
88   switch (key) {
89      case GLUT_KEY_UP:
90         Xrot -= step;
91         break;
92      case GLUT_KEY_DOWN:
93         Xrot += step;
94         break;
95      case GLUT_KEY_LEFT:
96         Yrot -= step;
97         break;
98      case GLUT_KEY_RIGHT:
99         Yrot += step;
100         break;
101   }
102   glutPostRedisplay();
103}
104
105
106static void Init( void )
107{
108   GLint errnum;
109   GLuint prognum, fprognum;
110
111   static const char prog[] =
112      "!!ARBvp1.0\n"
113      "DP4   result.position.x, state.matrix.mvp.row[0], vertex.position ;\n"
114      "DP4   result.position.y, state.matrix.mvp.row[1], vertex.position ;\n"
115      "DP4   result.position.z, state.matrix.mvp.row[2], vertex.position ;\n"
116      "DP4   result.position.w, state.matrix.mvp.row[3], vertex.position ;\n"
117      "MOV   result.color.front.primary,   {.5, .5, .5, 1};\n"
118      "MOV   result.color.front.secondary, {1, 1, 1, 1};\n"
119      "END";
120
121    static const char fprog[] =
122      "!!ARBfp1.0\n"
123      "MOV result.color, fragment.color.secondary;\n"
124      "END";
125
126   if (!glutExtensionSupported("GL_ARB_vertex_program")) {
127      printf("Sorry, this program requires GL_ARB_vertex_program");
128      exit(1);
129   }
130
131   if (!glutExtensionSupported("GL_ARB_fragment_program")) {
132      printf("Sorry, this program requires GL_ARB_fragment_program");
133      exit(1);
134   }
135
136   glGenProgramsARB(1, &prognum);
137   glGenProgramsARB(1, &fprognum);
138
139   glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prognum);
140   glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
141                        strlen(prog), (const GLubyte *) prog);
142
143   assert(glIsProgramARB(prognum));
144   errnum = glGetError();
145   printf("glGetError = %d\n", errnum);
146   if (errnum != GL_NO_ERROR)
147   {
148      GLint errorpos;
149
150      glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
151      printf("errorpos: %d\n", errorpos);
152      printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB));
153   }
154
155   glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fprognum);
156   glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
157                        strlen(fprog), (const GLubyte *) fprog);
158   errnum = glGetError();
159   printf("glGetError = %d\n", errnum);
160   if (errnum != GL_NO_ERROR)
161   {
162      GLint errorpos;
163
164      glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
165      printf("errorpos: %d\n", errorpos);
166      printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB));
167   }
168
169   glEnable(GL_VERTEX_PROGRAM_ARB);
170   glEnable(GL_FRAGMENT_PROGRAM_ARB);
171   glEnable(GL_DEPTH_TEST);
172   glClearColor(0.3, 0.3, 0.3, 1);
173}
174
175
176int main( int argc, char *argv[] )
177{
178   glutInit( &argc, argv );
179   glutInitWindowPosition( 0, 0 );
180   glutInitWindowSize( 250, 250 );
181   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
182   glutCreateWindow(argv[0]);
183   glewInit();
184   glutReshapeFunc( Reshape );
185   glutKeyboardFunc( Key );
186   glutSpecialFunc( SpecialKey );
187   glutDisplayFunc( Display );
188   if (Anim)
189      glutIdleFunc(Idle);
190   Init();
191   glutMainLoop();
192   return 0;
193}
194