-2

I'm trying to apply transformation to a specific part of my drawing that will only come in effect when a key is pressed. I'm using glPushMatrix() then I do my transformation, then glPopMatrix(). What happens when I press my key is that the transformation is done only once, then no matter how many times I press the key again nothing happens.

Here's a snippet from my code:

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
}

when I remove the pushMatrix and popMatrix it works but it applies the transformation to all of my shapes, which is not what I want. Any help would be appreciated..

[Update] I've tried to run my code on 4 other computers (one macbook Air, one macbook Pro -an exact copy of mine- and 2 iMacs and) and it doesn't even run on any of them, could this mean the problem lies in my own macbook?! Also I copied code that runs perfectly fine on the macbook air, rotating parts and everything and when I run it on mine I got the same disappointing result as my own code.

Here's the heypasteit code for my code 31JL

  • 2
    What happens if you put the `glTranslated` statement outside the `if` statement? – Xirema Oct 17 '16 at 20:08
  • Have you tried calling `glLoadIdentity()` directly after `glPushMatrix`? – BDL Oct 17 '16 at 20:09
  • You are missing `glMatrixMode` call (or just forget to copy here) do not assume it is set to ModelView... have you tried to debug? place a break point and see if the `translateflag` is set on each keystroke ... – Spektre Oct 18 '16 at 06:59
  • take a look at this [Controling GL object by keyboard](http://stackoverflow.com/a/40104549/2521214) – Spektre Oct 18 '16 at 09:53
  • I've tried all that and I'm still stuck with the same result.. It only rotates once when I first click the specified key, and it doesn't move if I press it again. – WaterPlimpie Oct 21 '16 at 13:22

1 Answers1

0

possible problems:

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.)
}
Fennekin
  • 216
  • 3
  • 12