Point-triangle intersection in 3d from mouse coordinates?

我的梦境 提交于 2019-12-11 01:09:19

问题


I know how to test intersection between a point and a triangle.

...But i dont get it, how i can move the starting position of the point onto the screen plane precisely by using my mouse coordinates, so the point angle should change depending on where mouse cursor is on the screen, also this should work perfectly no matter which perspective angle i am using in my OpenGL application, so the point angle would be different on different perspective angles... gluPerspective() is the function im talking about.


回答1:


Well, gonna take a shot and guess what you mean. The guess is that you would like to pick objects with your mouse. Check out:

glUnProject.

This transforms the screen coordinates back into 3d world coordinates.

Google has more information if you run into problems.

Cheers !




回答2:


yes, i want to move the point on the screen plane, so for example i could render a cube on that point where my mouse is currently, by using 3d coordinates, and then i shoot a line from that position to the place where my mouse is pointing, so it would hit the triangle in my 3d world, and that how i could select that object with mouse.

sorry for being unclear :/

--

Edit: yay i got it working with that nehe tutorial! thanks, i didnt know it would be that easy!

This is the code im using now and it works great:

void GetOGLPos(int x, int y, GLdouble &posX, GLdouble &posY, GLdouble &posZ){
    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLfloat winX, winY, winZ;

    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
    glGetDoublev(GL_PROJECTION_MATRIX, projection);
    glGetIntegerv(GL_VIEWPORT, viewport);

    winX = (float)x;
    winY = (float)viewport[3]-(float)y;
    glReadPixels(x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);

    gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
}



回答3:


You need to generate a ray (line) passing through the mouse location perpendicular to the screen.

I would recommend getting some basic information on 3d geometry and 2d projections before you go much further.

Check out Wikipedia

A book search on Google has come up with quite a few titles.

Foley & Van Dam though is the definitive book - here on Amazon.co.uk or here on Amazon.com



来源:https://stackoverflow.com/questions/902348/point-triangle-intersection-in-3d-from-mouse-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!