如何使用键盘移动对象?
Posted
技术标签:
【中文标题】如何使用键盘移动对象?【英文标题】:How can i move the object using the keyboard? 【发布时间】:2015-04-11 18:05:16 【问题描述】:我试图通过按键盘上的 h 键将整个桌子向左移动,同时还旋转椅子,好像整个场景都在向右移动一样。但似乎什么都没有发生。 glpushmatrix() 和 glpopmatrix() 似乎最适合这项工作。 我尝试使用 auto [] 函数将每个整个对象(椅子和桌子)“分组”成由各自结构组成的组。 感谢您提供任何帮助或建议。
#include <GLUT/GLUT.h>
char press;
int anglex,angley,x,y,xold,yold;
int angleHead = 0;
void display();
void keyboard(unsigned char touche,int x,int y);
void reshape(int x,int y);
void idle();
void mouse(int bouton,int etat,int x,int y);
void mousemotion(int x,int y);
int main(int argc,char **argv)
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow("Scene");
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0,1.0,1.0);
glPointSize(2.0);
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(mousemotion);
glutMainLoop();
return 0;
void display()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
auto chair = []
glColor3f(0.0, 0.0, 1.0);
glLoadIdentity();
glRotatef(-angley,1.0,0.0,0.0);
glRotatef(-anglex,0.0,1.0,0.0);
glPushMatrix();
glScalef(0.15, 1.65, 0.6);
glTranslatef(5, 0.0, 0.0);
glutWireCube(0.5);
glPopMatrix();
glPushMatrix();
glScalef(0.5, 0.2, 0.6);
glTranslatef(1.18, -0.5, 0);
glutWireCube(0.5);
glPopMatrix();
glScalef(0.2, 0.5, 0.6);
glTranslatef(2.59, -0.55, 0);
glutWireCube(0.5);
;
auto table =[]
glColor3f(0.36, 0.25, 0.20);
glLoadIdentity();
glRotatef(-angley,1.0,0.0,0.0);
glRotatef(-anglex,0.0,1.0,0.0);
glPushMatrix();
glScalef(1.5, 0.15, 0.6);
glTranslatef(-0.055, 0.26, 0.0);
glutWireCube(0.5);
glPopMatrix();
glPushMatrix();
glScalef(0.15, 0.8, 0.6);
glTranslatef(1.7, -0.25, 0.0);
glutWireCube(0.5);
glPopMatrix();
glScalef(0.15, 0.8, 0.6);
glTranslatef(-2.8, -0.25, 0.0);
glutWireCube(0.5);
auto box = []
glColor3f(0.0, 1.0, 0.0);
glLoadIdentity();
glRotatef(-angley,1.0,0.0,0.0);
glRotatef(-anglex,0.0,1.0,0.0);
glPushMatrix();
glScalef(0.75, 0.2, 0.6);
glTranslatef(-0.1, 0.65, 0.0);
glutWireCube(0.5);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glScalef(0.2, 0.2, 0.2);
glTranslatef(0.1, 1, 0.0);
glutWireSphere(0.4, 30, 30);
glPopMatrix();
glColor3f(1.0, 1.0, 0.0);
glScalef(0.2, 0.2, 0.2);
glTranslatef(-0.4, 1, 0.0);
glutWireSphere(0.4, 30, 30);
;
box();
;
glPushMatrix();
glTranslatef(-0.5 - angleHead, 0.0, 0.0);
chair();
table();
glPopMatrix();
glLoadIdentity();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(0.0, -1.0, 0.0);
glScalef(7, 0.1, 5);
glutWireCube(1);
glutSwapBuffers();
void keyboard(unsigned char touche,int x,int y)
if (touche =='h')
angleHead=angleHead + 0.5;
glutPostRedisplay();
void reshape(int x,int y)
if (x<y)
glViewport(0,(y-x)/2,x,x);
else
glViewport((x-y)/2,0,y,y);
void mouse(int button, int state,int x,int y)
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
press = 1;
xold = x;
yold=y;
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
press=0;
void mousemotion(int x,int y)
if (press)
anglex=anglex+(x-xold);
angley=angley+(y-yold);
glutPostRedisplay();
xold=x;
yold=y;
【问题讨论】:
【参考方案1】:没有任何反应的原因有两个:
-
您将 0.5 添加到 int。 int 表示整数。所以你基本上在
angleHead
的每个增量处添加0
在调用glTranslatef()
后,您再次调用glLoadIdentity()
会重置您在glTranslatef()
中的翻译
【讨论】:
我尝试添加一个整数,但仍然没有任何反应,我还尝试删除 glLoadIdentity,但仍然没有任何反应。感谢您的建议。 您必须在正确的时刻使用 glTranslate,例如在 glRotatef 之前,具体取决于您想要实现的目标以上是关于如何使用键盘移动对象?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 SwiftUI 在多个屏幕上获取键盘高度并移动按钮