Coordinate System issues

Hey guys. I’ve been working with java for some time and just recently started using opengl/jogl. Drawing shapes and even lighting isn’t an issue, however I still fail to grasp the concept of the coordinate system.

For instance I’m currently working on a java app that draws some shapes in a GLCanvas and then checks to see if the mouse is hovered over it, if so it does some action (currently just prints to the console). I have the app running and the cavas set up drawing several shapes, I’m using a MouseMotionListener to check for a collision when the mouse moves.

However when I check coordinates, the coordinates it checks are that of the Frame below the canvas and range from 0 to the Frame size. The GLCanvas uses the openGL coordinates which at this point are from -1 to 1 on the x and y axis.

I guess I could write an algorithm to translate the plotted shapes points into the Frame coordinate system but that seems like an awful hassle. My next idea was to set the coordinates in the Canvas to match those of the Frame, yet I have no clue how to even go about it. It seems that my canvas, regardless of the used perspective or how I try to move the camera, always goes from -1 to 1. I was wondering if anyone could shed some insight on manipulating the coordinate system/camera. Every openGL tutorial I’ve looked at has touched upon the topic, but at the same time danced around it.

Then again, I could be over complicating things, any help/insight at allw ould be greatly appreciated.

-Me

If I understand you correctly, then the GLCanvas’ XY co-ordinates are always between -1 and 1?

Have you tried using gl.glViewport(0, 0, framewidth, frameheight) to set the GLCanvas’ co-ordinates to match your frame?

hey ultraq, thanks for the reply. Yea that’s basically what I was asking and just gave it a try.

Frame frame = new frame()



gl.glViewport(0, 0, frame.getWidth(), frame.getHeight());

it still seems to be -1 to 1. To test it I’m drawing a circle to the screen using:

glu.gluSphere(glu.gluNewQuadric(), 1, 25, 25);

so it has a radius of 1.

actually, here’s my entire reshape function, since that’s where I set up everything.


			GL gl = drawable.getGL();
			GLU glu = new GLU();
			
			//don't like dividing by 0
			if (y == 0 || x == 0) return;
			
			gl.glMatrixMode(GL.GL_PROJECTION);
			gl.glLoadIdentity();
			
			glu.gluPerspective(40, x/y, .5, 20);
			gl.glMatrixMode(GL.GL_MODELVIEW);
			gl.glViewport(0, 0, frame.getWidth(), frame.getHeight());

any help would be appreciated.

You want to read about / manipulate the projection-matrix

you can translate/rotate/scale it just like the modelview matrix, allowing you to adjust the 2D coordinate-system to your needs

Looking at the view setup part in my app, it looks (something) like this:


// Setup the viewing area
gl.glViewport(0, 0, 640, 480);
gl.glMatrixMode(GL_PROJECTION);
gl.glLoadIdentity();

// Setup the clipping-planes currently being shown
gl.glOrtho(0, 640, 0, 480, 0, 100);

// Return to modelview mode, pull the camera 'back'
gl.glMatrixMode(GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -100);

The differences I see are that I’m using orthographic projection, and that the viewport method is the first line instead of the last. Also, I call glLoadIdentity() after going to the modelview matrix.