C++ 光线跟踪三角形网格对象

C++ 光线跟踪三角形网格对象,c++,geometry,raytracing,C++,Geometry,Raytracing,我正在尝试为三角形网格形成的任何对象编写光线跟踪器。我使用一个外部库从.ply格式加载一个多维数据集,然后跟踪它。到目前为止,我已经实现了大部分跟踪器,现在我尝试用一个立方体来测试它,但由于某些原因,我在屏幕上看到的只是一条红线。我试过几种方法来修复它,但我再也想不出来了。对于这个主要测试,我只创建主要光线,如果它们击中我的立方体,那么我将该像素着色为立方体的漫反射颜色并返回。为了检查光线对象的交点,我将遍历形成该对象的所有三角形,并返回距离最近的三角形的距离。如果你能看看代码,告诉我哪里出了问

我正在尝试为三角形网格形成的任何对象编写光线跟踪器。我使用一个外部库从.ply格式加载一个多维数据集,然后跟踪它。到目前为止,我已经实现了大部分跟踪器,现在我尝试用一个立方体来测试它,但由于某些原因,我在屏幕上看到的只是一条红线。我试过几种方法来修复它,但我再也想不出来了。对于这个主要测试,我只创建主要光线,如果它们击中我的立方体,那么我将该像素着色为立方体的漫反射颜色并返回。为了检查光线对象的交点,我将遍历形成该对象的所有三角形,并返回距离最近的三角形的距离。如果你能看看代码,告诉我哪里出了问题,哪里出了问题,那就太好了。我将不胜感激

射线三角形交点:

bool intersectTri(const Vec3D& ray_origin, const Vec3D& ray_direction, const Vec3D& v0, const Vec3D& v1, const Vec3D& v2, double &t, double &u, double &v) const
    {

        Vec3D edge1 = v1 - v0;  
        Vec3D edge2 = v2 - v0;
        Vec3D pvec = ray_direction.cross(edge2);
        double det = edge1.dot(pvec);
        if (det > - THRESHOLD && det < THRESHOLD)
            return false;
        double invDet = 1/det;  
        Vec3D tvec = ray_origin - v0;
        u = tvec.dot(pvec)*invDet;
        if (u < 0 || u > 1)
            return false;
        Vec3D qvec = tvec.cross(edge1);
        v = ray_direction.dot(qvec)*invDet;
        if (v < 0 || u + v > 1)
            return false;
        t = edge2.dot(qvec)*invDet;
        if (t < 0)
            return false;
        return true;
    }   

//Object intersection
bool intersect(const Vec3D& ray_origin, const Vec3D& ray_direction, IntersectionData& idata, bool enforce_max) const
    {

        double tClosest;
        if (enforce_max)
        {
            tClosest = idata.t;
        }
        else
        {
            tClosest = TMAX;
        }

        for (int i = 0 ; i < indices.size() ; i++)
        {
            const Vec3D v0 = vertices[indices[i][0]];
            const Vec3D v1 = vertices[indices[i][1]];
            const Vec3D v2 = vertices[indices[i][2]];
            double t, u, v;
            if (intersectTri(ray_origin, ray_direction, v0, v1, v2, t, u, v))
            {
                if (t < tClosest)   
                {
                    idata.t = t;
                    tClosest = t;                   
                    idata.u = u;
                    idata.v = v; 
                    idata.index = i;
                }
            }
        }
        return (tClosest < TMAX && tClosest > 0) ? true : false;
    }

Vec3D trace(World world, Vec3D &ray_origin, Vec3D &ray_direction)
{

Vec3D objColor = world.background_color;
IntersectionData idata;
double coeff = 1.0;
int depth = 0;

double tClosest = TMAX; 
Object *hitObject = NULL;   
for (unsigned int i = 0 ; i < world.objs.size() ; i++)
{       
    IntersectionData idata_curr;
    if (world.objs[i].intersect(ray_origin, ray_direction, idata_curr, false)) 
    {
        if (idata_curr.t < tClosest && idata_curr.t > 0) 
        {
            idata.t = idata_curr.t;
            idata.u = idata_curr.u;
            idata.v = idata_curr.v;
            idata.index = idata_curr.index; 
            tClosest = idata_curr.t;            
            hitObject = &(world.objs[i]);
        }
    }
}
if (hitObject == NULL)
{
    return world.background_color;
}
else
{
    return hitObject->getDiffuse();
}
}

