// ccube.c
// OpenGL SuperBible
// Demonstrates primative RGB Color Cube
// Program by Richard S. Wright Jr.
#include "../../Common/OpenGLSB.h" // System and OpenGL Stuff
/* 颜色立方体 */
//旋转角度
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
//渲染回调函数
void RenderScene(void)
{
//清除缓冲区
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//保存矩阵
glPushMatrix();
//旋转
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
//画六个面
glBegin(GL_QUADS);
// 前
// 白色
glColor3ub((GLubyte) 255, (GLubyte)255, (GLubyte)255);
glVertex3f(50.0f,50.0f,50.0f);
// 黄色
glColor3ub((GLubyte) 255, (GLubyte)255, (GLubyte)0);
glVertex3f(50.0f,-50.0f,50.0f);
// 红色
glColor3ub((GLubyte) 255, (GLubyte)0, (GLubyte)0);
glVertex3f(-50.0f,-50.0f,50.0f);
// 品红
glColor3ub((GLubyte) 255, (GLubyte)0, (GLubyte)255);
glVertex3f(-50.0f,50.0f,50.0f);
// 后
// 蓝绿
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(50.0f,50.0f,-50.0f);
// 绿色
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(50.0f,-50.0f,-50.0f);
// 黑色
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-50.0f,-50.0f,-50.0f);
// 蓝色
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-50.0f,50.0f,-50.0f);
// 顶面
// 蓝绿
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(50.0f,50.0f,-50.0f);
// 白色
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(50.0f,50.0f,50.0f);
// 品红
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(-50.0f,50.0f,50.0f);
// 蓝色
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-50.0f,50.0f,-50.0f);
// 底面
// 绿色
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(50.0f,-50.0f,-50.0f);
// 黄色
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(50.0f,-50.0f,50.0f);
// 红色
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-50.0f,-50.0f,50.0f);
// 黑色
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-50.0f,-50.0f,-50.0f);
// 左面
// 白色
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(50.0f,50.0f,50.0f);
// 蓝绿色
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(50.0f,50.0f,-50.0f);
// 滤色
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(50.0f,-50.0f,-50.0f);
// 黄色
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(50.0f,-50.0f,50.0f);
// 右面
// 品红
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(-50.0f,50.0f,50.0f);
// 蓝色
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-50.0f,50.0f,-50.0f);
// 黑色
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-50.0f,-50.0f,-50.0f);
// 红色
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-50.0f,-50.0f,50.0f);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void SetupRC()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
glEnable(GL_DEPTH_TEST);
glEnable(GL_DITHER);
glShadeModel(GL_SMOOTH);
}
/////////////////////////////////////////////////
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
xRot += 5.0f;
if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;
if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;
if(key > 356.0f)
xRot = 0.0f;
if(key < -1.0f)
xRot = 355.0f;
if(key > 356.0f)
yRot = 0.0f;
if(key < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
void ChangeSize(int w, int h)
{
GLfloat fAspect;
if(h == 0)
h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
fAspect = (GLfloat)w / (GLfloat)h;
gluPerspective(35.0f, fAspect, 1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -400.0f);
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800,600);
glutCreateWindow("RGB Cube");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}