19,468
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h> // FOR Sleep
#include "glut.h" // and glut32.lib ,glut32.dll
///////////////////////////////////////////////////////
static GLfloat xrot = 0.0;
static GLfloat yrot = 0.0;
static GLfloat zrot = 0.0;
void cube()//绘制立方体
{
glBegin(GL_LINES);//绘制坐标轴
glPointSize(8);
glLineWidth(2);
glColor3f(1,0.5,0);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glVertex3f(0,0,0);
glVertex3f(0,0,1);
glEnd();
//绘制立方体
glBegin(GL_QUADS);
glColor3f(1.0,0.0,0.0); //红 y轴
glVertex3f( 0.5, 0.5,-0.5);
glVertex3f(-0.5, 0.5,-0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f( 0.5, 0.5, 0.5);
glColor3f(0.0,1.0,0.0); //绿
glVertex3f( 0.5,-0.5, 0.5);
glVertex3f(-0.5,-0.5, 0.5);
glVertex3f(-0.5,-0.5,-0.5);
glVertex3f( 0.5,-0.5,-0.5);
glColor3f(0.0,0.0,1.0); //蓝 x轴
glVertex3f( 0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5,-0.5, 0.5);
glVertex3f( 0.5,-0.5, 0.5);
glColor3f(1.0,1.0,0.0); //黄
glVertex3f( 0.5,-0.5,-0.5);
glVertex3f(-0.5,-0.5,-0.5);
glVertex3f(-0.5, 0.5,-0.5);
glVertex3f( 0.5, 0.5,-0.5);
glColor3f(0.6,0.2,0.2); //棕
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5,-0.5);
glVertex3f(-0.5,-0.5,-0.5);
glVertex3f(-0.5,-0.5, 0.5);
glColor3f(0.6,0.1,0.9); //紫 z轴
glVertex3f( 0.5, 0.5,-0.5);
glVertex3f( 0.5, 0.5, 0.5);
glVertex3f( 0.5,-0.5, 0.5);
glVertex3f( 0.5,-0.5,-0.5);
glEnd();
}
void display(void)//显示
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//glPushMatrix();
glTranslatef(0, 0, -5);
//glTranslatef(0,0,0);
glRotatef(xrot, 0.5, 0, 0);
//glTranslatef(0,0,0);
//glTranslatef(0,0.5,0);
glRotatef(yrot, 0, 0.5, 0);
//glTranslatef(0,-0.5,0);
//glTranslatef(0,0,0.5);
glRotatef(zrot, 0, 0, 0.5);
//glTranslatef(0,0,-0.5);
cube();
glutSwapBuffers();
glPopMatrix();
//
xrot += 0.5;
yrot += 0.5;
zrot += 0.5;
// speed control
Sleep(20);
}
void reshape(int w, int h)//重新绘制窗口
{
if(h==0) h = 1;
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void init(int width, int height )
{
if(height == 0) height = 1;
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat)width/(GLfloat)height, 1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key, int w, int h)
{
if(key == 'f') // 进入全屏
glutFullScreen();
if(key == 'F') // 退出全屏
{
glutReshapeWindow(640, 480); // 设置窗口大小(不能用初始化的函数)
glutPositionWindow(400, 100); // 设置窗口位置(不能用初始化的函数)
}
if(key == 27) // ESC退出程序
exit(0);
if(key=='x') xrot = xrot + 10;
if(key=='c') xrot = xrot - 10;
if(key=='y') yrot = yrot + 10;
if(key=='z') zrot = zrot + 10;
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(400, 100);
glutInitWindowSize(640, 480);
glutCreateWindow("");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
init(640, 480);
glutMainLoop();
return 0;
}
3轴一起 转