我想讓我的對象旋轉取決於哪個軸是想要的(通過按x,y,z),然後用同一個鍵開始/停止rotaion。這裏是我的代碼:使用鍵盤旋轉 - 過剩/ OpenGL
#include <glut.h>
#include <stdio.h>
static GLfloat spin = 0.0;
void init(void){
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
}
void action(void)
{
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glBegin(GL_LINE_LOOP);
glColor3f(1.00, 0.00, 0.00);
glVertex3f(0.00, 0.00, 0.00);
glVertex3f(0.00, 0.00, -1.00);
glVertex3f(0.50, 0.00, -1.00);
glVertex3f(0.75, 0.0, -0.75);
glVertex3f(0.25, 0.25, -1.0);
glVertex3f(0.50, 0.50, -0.50);
glColor3f(0.00, 1.00, 0.00);
glVertex3f(0.00, 0.00, 0.00);
glVertex3f(0.00, 0.00, 1.00);
glVertex3f(0.50, 0.00, 1.00);
glVertex3f(0.75, 0.0, 0.75);
glVertex3f(0.25, 0.25, 1.0);
glVertex3f(0.50, 0.50, 0.50);
glEnd();
glPopMatrix();
glutSwapBuffers();
glFlush();
}
//spin function
void spinDisplay(void){
spin = spin + 0.02;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 0.5, 15.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.5, 1.5, 1.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
//keyboard event handler
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27: // Escape key
exit(0);
break;
case 'x':
glRotatef(spin, 1.0, 0.0, 0.0);
break;
case 'y':
glRotatef(spin, 0.0, 1.0, 0.0);
break;
case 'z':
glRotatef(spin, 0.0, 0.0, 1.0);
break;
case 'r':
glutIdleFunc(action);
break;
}
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y){
switch (button){
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(spinDisplay);
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutIdleFunc(action);
glutMainLoop();
return 0;
}
我在鍵盤功能,我想,但我想知道glRotatef每個鍵 - 也就是說,它是否屬於那裏。我創建了函數操作,我打算使用它來調用glutIdleFunc(),但我不確定如何將旋轉合併到它中。即使我可以對如何/爲什麼我的鍵盤調用不正確做出澄清,那也是非常棒的!
我會建議看看[this](http://www.lighthouse3d.com/tutorials/glut-tutorial/animation/)和[this](http://www.lighthouse3d.com/tutorials/glut-教程/鍵盤/)教程。實際上,該頁面上的所有教程都非常有用。 – vesan 2014-09-26 01:28:41
或這些教程:http://ogldev.atspace.co.uk/ – StarShine 2014-09-26 09:46:16