Skip to content

Commit

Permalink
Finish lighting calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
jianhe25 committed Jan 9, 2013
1 parent bdf66bd commit 6fdcc55
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 34 deletions.
30 changes: 22 additions & 8 deletions hw3-RayTracer/RayTracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ Color RayTracer::Trace(const Ray& ray, const Scene& scene, int depth) {
if (!GetIntersect(ray, scene, hit_object, &nearest_dist))
return BLACK;

Color color(BLACK);
Materials materials = hit_object->materials;
Color color(scene.ambient);
Point hit_point = ray.o + ray.direction * nearest_dist;
for (int i = 0; i < scene.lights.size(); ++i) {
Ray light_ray(lights[i], hit_point-light[i]);
Expand All @@ -50,24 +49,39 @@ Color RayTracer::Trace(const Ray& ray, const Scene& scene, int depth) {
GetIntersect(light_ray, scene, tmp_obj, &tmp_dist);
assert(tmp_obj != NULL);
if (sgn(tmp_dist - nearest_dist) == 0) {
color += CalcLight(scene.lights[i], materials, hit_object, ray, hit_point);
color += CalcLight(scene.lights[i], hit_object, ray, hit_point);
}
}


/*
if (materials.specular > 0) {
Ray reflect_ray = ray.CreateReflectRay(hit_point, normal);
// Make a recursive call to trace the reflected ray
Color temp_color = Trace(reflect_ray, scene, depth+1);
color += materials.specular * temp_color;
}

*/
return color;

}

Color RayTracer::CalcLight(const Light& light, const Materials& materials,
const Object* hit_object, const Ray& ray, const vec3& hit_point) {
Color RayTracer::CalcLight(const Light& light, const Object* hit_object, const Ray& ray, const vec3& hit_point) {

vec3 light_direction;
if (light.type == Light::point) {
light_direction = glm::normalize(light.positionOrDirection - hit_point);
} else
light_direction = glm::normalize(light.positionOrDirection);

vec3 normal = hit_object->InterpolatePointNormal(hit_point);

const Materials& materials = hit_object->materials;
float nDotL = max(glm::dot(normal, light_direction), 0.0);
vec3 diffuse = materials.diffuse * light.color * nDotL;

vec3 halfvec = glm::normalize(light_direction + ray.direction);
float nDotH = max(glm::dot(normal, halfvec), 0.0);
vec3 specular = materials.specular * light.color * pow(nDotH, materials.shininess);

return diffuse + specular;
}

2 changes: 2 additions & 0 deletions hw3-RayTracer/RayTracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ class RayTracer {
Color Trace(const Ray& ray, const Scene& scene, int depth);
Ray GenerateRay(const Camera& camera, int i, int j, int height, int width);
bool IsBlocked(const Ray& ray, const Scene& scene);
Color CalcLight(const Light& light, const Object* hit_object,
const Ray& ray, const vec3& hit_point);
};
#endif // _RAY_TRACER_H_
2 changes: 1 addition & 1 deletion hw3-RayTracer/RayTracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RayTracerTester {
Camera camera2(vec3(0,0,5), vec3(0,0,0), vec3(0,1,0), 90);
ray = ray_tracer->GenerateRay(camera2, 0, 0, 640, 480);
//printf("ray.direction = %.2f %.2f %.2f\n",ray.direction.x, ray.direction.y, ray.direction.z);
assert(glm::length(ray.direction == glm::normalize(vec3(-480.0 / 640.0, -1, -1))) < 1e-3);
assert(glm::length(ray.direction - glm::normalize(vec3(-480.0 / 640.0, -1, -1))) < 1e-3);

//assert(ray.direction == glm::normalize(vec3(-640.0 / 480.0, -1, -1)));
}
Expand Down
21 changes: 12 additions & 9 deletions hw3-RayTracer/Raytracer.depend
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@
"Transform.h"
<stdio.h>

1357303826 source:e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\scene.cpp
1357702250 source:e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\scene.cpp
<iostream>
<string>
<fstream>
Expand Down Expand Up @@ -504,15 +504,16 @@

1357297374 e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\glm\gtc\matrix_transform.inl

1357303585 e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\scene.h
1357702250 e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\scene.h
<vector>
<stack>
<sstream>
"object.h"

1357358689 e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\object.h
1357717824 e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\object.h
"glm/glm.hpp"
"glm/gtc/matrix_transform.hpp"
"config.h"

1357299050 source:e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\scene_test.cc
"scene.h"
Expand All @@ -526,7 +527,7 @@
1357297375 source:e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\object.cpp
"object.h"

1357306875 source:e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\main.cpp
1357702250 source:e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\main.cpp
<iostream>
<string>
<fstream>
Expand All @@ -536,16 +537,18 @@
"Transform.h"
<FreeImage.h>
<stdio.h>
"scene_test.cc"
<cassert>
"RayTracer.h"
"object_test.cc"
"scene_test.cc"
"RayTracer_test.cc"

1357299050 e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\scene_test.cc
"scene.h"
"object.h"
<iostream>

1357356776 e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\raytracer.h
1357718214 e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\raytracer.h
"scene.h"

1357123116 e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\include\freeimage.h
Expand All @@ -556,15 +559,15 @@
"config.h"
"RayTracer.h"

1357307818 e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\config.h
1357702250 e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\config.h

1357303572 e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\global_variables.h

1357308719 source:e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\raytracer_test.cc
1357702250 source:e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\raytracer_test.cc
"RayTracer.h"
<stdio.h>

1357308719 e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\raytracer_test.cc
1357718278 e:\hejian\computer_graphics\computergraphics-berkeley-cs184\hw3-raytracer\raytracer_test.cc
"RayTracer.h"
<stdio.h>

Expand Down
26 changes: 13 additions & 13 deletions hw3-RayTracer/Raytracer.layout
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<ActiveTarget name="Debug" />
<File name="RayTracer.cpp" open="1" top="1" tabpos="4">
<Cursor position="2390" topLine="49" />
<File name="RayTracer.cpp" open="1" top="1" tabpos="2">
<Cursor position="2305" topLine="35" />
</File>
<File name="RayTracer.h" open="1" top="0" tabpos="5">
<Cursor position="264" topLine="0" />
<File name="RayTracer.h" open="1" top="0" tabpos="3">
<Cursor position="313" topLine="0" />
</File>
<File name="RayTracer_test.cc" open="0" top="0" tabpos="2">
<Cursor position="512" topLine="0" />
<File name="RayTracer_test.cc" open="1" top="0" tabpos="6">
<Cursor position="762" topLine="0" />
</File>
<File name="Transform.cpp" open="0" top="0" tabpos="0">
<Cursor position="1014" topLine="10" />
</File>
<File name="main.cpp" open="1" top="0" tabpos="2">
<Cursor position="717" topLine="21" />
<File name="main.cpp" open="1" top="0" tabpos="7">
<Cursor position="279" topLine="3" />
</File>
<File name="object.cpp" open="0" top="0" tabpos="2">
<Cursor position="879" topLine="6" />
<File name="object.cpp" open="1" top="0" tabpos="5">
<Cursor position="1809" topLine="48" />
</File>
<File name="object.h" open="1" top="0" tabpos="6">
<Cursor position="1116" topLine="39" />
<File name="object.h" open="1" top="0" tabpos="4">
<Cursor position="775" topLine="18" />
</File>
<File name="object_test.cc" open="1" top="0" tabpos="1">
<Cursor position="739" topLine="2" />
Expand All @@ -31,7 +31,7 @@
<File name="scene.h" open="0" top="0" tabpos="3">
<Cursor position="1251" topLine="36" />
</File>
<File name="scene1.test" open="1" top="0" tabpos="3">
<File name="scene1.test" open="1" top="0" tabpos="1">
<Cursor position="613" topLine="6" />
</File>
<File name="scene_test.cc" open="0" top="0" tabpos="1">
Expand Down
Binary file modified hw3-RayTracer/obj/Debug/RayTracer_test.o
Binary file not shown.
36 changes: 33 additions & 3 deletions hw3-RayTracer/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ bool Object::Intersect(const Ray& ray, float* dis_to_ray) {
throw 2; // This should never happen
}

vec3 Object::InterpolatePointNormal(const vec3& point) {
std::cerr << "Can't interpolate normal in abstract object" << std::endl;
throw 2;
}

Sphere::Sphere(const vec3& _o, const float& _r) :
o(_o), r(_r) {
}
Expand All @@ -39,10 +44,18 @@ bool Sphere::Intersect(const Ray& ray, float* dis_to_ray) {
}
}

vec3 Sphere::InterpolatePointNormal(const vec3& point) {
assert(sgn(glm::length(point - this->o) - this->r) == 0); // ensure point on the surface
return glm::normalize(point - this->o);
}

bool Color::operator == (const Color &otherColor) {
return r == otherColor.r && g == otherColor.g && b == otherColor.b;
}
Color Color::operator * (const Color& otherColor) {
return Color(r * otherColor.r, g * otherColor.g, b * otherColor.b);
}

Triangle::Triangle(
const vec3& _a,
const vec3& _b,
Expand All @@ -59,11 +72,17 @@ Triangle::Triangle(
a = _a;
b = _b;
c = _c;
na = _na;
nb = _nb;
nc = _nc;
type = triangle;
// no specified normal
if (_na == vec3(0,0,0)) {
na = nb = nc = glm::cross(b-a, c-a);
} else {
na = _na;
nb = _nb;
nc = _nc;
}
};

bool Triangle::Intersect(const Ray& ray, float* dis_to_ray) {
vec3 n = glm::normalize(glm::cross(b-a, c-a));
const vec3& p = ray.o;
Expand Down Expand Up @@ -92,6 +111,17 @@ bool Triangle::Intersect(const Ray& ray, float* dis_to_ray) {
} else
return false;
}
vec3 Triangle::InterpolatePointNormal(const vec3& point) {
vec3 n = glm::cross(a-b, a-c);
vec3 tmp_nb = glm::cross(c-point, a-point);
vec3 tmp_nc = glm::cross(a-point, b-point);

float beta = glm::dot(n, tmp_nb) / glm::dot(n,n);
float gamma = glm::dot(n, tmp_nc) / glm::dot(n,n);
float alpha = 1.0 - beta - gamma;
return (na * alpha) + (nb * beta) + (nc * gamma);
}

Object::~Object() {
}
Triangle::~Triangle() {
Expand Down
4 changes: 4 additions & 0 deletions hw3-RayTracer/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct Color
BYTE Gbyte() { return g * 255; }
BYTE Bbyte() { return b * 255; }
bool operator == (const Color &otherColor);
Color operator * (const Color& otherColor);
};

const Color BLACK(0, 0, 0);
Expand All @@ -57,6 +58,7 @@ class Object {
Object() {}
virtual ~Object();
virtual bool Intersect(const Ray& ray, float* dis_to_ray);
virtual vec3 InterpolatePointNormal(const vec3& point);
};

class Sphere : public Object {
Expand All @@ -67,6 +69,7 @@ class Sphere : public Object {

virtual ~Sphere();
virtual bool Intersect(const Ray& ray, float* dis_to_ray);
virtual vec3 InterpolatePointNormal(const vec3& point);
};

class Triangle : public Object {
Expand All @@ -84,5 +87,6 @@ class Triangle : public Object {

virtual ~Triangle();
virtual bool Intersect(const Ray& ray, float* dis_to_ray);
virtual vec3 InterpolatePointNormal(const vec3& point);
};
#endif // _OBJECT_H

0 comments on commit 6fdcc55

Please sign in to comment.