Jump to content
  • Advertisement
Sign in to follow this  
Colby_

"Picking" with the mouse

This topic is 2394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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();

Share this post


Link to post
Share on other sites
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]

I've seen that also, but I dont quite understand the parameters

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites

[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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!