使用键盘输入的OpenGL转换不工作

OpenGL transformation with keyboard input not working

本文关键字:转换 工作 OpenGL 键盘 输入      更新时间:2023-10-16

我正试图将转换应用于我的绘图的特定部分,只有在按下键时才会生效。我使用glPushMatrix()然后进行转换,然后是glPopMatrix()当我按下我的键时发生的是,转换只完成一次,然后无论我再按多少次键都没有发生任何变化。

下面是我的代码片段:

void drawShapes(){
  glClear(GL_COLOR_BUFFER_BIT);
  //setting my variables
  glPushMatrix();
  if (translateFlag) //this is set to true in the keyboard function
  {
    x += 10;
    glTranslated(x, 0, 0);
    translateFlag = false;
  }
  drawCircle();
  glPopMatrix();
//more drawings
}

当我删除pushMatrix和popMatrix时,它可以工作,但它将转换应用于我所有的形状,这不是我想要的。如有任何帮助,我将不胜感激。

(更新)我试着在其他4台电脑上运行我的代码(一台macbook Air,一台macbook Pro——和我的一模一样——还有2台imac和),它甚至不能在任何一台电脑上运行,这是否意味着问题出在我自己的macbook上?!我还复制了在macbook air上运行得很好的代码,旋转部件和所有东西,当我在我的电脑上运行它时,我得到了和我自己的代码一样令人失望的结果。

这是我的代码31JL的heypasteit代码

可能出现的问题:

void drawShapes(){
 glClear(GL_COLOR_BUFFER_BIT);
  //setting my variables
  // maybe you have forgot to set matrix mode correctly. make the projection and modelview 
 matrices correct.
  glPushMatrix();
  if (translateFlag) //this is set to true in the keyboard function
  //probably your key hit function is not working properly (I can't say without looking at full code)
  {
    x += 10;
    glTranslated(x, 0, 0);
    translateFlag = false;
  }
  drawCircle();
  glPopMatrix();
//more drawings
// you are using glFlush() ?????  try swapbuffers. exact function depends
 on the API you are using (GLUT  GLFW etc.)
}