From 3396a44710415bde9509afeb02f3ec10a22c6661 Mon Sep 17 00:00:00 2001 From: Roland Hieber Date: Wed, 27 Jan 2010 12:57:24 +0100 Subject: [PATCH 1/1] not finished yet, but better than before --- Image.cxx | 18 ++++++++++++------ PhongShader.cxx | 13 +++++++------ PointLight.cxx | 7 +++++-- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Image.cxx b/Image.cxx index b45cac9..bc8134b 100644 --- a/Image.cxx +++ b/Image.cxx @@ -39,28 +39,34 @@ 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) (*this)(x,y)[0] = 255; + if ((*this)(x,y)[1] > 255) (*this)(x,y)[1] = 255; + if ((*this)(x,y)[2] > 255) (*this)(x,y)[2] = 255; + file << (int)(255.99999999 * (*this)(x,y)[0]) << " " << (int)(255.99999999 * (*this)(x,y)[1]) << " " << (int)(255.99999999 * (*this)(x,y)[2]) << " " diff --git a/PhongShader.cxx b/PhongShader.cxx index 67363a6..c44966e 100644 --- a/PhongShader.cxx +++ b/PhongShader.cxx @@ -34,16 +34,16 @@ PhongShader::Shade(Ray& ray) { // surface normal at hit point of object Vec3f normal = ray.hit()->GetNormal(ray); - // turn to fron if angle < 90° + // turn to front if angle > 90° if(normal.dot(ray.direction()) < 0) { normal = normal * -1; } // direction of reflection - Vec3f refl_dir = ray.direction() - normal * 2 * (normal % ray.direction()); + Vec3f refl_dir = ray.direction() - normal * 2 * normal.dot(ray.direction()); refl_dir.normalize(); - // ambient color term - radiance is 1 since we have point light - Vec3f amb_color = m_ambient_color * m_ka * 1.0f; + // ambient color term + Vec3f amb_color = m_ambient_color * m_ka; Vec3f intensity, light_dir; @@ -55,10 +55,11 @@ PhongShader::Shade(Ray& ray) Ray light_ray; (*it)->Illuminate(light_ray, intensity); diff_sum += intensity * light_ray.direction().dot(normal); - spec_sum += intensity * light_ray.direction().dot(refl_dir); + Vec3f view_dir = ray.direction()*-1; // direction from hitpoint to viewer + spec_sum += intensity * pow(fmax(view_dir.dot(refl_dir),0), m_ke); } Vec3f diff_color = m_diffuse_color * m_kd * diff_sum; Vec3f spec_color = m_specular_color * m_ks * spec_sum; - return amb_color + diff_color + spec_sum; + return amb_color + diff_color + spec_color; } diff --git a/PointLight.cxx b/PointLight.cxx index 2efe585..925a6b5 100644 --- a/PointLight.cxx +++ b/PointLight.cxx @@ -21,15 +21,18 @@ PointLight::PointLight() bool PointLight::Illuminate(Ray& ray, Vec3f& intensity) { - float dist = ((ray.origin() + ray.direction() * ray.t()) - m_pos).norm(); + Vec3f dir = (ray.origin() + ray.direction() * (ray.t()-Epsilon)) - m_pos; + float dist = dir.norm(); + dir.normalize(); float c1 = 1, c2 = 0.5, c3 = 0; + //float f_att = 1 / (dist*dist); float f_att = 1 / (c1 + c2*dist + c3*dist*dist); intensity = m_intensity * f_att; // store direction from light to hitpoint - ray.setDir(ray.origin() + ray.direction() * (ray.t()-Epsilon) - m_pos); + ray.setDir(dir); return true; } -- 2.20.1