PDA

View Full Version : Getting the correct x y and z

Formatwizard
03-02-2011, 11:13 AM
Hi all,

Been busy for a while figuring out how to get the coordinates from the mouse x and y.

If i move my camera to lets say x, y, z and i click with my mouse somewhere on screen it can return between:

x-1 .. x+1 //
y-1 .. y+1 //

Where x and y equals current camera position.

Problem is i want the x and y location of the current click. e.g. if i place a model @ 50, 40, 10 i want to retreive that location not 0, 0, ,0 ( if my camera is pointed there ) i hope someone has a clue or a directory for me to search in. thnx in advance Wizard

FlorianR
03-02-2011, 11:26 AM
I think you might want to look at gluUnProject ( http://www.opengl.org/sdk/docs/man/xhtml/gluUnProject.xml ). You read the depth at the point of the mouse click (with glReadPixels with a width and height of one) and use it as winZ coordinate, then (objX, objY, objZ) will be the world space location of the clicked at pixel.

Formatwizard
03-02-2011, 11:34 AM
yes i've been busy with gluUnproject already and it does return coordinates but not the right ones.

I'll post some code here.

//have to get processing to dump all it's matricies into GL, so the functions work.
int viewport[] = new int[4];
//For the viewport matrix... not sure what all the values are,
//I think the first two are width and height, and all Matricies in GL seem to be 4 or 16...
double[] proj=new double[16];
//For the Projection Matrix, 4x4
double[] model=new double[16];
//For the Modelview Matrix, 4x4
gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
//fill the viewport matrix
gl.glGetDoublev(GLMatrixFunc.GL_PROJECTION_MATRIX, proj, 0);
//projection matrix
gl.glGetDoublev(GLMatrixFunc.GL_MODELVIEW_MATRIX, model, 0);
//modelview matrix
FloatBuffer floatBuffer=ByteBuffer.allocateDirect(4).order(Byt eOrder.nativeOrder()).asFloatBuffer();
//set up a floatbuffer to get the depth buffer value of the mouse position

gl.glReadPixels(mouseX, settings.getDisplayHeigth() - mouseY, 1, 1, GL2.GL_DEPTH_COMPONENT, GL.GL_FLOAT, floatBuffer);
//Get the depth buffer value at the mouse position.
//have to do height-mouseY, as GL puts 0,0 in the bottom left, not top left.
floatBuffer.rewind();
//finish setting up this.
double[] mousePosArr=new double[4];
//the result x,y,z will be put in this.. 4th value will be 1, but I think it's "scale" in GL terms,
//but I think it'll always be 1.
glu.gluUnProject((double)mouseX, settings.getDisplayHeigth() - (double)mouseY, (double)floatBuffer.get(0),
model, 0, proj,
0, viewport, 0, mousePosArr, 0);

//the magic function. You put all the values in, and magically the x,y,z values come out :)
gl.glEnd();

//return new float[]{(float)mousePosArr[0],(float)mousePosArr[1],(float)mousePosArr[2]};
//The values are all doubles, so throw them into floats to make life easier.}
Vector3D vector = new Vector3D(mousePosArr[0], mousePosArr[1], mousePosArr[2]);

System.out.println("mouse x "+ mousePosArr[0] );
System.out.println("mouse y "+ mousePosArr[1] );
System.out.println();

System.out.println("worldx "+ x);
System.out.println("worldy "+ y);
System.out.println("worldz "+ z);

System.out.println();

This returns the coordinates of the x and y of the camera but never the correct location.

think of a game like red alert where you click somewhere in the 3D space and it gets the location of the click i need to do the same thing.

again any help is welcome.

p.s. the x, y, z variables that are being System outed, are the x, y, z of the camera glu.gluLookat function

FlorianR
03-02-2011, 11:49 AM
What value do you get for the depth? What is that glEnd() for there? And what is worldX/Y/Z?

Formatwizard
03-02-2011, 12:00 PM
Depth is not really important here since i only need the x and y.
the glEnd() was not necessary so i've removed that one.

worldX/Y/Z are the variables used here that define my eyex, eyey and eyez.

glu.gluPerspective(45, widthHeightRatio, 1, 600);
glu.gluLookAt(x, y, z, x, y + 120, 9,
0, 1, 0 );

I then draw a square of lines that "simulate" a flatterrain.

gl.glBegin(GL2.GL_LINES);
gl.glColor4d(0, 1, 0, 1d);

for ( int i = 0; i < map.length; i++ ) {

for ( int j = 0; j < map[i].length; j++ ) {

gl.glVertex3d(i, j, map[i][j]);
}
}

gl.glEnd();

FlorianR
03-02-2011, 12:08 PM
I meant the depth value you read with glReadPixels, what value is it?

Formatwizard
03-02-2011, 12:21 PM
There is only one value in my buffer.
If i get that one it returns 0.0 not sure if that is correct

FlorianR
03-02-2011, 12:52 PM
It sounds like your depth read is incorrect. Are you sure you are clicking on one of the lines and not missing it? You could check by also reading the color. If it's equal to the clear color, you probably read from a spot on which you haven't drawn anything yet.

Formatwizard
03-02-2011, 01:10 PM
The lines are there just to simulate terrain i am not actually using it for example by pushing a name on the stack for each line. Perhaps the image below can be more explaining.

http://img710.imageshack.us/i/explanationg.png/

The models there have there own coordinates ( as shown in the image ) I want to get the coordinates of where I click.

At the moment when i click i receive the camera ( lookat ) x and y