奇怪的光线追踪伪影问题

4
我正在尝试使用Qt创建一个光线追踪器,但是我遇到了一些奇怪的伪影问题。
在我实现着色之前,我的场景中只有4个球体、3个三角形和2个边界平面。它们都像预期的那样显示出来,颜色也符合预期,但对于我的平面而言,我会看到与背景相同颜色的点。这些点会从我的视角位置静止不动,所以如果我移动相机,这些点也会随之移动。但是它们只影响平面和三角形,永远不会出现在球体上。 当我实现着色后,问题变得更严重了。这些点现在也出现在光源中的球体上,因此任何受漫反射影响的部分都会受到影响。 另外,我的一个全蓝色平面(RGB 0,0,255)变成了纯黑色。由于我有两个平面,我交换了它们的颜色,然后再次发现蓝色变成了黑色,因此这是一个颜色问题,而不是一个平面问题。
如果有人有关于问题可能是什么或者想要查看特定代码的建议,请告诉我。

#include "plane.h"
#include "intersection.h"

#include <math.h>
#include <iostream>

Plane::Plane(QVector3D bottomLeftVertex, QVector3D topRightVertex, QVector3D normal, QVector3D point, Material *material)
{
    minCoords_.setX(qMin(bottomLeftVertex.x(),topRightVertex.x()));
    minCoords_.setY(qMin(bottomLeftVertex.y(),topRightVertex.y()));
    minCoords_.setZ(qMin(bottomLeftVertex.z(),topRightVertex.z()));
    maxCoords_.setX(qMax(bottomLeftVertex.x(),topRightVertex.x()));
    maxCoords_.setY(qMax(bottomLeftVertex.y(),topRightVertex.y()));
    maxCoords_.setZ(qMax(bottomLeftVertex.z(),topRightVertex.z()));
    normal_ = normal;
    normal_.normalize();
    point_ = point;
    material_ = material;
}

Plane::~Plane()
{

}

void Plane::intersect(QVector3D rayOrigin, QVector3D rayDirection, Intersection* result)
{
    if(normal_ == QVector3D(0,0,0)) //plane is degenerate
    {
        cout << "degenerate plane" << endl;
        return;
    }
    float minT;
    //t = -Normal*(Origin-Point) / Normal*direction
    float numerator = (-1)*QVector3D::dotProduct(normal_, (rayOrigin - point_));
    float denominator = QVector3D::dotProduct(normal_, rayDirection);
    if (fabs(denominator) < 0.0000001) //plane orthogonal to view
    {
        return;
    }
    minT = numerator / denominator;
    if (minT < 0.0)
    {
        return;
    }
    QVector3D intersectPoint = rayOrigin + (rayDirection * minT);
    //check inside plane dimensions
    if(intersectPoint.x() < minCoords_.x() || intersectPoint.x() > maxCoords_.x() ||
       intersectPoint.y() < minCoords_.y() || intersectPoint.y() > maxCoords_.y() ||
       intersectPoint.z() < minCoords_.z() || intersectPoint.z() > maxCoords_.z())
    {
        return;
    }
    //only update if closest object
    if(result->distance_ > minT)
    {
        result->hit_ = true;
        result->intersectPoint_ = intersectPoint;
        result->normalAtIntersect_ = normal_;
        result->distance_ = minT;
        result->material_ = material_;
    }
}

QVector3D MainWindow::traceRay(QVector3D rayOrigin, QVector3D rayDirection, int depth)
{
    if(depth > maxDepth)
    {
        return backgroundColour;
    }
    Intersection* rayResult = new Intersection();
    foreach (Shape* shape, shapeList)
    {
        shape->intersect(rayOrigin, rayDirection, rayResult);
    }
    if(rayResult->hit_ == false)
    {
        return backgroundColour;
    }
    else
    {
        QVector3D intensity = QVector3D(0,0,0);
        QVector3D shadowRay = pointLight - rayResult->intersectPoint_;
        shadowRay.normalize();
        Intersection* shadowResult = new Intersection();
        foreach (Shape* shape, shapeList)
        {
            shape->intersect(rayResult->intersectPoint_, shadowRay, shadowResult);
        }
        if(shadowResult->hit_ == true)
        {
            intensity += shadowResult->material_->diffuse_ * intensityAmbient;
        }
        else
        {
            intensity += rayResult->material_->ambient_ * intensityAmbient;
            // Diffuse
            intensity += rayResult->material_->diffuse_ * intensityLight * qMax(QVector3D::dotProduct(rayResult->normalAtIntersect_,shadowRay), 0.0f);
            // Specular
            QVector3D R = ((2*(QVector3D::dotProduct(rayResult->normalAtIntersect_,shadowRay))* rayResult->normalAtIntersect_) - shadowRay);
            R.normalize();
            QVector3D V = rayOrigin - rayResult->intersectPoint_;
            V.normalize();
            intensity += rayResult->material_->specular_ * intensityLight * pow(qMax(QVector3D::dotProduct(R,V), 0.0f), rayResult->material_->specularExponent_);
        }
        return intensity;
    }
}


请展示一些代码,特别是针对飞机的代码。 - Mare Infinitus
添加了飞机的代码以及阴影。 - Stephen B
1个回答

2

我找到了问题的原因。这是由于浮点数精度很差导致的,任何小于0.0的检查都会间歇性地失败,因为浮点数的精度问题。我不得不给所有的检查添加一个偏移量,以便我检查的是小于0.001。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接