opengl画正方体并旋转

有没有人在啊,想请问下,opengl画正方体并旋转
最新回答
逗比在此

2024-06-27 09:33:33

#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();
}
黑后

2024-06-27 20:32:47

你可以在这个网站里得到答案:
http://nehe.gamedev.net/

里面有很多例子,你所说的画正方体并旋转在里面算是很简单的,你直接下载就可以使用。