I have some experience doing OpenGL with C/C++, but I’m new to jogl and having issues getting key input to work. I’m trying to write a simple translation function and all my debugging printlns work, but the actual display doesn’t.
Can anyone see what I’m doing wrong?
package cubepackage;
import com.sun.opengl.util.FPSAnimator;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLJPanel;
import javax.media.opengl.glu.GLU;
/**
* Engine.java
*
*/
public class Engine implements GLEventListener
{
cubeFactory cube = new cubeFactory();
public void init(GLAutoDrawable drawable)
{
// Use debug pipeline
// drawable.setGL(new DebugGL(drawable.getGL()));
GL gl = drawable.getGL();
System.err.println("INIT GL IS: " + gl.getClass().getName());
// Enable VSync
gl.setSwapInterval(1);
// Setup the drawing area and shading mode
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
{
GL gl = drawable.getGL();
GLU glu = new GLU();
if (height <= 0)
{ // avoid a divide by zero error!
height = 1;
}
final float h = (float) width / (float) height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 20.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
float x = 1;
float y = (float) 1.0;
float z = (float) 1.0;
float nearX = 0;
float farX = x;
float nearY = y;
float farY = 0;
float nearZ = 0;
float farZ = z;
int numBoxes = 3;
public void display(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
// Clear the drawing area
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
// Reset the current matrix to the "identity"
gl.glLoadIdentity();
gl.glRotatef(0.0f, 0.0f, 0.0f, 0.0f);
// Move the "drawing cursor" around
//gl.glTranslatef(0.0f, 0.0f, -10.0f);
cube.finalizeTranslate(gl);
//Here's where we start drawing. ORDER MATTERS.
//In case you want to do textures or something, you need to draw
//everything in counter-clockwise order.
// Draw A Quad
gl.glBegin(GL.GL_QUADS);
gl.glColor3f(0.5f, 0.5f, 1.0f); // Set the current drawing color to light blue
cube.drawCube(gl);
// Done Drawing The Quad
gl.glEnd();
// Flush all drawing operations to the graphics card
gl.glFlush();
}
public void main()
{
userInterface frame = new userInterface();
GLJPanel canvas = frame.getGLJPanel();
//layout setup
frame.add(canvas);
canvas.addGLEventListener(new Engine());
//DO THIS OR KEYLISTENERS WON'T WORK
canvas.requestFocusInWindow();
//frame.add(canvas);
frame.setSize(800, 600);
FPSAnimator animator = new FPSAnimator(canvas, 10);
animator.setRunAsFastAsPossible( false );
frame.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
// Run this on another thread than the AWT event queue to
// make sure the call to Animator.stop() completes before
// exiting
new Thread(new Runnable()
{
public void run()
{
System.exit(0);
}
}).start();
}
});
// Center frame
frame.setLocationRelativeTo(null);
frame.setVisible(true);
animator.start();
commandHub hub = new commandHub(this, frame, cube);
canvas.addKeyListener(hub);
frame.addMouseListener(hub);
frame.addMouseMotionListener(hub);
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
{
}
}
Here’s where the translation code is:
public void translatePosX()
{
distanceX += 0.5;
}
public void translateNegX()
{
distanceX -= 0.5;
}
public void translatePosY()
{
distanceY += 0.5;
}
public void translateNegY()
{
distanceY -= 0.5;
}
public void translatePosZ()
{
distanceZ += 0.5;
}
public void translateNegZ()
{
distanceZ -= 0.5;
}
public void finalizeTranslate(GL gl)
{
gl.glTranslatef(distanceX, distanceY, distanceZ);
}
Key input method:
public void keyTyped(KeyEvent e)
{
System.out.println(e.getKeyChar());
switch(e.getKeyChar())
{
case 'd':
System.out.println("move right");
cubeFac.translatePosX();
break;
case 'a':
System.out.println("move left");
cubeFac.translateNegX();
break;
case 'p':
System.out.println("move up");
cubeFac.translatePosY();
break;
case ';':
System.out.println("move down");
cubeFac.translateNegY();
break;
case 's':
System.out.println("move back");
cubeFac.translatePosZ();
break;
case 'w':
System.out.println("move forward");
cubeFac.translateNegZ();
break;
}
}
Thanks