{
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<m_resX;x++)
{
- file
- << (int)(255.99999999 * (*this)(x,y)[0]) << " "
- << (int)(255.99999999 * (*this)(x,y)[1]) << " "
- << (int)(255.99999999 * (*this)(x,y)[2]) << " "
+ Vec3f cur = (*this)(x,y);
+ if(cur[0] < 0) cur[0] = 0;
+ if(cur[1] < 0) cur[1] = 0;
+ if(cur[2] < 0) cur[2] = 0;
+ if(cur[0] > 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;
{
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);
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;
}
}
int main(int, char**)
{
- RenderFrameFlat("flatshaded.ppm");
- RenderFrameEyeLight("eyelight.ppm");
+ //RenderFrameFlat("flatshaded.ppm");
+ //RenderFrameEyeLight("eyelight.ppm");
RenderFrameReflectiveEyeLight("reflective.ppm");
}
#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<float>::max();
}
: m_org(org),
m_dir(dir),
m_hit(0),
- m_level(10)
+ m_level(MAX_RECURSION_DEPTH)
{
m_t = std::numeric_limits<float>::max();
}
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="<<cos_theta <<", flipping normal" <<std::endl;
+ n = n * -1;
+ }
+
+ // shoot secondary rays from intersection
+ Ray sec(ray.origin() + ray.direction() * (ray.t() - Epsilon),
+ ray.direction() + n * 2);
sec.setRecursionDepth(ray.recursionDepth() - 1);
- if(ray.recursionDepth() > 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);
}
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);
+ }
}