新手求助(急),一个旋转的立方体问题
小鸟刚接触OpenGL,老师给了一个实验,要求是绘制一个3D的彩色立方体,在空闲时绕顶点(1,1,1)X轴方向旋转。下面是我写的代码,但是问题是:1.它不转....(无奈~) 2.绕顶点(1,1,1)不知道怎么做~
帮我修改下面的代码,指点一下吧~
#include <stdlib.h>
#include <GL/glut.h>
#include <stdio.h>
static GLfloat spin = 36.0;
int singleb, doubleb;
void colorcube(void)
{
glRotated(30,0,0,1);
glBegin(GL_QUADS);
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f( 1.0, 1.0, 1.0);
glColor3f(1.0, 0.5, 0.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f( 1.0, -1.0, -1.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.0);
glVertex3f( 1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.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);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.0);
glVertex3f( 1.0, 1.0, -1.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, -1.0);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(5.0,5.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
colorcube();
glFlush();
glutSwapBuffers();
}
void spinDisplay (void)
{
spin = spin + 36.0;
if (spin > 360.0)
spin = spin - 360.0;
glutSetWindow(singleb);
glLoadIdentity();
glRotatef (spin, 0.0,1.0, 0.0);
glutPostRedisplay();
glutSetWindow(doubleb);
glLoadIdentity();
glRotatef(spin, 0.0,1.0, 0.0);
glutPostRedisplay();
glFlush();
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, float(w) / h, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void init(int width,int height)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)width / (float)height, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
/* need both double buffering and z buffer */
int winWidth = 650;
int winHeight = 500;
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE |GLUT_DEPTH);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("3D Cube");
glutDisplayFunc(display);
init(winWidth,winHeight);
glutReshapeFunc(myReshape);
glutIdleFunc(spinDisplay);
glutMainLoop();
}