光线追踪器中的UV错误

时间:2018-10-30 17:19:42

标签: c++ raytracing

我的光线跟踪程序有问题。图像看起来不对。这是输出图像:

out

重心坐标和碰撞计算代码如下:

bool CTriangle::Intersect(Calculus::CRay& ray, CIntersection* isect) const {
// Möller–Trumbore intersection algorithm
const Calculus::CPoint3<float>& p1 = v_points[0];
const Calculus::CPoint3<float>& p2 = v_points[1];
const Calculus::CPoint3<float>& p3 = v_points[2];

Calculus::CVector3<float> e1 = p2 - p1;
Calculus::CVector3<float> e2 = p3 - p1;
Calculus::CVector3<float> s1 = Calculus::Math::Cross(ray.direction, e2);
float determinant = Calculus::Math::Dot(s1, e1);

if (determinant == 0.0f)
    return false;

float inv_determinant = 1.0f / determinant;

Calculus::CVector3<float> s = ray.origin - p1;
float b1 = Calculus::Math::Dot(s, s1) * inv_determinant;
if (b1 < 0.0f || b1 > 1.0f)
    return false;

Calculus::CVector3<float> s2 = Calculus::Math::Cross(s, e1);
float b2 = Calculus::Math::Dot(ray.direction, s2) * inv_determinant;
if (b2 < 0.0f || b1 + b2 > 1.0f)
    return false;

float b0 = 1 - b1 - b2;

float thit = Calculus::Math::Dot(e2, s2) * inv_determinant;
if (thit < ray.mint || thit > ray.maxt)
    return false;

isect->p = ray(thit);
isect->n = Calculus::Math::Normalize(Calculus::CVector3<float> 
(v_normals[0].x, v_normals[0].y, v_normals[0].z) * b0 +
    Calculus::CVector3<float>(v_normals[1].x, v_normals[1].y, 
v_normals[1].z) * b1 +
    Calculus::CVector3<float>(v_normals[2].x, v_normals[2].y, 
v_normals[2].z) * b2);
isect->uv = v_uvs[0] * b0 + v_uvs[1] * b1 + v_uvs[2] * b2;
isect->tHit = thit;
isect->ray_epsilon = 1e-5f * thit;

return true;
}

我在光线跟踪程序中使用了纹理:(文件类型:bmp)

Texture1

我的obj文件如下。背景形状由两个三角形组成。纹理投影仅应用于背景形状:

v  -24.1456 -11.1684 -26.2413
v  24.1455 -11.1684 -26.2413
v  -24.1456 37.1227 -26.2413
v  24.1455 37.1227 -26.2413
# 4 vertices

vn 0.0000 0.0000 1.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 0.0000 1.0000
# 6 vertex normals

vt 0.9995 0.0005 0.0000
vt 0.0005 0.0005 0.0000
vt 0.9995 0.9995 0.0000
vt 0.0005 0.9995 0.0000
# 4 texture coords

o back
g back
usemtl default
s 1
f 1/1/1 2/2/2 4/4/3 
f 4/4/4 3/3/5 1/1/6 
# 2 faces

这是插值的uv draw调用。

enter image description here

这里是索引算法,我从零开始:

...
Calculus::CPoint3<unsigned short> p, t, n;
sscanf_s(token, "%hu/%hu/%hu %hu/%hu/%hu %hu/%hu/%hu",
    &p.x, &t.x, &n.x, &p.y, &t.y, &n.y, &p.z, &t.z, &n.z);
pi.push_back(p);
ti.push_back(t);
ni.push_back(n);
…

index = ti[i].x - 1;
temp_t[0] = vt[index]; // first uv
index = ti[i].y - 1;
temp_t[1] = vt[index]; // second uv
index = ti[i].z - 1;
temp_t[2] = vt[index]; // third uv

我想知道我在哪里犯错。谢谢。

1 个答案:

答案 0 :(得分:2)

% extract your latitude, longitude data from your input file (which I do not have)
% lat = <your latitude data>
% lon = <your longitude data>

% Your z-values for the heatmap (Concentration). I used random number here
concentration = rand(size(lat));

% Plot a scatter plot with a colorscaling on concentration
scatter(lat,lon,50,concentration,'o','filled')

% Add colorbar
colorbar

% Add google map section of your lat and lon data as background on the plot
plot_google_map

这不是正确的顶点属性参数插值:

  • 参数isect->uv = v_uvs[0] * b1 + v_uvs[1] * b2; 应用于错误的顶点
  • 您没有考虑到第三个顶点b1, b2

正确版本:

v_uvs[2]