LWJGL 3D picking

痞子三分冷 提交于 2019-12-24 07:59:29

问题


So I have been trying to understand the concept of 3D picking but as I can't find any video guides nor any concrete guides that actually speak English, it is proving to be very difficult. If anyone is well experienced with 3D picking in LWJGL, could you give me an example with line by line explanation of what everything means. I should mention that all I am trying to do it shoot the ray out of the center of the screen (not where the mouse is) and have it detect just a normal cube (rendered in 6 QUADS).


回答1:


Though I am not an expert with 3D picking, I have done it before, so I will try to explain.

You mentioned that you want to shoot a ray, rather than go by mouse position; as long as this ray is parallel to the screen, this method will still work, just the same as it will for a random screen coordinate. If not, and you actually wish to shoot a ray out, angled in some direction, things get a little more complicated, but I will not go in to it (yet).

Now how about some code?

Object* picking3D(int screenX, int screenY){
    //Disable any lighting or textures
    glDisable(GL_LIGHTING);
    glDisable(GL_TEXTURE);

    //Render Scene
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    orientateCamera();

    for(int i = 0; i < objectListSize; i++){
        GLubyte blue = i%256;
        GLubyte green = min((int)((float)i/256), 255);
        GLubyte red = min((int)((float)i/256/256), 255);
        glColor3ub(red, green, blue);
        orientateObject(i);
        renderObject(i);
    }

    //Get the pixel
    GLubyte pixelColors[3];
    glReadPixels(screenX, screenY, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixelColors);

    //Calculate index
    int index = pixelsColors[0]*256*256 + pixelsColors[1]*256 + pixelColors[2];

    //Return the object
    return getObject(index);
}

Code Notes:

  • screenX is the x location of the pixel, and screenY is the y location of the pixel (in screen coordinates)
  • orientateCamera() simply calls any glTranslate, glRotate, glMultMatrix, etc. needed to position (and rotate) the camera in your scene
  • orientateObject(i) does the same as orientateCamera, except for object 'i' in your scene
  • when I 'calculate the index', I am really just undoing the math I performed during the rendering to get the index back

The idea behind this method is that each object will be rendered exactly how the user sees it, except that all of a model is a solid colour. Then, you check the colour of the pixel for the screen coordinate requested, and which ever model the colour is indexed to: that's your object!

I do recommend, however, adding a check for the background color (or your glClearColor), just in case you don't actually hit any objects.

Please ask for further explanation if necessary.



来源:https://stackoverflow.com/questions/10809021/lwjgl-3d-picking

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