c++ - Raytracing Reflection distortion -


i've started coding raytracer, today encounter problem when dealing reflection.

first, here image of problem:

enter image description here

i computed object's reflected color (so no light effect applied on reflected object) problem distortion don't understand. looked @ angle between rayvector , normalvector , looks ok, reflected vector looks fine.

vector math::calcreflectedvector(const vector &ray,                                  const vector &normal) const {   double cosangle;   vector copynormal = normal;   vector copyview = ray;    copynormal.makeunit();   copyview.makeunit();   cosangle = copyview.scale(copynormal);   return (-2.0 * cosangle * normal + ray); } 

so example when ray hitting bottom of sphere have following values:

cos: 1

viewvector: [185.869,-2.44308,-26.3504]

normalvector: [185.869,-2.44308,-26.3504]

reflectedvector: [-185.869,2.44308,26.3504]

bellow if code handles reflection:

color rt::getreflectedcolor(std::shared_ptr<sceneobj> obj, camera camera,                             vector rayvec, double k, unsigned int pass) {   if (pass > 10)     return obj->getcolor();   if (obj->getreflectionindex() == 0) {     // apply effects     return obj->getcolor();   }    color cucolor(obj->getcolor());   color newcolor(0);   math math;   vector view;   vector normal;   vector reflected;   position impact;   std::pair<std::shared_ptr<sceneobj>, double> reflectedobj;    normal = math.calcnormalvector(camera.pos, obj, rayvec, k, impact);   view = vector(impact.x, impact.y, impact.z) -          vector(camera.pos.x, camera.pos.y, camera.pos.z);   reflected = math.calcreflectedvector(view, normal);   reflectedobj = this->getclosestobj(reflected, camera(impact));   if (reflectedobj.second <= 0) {     cucolor.mix(0x000000, obj->getreflectionindex());     return cucolor;   }   newcolor = this->getreflectedcolor(reflectedobj.first, camera(impact),                                      reflected, reflectedobj.second, pass + 1);   // apply effects   cucolor.mix(newcolor, obj->getreflectionindex());   return newcolor; } 

to calculate normal , reflected vector:

vector math::calcreflectedvector(const vector &ray,                                  const vector &normal) const {   double cosangle;   vector copyray = ray;    copyray.makeunit();   cosangle = copyray.scale(normal);   return (-2.0 * cosangle * normal + copyray); }  vector math::calcnormalvector(position pos, std::shared_ptr<sceneobj> obj,                               vector rayvec, double k, position& impact) const {   const position &objpos = obj->getposition();   vector normal;    impact.x = pos.x + k * rayvec.x;   impact.y = pos.y + k * rayvec.y;   impact.z = pos.z + k * rayvec.z;   obj->calcnormal(normal, impact);   return normal; } 

[edit1]

i have new image, removed plane keep spheres:

enter image description here

as can see there blue , yellow on border of sphere. neam colored sphere applying following formula:

  newcolor.r = reflected.x * 127.0 + 127.0;   newcolor.g = reflected.y * 127.0 + 127.0;   newcolor.b = reflected.z * 127.0 + 127.0; 

bellow visual result:

enter image description here

ask me if need information. in advance

there many little things example provided. may -- or may not -- answer question, suppose you're doing raytracer learning purposes (either @ school or in free time) i'll give hints.

  • you have 2 classes vector , position. may seems it's idea, why not seeing position translation vector origin ? avoid code duplication think (except if you've done using position = vector;). may want @ libraries mathematical things (like glm do). (and way, you'll avoid errors naming dot function scale())

  • you create camera position (that really strange thing). reflections doesn't involve camera. in typical raytracer, have 1 camera {position + direction + fov + ...} , each pixels of image/reflections/refractions/..., cast rays {origin + direction} (thus name raytracer, isn't cameratracer). camera class tied concept of physical camera things focal, depth of field, aperture, chromatic aberration, ... whereas ray simply... ray. (could ray plane output image mapped first object, or ray created reflection, diffraction, scattering, ...).

  • and final point, think error may comes math::calcnormalvector(...) function. sphere @ position p , intersection point i, normal n is: n = normalize(i - p);.

edit: seems problem comes rt::getclosestobj. else looking fine

there's ton websites/blogs/educative content online creating simple raytracer, first 2 points let them teach you. take @ glm. if don't figure out wrong calcnormalvector(...) please post code :)


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -