Opengl/Glut 键盘输入问题
Posted
技术标签:
【中文标题】Opengl/Glut 键盘输入问题【英文标题】:Opengl/Glut keyboard input trouble 【发布时间】:2020-02-19 15:58:38 【问题描述】:我有一个任务是在屏幕上绘制一个金字塔,然后在我按下键盘上的“k”键后旋转。 一切正常,除了旋转部分,因为程序似乎没有注意到我按下键。代码如下:
void changeSize(int w, int h)
if(h == 0)
h = 1;
float ratio = w * 1.0 / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
// Set perspective
gluPerspective(45.0f ,ratio, 1.0f ,1000.0f);
// return to the model view matrix mode
glMatrixMode(GL_MODELVIEW);
void renderScene(void)
// clear buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set the camera
glLoadIdentity();
gluLookAt(5.0,5.0,5.0,
0.0,0.0,0.0,
0.0f,2.0f,0.0f);
// put the geometric transformations here
// put drawing instructions here
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_LINES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-100.0f, 0.0f, 0.0f);
glVertex3f( 100.0f, 0.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, -100.0f, 0.0f);
glVertex3f(0.0f, 100.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, -100.0f);
glVertex3f(0.0f, 0.0f, 100.0f);
glEnd();
//triangulos da base
glBegin(GL_TRIANGLES);
glColor3f(255.0f,255.0f,255.0f);
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(-1.0f,0.0f,-1.0f);
glVertex3f(-1.0f,0.0f,1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(1.0f,0.0f,-1.0f);
glVertex3f(-1.0f,0.0f,-1.0f);
glEnd();
//triangulos das faces
glBegin(GL_TRIANGLES);
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(0.0f,2.0f,0.0f);
glVertex3f(-1.0f,0.0f,1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(1.0f,0.0f,-1.0f);
glVertex3f(0.0f,2.0f,0.0f);
glVertex3f(1.0f,0.0f,1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(1.0f,0.0f,-1.0f);
glVertex3f(0.0f,2.0f,0.0f);
glVertex3f(-1.0f,0.0f,-1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(-1.0f,0.0f,-1.0f);
glVertex3f(0.0f,2.0f,0.0f);
glVertex3f(-1.0f,0.0f,1.0f);
glEnd();
// End of frame
glutSwapBuffers();
// write function to process keyboard events
void rotate (unsigned char key, int x, int y)
if (key == 'k')
glRotatef(45,1.0,1.0,0.0);
glutPostRedisplay();
int main(int argc, char **argv)
// init GLUT and the window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(1200,1200);
glutCreateWindow("CG@DI-UM");
// Required callback registry
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
// put here the registration of the keyboard callbacks
glutKeyboardFunc(rotate);
// OpenGL settings
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
// enter GLUT's main cycle
glutMainLoop();
return 0;
我的程序似乎没有注意到我在按下键。我尝试只打印正在按下的键,这很有效,所以我真的迷路了。
【问题讨论】:
问题解决了吗?请告诉我缺少什么。请阅读What should I do when someone answers my question? 【参考方案1】:glRotatef
在rotate
中似乎没有作用,因为glLoadIdentity()
在renderScene
的开头被调用。实际上glRotatef
改变了当前矩阵,但是当场景被渲染时(renderScene
),然后glLoadIdentity()
将Identity matrix 加载到当前矩阵。
无论如何,在输入事件回调中对当前矩阵进行更改是一种不好的风格。在renderScene
中渲染场景之前,更改输入事件中变量的状态和值,并使用变量设置当前模型视图矩阵。
添加一个全局变量angle
:
float angle = 0.0f;
更改renderScene
中变量的值。例如:
void rotate (unsigned char key, int x, int y)
if (key == 'k')
angle += 45.0f;
glutPostRedisplay();
并将旋转应用于renderScene
中的当前矩阵:
void renderScene(void)
// clear buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set the camera
glLoadIdentity();
gluLookAt(5.0,5.0,5.0,
0.0,0.0,0.0,
0.0f,2.0f,0.0f);
// apply rotation
glRotatef(angle, 1.0f, 1.0f, 0.0f);
// [...]
【讨论】:
以上是关于Opengl/Glut 键盘输入问题的主要内容,如果未能解决你的问题,请参考以下文章