Update Scene a bit for new code style and namespaces in Object and Sphere
This commit is contained in:
parent
cf7806484f
commit
5a8d634590
2 changed files with 29 additions and 29 deletions
52
src/scene.cc
52
src/scene.cc
|
@ -15,6 +15,8 @@
|
||||||
#include "scene.h"
|
#include "scene.h"
|
||||||
#include "writer.h"
|
#include "writer.h"
|
||||||
|
|
||||||
|
using namespace charles;
|
||||||
|
|
||||||
|
|
||||||
Scene::Scene()
|
Scene::Scene()
|
||||||
: width(640), height(480),
|
: width(640), height(480),
|
||||||
|
@ -37,9 +39,6 @@ Scene::~Scene()
|
||||||
delete ambient;
|
delete ambient;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Shape *s : shapes) {
|
|
||||||
delete s;
|
|
||||||
}
|
|
||||||
shapes.clear();
|
shapes.clear();
|
||||||
|
|
||||||
for (PointLight *l : lights) {
|
for (PointLight *l : lights) {
|
||||||
|
@ -177,7 +176,7 @@ Scene::render()
|
||||||
* Add a shape to the scene.
|
* Add a shape to the scene.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
Scene::add_shape(Shape *shape)
|
Scene::add_shape(Object::Ptr shape)
|
||||||
{
|
{
|
||||||
shapes.push_back(shape);
|
shapes.push_back(shape);
|
||||||
}
|
}
|
||||||
|
@ -210,35 +209,35 @@ Scene::trace_ray(const Ray &ray,
|
||||||
}
|
}
|
||||||
|
|
||||||
Color out_color = Color::Black;
|
Color out_color = Color::Black;
|
||||||
Shape *intersected_shape = NULL;
|
Object::Ptr intersected_shape;
|
||||||
float *t = NULL;
|
TVector ts;
|
||||||
float nearest_t = INFINITY;
|
Double nearest_t = INFINITY;
|
||||||
int nints;
|
|
||||||
|
ts.reserve(2);
|
||||||
|
|
||||||
// Find intersections of this ray with objects in the scene.
|
// Find intersections of this ray with objects in the scene.
|
||||||
for (Shape *s : shapes) {
|
for (Object::Ptr s : shapes) {
|
||||||
nints = s->does_intersect(ray, &t);
|
ts.clear();
|
||||||
if (nints > 0) {
|
if (s->DoesIntersect(ray, ts)) {
|
||||||
for (int i = 0; i < nints; i++) {
|
for (Double t : ts) {
|
||||||
if (t[i] < 1e-2) {
|
if (t < 1e-2) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (t[i] < nearest_t) {
|
if (t < nearest_t) {
|
||||||
intersected_shape = s;
|
intersected_shape = s;
|
||||||
nearest_t = t[i];
|
nearest_t = t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delete[] t;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there was no intersection, return black.
|
// If there was no intersection, return black.
|
||||||
if (intersected_shape == NULL) {
|
if (!intersected_shape) {
|
||||||
return out_color;
|
return out_color;
|
||||||
}
|
}
|
||||||
|
|
||||||
Material* shape_material = intersected_shape->get_material();
|
Material& shape_material = intersected_shape->GetMaterial();
|
||||||
Color shape_color = shape_material->get_diffuse_color();
|
const Color& shape_color = shape_material.GetDiffuseColor();
|
||||||
|
|
||||||
Vector3 intersection = ray.parameterize(nearest_t);
|
Vector3 intersection = ray.parameterize(nearest_t);
|
||||||
Vector3 normal = intersected_shape->compute_normal(intersection);
|
Vector3 normal = intersected_shape->compute_normal(intersection);
|
||||||
|
@ -248,22 +247,22 @@ Scene::trace_ray(const Ray &ray,
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Vector3 light_direction;
|
Vector3 light_direction;
|
||||||
float ldotn, diffuse_level, ambient_level;
|
Double ldotn, diffuse_level, ambient_level;
|
||||||
Ray shadow_ray;
|
Ray shadow_ray;
|
||||||
|
|
||||||
for (PointLight *l : lights) {
|
for (PointLight *l : lights) {
|
||||||
light_direction = (intersection - l->get_origin()).normalize();
|
light_direction = (intersection - l->GetOrigin()).normalize();
|
||||||
ldotn = light_direction.dot(normal);
|
ldotn = light_direction.dot(normal);
|
||||||
|
|
||||||
if (ldotn < 0) {
|
if (ldotn < 0) {
|
||||||
ldotn = 0.0;
|
ldotn = 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
diffuse_level = shape_material->get_diffuse_level();
|
diffuse_level = shape_material.GetDiffuseIntensity();
|
||||||
ambient_level = 1.0 - diffuse_level;
|
ambient_level = 1.0 - diffuse_level;
|
||||||
|
|
||||||
shadow_ray = Ray(intersection, light_direction);
|
shadow_ray = Ray(intersection, light_direction);
|
||||||
for (Shape *s : shapes) {
|
for (Object::Ptr s : shapes) {
|
||||||
// Skip the intersected shape.
|
// Skip the intersected shape.
|
||||||
if (s == intersected_shape) {
|
if (s == intersected_shape) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -272,7 +271,8 @@ Scene::trace_ray(const Ray &ray,
|
||||||
mStats.shadowRays++;
|
mStats.shadowRays++;
|
||||||
|
|
||||||
// Figure out if we're in shadow.
|
// Figure out if we're in shadow.
|
||||||
if (s->does_intersect(shadow_ray, NULL) > 0) {
|
ts.clear();
|
||||||
|
if (s->DoesIntersect(shadow_ray, ts)) {
|
||||||
diffuse_level = 0.0;
|
diffuse_level = 0.0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -289,8 +289,8 @@ Scene::trace_ray(const Ray &ray,
|
||||||
* Specular lighting. (Reflections, etc.)
|
* Specular lighting. (Reflections, etc.)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
float specular_level = shape_material->get_specular_level();
|
Double specular_level = shape_material.GetSpecularIntensity();
|
||||||
const Color &specular_color = shape_material->get_specular_color();
|
const Color& specular_color = shape_material.GetSpecularColor();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Compute the reflection ray. Computing the direction of the reflection ray
|
* Compute the reflection ray. Computing the direction of the reflection ray
|
||||||
|
|
|
@ -15,11 +15,11 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "basics.h"
|
#include "basics.h"
|
||||||
#include "camera.h"
|
#include "camera.h"
|
||||||
|
#include "object.h"
|
||||||
|
|
||||||
|
|
||||||
class AmbientLight;
|
class AmbientLight;
|
||||||
class PointLight;
|
class PointLight;
|
||||||
class Shape;
|
|
||||||
class Writer;
|
class Writer;
|
||||||
|
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ public:
|
||||||
void write(Writer &writer, const std::string &filename);
|
void write(Writer &writer, const std::string &filename);
|
||||||
void render();
|
void render();
|
||||||
|
|
||||||
void add_shape(Shape *obj);
|
void add_shape(charles::Object::Ptr obj);
|
||||||
void add_light(PointLight *light);
|
void add_light(PointLight *light);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -66,7 +66,7 @@ private:
|
||||||
|
|
||||||
// Scene objects.
|
// Scene objects.
|
||||||
AmbientLight *ambient;
|
AmbientLight *ambient;
|
||||||
std::list<Shape *> shapes;
|
std::list<charles::Object::Ptr> shapes;
|
||||||
std::list<PointLight *> lights;
|
std::list<PointLight *> lights;
|
||||||
|
|
||||||
// Rendering stats
|
// Rendering stats
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue