c++ - Wierd Raytracing Artifacts -
i trying create ray tracer using qt, have weird artifacts going on.
before implemented shading, had 4 spheres, 3 triangles , 2 bounded planes in scene. showed expected , color expected however, planes, see dots same color background. these dots stay static view position, if moved camera around dots move around well. affected planes , triangles , never appear on spheres. 1 implemented shading issue got worse. dots appear on spheres in light source, part affected diffuse. also, 1 plane of pure blue (rgb 0,0,255) has gone straight black. since have 2 planes switched colors , again blue 1 went black, it's color issue , not plane issue.
if has suggestions problem or wants see particular code let me know.
#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 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 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; } }
so figured out issues. due float being terrible @ precision, check < 0.0 intermittently fail because of floats precision. had add offset checks checking < 0.001.
Comments
Post a Comment