Changing the model coordinate system

I feel bad cluttering the board with another newbie question, but I’ve really tried my best to find the answer to this.

I saw in my 2d tests that I could use glOrtho2d() to set the coordinate system to use width, height, and placing objects at, say 500,350, and they’d be roughly in the center of my 1024x768 screen. I also found with glOrtho a similar effect was acheived. However, I came to realize orthogon(ic/al?) projection was not what I was trying to achieve. So I stuck to the default coordinate system to write the following example:


/*
* BaseExample is nothing more than JFrame setup
*/
public class MouseCoords extends BaseExample implements MouseMotionListener {
	private int mouseX = 0;
	private int mouseY = 0;

	public static void main(String[] args) {
		new MouseCoords();
	}
	
	public MouseCoords() {
		super("Mouse Coords");
		
		canvas.addMouseMotionListener(this);
	}
	
	public void init(GLAutoDrawable drawable) {
		GL gl = drawable.getGL();
		GLU glu = new GLU();
		int width = getSize().width;
		int height = getSize().height;
		
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();
		
		gl.glViewport(0, 0, width, height); 
		glu.gluPerspective(60d, width / height, 1, 40);
	}
	
	public void display(GLAutoDrawable drawable) {
		GL gl = drawable.getGL();
		GLU glu = new GLU();
		GLUT glut = new GLUT();
		
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
		
		gl.glClear(GL.GL_COLOR_BUFFER_BIT);
		gl.glClearColor(0f, 0f, 0f, 1f);
		
		glu.gluLookAt(0, 0, 0,  // eye
				  	  0, 0, 20, // at
				      0, 3, 0); // up
		
		gl.glColor3f(0.45f, 0.54f, 0.64f);
		gl.glTranslatef(0f, 0f, 5f);
		glut.glutWireIcosahedron();
		
		/* mouse coords */
		{
			Formatter formatter = new Formatter();
			String format;
			int realY = 0;
			int[] viewport = new int[4];
			double[] modelMatrix = new double[16];
			double[] projectionMatrix = new double[16];
			double[] world = new double[3];
			float x = 0f;
			float y = 0f;
			float z = 0f;
			
			gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
			gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, modelMatrix, 0);
			gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projectionMatrix, 0);
			
			realY = (viewport[3] - mouseY - 1);
			
			System.out.println("Cursor: " + mouseX + "," + realY);
			
			glu.gluUnProject((double) mouseX, (double) realY, 0d, 
						modelMatrix, 0,
						projectionMatrix, 0,
						viewport, 0,
						world, 0);
			
			x = (float) world[0];
			y = (float) world[1];
			z = (float) world[2];
			
			format = formatter.format("%.2f,%.2f,%.2f", 
						new Object[] { new Float(x), new Float(y), new Float(z) }).toString();
			
			System.out.print("World: " + format + "\n");
			
			gl.glRasterPos3f((float) world[0], (float) world[1], (float) world[2]);
			glut.glutBitmapString(GLUT.BITMAP_TIMES_ROMAN_24, format);
		}
	}

	public void mouseDragged(MouseEvent e) {
		
	}

	public void mouseMoved(MouseEvent e) {
		mouseX = e.getX();
		mouseY = e.getY();
		
		canvas.repaint();
	}
}

and while this works well for it’s intention, I was wondering if it’s at all possible to change the world coordinate system, so I could place an object at 500,350,10, and have it render roughly where the Icosahedron in this example is? Is there a function call I’m missing here, or just different args to a function?

On an unrelated question, what’s up with the int args after array arguments, as in glUnproject()? I thought they might be the size of the array, as many C functions require, but that aroused a deep fury from within JNI. Setting those types of parameters to 0 works, but I’m curious as to why.

Please feel free to unleash constructive critisicm on this code, as I would like to enforce best practices with OpenGL. Thanks.

Take a look at gluLookAt(). You might also want to look at the sources for the JOGL demos which use the gleem ExaminerViewer like the VertexProgRefract demo. I don’t know whether you intend to do mouse interaction, but the ExaminerViewer has a viewAll() method where if you can provide the world-coordinate bounding sphere of your model and set the viewing direction of the ExaminerViewer then it will compute the appropriate distance from the model to place the camera.

Regarding the integer offsets, they’re now used uniformly throughout the API to allow users to index into the middle of arrays, similarly to core Java APIs taking an array, offset and length. Passing 0 is the most common case, but we don’t provide overloadings having this as a default argument in order to keep down the number of exposed methods.