gpt4 book ai didi

opengl - 使用射线转换进行对象拾取

转载 作者:行者123 更新时间:2023-12-04 03:45:37 50 4
gpt4 key购买 nike

我在检测框内鼠标命中的射线转换算法中存在不准确的问题。我对如何正确解决这个问题完全不知所措,这困扰了我好几个星期。

用图片(方框以[0,0,-30]为中心)最容易描述问题:

黑线代表绘制的实际命中框,绿框代表实际看起来要命中的框。请注意它的偏移量(如果该框离原点更远,它似乎会变大)并且比绘制的命中框略小。

这是一些相关的代码,

射线盒 Actor :

double BBox::checkFaceIntersection(Vector3 points[4], Vector3 normal, Ray3 ray) {

double rayDotNorm = ray.direction.dot(normal);
if(rayDotNorm == 0) return -1;

Vector3 intersect = points[0] - ray.origin;
double t = intersect.dot(normal) / rayDotNorm;
if(t < 0) return -1;

// Check if first point is from under or below polygon
bool positive = false;
double firstPtDot = ray.direction.dot( (ray.origin - points[0]).cross(ray.origin - points[1]) );
if(firstPtDot > 0) positive = true;
else if(firstPtDot < 0) positive = false;
else return -1;

// Check all signs are the same
for(int i = 1; i < 4; i++) {
int nextPoint = (i+1) % 4;
double rayDotPt = ray.direction.dot( (ray.origin - points[i]).cross(ray.origin - points[nextPoint]) );
if(positive && rayDotPt < 0) {
return -1;
}
else if(!positive && rayDotPt > 0) {
return -1;
}
}

return t;
}

鼠标发出光线:
GLint viewport[4];
GLdouble modelMatrix[16];
GLdouble projectionMatrix[16];

glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);

GLfloat winY = GLfloat(viewport[3] - mouse_y);

Ray3 ray;
double x, y, z;
gluUnProject( (double) mouse_x, winY, 0.0f, // Near
modelMatrix, projectionMatrix, viewport,
&x, &y, &z );
ray.origin = Vector3(x, y, z);

gluUnProject( (double) mouse_x, winY, 1.0f, // Far
modelMatrix, projectionMatrix, viewport,
&x, &y, &z );
ray.direction = Vector3(x, y, z);

if(bbox.checkBoxIntersection(ray) != -1) {
std::cout << "Hit!" << std::endl;
}

我尝试将实际的光线绘制为一条线,它似乎与绘制的框正确相交。

通过将所有点和射线原点/方向减去盒子的位置,可以部分解决偏移问题,但是我不知道为什么这样行得通,而且命中盒的大小仍然不准确。

有什么想法/替代方法吗?如果需要,我还有其他代码可以提供。

最佳答案

您假设方向错误。正确的是:

ray.direction = Vector3(far.x - near.x, far.y - near.y, far.z - near.z);

不减去近和远相交点,您的方向将关闭。

关于opengl - 使用射线转换进行对象拾取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9406269/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com