x-Coordinates go in negative direction!?! Everything is "mirrored"!

Good morning folks.
I got a very strange problem here.
ANYTHING I draw on the screen appears “mirrored”, as if all x-coordinates would have
their own negative value. Example:
I go to the origin of the “word coordinate system”: (0,0,0) There I draw 2 lines: A black one
from (0,0,0) to (0,0,50) and a green one from (0,0,0) to (50,0,0). When starting, the black line
is being drawn as expected, straight upward. But the green one goes from the origin 50 steps
to the LEFT!!!

Here’s the code:

//Green Line in x-direction
gl.glColor3f(0.0f, 1.0f, 0.0f);
gl.glLineWidth(3);
gl.glBegin(gl.GL_LINES);
gl.glVertex3d(0, 0, 0);
gl.glVertex3d(50, 0, 0);
gl.glEnd();

//Black line in z-direction
gl.glColor3f(0.0f, 0.0f, 0.0f);
gl.glLineWidth(3);
gl.glBegin(gl.GL_LINES);
gl.glVertex3d(0, 0, 0);
gl.glVertex3d(0, 0, 50);
gl.glEnd();

The camera is set via

glu.gluLookAt(eyeX, eyeY, eyeZ,
centerX, centerY, centerZ,
0, 1, 0);

then

setCamera() {
centerX = 0;
centerZ = 0;
eyeX = centerX;
eyeZ = centerZ-50;
}

As you can see, all y-coordinates are always 0, so that everything lies “flat” in the world.

Can anybody help me? I’m sure there’s some stupid line of code in here, but I can’t
figure it out. It’s very urgent (it always is, I guess ;)), but we gotta finish our program
today, so any help would be much appreciated!!!

Thx in advance & desperate greetings, Morte

You might look at it from the wrong direction. (gluLookAt)

Did you setup the projection-matrix properly?

So far for my guesses, please post compilable code.

Hi, thanks for your quick response :)!
Unfortunately, it’s impossible to post all of the code here, since the view-class interacts
with many other classes and has alone about 1000 lines of code.
I’m totally new to JOGL, as well as to OpenGL in general, so I don’t even know, how to
“set up the projection-matrix properly”. I had started with the code of a tutorial lesson
as a base for creating a 3d-world and-perspective. So how do you set up this matrix
properly?

Greetings

In case you’re relatively new to programming: whenever you run into a non-trival bug, narrow it down. If you can’t find the bug quickly, make a new class for example and make that your playground, try to see if you can get the wrong behaviour in your tiny test-case.

In this process you’ll most likely either find the bug in your test-case and fix it, or find out (and really understand) what you did wrong in the original code.

Along with unit-testing this will save you much time, and you don’t really need help anymore with your own bugs.

As a sidenote: try to keep your classes small. Anything over 300 lines is propably too much, split it into several classes, unless you really know what you’re doing. I prefer a maximum of 200 lines, but that’s personal.

Thx for your advice!
That’s exactly the way I do it usually (create a test class etc.) but since time is running out,
I thought it was worth the try to ask, if someone had come across the same problem
already.
I “fixed” the problem by multiplying the x-value by -1, which has to be done just in one
single line of a different class. It works for now, but I’m still curious about what caused
this problem, so as soon as time allows it, I’ll try and “hunt” the bug down (die bug, die!!!).
However, thanks again for your advice and time, I really appreciate that!

Very cool forum btw! Reading through some threads has already helped me a lot :).

Greetings, Morte

I’m probably completely wrong, but doesn’t OpenGL use a right-handed coordinate system? In which case this behaviour is as expected?

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) {}

}

The answer lies here:

glu.gluLookAt(eyeX, eyeY, eyeZ,
centerX, centerY, centerZ,
0, 1, 0);

As eyeZ is assigned a negative value of -10, your “camera” resides behind the X/Y-plane at Z=0 and thus everything seems mirrored. Moving the eyeZ to value of 10 should make the world seem normal again.