19,468
社区成员
发帖
与我相关
我的任务
分享
#include<GL/glut.h>
/*
* Tips:
*
* OpenGL 使用右手直角坐标系, 屏幕左为X, 屏幕上为Y, 屏幕外为Z
*/
static const float X_LEN = 0.618F;
static const float Y_LEN = X_LEN * 0.618F * 0.618F ;
static const float Z_LEN = X_LEN * 0.618F ;
static const float fVertexList[8][3] =
{
{X_LEN * -1.0F, Y_LEN, Z_LEN },
{X_LEN * -1.0F, Y_LEN * -1.0F, Z_LEN },
{X_LEN, Y_LEN * -1.0F, Z_LEN },
{X_LEN, Y_LEN, Z_LEN },
{X_LEN * -1.0F, Y_LEN, Z_LEN * -1.0F },
{X_LEN * -1.0F, Y_LEN * -1.0F, Z_LEN * -1.0F },
{X_LEN, Y_LEN * -1.0F, Z_LEN * -1.0F },
{X_LEN, Y_LEN, Z_LEN * -1.0F }
};
static const float fColorList[6][3] =
{
{0.7F, 0.1F, 0.1F},
{0.1F, 0.7F, 0.1F},
{0.1F, 0.1F, 0.7F},
{0.7F, 0.7F, 0.1F},
{0.7F, 0.1F, 0.7F},
{0.1F, 0.7F, 0.7F}
};
static const GLint iIndexList[6][4] =
{
{0, 1, 2, 3},
{2, 3, 7, 6},
{0, 3, 7, 4},
{0, 1, 5, 4},
{1, 2, 6, 5},
{4, 5, 6, 7}
};
void DrawCube(void)
{
int i = 0;
int j = 0;
glBegin(GL_QUADS);
for (i=0; i<6; i++)
{
// Set color by RGB value.
glColor3fv(fColorList[i]);
// Draw a quad
for (j=0; j<4; j++)
{
glVertex3fv(fVertexList[iIndexList[i][j]]);
}
}
glEnd();
}
static float rotate = 0;
static int times = 0;
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearDepth(1.0F);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
//glTranslatef(-0.2, 0, 0); // 平移
//glScalef(0.001, 0.001, 0.001); // 缩放
times++;
if(times > 100)
{
times = 0;
}
if(times % 100 == 0)
{
rotate += 0.3;
}
glColor3f(0.4, 0.4, 0.4);
glRotatef(30, 0, 1, 0);
glRotatef(30, 1, 0, 0);
//glRotatef(rotate, 0, 0, 1);
DrawCube();
glPopMatrix();
glutSwapBuffers();
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(600, 600);
glutCreateWindow("GLDemo");
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutMainLoop();
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(600, 600);
glutCreateWindow("GLDemo");
glEnable( GL_DEPTH_TEST );// 重点
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutMainLoop();
}