LWJGL 3D拾取 [英] LWJGL 3D picking

查看:124
本文介绍了LWJGL 3D拾取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我一直在尝试理解3D拾取的概念,但是由于我找不到任何视频指南或任何实际讲英语的具体指南,因此事实证明这非常困难.如果有人在LWJGL中进行3D拾取方面有丰富的经验,您能否举个例子逐行说明所有含义.我应该提到的是,我要做的所有事情都是将光线从屏幕中心发射出去(而不是鼠标所在的位置),并使它只能检测到正常的立方体(以6个QUADS呈现).

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

推荐答案

尽管我不是3D拾取的专家,但我之前已经做过,所以我将尽力解释.

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

代码注释:

  • screenX是像素的x位置,而screenY是像素的y位置(在屏幕坐标中)
  • orientateCamera()只需调用在场景中定位(和旋转)相机所需的任何glTranslate,glRotate,glMultMatrix等
  • orientateObject(i)与orientateCamera的功能相同,除了场景中的对象"i"
  • 当我计算索引"时,我实际上只是撤消了在渲染过程中执行的数学运算以恢复索引

此方法背后的想法是,将完全按照用户的观看方式渲染每个对象,但所有模型都是纯色的.然后,您检查所请求的屏幕坐标的像素颜色,以及将颜色索引到哪个模型:这就是您的对象!

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!

但是,我确实建议添加背景色(或glClearColor)检查,以防万一您实际上没有碰到任何物体.

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.

这篇关于LWJGL 3D拾取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