运行结果演示
源代码
#include "stdafx.h"
#include
#include
using namespace std;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glutSolidOctahedron();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void myKeyboard(unsigned char key, int x, int y)
{
glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_PROJECTION);
switch (key)
{
case 'a': case 'A': glTranslated(0.1, 0, 0); break;
case 'd': case 'D': glTranslated(-0.1, 0, 0); break;
case 'w': case 'W': glTranslated(0, 0.1, 0); break;
case 's': case 'S': glTranslated(0, -0.1, 0); break;
case 'q': case 'Q': glTranslated(0, 0, 0.1); break;
case 'e': case 'E': glTranslated(0, 0, -0.1); break;
case 'j': case 'J': glRotated(10, 0, 1, 0); break;
case 'l': case 'L': glRotated(10, 0, -1, 0); break;
case 'i': case 'I': glRotated(10, 1, 0, 0); break;
case 'k': case 'K': glRotated(10, -1, 0, 0); break;
case 'u': case 'U': glRotated(10, 0, 0, 1); break;
case 'o': case 'O': glRotated(10, 0, 0, -1); break;
case 'z': case 'Z': glScalef(1.5, 1.5, 1.5); break;
case 'x': case 'X': glScalef(0.5, 0.5, 0.5); break;
default: break;
}
glutPostRedisplay();
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutWireIcosahedron();
glFlush();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(myKeyboard);
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}