424 lines
13 KiB
C++
424 lines
13 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 <typeinfo> /* typeid operator support */
|
|
#include <math.h>
|
|
#include "BMP.h"
|
|
#include "util/Color.h"
|
|
#include "shapes/Shape.h"
|
|
#include "Light.h"
|
|
|
|
using namespace std;
|
|
|
|
Scene::Scene(const 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 = true;
|
|
m_data = NULL;
|
|
m_ambient_light = Color(0.2, 0.2, 0.2);
|
|
m_max_depth = 10;
|
|
m_transforms.push(Transform());
|
|
m_server = true;
|
|
|
|
load(filename);
|
|
|
|
/* after loading the scene file, apply any command-line render options */
|
|
for (map<const string, const char *>::const_iterator it = options.begin();
|
|
it != options.end();
|
|
it++)
|
|
{
|
|
if (it->first == "width")
|
|
{
|
|
m_width = atoi(it->second);
|
|
m_client_options.push_back("--width");
|
|
m_client_options.push_back(it->second);
|
|
}
|
|
else if (it->first == "height")
|
|
{
|
|
m_height = atoi(it->second);
|
|
m_client_options.push_back("--height");
|
|
m_client_options.push_back(it->second);
|
|
}
|
|
else if (it->first == "multisample")
|
|
{
|
|
m_multisample_level = atoi(it->second);
|
|
m_client_options.push_back("--multisample");
|
|
m_client_options.push_back(it->second);
|
|
}
|
|
else if (it->first == "field-of-view")
|
|
{
|
|
m_vfov = atof(it->second);
|
|
m_client_options.push_back("--field-of-view");
|
|
m_client_options.push_back(it->second);
|
|
}
|
|
else if (it->first == "output-file")
|
|
{
|
|
m_output_file_name = it->second;
|
|
/* no client option necessary */
|
|
}
|
|
else if (it->first == "max-depth")
|
|
{
|
|
m_max_depth = atoi(it->second);
|
|
m_client_options.push_back("--max-depth");
|
|
m_client_options.push_back(it->second);
|
|
}
|
|
else if (it->first == "verbose")
|
|
{
|
|
m_verbose = true;
|
|
/* no client option necessary */
|
|
}
|
|
else if (it->first == "host")
|
|
{
|
|
m_server_name = it->second;
|
|
m_server = false;
|
|
/* no client option necessary */
|
|
}
|
|
else if (it->first == "port")
|
|
{
|
|
m_server_port = atoi(it->second);
|
|
/* no client option necessary */
|
|
}
|
|
else if (it->first == "hosts")
|
|
{
|
|
m_hosts_file = it->second;
|
|
/* no client option necessary */
|
|
}
|
|
}
|
|
m_client_options.push_back(filename);
|
|
|
|
/* start the distribution infrastructure */
|
|
if (m_server)
|
|
{
|
|
if (m_hosts_file != "")
|
|
m_distrib.readHostFile(m_hosts_file.c_str());
|
|
m_distrib.startServer();
|
|
m_distrib.startClients(m_client_options);
|
|
}
|
|
else
|
|
{
|
|
m_distrib.startClient(m_server_name.c_str(), m_server_port);
|
|
}
|
|
|
|
/* 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;
|
|
}
|
|
|
|
void Scene::render()
|
|
{
|
|
if (m_server)
|
|
{
|
|
/* server version */
|
|
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]; /* 24bpp */
|
|
|
|
if (m_distrib.getNumClients() < 1)
|
|
{
|
|
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)]);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
m_distrib.set_data(m_data);
|
|
/* work on tasks in this thread until there are no more */
|
|
taskLoop();
|
|
}
|
|
|
|
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);
|
|
}
|
|
else
|
|
{
|
|
/* client version */
|
|
taskLoop();
|
|
}
|
|
}
|
|
|
|
void Scene::taskLoop()
|
|
{
|
|
for (;;)
|
|
{
|
|
int task_id = m_distrib.getTask();
|
|
if (task_id < 0)
|
|
break;
|
|
unsigned char data[3 * UNIT_TASK_SIZE];
|
|
int i = task_id / m_width;
|
|
int j = task_id % m_width;
|
|
for (int t = 0; t < UNIT_TASK_SIZE; t++)
|
|
{
|
|
renderPixel(j, i, &data[3 * t]);
|
|
j++;
|
|
if (j == m_width)
|
|
{
|
|
j = 0;
|
|
i++;
|
|
}
|
|
}
|
|
m_distrib.send_data(task_id, data, 3 * UNIT_TASK_SIZE);
|
|
}
|
|
}
|
|
|
|
void Scene::renderPixel(int x, int y, unsigned char * pixel)
|
|
{
|
|
/* calculate the ray going from the camera through this pixel */
|
|
Color finalColor;
|
|
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));
|
|
|
|
finalColor += traceRay(ray);
|
|
}
|
|
}
|
|
|
|
/* take the average of all the samples as the final pixel value */
|
|
pixel[BMP_RED] = (unsigned char)
|
|
(0xFF * finalColor.r / m_multisample_level_squared);
|
|
pixel[BMP_GREEN] = (unsigned char)
|
|
(0xFF * finalColor.g / m_multisample_level_squared);
|
|
pixel[BMP_BLUE] = (unsigned char)
|
|
(0xFF * finalColor.b / m_multisample_level_squared);
|
|
}
|
|
|
|
Color Scene::traceRay(const Ray & ray)
|
|
{
|
|
return traceRayRecurse(ray, m_max_depth, 1.0);
|
|
}
|
|
|
|
/**
|
|
* factor: the proportion of the final color that this computation is worth
|
|
*/
|
|
Color Scene::traceRayRecurse(const Ray & ray, int depth, double factor)
|
|
{
|
|
Color color;
|
|
|
|
Shape::Intersection hit = getRayClosestHit(ray);
|
|
|
|
if ( ! hit.shape.isNull() )
|
|
{
|
|
/* compute the Phong lighting for each hit */
|
|
refptr<Material> material = hit.shape->getMaterial();
|
|
|
|
/* check for backfaces */
|
|
if (ray.getDirection() % hit.normal > 0.0)
|
|
{
|
|
/* if dot product is positive, this is a back-face */
|
|
hit.normal = -hit.normal;
|
|
}
|
|
|
|
color = computePhong(material,
|
|
ray,
|
|
hit.position,
|
|
hit.normal);
|
|
|
|
if (depth > 0 && factor > SCENE_FACTOR_THRESHOLD)
|
|
{
|
|
double reflectance = material->getReflectance();
|
|
if (factor * reflectance > SCENE_FACTOR_THRESHOLD)
|
|
{
|
|
color *= (1.0 - reflectance);
|
|
Vector reflected_direction =
|
|
(-ray.getDirection()).reflect(hit.normal);
|
|
Ray newRay(hit.position, reflected_direction);
|
|
Vector jitter_surface_point = newRay[0.0001];
|
|
Ray jitterNewRay(jitter_surface_point, reflected_direction);
|
|
Color c = traceRayRecurse(jitterNewRay,
|
|
depth - 1,
|
|
factor * reflectance);
|
|
color += c * reflectance;
|
|
}
|
|
|
|
double transparency = material->getTransparency();
|
|
if (factor * transparency > SCENE_FACTOR_THRESHOLD)
|
|
{
|
|
color *= (1.0 - transparency);
|
|
Vector jitter_surface_point = hit.position
|
|
+ ray.getDirection() * 0.0001;
|
|
Ray newRay(jitter_surface_point, ray.getDirection());
|
|
Color c = traceRayRecurse(newRay,
|
|
depth - 1,
|
|
factor * transparency);
|
|
color += c * transparency;
|
|
}
|
|
}
|
|
}
|
|
|
|
return color;
|
|
}
|
|
|
|
Shape::Intersection Scene::getRayClosestHit(const Ray & ray)
|
|
{
|
|
Shape::Intersection hit;
|
|
double min_dist = 0.0;
|
|
bool foundOne = false;
|
|
|
|
/* loop through all shapes in the scene */
|
|
for (vector< refptr<Shape> >::iterator it = m_shapes.begin();
|
|
it != m_shapes.end();
|
|
it++)
|
|
{
|
|
Shape::IntersectionList intersections = (*it)->intersect(*it, ray);
|
|
|
|
for (int i = 0, num_results = intersections.size();
|
|
i < num_results;
|
|
i++)
|
|
{
|
|
refptr<Shape> shape = intersections[i].shape;
|
|
const Vector & isect_point = intersections[i].position;
|
|
double intersect_dist = ray.getOrigin().dist_to(isect_point);
|
|
if (foundOne == false || intersect_dist < min_dist)
|
|
{
|
|
hit = intersections[i];
|
|
min_dist = intersect_dist;
|
|
foundOne = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return hit;
|
|
}
|
|
|
|
Color Scene::computePhong(const refptr<Material> material,
|
|
const Ray & viewRay,
|
|
const Vector & surfacePoint,
|
|
const Vector & surfaceNormal)
|
|
{
|
|
Color result = m_ambient_light * material->getAmbientColor();
|
|
|
|
Vector viewDirection = -viewRay.getDirection();
|
|
double shininess = material->getShininess();
|
|
const Color & diffuseColor = material->getDiffuseColor();
|
|
const Color & specularColor = material->getSpecularColor();
|
|
|
|
for (std::vector< refptr<Light> >::const_iterator it = m_lights.begin();
|
|
it != m_lights.end();
|
|
it++)
|
|
{
|
|
Vector directionToLight = (*it)->getPosition() - surfacePoint;
|
|
directionToLight.normalize();
|
|
Vector reflectedLightDirection =
|
|
directionToLight.reflect(surfaceNormal);
|
|
|
|
Ray surfaceToLight(surfacePoint, directionToLight);
|
|
Color light_contribution =
|
|
calculateLightContribution(surfaceToLight.shift(0.0001), *it);
|
|
|
|
if ( light_contribution.r > 0.0
|
|
|| light_contribution.g > 0.0
|
|
|| light_contribution.b > 0.0 )
|
|
{
|
|
/* calculate the diffuse term */
|
|
double diffuse_coef = directionToLight % surfaceNormal;
|
|
if (diffuse_coef > 0.0)
|
|
{
|
|
result += diffuseColor
|
|
* (*it)->getDiffuseColor()
|
|
* diffuse_coef
|
|
* light_contribution;
|
|
}
|
|
|
|
/* calculate the specular term */
|
|
double specular_coef = reflectedLightDirection % viewDirection;
|
|
if (specular_coef > 0.0)
|
|
{
|
|
result += specularColor
|
|
* (*it)->getSpecularColor()
|
|
* pow(specular_coef, shininess)
|
|
* light_contribution;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* TODO: figure out better scaling */
|
|
if (result.r > 1.0)
|
|
result.r = 1.0;
|
|
if (result.g > 1.0)
|
|
result.g = 1.0;
|
|
if (result.b > 1.0)
|
|
result.b = 1.0;
|
|
|
|
return result;
|
|
}
|
|
|
|
Color Scene::calculateLightContribution(const Ray & toLight,
|
|
refptr<Light> light)
|
|
{
|
|
Color contrib(1.0, 1.0, 1.0);
|
|
double dist_to_light = toLight.getOrigin().dist_to(light->getPosition());
|
|
double dist_so_far = 0.0;
|
|
|
|
Ray currentRay = toLight;
|
|
|
|
for (;;)
|
|
{
|
|
Shape::Intersection hit = getRayClosestHit(currentRay);
|
|
|
|
if ( hit.shape.isNull() )
|
|
break;
|
|
|
|
double offset = currentRay.getOrigin().dist_to(hit.position) + 0.0001;
|
|
|
|
if ( dist_so_far + offset > dist_to_light )
|
|
break;
|
|
|
|
contrib *= hit.shape->getMaterial()->getTransparency();
|
|
contrib *= hit.shape->getMaterial()->getDiffuseColor();
|
|
|
|
if ( contrib.r < SCENE_FACTOR_THRESHOLD
|
|
&& contrib.g < SCENE_FACTOR_THRESHOLD
|
|
&& contrib.b < SCENE_FACTOR_THRESHOLD )
|
|
break;
|
|
|
|
dist_so_far += offset;
|
|
currentRay = currentRay.shift(offset);
|
|
}
|
|
|
|
return contrib;
|
|
}
|