"Picking" with the mouse

Started by
4 comments, last by spooderw 12 years ago
I've read tutorials on this, but I can't seem to get it to work. I don't think I understand what exactly is going on at all. What I'm trying to do is get the number of objects (and later the actual objects) that are intersected by a ray cast from the center of the screen. (Or where the mouse is)

Heres what I have to so far, and it's printing -1 always.
glLoadIdentity();
glRotatef(pit, 1.0f, 0.0f, 0.0f);
glRotatef(yaw, 0.0f, 1.0f, 0.0f);
glTranslatef(-x, -y, -z);

glRenderMode(GL_SELECT);
IntBuffer i = ByteBuffer.allocateDirect(64).asIntBuffer();
glGetInteger(GL_VIEWPORT, i);
gluPickMatrix(canv.getWidth() / 2, canv.getHeight() / 2, 1.0f, 1.0f, i);
gluPerspective(60f, 1.0f, 0.0001f, 1000.0f);
world.draw();
int hits = glRenderMode(GL_RENDER);
System.out.println(hits);
glLoadIdentity();
glRotatef(pit, 1.0f, 0.0f, 0.0f);
glRotatef(yaw, 0.0f, 1.0f, 0.0f);
glTranslatef(-x, -y, -z);
world.draw();
Advertisement

[font=arial,helvetica,sans-serif]If you're trying to select objects in a 3D world with the mouse, check out [color=#000000]gluUnProject.[/font]


[font=arial,helvetica,sans-serif]If you're trying to select objects in a 3D world with the mouse, check out [color=#000000]gluUnProject.[/font]

I've seen that also, but I dont quite understand the parameters
Nehe's got a tutorial for that.
Nehe's got a tutorial for that.

Looking from that tutorial, I was able to convert the C code to Java. However, it returns things such as 0, NaN, or sometimes insane coordinates, even if the point is on top of a rendered polygon. Am I missing a step?

float[] screenToWorld(int x, int y) {
IntBuffer viewport = ByteBuffer.allocateDirect(16 * 4).asIntBuffer();
FloatBuffer modelview = ByteBuffer.allocateDirect(16 * 4).asFloatBuffer();
FloatBuffer projection = ByteBuffer.allocateDirect(16 * 4).asFloatBuffer();
float winX, winY;
glGetFloat(GL_MODELVIEW_MATRIX, modelview);
glGetFloat(GL_PROJECTION_MATRIX, projection);
glGetInteger(GL_VIEWPORT, viewport);
winX = (float) x;
winY = (float) viewport.get(3) - (float) y;
FloatBuffer winZ = ByteBuffer.allocateDirect(1 * 4).asFloatBuffer();
glReadPixels(x, (int) winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, winZ);
FloatBuffer pos = ByteBuffer.allocateDirect(3 * 4).asFloatBuffer();
gluUnProject(winX, winY, winZ.get(0), modelview, projection, viewport, pos);
float[] raw = new float[3];
pos.get(raw);
return raw;
}

[quote name='spooderw' timestamp='1332979106' post='4926168'] Nehe's got a tutorial for that.

Looking from that tutorial, I was able to convert the C code to Java. However, it returns things such as 0, NaN, or sometimes insane coordinates, even if the point is on top of a rendered polygon. Am I missing a step?

float[] screenToWorld(int x, int y) {
IntBuffer viewport = ByteBuffer.allocateDirect(16 * 4).asIntBuffer();
FloatBuffer modelview = ByteBuffer.allocateDirect(16 * 4).asFloatBuffer();
FloatBuffer projection = ByteBuffer.allocateDirect(16 * 4).asFloatBuffer();
float winX, winY;
glGetFloat(GL_MODELVIEW_MATRIX, modelview);
glGetFloat(GL_PROJECTION_MATRIX, projection);
glGetInteger(GL_VIEWPORT, viewport);
winX = (float) x;
winY = (float) viewport.get(3) - (float) y;
FloatBuffer winZ = ByteBuffer.allocateDirect(1 * 4).asFloatBuffer();
glReadPixels(x, (int) winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, winZ);
FloatBuffer pos = ByteBuffer.allocateDirect(3 * 4).asFloatBuffer();
gluUnProject(winX, winY, winZ.get(0), modelview, projection, viewport, pos);
float[] raw = new float[3];
pos.get(raw);
return raw;
}

[/quote]

My java is really rusty, so I could not tell you specifically what may be wrong with your code...

This topic is closed to new replies.

Advertisement