#include "Triangle.hxx"
Scene::Scene()
- : m_camera(new PerspectiveCamera(Vec3f(0,0,8),
- Vec3f(0,0,-1),
- Vec3f(0,1,0),
- 60,
- 640,
+ : m_camera(new PerspectiveCamera(Vec3f(0,0,8),
+ Vec3f(0,0,-1),
+ Vec3f(0,1,0),
+ 60,
+ 640,
480)
),
m_bgColor(Vec3f(0,0,0)),
return *this;
}
-void
+void
Scene::Add(Primitive* p)
{
m_primitives.push_back(p);
{
intersect |= m_primitives[i]->Intersect(ray);
}
-
+
return intersect;
}
return (intersect) ? ray.hit()->shader()->Shade(ray) : m_bgColor;
}
-const Camera*
+const Camera*
Scene::camera() const
{
return m_camera;
}
-std::vector<Light*>
+std::vector<Light*>
Scene::lights() const
{
return m_lights;
Scene::ParseOBJ(const std::string& file, float scale)
{
std::cerr << "(Scene): Parsing OBJ file : " << file << std::endl;
-
+
// clear old buffers
m_vertices.clear();
m_faces.clear();
-
+
// for the moment, we will attach a white eyelight shader to each object
// in the future, you will extend your parser to also read in material definitiions
if(m_shader == 0) // not yet defined
m_shader = new EyeLightShader(this, Vec3f(1.0,1.0,1.0));
-
+
// now open file
std::fstream in;
std::cerr << "(Scene): Could not open file " << file << std::endl;
return;
}
-
+
// read lines
std::string line;
while(!in.eof())
// build triangle list from parsed vertices
this->buildTriangleList(scale);
-
+
std::cerr << "(Scene): Finished parsing." << std::endl;
}
std::istringstream iss(line);
std::string key;
iss >> key;
- if (key == "v")
+ if (key == "v")
{
// parse vertex //
this->parseVertex(iss);
}
- else if (key == "f")
+ else if (key == "f")
{
// parse face //
this->parseFace(iss);
// stores indices of triangle into vertex list
// remember: indices start at 1!!
Vec3f face_idx = m_faces[f];
- this->Add(new Triangle(m_vertices[ face_idx[0]-1 ] * fac,
- m_vertices[ face_idx[1]-1 ] * fac,
+ this->Add(new Triangle(m_vertices[ face_idx[0]-1 ] * fac,
+ m_vertices[ face_idx[1]-1 ] * fac,
m_vertices[ face_idx[2]-1 ] * fac,
m_shader));
-
- }
+
+ }
m_centroid /= static_cast<float>(m_vertices.size());
std::cerr << "(Scene): Model centroid = " << m_centroid * fac << std::endl;
}
void
Scene::CalcBounds()
{
+ for(unsigned int i = 0; i < m_primitives.size(); ++i)
+ {
+ m_scene_box.Extend(m_primitives[i]->CalcBounds());
+ }
}
const Box&