diff --git a/main/Scene.cc b/main/Scene.cc index 08d84c8..ec2ae0c 100644 --- a/main/Scene.cc +++ b/main/Scene.cc @@ -52,6 +52,9 @@ Scene::Scene(map options, m_verbose = true; } } + + /* view plane distance is calculated based on the field of view */ + m_view_plane_dist = (m_height / 2.0) / atan(m_vfov / 2.0); } Scene::~Scene() @@ -69,8 +72,8 @@ Scene::~Scene() void Scene::load(const char * filename) { /* TODO: parse file somehow */ - Shape * shape = new Sphere(2.0); - m_transform.translate(2.0, 6.0, 1.0); + Shape * shape = new Sphere(1.0); + m_transform.translate(2.0, 2.0, 0.0); shape->setTransform(m_transform); m_shapes.push_back(shape); } @@ -110,12 +113,10 @@ void Scene::render() void Scene::renderPixel(int x, int y, unsigned char * pixel) { /* calculate the ray going from the camera through this pixel */ - double rx = (x + 0.5) - (m_width >> 1); - double rz = (m_height >> 1) - (y + 0.5); - /* ry is calculated based on the field of view */ - double ry = (m_height / 2.0) / atan(m_vfov / 2.0); + double rx = (x + 0.5) - (m_width / 2.0); + double rz = (m_height / 2.0) - (y + 0.5); - Ray ray(Vector(0, 0, 0), Vector(rx, ry, rz)); + Ray ray(Vector(0, 0, 0), Vector(rx, m_view_plane_dist, rz)); /* loop through all shapes in the scene */ for (vector::iterator it = m_shapes.begin(); @@ -128,5 +129,18 @@ void Scene::renderPixel(int x, int y, unsigned char * pixel) /* then intersect this inversely transformed ray with the shape */ Solver::Result intersections = (*it)->intersect(transformed_ray); + + if (intersections.numResults > 0) + { + pixel[BMP_RED] = 0xFF; + pixel[BMP_GREEN] = 0xFF; + pixel[BMP_BLUE] = 0xFF; + } + else + { + pixel[BMP_RED] = 0x0; + pixel[BMP_GREEN] = 0x0; + pixel[BMP_BLUE] = 0x0; + } } } diff --git a/main/Scene.h b/main/Scene.h index 8003c8d..c60caa2 100755 --- a/main/Scene.h +++ b/main/Scene.h @@ -31,6 +31,7 @@ class Scene /* private data */ std::vector m_shapes; Transform m_transform; + double m_view_plane_dist; /* framebuffer */ unsigned char * m_data; diff --git a/shapes/Sphere.cc b/shapes/Sphere.cc index 1569792..f23e1f6 100644 --- a/shapes/Sphere.cc +++ b/shapes/Sphere.cc @@ -1,10 +1,13 @@ #include "Sphere.h" #include "util/Solver.h" +#include +using namespace std; Sphere::Sphere(double radius) { m_radius = radius; + m_radius2 = radius * radius; } Solver::Result Sphere::intersect(const Ray & ray) @@ -16,7 +19,8 @@ Solver::Result Sphere::intersect(const Ray & ray) + ray.getOrigin()[2] * ray.getDirection()[2] ), ray.getOrigin()[0] * ray.getOrigin()[0] + ray.getOrigin()[1] * ray.getOrigin()[1] - + ray.getOrigin()[2] * ray.getOrigin()[2] ); + + ray.getOrigin()[2] * ray.getOrigin()[2] + - m_radius2); Solver::Result quadSolutions = solver.solve(); int resIdx = 0; for (int i = 0; i < quadSolutions.numResults; i++) diff --git a/shapes/Sphere.h b/shapes/Sphere.h index d577b72..9268b3f 100644 --- a/shapes/Sphere.h +++ b/shapes/Sphere.h @@ -12,6 +12,7 @@ class Sphere : public Shape private: double m_radius; + double m_radius2; }; #endif