Hi I’m very new to open gl and am a bit stuck at the moment.
I’ve got a basic polygon which i’v gotten to rotate. But I’ve come to a stop with keyboard input.
Thing is I want my polygon to rotate right when I press the right key and left when I press the left key.
I’ve implemented the basic keyboard input commands and have put my keyboard methods and a switch statement after my display method (not in it).
my program compiles just fine but nothing happens!!!
I was just wondering if
a) I’v put my key input code in the wrong place
b) I’m using the wrong commands to get my polygons to rotate the other way (i.e gl.glRotated(Rotate, 0,0,-1); )
//MY CODE
public void keyPressed(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_RIGHT:
gl.glRotated(Rotate, 0,0,1);
break;
case KeyEvent.VK_LEFT:
gl.glRotated(Rotate, 0,0,-1);
break;
}
}
I’m programing in java btw
Any help would be very much appreciated Thanks
You probably want to start the rotating on a key press and stop on a release. You also need to redraw the screen. For animation is some kind of idle function that rotates and redraws required.
the problem with your code is that you modify the modelview matrix inside your key event processing, and after that I’m quite sure you call glLoadIdenty, which will reset your transformation stack
do as prashantgp says, with a glut key callback and it should work.