我如何在此代码中使用键盘交互
How I can use Keyboard interaction in this code
我想使用键盘键在路径上移动对象,但是在使用键盘功能后,对象不移动。请帮助我。从它的位置。
我想为波/路径添加3个不同的功能,因此键盘交互对我来说是必要的,因此我可以在不同的场景中使用不同的键。
void object()
{
glPushMatrix();
glTranslatef(x, y, 0);
glBegin(GL_LINES);
glColor3f(0, 0, 0);
glVertex2f(-0.3, 0.1);
glVertex2f(0.3, 0.1);
glEnd();
glPopMatrix();
glFlush();
}
void drawsine()
{ glBegin(GL_LINE_STRIP);//Primitive
glColor3f(255, 0, 0);//Set drawing color
int i = 0;
float x = 0, y = 0;
for (x = -5; x < 6; x = x + 0.1)
{ y = (sin(3.142*x)) / 3.142*x;
glVertex2f(x, y);
sinex[i] = x;
siney[i] = y;
i++;}
glEnd();
glFlush();
}
void doFrame(int v) {
//x = sinex[i];
//y = siney[i];
if (x < 5.9)
{
x += 0.1;
y = (sin(3.142*x)) / 3.142*x;
}
glutPostRedisplay();
//glutTimerFunc(x,doFrame,0);
}
void scene1()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
drawsine();
glPopMatrix();
//glScaled(0.3, 0.3, 1);
object();
//glutTimerFunc(30,doFrame,0);
glutSwapBuffers();
}
void exit(void)
{
exit(-1);
}
void myKeyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'h':
{
scene1(); }
break;
case 'e':
{
exit(); }
break;
}
}
void display() {
//glClear(GL_COLOR_BUFFER_BIT);
}
void init() {
glClearColor(1, 1, 1, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5, 7, -5, 5, -1, 1);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(700, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Roller Coaster Simulation");
init();
glutDisplayFunc(display);
glutTimerFunc(200, doFrame, 0);
glutKeyboardFunc(myKeyboard);
glutMainLoop();
return 0;
}
您可以在显示回调glutDisplayFunc
中绘制场景。
添加场景状态(current_scene
(并在按键时切换状态:
,例如
int current_scene = 1;
void myKeyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'h': current_scene = 1; break;
case 'i': current_scene = 2; break;
// [...]
}
}
在显示回调中绘制依赖状态的场景:
void display() {
glClear(GL_COLOR_BUFFER_BIT);
switch(current_scene) {
case 1: scene1(); break;
case 2: scene2(); break;
// [...]
}
glutSwapBuffers();
glutPostRedisplay();
}
我建议仅在disaply
中进行缓冲互换(glutSwapBuffers
(。将其删除形成其他功能。
使用glutPostRedisplay
将当前窗口标记为需要连续重新播放。
相关文章:
- Qt VTK交互风格的信号到小部件
- 物理键盘上的触发器按键
- 如何使用C/C++在MacOSX中获得键盘布局
- Qt Quick-如何仅从c++代码与qml属性交互
- 视窗键盘输入 c++
- 复制和交换习惯用法与移动操作之间的交互
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- C ++异步键盘输入(标准方式)
- 键盘不起作用 - Arduino Uno - 与变量有关的多个错误
- 为什么在访问 vtkRenderWindow 的"交互器"变量时会发生段错误?
- 如何在 WindowProc 处理程序中区分箭头键和数字键盘?
- 覆盖应用程序的低级别键盘挂钩问题
- C++键盘挂钩 CTRL 键卡住
- Qt QGraphicsProxyWidget 与 QGraphicsScene 的交互
- 在 OpenGL 中使用键盘移动 3D 形状,C++
- SFML 键盘条件或
- 找不到键盘,即使我包括键盘。
- 在C++中隐藏键盘记录器的控制台窗口
- 盖奇不读取键盘输入
- 我如何在此代码中使用键盘交互