int main(int argc, char** argv)
{

parse("cube.ply");
Vec3D diffusion1(1, 0, 0);
Vec3D specular1(1, 1, 1);
Object cube1(coordinates, connected_vertices, diffusion1, specular1, 0, 0);
World wrld;
// Add objects to the world
wrld.objs.push_back(cube1);
Vec3D background(0, 0, 0);
wrld.background_color = background;
// Set light color
Vec3D light_clr(1, 1, 1);
wrld.light_colors.push_back(light_clr);
// Set light position
Vec3D light(0, 64, -10);
wrld.light_positions.push_back(light);

int width = 128;
int height = 128;
Vec3D *image = new Vec3D[width*height];
Vec3D *pixel = image;

// Trace rays
for (int y = -height/2 ; y < height/2 ; ++y)
{
    for (int x = -width/2 ; x < width/2 ; ++x, ++pixel)
    {
        Vec3D ray_dir(x+0.5, y+0.5, -1.0);
        ray_dir.normalize();
        Vec3D ray_orig(0.5*width, 0.5*height, 0.0);
        *pixel = trace(wrld, ray_orig, ray_dir);        
    }
}   

savePPM("./test.ppm", image, width, height);
return 0; 
}
bool intersectTri(const-Vec3D&ray_原点、const-Vec3D&ray_方向、const-Vec3D&v0、const-Vec3D&v1、const-Vec3D&v2、double&t、double&u、double&v)const
{
Vec3D edge1=v1-v0;
Vec3D edge2=v2-v0;
Vec3D pvec=射线方向交叉(边2);
双det=边1.点(pvec);
如果(检测值>阈值和检测值<阈值)
返回false;
双invDet=1/det;
Vec3D tvec=射线源-v0;
u=tvec.dot(pvec)*invDet;
如果(u<0 | | u>1)
返回false;
Vec3D qvec=tvec.交叉(边1);
v=射线方向点(qvec)*invDet;
如果(v<0 | | u+v>1)
返回false;
t=边缘2.圆点(qvec)*invDet;
if(t<0)
返回false;
返回true;
}   
//对象相交
布尔相交(常量向量3D和射线原点,常量向量3D和射线方向,相交数据和数据,布尔强制最大值)常量
{
双tClosest;
如果(强制执行最大值)
{
tClosest=idata.t;
}
其他的
{
tClosest=TMAX;
}
对于(int i=0;i0)?真:假;
}
Vec3D跟踪(世界世界、Vec3D和射线源、Vec3D和射线方向)
{
Vec3D objColor=world.background\u color;
交叉数据;
双系数=1.0;
int深度=0;
双tClosest=TMAX;
Object*hitObject=NULL;
for(无符号int i=0;i0)
{
idata.t=idata_curr.t;
idata.u=idata_curr.u;
idata.v=idata_curr.v;
idata.index=idata_当前索引;
tClosest=idata_curr.t;
hitObject=&(world.objs[i]);
}
}
}
if(hitObject==NULL)
{
返回world.background\u颜色;
}
其他的
{
返回hitObject->getDiffuse();
}
}
int main(int argc,字符**argv)
{
解析(“cube.ply”);
Vec3D扩散1(1,0,0);
Vec3D镜面反射镜1(1,1,1);
对象立方体1(坐标、连接的顶点、扩散1、镜面反射1、0、0);
世界wrld;
//向世界添加对象
wrld.objs.推回(立方体1);
Vec3D背景(0,0,0);
wrld.background_color=背景;
//设置浅色
Vec3D灯光_clr(1,1,1);
wrld。浅颜色。向后推(浅颜色);
//设置灯光位置
Vec3D光(0,64,-10);
警告灯位置。向后推(灯);
整数宽度=128;
整数高度=128;
Vec3D*图像=新的Vec3D[宽度*高度];
Vec3D*像素=图像;
//微量射线
对于(int y=-height/2;y
我刚刚运行了一个测试用例,得到了以下结果:

对于以(0,0,-1.5)为中心并在X和Y轴上缩放100的单位立方体。看起来这个投影有点问题,但我真的不能确切地从结果中看出什么。此外,不应该,在这种情况下(立方体以(0,0)为中心),最终对象也出现在图片的中间。
修复:在规范化和调用跟踪函数之前,我通过执行ray\u dir=ray\u dir-ray\u orig修复了定心问题。尽管如此,透视图似乎是完全错误的。

我继续工作,现在我开始根据Phong实现漫反射

Vec3D跟踪(世界世界、Vec3D和射线源、Vec3D和射线方向) {

Vec3D-objColor=Vec3D(0);
交叉数据;
双系数=1.0;
int深度=0;
做
{
双tClosest=TMAX;
Object*hitObject=NULL;
for(无符号int i=0;i0)
{
idata.t=idata_curr.t;
idata.u=idata_curr.u;
idata.v=idata_curr.v;
idata.index=idata_当前索引;
tClosest=idata_curr.t;
hitObject=&(world.objs[i]);
}
}
}   
如果(hitObjec
Vec3D objColor = Vec3D(0);
IntersectionData idata;
double coeff = 1.0;
int depth = 0;
do
{
    double tClosest = TMAX; 
    Object *hitObject = NULL;   
    for (unsigned int i = 0 ; i < world.objs.size() ; i++)
    {       
        IntersectionData idata_curr;
        if (world.objs[i].intersect(ray_origin, ray_direction, idata_curr, false)) 
        {
            if (idata_curr.t < tClosest && idata_curr.t > 0) 
            {
                idata.t = idata_curr.t;
                idata.u = idata_curr.u;
                idata.v = idata_curr.v;
                idata.index = idata_curr.index; 
                tClosest = idata_curr.t;            
                hitObject = &(world.objs[i]);
            }
        }
    }   
    if (hitObject == NULL)
    {
        return world.background_color;
    }

    Vec3D newStart = ray_origin + ray_direction*idata.t;

    // Compute normal at intersection by interpolating vertex normals (PHONG Idea)
    Vec3D v0 = hitObject->getVertices()[hitObject->getIndices()[idata.index][0]];
    Vec3D v1 = hitObject->getVertices()[hitObject->getIndices()[idata.index][1]];
    Vec3D v2 = hitObject->getVertices()[hitObject->getIndices()[idata.index][2]];   

    Vec3D n1 = hitObject->getNormals()[hitObject->getIndices()[idata.index][0]];
    Vec3D n2 = hitObject->getNormals()[hitObject->getIndices()[idata.index][1]];
    Vec3D n3 = hitObject->getNormals()[hitObject->getIndices()[idata.index][2]];

//  Vec3D N = n1 + (n2 - n1)*idata.u + (n3 - n1)*idata.v;
    Vec3D N = v0.computeFaceNrm(v1, v2);
    if (ray_direction.dot(N) > 0)
    {
        N = N*(-1);
    }
    N.normalize();

    Vec3D lightray_origin = newStart;

    for (unsigned int itr = 0 ; itr < world.light_positions.size() ; itr++)
    {

        Vec3D lightray_dir = world.light_positions[0] - newStart;
        lightray_dir.normalize();

        double cos_theta = max(N.dot(lightray_dir), 0.0);
        objColor.setX(objColor.getX() + hitObject->getDiffuse().getX()*hitObject->getDiffuseReflection()*cos_theta);
        objColor.setY(objColor.getY() + hitObject->getDiffuse().getY()*hitObject->getDiffuseReflection()*cos_theta);
        objColor.setZ(objColor.getZ() + hitObject->getDiffuse().getZ()*hitObject->getDiffuseReflection()*cos_theta);
        return objColor;
    }

    depth++;

} while(coeff > 0 && depth < MAX_RAY_DEPTH);
return objColor;