2 * A lit, rotating torus via vertex program
11 #include "glut_wrap.h"
13 static float Xrot
= 0.0, Yrot
= 0.0, Zrot
= 0.0;
14 static GLboolean Anim
= GL_TRUE
;
17 static void Idle( void )
26 static void Display( void )
28 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
31 glRotatef(Xrot
, 1, 0, 0);
32 glRotatef(Yrot
, 0, 1, 0);
33 glRotatef(Zrot
, 0, 0, 1);
34 glutSolidTorus(0.75, 2.0, 10, 20);
41 static void Reshape( int width
, int height
)
43 glViewport( 0, 0, width
, height
);
44 glMatrixMode( GL_PROJECTION
);
46 glFrustum( -2.0, 2.0, -2.0, 2.0, 5.0, 25.0 );
47 glMatrixMode( GL_MODELVIEW
);
49 glTranslatef( 0.0, 0.0, -12.0 );
53 static void Key( unsigned char key
, int x
, int y
)
59 Xrot
= Yrot
= Zrot
= 0;
82 static void SpecialKey( int key
, int x
, int y
)
84 const GLfloat step
= 3.0;
105 static void Init( void )
110 /* borrowed from an nvidia demo:
111 * c[0..3] = modelview matrix
112 * c[4..7] = invtrans modelview matrix
114 * c[35] = diffuse color
116 static const char prog
[] =
118 "OPTION ARB_position_invariant ;"
121 "# normal x MV-1T -> lighting normal\n"
122 "DP3 R1.x, state.matrix.modelview.invtrans.row[0], vertex.normal ;\n"
123 "DP3 R1.y, state.matrix.modelview.invtrans.row[1], vertex.normal;\n"
124 "DP3 R1.z, state.matrix.modelview.invtrans.row[2], vertex.normal;\n"
126 "DP3 R0, program.local[32], R1; # L.N\n"
128 "MUL result.color.xyz, R0, program.local[35] ; # col = L.N * diffuse\n"
130 "MUL result.color.primary.xyz, R0, program.local[35] ; # col = L.N * diffuse\n"
132 "MOV result.texcoord, vertex.texcoord;\n"
135 if (!glutExtensionSupported("GL_ARB_vertex_program")) {
136 printf("Sorry, this program requires GL_ARB_vertex_program");
141 glGenProgramsARB(1, &prognum
);
143 glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, prognum
);
144 glProgramStringARB(GL_VERTEX_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
145 strlen(prog
), (const GLubyte
*) prog
);
147 assert(glIsProgramARB(prognum
));
148 errno
= glGetError();
149 printf("glGetError = %d\n", errno
);
150 if (errno
!= GL_NO_ERROR
)
154 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errorpos
);
155 printf("errorpos: %d\n", errorpos
);
156 printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
));
160 glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB
, 32, 2, 2, 4, 1);
161 /* Diffuse material color */
162 glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB
, 35, 0.25, 0, 0.25, 1);
164 glEnable(GL_VERTEX_PROGRAM_ARB
);
165 glEnable(GL_DEPTH_TEST
);
166 glClearColor(0.3, 0.3, 0.3, 1);
170 int main( int argc
, char *argv
[] )
172 glutInit( &argc
, argv
);
173 glutInitWindowPosition( 0, 0 );
174 glutInitWindowSize( 250, 250 );
175 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
176 glutCreateWindow(argv
[0]);
178 glutReshapeFunc( Reshape
);
179 glutKeyboardFunc( Key
);
180 glutSpecialFunc( SpecialKey
);
181 glutDisplayFunc( Display
);