View Full Version : OpenGL coordinate system
tonkata
03-27-2007, 05:39 AM
Hello,
I'm developping some soft in Java and OpenGL.
My problem is: What I have to do so the coodrinate system of openGL will be the same of the coordinates collected by the mouse move handler? For example the coordinates of the mouse become (0,0) at the left up corner of the window, but in OpenGL the (0,0) is in the center of the screen. And is it useful to make that ...
I want to do that because I would like to draw a coordinate system grid byt only the X and Y axis, and I need only the posivite part of X. Something like that:
|Y
|
|--------------> X
|0
|
And I want that the 0 in this grid collides with the 0 from OpenGL ...
Is there any standart solution or may be just an idea ?
Regards,
Anton
Wernaeh
03-27-2007, 06:47 AM
Well, actually, the OpenGl coordinate system depends on how you set up all your matrices.
Perhaps the easiest way is to set up a orthographic projection matrix that simply has (0, 0, ResolutionX, ResolutionY) as its border planes, i.e.:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, ResolutionX, ResolutionY, 0, -1, 1);
Cheers,
- Wernaeh
tonkata
03-27-2007, 06:53 AM
No, I'm using glPerspective because I need to make a zoom sometimes over the function's curv
Wernaeh
03-27-2007, 07:18 AM
Well, then there are several possible ways to achieve this.
First, why not transform your mouse coordinates into world coordinates (gluProject / gluUnproject) can be used for that.
Alternatively, you could still find the extents of the near clipping quad using simple trigonometry, then, add a translation matrix at the bottom of the modelview transform queue which places the upper left edge of the clipping quad onto the origin.
Maybe I also don't get what your point actually is.
What do you want to do in detail ?
Cheers,
- Wernaeh
Reedbeta
03-27-2007, 11:45 AM
If you are graphing a 2D function and want to zoom it, it would be better to use an orthographic projection and do the zoom by changing the viewport extent. Only use a perspective projection if you are truly rendering something 3D.
tonkata
03-28-2007, 04:17 AM
Hi again,
Thanks about the answers :)
first I want to say that i'm not very good with OpenGL :). I'll try to explaine in more details what I would like to do.
So, as I said that I want to obtain a coordinate system with X et Y axis, and where the X is always positive. At the same time I want to be able to zoom over the function's curve. So in my program code I'm fixing X and Y coordinates to 0 (gluLookAt(0,0,Z,...)) , and I'm changing Z if I want to zoom(in/out).
Every object that I'm drawing is with Z = 0, so I'm using only the (X,Y) plane.
In my case the functions that I want be able to draw, are always with positive Xs , in fact X in my case is the time T.
I would like to construct my coordinates system and to draw also the X and the Y axis over the screen, I want to draw also a grid.
At this stage, my program is able to zoom, to draw the axis (X,Y) and to draw the grid. This is one of my "big" problems:)
The second one is how to setup the mouse coordinates that are in the range of (0,600 my window size is 600x600) to be the same as the coordinates of OpenGL.
For example, if I draw a triangle :
gl.glBegin (GL.GL_TRIANGLES);
gl.glVertex2f (10.0f, 0.0f);
gl.glVertex2f (10.0f, 10.0f);
gl.glVertex2f (1.0f, 0.0f);
gl.glEnd ();
and if I point one of the vertex of this triangle I want to see (10,10), and not (150,140) or something else.
That's all I think.
Here is some source code from my program:
//ZOOM 1
public void zooming(float z){
glu.gluLookAt(0.0, 0.0, z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
public void init(GLAutoDrawable glDrawable) {
GL gl = glDrawable.getGL();
gl.glShadeModel(GL.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClearDepth(1.0);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
}
public void reshape(GLAutoDrawable glDrawable, int x, int y, int W, int H) {
if (H == 0) H = 1;
GL gl = glDrawable.getGL();
gl.glViewport(0, 0, W, H);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(90.0f, (float) W / (float) H, 0.1f, 100.0f);
gl.glMatrixMode(GL.GL_MODELVIEW);
glu.gluLookAt(0.0, 0.0, z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
gl.glLoadIdentity();
gl.glEnable (GL.GL_LINE_SMOOTH);
gl.glEnable (GL.GL_LINE_STIPPLE);
}
public void display(GLAutoDrawable drawable) {
update(); //updates the Z
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
zooming(z);
//....
}
Cheers,
Anton
Reedbeta
03-28-2007, 08:58 AM
Please use the code tags when you post code.
vBulletin, Copyright ©2000-2010, Jelsoft Enterprises Ltd.