2.4.5 OpenGL程序实例分析
#include <GL/glut.h>
float angle = 0.0f;
void Init()
{
GLfloat light_ambient[] = { 1.5,1.5,1.5,1.0 };
float lpos[4] = { 1.0,1.0,1,0 };
glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
}
void Reshape(int w, int h)
{
if (0 == h) h = 1;
float ratio = 1.0f* w / h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, ratio, 1.0f, 10.0f);
glMatrixMode(GL_MODELVIEW);
}
void myDisplay ()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.8f, 0.8f, 0.8f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-1.0f, 0.0f, -5.0f);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glutWireCube(1);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLoadIdentity();
glTranslatef(1.0f, 0.0f, -5.0f);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glutSolidCube(1);
glDisable(GL_LIGHTING);
angle += 0.01f;
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100, 100);
glutInitWindowSize(600, 400);
glutCreateWindow("旋转的立方体");
glutReshapeFunc(Reshape);
glutDisplayFunc(myDisplay);
glutIdleFunc(myDisplay);
Init();
glutMainLoop();
return 0;
}