From 13c3b8d1e2ecf7f1dbd02f9d4bfeb813b36fd40a Mon Sep 17 00:00:00 2001 From: Roland Hieber Date: Wed, 20 Jan 2010 19:45:52 +0100 Subject: [PATCH] still broken ReflectiveEyeLightShader, saving it for later --- Image.cxx | 26 +++++++++++++++++--------- MicroTrace.cxx | 8 ++++---- Ray.cxx | 6 ++++-- ReflectiveEyeLightShader.cxx | 20 +++++++++++++++----- Sphere.cxx | 18 +++++++++++------- 5 files changed, 51 insertions(+), 27 deletions(-) diff --git a/Image.cxx b/Image.cxx index b45cac9..899ac6f 100644 --- a/Image.cxx +++ b/Image.cxx @@ -39,31 +39,39 @@ Image::operator()(int x, int y) { assert(x >= 0 && x < m_resX); assert(y >= 0 && y < m_resY); - - return m_pixel[y*m_resX+x]; + + return m_pixel[y*m_resX+x]; } void Image::WritePPM(const std::string& fileName) { std::cerr << "(Image): Writing to file " << fileName << std::endl; std::ofstream file(fileName.c_str()); - + if(!file.is_open()) { std::cerr << "(Image): Could not open file " << fileName << std::endl; return; } - + file << "P3" << std::endl; file << m_resX << " " << m_resY << " " << 255 << std::endl; - for (int y=m_resY-1;y>=0;y--) + for (int y=m_resY-1;y>=0;y--) { for (int x=0;x 255) cur[0] = 255; + if(cur[1] > 255) cur[1] = 255; + if(cur[2] > 255) cur[2] = 255; + + file + << (int)(255.99999999 * cur[0]) << " " + << (int)(255.99999999 * cur[1]) << " " + << (int)(255.99999999 * cur[2]) << " " << "\t"; } file << std::endl; diff --git a/MicroTrace.cxx b/MicroTrace.cxx index 252ec18..3a27182 100644 --- a/MicroTrace.cxx +++ b/MicroTrace.cxx @@ -163,7 +163,7 @@ void RenderFrameReflectiveEyeLight(const std::string& fileName) { for (int x = 0; x < scene.camera()->resX(); x++) { - /* Initialize your ray here */ + /* Initialize your ray here */ //int x = 319, y = 59; // shoot four rays for antialiasing scene.camera()->InitRay(x+0.5,y+0.5, ray); // initialize ray Vec3f col1 = scene.RayTrace(ray); @@ -174,7 +174,7 @@ void RenderFrameReflectiveEyeLight(const std::string& fileName) scene.camera()->InitRay(x-0.5,y-0.5, ray); // initialize ray Vec3f col4 = scene.RayTrace(ray); - img(x,y) = (col1 + col2 + col3 + col4) / 4.0; // store pixel color + img(x,y) = (col1 /*+ col2 + col3 + col4) / 4.0*/ ); // store pixel color //std::cerr << "Main: Image color = " << img(x,y) << std::endl; } } @@ -188,7 +188,7 @@ void RenderFrameReflectiveEyeLight(const std::string& fileName) int main(int, char**) { - RenderFrameFlat("flatshaded.ppm"); - RenderFrameEyeLight("eyelight.ppm"); + //RenderFrameFlat("flatshaded.ppm"); + //RenderFrameEyeLight("eyelight.ppm"); RenderFrameReflectiveEyeLight("reflective.ppm"); } diff --git a/Ray.cxx b/Ray.cxx index a3c76cb..584db26 100644 --- a/Ray.cxx +++ b/Ray.cxx @@ -2,11 +2,13 @@ #include "Ray.hxx" +#define MAX_RECURSION_DEPTH 5 + Ray::Ray() : m_org(Vec3f()), m_dir(Vec3f()), m_hit(0), - m_level(10) + m_level(MAX_RECURSION_DEPTH) { m_t = std::numeric_limits::max(); } @@ -16,7 +18,7 @@ Ray::Ray(const Vec3f& org, : m_org(org), m_dir(dir), m_hit(0), - m_level(10) + m_level(MAX_RECURSION_DEPTH) { m_t = std::numeric_limits::max(); } diff --git a/ReflectiveEyeLightShader.cxx b/ReflectiveEyeLightShader.cxx index e7fa2ac..87369bd 100644 --- a/ReflectiveEyeLightShader.cxx +++ b/ReflectiveEyeLightShader.cxx @@ -26,13 +26,23 @@ ReflectiveEyeLightShader::Shade(Ray& ray) Vec3f eyeColor = EyeLightShader::Shade(ray); Vec3f reflColor; - // shoot secondary rays from intersection + // get normal and turn towards ray if angle > 90° Vec3f n = ray.hit()->GetNormal(ray); - Ray sec(ray.origin() + ray.direction() * ray.t(), ray.direction() + n * 2); + float cos_theta = n.dot(ray.direction()); + // we just need the sign, no value + if( cos_theta > 0 ) { + std::cout << "cos_theta="< 0 && m_scene->Intersect(sec)) { - reflColor = Shade(sec); + if(sec.recursionDepth() + 1 > 0) { + reflColor = m_scene->RayTrace(sec); + //reflColor = Shade(sec); } - return eyeColor + reflColor; + return eyeColor + (reflColor * m_reflectivity); } diff --git a/Sphere.cxx b/Sphere.cxx index 5706cc3..77d4320 100644 --- a/Sphere.cxx +++ b/Sphere.cxx @@ -44,11 +44,15 @@ Sphere::GetNormal(Ray& ray) Ray tempRay = ray; // Surface normal is the difference between intersection and center point - Intersect(tempRay); - // intersection - Vec3f i = tempRay.origin() + tempRay.direction() * tempRay.t(); - // normal - Vec3f n = (i - m_center); - n.normalize(); - return n; + if(Intersect(tempRay)) { + // intersection point + Vec3f i = tempRay.origin() + tempRay.direction() * (tempRay.t() - Epsilon); + // normal + Vec3f n = (i - m_center); + n.normalize(); + return n; + } else { + // no intersection with ray, so no surface normal + return Vec3f(0,0,0); + } } -- 2.20.1