2个回答
展开全部
#include<gl/glut.h>
float i=1,j=1;
int axis;
float theta[2];
void cube()
{
glColor3f(1,0,0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0,1,1,0.0,0.0,0.0,0.0,1.0,0.0);
glTranslatef(i,j,-0);
glRotatef(theta[0],1,0,0);
glRotatef(theta[1],0,1,0);
GLfloat vertices[][3]={{0,0,2},{0,2,2},{2,2,2},{2,0,2},{0,0,0},{0,2,0},{2,2,0},{2,0,0}};
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,vertices);
GLubyte sx[]={7,4,5,6,4,0,1,5,1,2,6,5,7,3,0,4};
glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,sx);
glColor3f(0,1,0);
GLubyte sxs[]={1,0,3,2};
glDrawElements(GL_POLYGON,4,GL_UNSIGNED_BYTE,sxs);
glColor3f(0,0,1);
GLubyte sxss[]={2,3,7,6};
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,sxss);
theta[axis]+=2;
if(theta[axis]>360)theta[axis]-=360;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0,1,1,0.0,0.0,0.0,0.0,1.0,0.0);
cube();
glutSwapBuffers();
}
void reshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4,4.0,-4.0,4.0,-4.0,4.0);
}
void init()
{
glClearColor(1.0,1.0,0,1.0);
}
void jianpan(unsigned char key,int x,int y)
{
if(key=='q')
exit(0);
if(key=='w')
j+=0.1;
if(key=='s')
j-=0.1;
if(key=='a')
i-=0.1;
if(key=='d')
i+=0.1;
glutPostRedisplay();
}
void TimerFunc(int value)
{
glutPostRedisplay();
glutTimerFunc(2,TimerFunc,16);
}
void shubiao(int button,int state,int x, int y)
{
if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
axis=0;
if(button==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)
axis=1;
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("lingafngti");
glutTimerFunc(2,TimerFunc,16);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(jianpan);
glutMouseFunc(shubiao);
init();
glutMainLoop();
}
float i=1,j=1;
int axis;
float theta[2];
void cube()
{
glColor3f(1,0,0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0,1,1,0.0,0.0,0.0,0.0,1.0,0.0);
glTranslatef(i,j,-0);
glRotatef(theta[0],1,0,0);
glRotatef(theta[1],0,1,0);
GLfloat vertices[][3]={{0,0,2},{0,2,2},{2,2,2},{2,0,2},{0,0,0},{0,2,0},{2,2,0},{2,0,0}};
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,vertices);
GLubyte sx[]={7,4,5,6,4,0,1,5,1,2,6,5,7,3,0,4};
glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,sx);
glColor3f(0,1,0);
GLubyte sxs[]={1,0,3,2};
glDrawElements(GL_POLYGON,4,GL_UNSIGNED_BYTE,sxs);
glColor3f(0,0,1);
GLubyte sxss[]={2,3,7,6};
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,sxss);
theta[axis]+=2;
if(theta[axis]>360)theta[axis]-=360;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0,1,1,0.0,0.0,0.0,0.0,1.0,0.0);
cube();
glutSwapBuffers();
}
void reshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4,4.0,-4.0,4.0,-4.0,4.0);
}
void init()
{
glClearColor(1.0,1.0,0,1.0);
}
void jianpan(unsigned char key,int x,int y)
{
if(key=='q')
exit(0);
if(key=='w')
j+=0.1;
if(key=='s')
j-=0.1;
if(key=='a')
i-=0.1;
if(key=='d')
i+=0.1;
glutPostRedisplay();
}
void TimerFunc(int value)
{
glutPostRedisplay();
glutTimerFunc(2,TimerFunc,16);
}
void shubiao(int button,int state,int x, int y)
{
if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
axis=0;
if(button==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)
axis=1;
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("lingafngti");
glutTimerFunc(2,TimerFunc,16);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(jianpan);
glutMouseFunc(shubiao);
init();
glutMainLoop();
}
展开全部
你可以在这个网站里得到答案:http://nehe.gamedev.net/
里面有很多例子,你所说的画正方体并旋转在里面算是很简单的,你直接下载就可以使用。
里面有很多例子,你所说的画正方体并旋转在里面算是很简单的,你直接下载就可以使用。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询