[英]LWJGL 3D picking
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. 因此,我一直试图理解3D拾取的概念,但是由于我找不到任何视频指南或任何实际讲英语的具体指南,因此事实证明这非常困难。 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.
如果有人在LWJGL中进行3D拾取方面有丰富的经验,您可以逐行说明所有含义的示例。 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).
我应该提及的是,我正在尝试将所有光线从屏幕中心(而不是鼠标所在的位置)发射出去,并使其仅检测到正常的立方体(以6个QUADS呈现)。
Though I am not an expert with 3D picking, I have done it before, so I will try to explain. 尽管我不是3D拾取的专家,但我之前已经做过,所以我将尽力解释。
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: 代码说明:
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. 但是,我建议您添加检查背景色(或glClearColor)的方法,以防万一您实际上没有碰到任何物体。
Please ask for further explanation if necessary. 如有必要,请要求进一步的解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.