You’re right, OpenGL DOES use a right-handed coordinate system.
But in this case, positive x goes to the right, positive y up and positive
z out of the screen towards the user.
However, in my case pos. x goes left, pos. y up and pos. z into the
screen. This is weird, I’m really confused now.
I post the code of two classes, the starter class with a main method,
and the GLEventListener-class. It creates a ground with x- and z-axis.
As you can see there, left and right are switched, I dunno why.
The first class’ code is from a Tutorial by Gene Davis, just for errr
legal concerns…
At first the starter class (3DText):
import java.awt.;
import javax.swing.;
import net.java.games.jogl.*;
/**
-
This is a basic JOGL app. Feel free to
-
reuse this code or modify it.
*/
public class Text3D extends JFrame {
public Text3DView myListener;
public static void main(String[] args) {
final Text3D app = new Text3D();
// show what we've done
SwingUtilities.invokeLater (
new Runnable() {
public void run() {
app.setVisible(true);
}
}
);
}
public Text3D() {
//set the JFrame title
super("3D-Text Application");
//kill the process when the JFrame is closed
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//only three JOGL lines of code ... and here they are
GLCapabilities glcaps = new GLCapabilities();
GLCanvas glcanvas = GLDrawableFactory.getFactory().
createGLCanvas(glcaps);
myListener = new Text3DView(glcanvas);
glcanvas.addGLEventListener(myListener);
//add the GLCanvas just like we would any Component
getContentPane().add(glcanvas, BorderLayout.CENTER);
setSize(500, 300);
//center the JFrame on the screen
centerWindow(this);
}
public void centerWindow(Component frame) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.width > screenSize.width ) {
frameSize.width = screenSize.width;
}
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
frame.setLocation (
(screenSize.width - frameSize.width ) >> 1,
(screenSize.height - frameSize.height) >> 1
);
}
}
And the GLEventListener (3DTextView):
import java.awt.Component;
import java.awt.event.*;
import net.java.games.jogl.*;
public class Text3DView implements GLEventListener, KeyListener {
private GLCanvas glc;
private double eyeX = 0;
private double eyeY = 100;
private double eyeZ = -10;
private double centerX = 0;
private double centerY = 0;
private double centerZ = 0;
public Text3DView(GLCanvas canvas) {
glc = canvas;
glc.addKeyListener(this);
}
public void init(GLDrawable drawable) {
GL gl = drawable.getGL();
GLU glu = drawable.getGLU();
gl.glClearColor(0.5f, 1.0f, 1.0f, 1.0f);
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
double w = ((Component)drawable).getWidth();
double h = ((Component)drawable).getHeight();
double aspect = w/h;
glu.gluPerspective(60.0, aspect, 2.0, 1000.0);
glu.gluLookAt(eyeX, eyeY, eyeZ,
centerX, centerY, centerZ,
0, 1, 0);
gl.glMatrixMode(GL.GL_MODELVIEW);
}
public void display(GLDrawable drawable) {
drawGround(drawable);
drawAxes(drawable);
}
public void drawGround(GLDrawable drawable) {
GL gl = drawable.getGL();
gl.glColor3f(0.88235f, 0.70196f, 0.45882f);
gl.glBegin(GL.GL_QUADS);
gl.glVertex3d(0, 0, 0);
gl.glVertex3d(0, 0, 100);
gl.glVertex3d(100, 0, 100);
gl.glVertex3d(100, 0, 0);
gl.glEnd();
}
public void drawAxes(GLDrawable drawable) {
GL gl = drawable.getGL();
//x-Achse: blau
gl.glColor3f(0,0,1);
gl.glBegin(GL.GL_LINES);
gl.glVertex3d(0,0,0);
gl.glVertex3d(100,0,0);
gl.glEnd();
//z-Achse: grün
gl.glColor3f(0,1,0);
gl.glBegin(GL.GL_LINES);
gl.glVertex3d(0,0,0);
gl.glVertex3d(0,0,100);
gl.glEnd();
}
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == 'r') {
System.out.println("Refreshing screen...");
}
glc.repaint();
}
public void reshape(GLDrawable drawable,
int x, int y, int width, int height) {}
public void displayChanged(GLDrawable drawable,
boolean modeChange, boolean deviceChanged) {}
}