fart/main/Scene.cc
Josh Holtrop ecace9f5e8 added Scene::ShapeDistance construct
git-svn-id: svn://anubis/fart/trunk@52 7f9b0f55-74a9-4bce-be96-3c2cd072584d
2009-01-27 00:47:21 +00:00

206 lines
5.8 KiB
C++

#include "Scene.h"
#include <stdlib.h>
#include <string>
#include <vector>
#include <utility> /* pair */
#include <map>
#include <algorithm> /* sort() */
#include <functional> /* binary_function */
#include <math.h>
#include "BMP.h"
#include "shapes/Shape.h"
#include "shapes/Sphere.h"
#include "PointLight.h"
using namespace std;
Scene::Scene(map<string, const char *> options,
const char * filename)
{
m_width = 800;
m_height = 600;
m_multisample_level = 1;
m_output_file_name = "fart.bmp";
m_vfov = 60.0;
m_verbose = false;
m_data = NULL;
load(filename);
/* after loading the scene file, apply any command-line render options */
for (map<string, const char *>::iterator it = options.begin();
it != options.end();
it++)
{
if (it->first == "width")
{
m_width = atoi(it->second);
}
else if (it->first == "height")
{
m_height = atoi(it->second);
}
else if (it->first == "multisample")
{
m_multisample_level = atoi(it->second);
}
else if (it->first == "field-of-view")
{
m_vfov = atof(it->second);
}
else if (it->first == "output-file")
{
m_output_file_name = it->second;
}
else if (it->first == "verbose")
{
m_verbose = true;
}
}
/* view plane distance is calculated based on the field of view */
m_view_plane_dist = (m_height / 2.0) / tan(M_PI * m_vfov / 360.0);
m_sample_span = 1.0 / m_multisample_level;
m_half_sample_span = m_sample_span / 2.0;
m_multisample_level_squared = m_multisample_level * m_multisample_level;
}
Scene::~Scene()
{
if (m_data != NULL)
delete m_data;
for (vector<Shape *>::iterator it = m_shapes.begin();
it != m_shapes.end();
it++)
{
delete (*it);
}
for (vector<Light *>::iterator it = m_lights.begin();
it != m_lights.end();
it++)
{
delete (*it);
}
}
void Scene::load(const char * filename)
{
/* TODO: parse file somehow */
Shape * shape = new Sphere(1.0);
m_transform.translate(1.0, 5.0, 0.5);
shape->setTransform(m_transform);
m_shapes.push_back(shape);
Light * light = new PointLight(Vector(-1, -1, 1));
m_lights.push_back(light);
}
void Scene::render()
{
if (m_verbose)
{
cout << " *** Beginning scene render ***" << endl;
cout << "Parameters:" << endl;
cout << "----------------------------------------" << endl;
cout << " Width: " << m_width << endl;
cout << " Height: " << m_height << endl;
cout << " Multisample Level: " << m_multisample_level << endl;
cout << " Vertical Field of View: " << m_vfov << endl;
cout << "----------------------------------------" << endl;
}
m_data = new unsigned char[m_width * m_height * 3];
for (int i = 0; i < m_height; i++)
{
for (int j = 0; j < m_width; j++)
{
renderPixel(j, i, &m_data[3 * (m_width * i + j)]);
}
}
if (m_verbose)
{
cout << " *** Ending scene render ***" << endl;
cout << "Writing output file '" << m_output_file_name << '\'' << endl;
}
BMP outputImage(m_output_file_name.c_str(), m_width, m_height, m_data);
}
void Scene::renderPixel(int x, int y, unsigned char * pixel)
{
/* calculate the ray going from the camera through this pixel */
double red = 0.0, green = 0.0, blue = 0.0;
for (int i = 0; i < m_multisample_level; i++)
{
for (int j = 0; j < m_multisample_level; j++)
{
double rx = (x + i * m_sample_span + m_half_sample_span)
- (m_width / 2.0);
double rz = (m_height / 2.0)
- (y + j * m_sample_span + m_half_sample_span);
Ray ray(Vector(0, 0, 0), Vector(rx, m_view_plane_dist, rz));
Vector color = traceRay(ray);
red += color[0];
green += color[1];
blue += color[2];
}
}
/* take the average of all the samples as the final pixel value */
pixel[BMP_RED] = (unsigned char) (0xFF * red / m_multisample_level_squared);
pixel[BMP_GREEN] = (unsigned char) (0xFF * green / m_multisample_level_squared);
pixel[BMP_BLUE] = (unsigned char) (0xFF * blue / m_multisample_level_squared);
}
Vector Scene::traceRay(const Ray & ray)
{
Vector color;
vector<ShapeDistance> hits = getRayHits(ray);
if (hits.size() > 0)
{
color[0] = color[1] = color[2] = 1.0;
}
return color;
}
vector<Scene::ShapeDistance> Scene::getRayHits(const Ray & ray)
{
vector<ShapeDistance> hits;
/* loop through all shapes in the scene */
for (vector<Shape *>::iterator it = m_shapes.begin();
it != m_shapes.end();
it++)
{
Solver::Result intersections = (*it)->intersect(ray);
for (int i = 0; i < intersections.numResults; i++)
{
Vector normal =
(*it)->getNormalAt(ray[intersections.results[i]]);
double dot = normal % ray.getDirection();
if (dot < 0.0) /* cull back faces */
{
hits.push_back(ShapeDistance(*it, intersections.results[i]));
}
}
}
/* now that we have ALL the hits, sort them by distance */
sort(hits.begin(), hits.end());
return hits;
}
bool operator<(const Scene::ShapeDistance & sd1,
const Scene::ShapeDistance & sd2)
{
return sd1.second < sd2.second;
}