c – Wierd Raytracing Artifacts

我正在尝试使用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;
    }
}

最佳答案 所以我想出了我的问题.它们是由于漂浮精度很差,任何检查

点赞