基于OPENGL实现的彩色旋转立方体
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
基于OPENGL实现的彩色旋转立方体,插入了背景音乐,将背景音乐放到指定路径中
#include
#include
#include
#include"windows.h"
#include
#include
#pragma comment(lib, "WINMM.LIB")
GLfloat xrot = 0.0;
GLfloat yrot = 0.0;
GLfloat zrot = 0.0;
GLint Mode = 0; //控制旋转的模式
void cube() //绘制一个立方体
{
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 0.0);
glVertex3f(1.0, 1.0, -1.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, 1.0, -1.0);
glColor3f(0.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(-1.0, -1.0, -1.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(1.0, -1.0, -1.0);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glColor3f(0.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(1.0, -1.0, -1.0);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(-1.0, -1.0, -1.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, 1.0, -1.0);
glColor3f(1.0, 1.0, 0.0);
glVertex3f(1.0, 1.0, -1.0);
glColor3f(0.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, 1.0, -1.0);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(-1.0, -1.0, -1.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.0);
glVertex3f(1.0, 1.0, -1.0);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(1.0, -1.0, -1.0);
glEnd();
}
void display(void)
{
if (Mode == 0)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -5);
glRotatef(xrot, 1, 0, 0);
glRotatef(yrot, 0, 1, 0);
glRotatef(zrot, 0, 0, 1);
cube();
}
if (Mode == 1)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -5);
glRotatef(xrot, 1, 0, 0);
glRotatef(yrot, 0, 1, 0);
glRotatef(zrot, 0, 0, 1);
cube();
xrot = xrot + 1;
yrot = yrot + 1;
zrot = zrot + 1;
Sleep(10);
}
glutSwapBuffers();
}
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);
}