2 * A lit, rotating torus via vertex program
10 #define GL_GLEXT_PROTOTYPES
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
[] =
119 "#Simple transform and diffuse lighting\n"
120 "# object x MVP -> clip\n"
121 "DP4 result.position.x, state.matrix.mvp.row[0], vertex.position ;\n"
122 "DP4 result.position.y, state.matrix.mvp.row[1], vertex.position ;\n"
123 "DP4 result.position.z, state.matrix.mvp.row[2], vertex.position ;\n"
124 "DP4 result.position.w, state.matrix.mvp.row[3], vertex.position ;\n"
126 "# normal x MV-1T -> lighting normal\n"
127 "DP3 R1.x, state.matrix.modelview.invtrans.row[0], vertex.normal ;\n"
128 "DP3 R1.y, state.matrix.modelview.invtrans.row[1], vertex.normal;\n"
129 "DP3 R1.z, state.matrix.modelview.invtrans.row[2], vertex.normal;\n"
131 "DP3 R0, program.local[32], R1; # L.N\n"
133 "MUL result.color.xyz, R0, program.local[35] ; # col = L.N * diffuse\n"
135 "MUL result.color.primary.xyz, R0, program.local[35] ; # col = L.N * diffuse\n"
137 "MOV result.texcoord, vertex.texcoord;\n"
140 if (!glutExtensionSupported("GL_ARB_vertex_program")) {
141 printf("Sorry, this program requires GL_ARB_vertex_program");
146 glGenProgramsARB(1, &prognum
);
148 glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, prognum
);
149 glProgramStringARB(GL_VERTEX_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
150 strlen(prog
), (const GLubyte
*) prog
);
152 assert(glIsProgramARB(prognum
));
153 errno
= glGetError();
154 printf("glGetError = %d\n", errno
);
155 if (errno
!= GL_NO_ERROR
)
159 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errorpos
);
160 printf("errorpos: %d\n", errorpos
);
161 printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
));
165 glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB
, 32, 2, 2, 4, 1);
166 /* Diffuse material color */
167 glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB
, 35, 0.25, 0, 0.25, 1);
169 glEnable(GL_VERTEX_PROGRAM_ARB
);
170 glEnable(GL_DEPTH_TEST
);
171 glClearColor(0.3, 0.3, 0.3, 1);
175 int main( int argc
, char *argv
[] )
177 glutInit( &argc
, argv
);
178 glutInitWindowPosition( 0, 0 );
179 glutInitWindowSize( 250, 250 );
180 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
181 glutCreateWindow(argv
[0]);
182 glutReshapeFunc( Reshape
);
183 glutKeyboardFunc( Key
);
184 glutSpecialFunc( SpecialKey
);
185 glutDisplayFunc( Display
);