include/raytracer.h
author Radek Brich <radek.brich@devl.cz>
Sat, 10 May 2008 14:29:37 +0200
branchpyrit
changeset 95 ca7d4c665531
parent 94 4c8abb8977dc
child 98 64638385798a
permissions -rw-r--r--
build script fixes, add ldflags build option update and enhance demos fix bug in 4x grid oversampling warn if writePNG called while compiled without libpng make shapes in ShapeList const and add many other const needed due to snowball effect slightly optimize Camera::makeRayPacket using _mm_shuffle_ps make Vector SIMD vectorization disabled by default (causes problems) fix bug in implicit reflection of transmissive surfaces, when surface's reflection parameter is set to zero

/**
 * @file  raytracer.h
 * @brief The base class of the ray tracer
 *
 * This file is part of Pyrit Ray Tracer.
 *
 * Copyright 2006, 2007, 2008  Radek Brich
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef RAYTRACER_H
#define RAYTRACER_H

#include <vector>
#include <queue>

#include "common.h"
#include "container.h"
#include "scene.h"

class Raytracer;

/**
 * main ray tracer class
 */
class Raytracer
{
	Container *top;
	Sampler *sampler;
	Camera *camera;
	vector<Light*> lights;
	Colour bg_colour;
	Float ao_distance, ao_angle;
	int ao_samples;
	int num_threads;
	int max_depth;
	bool use_packets;

	Sample *sample_queue;
	int sample_queue_pos, sample_queue_size, sample_queue_count;
	bool end_of_samples;
	pthread_mutex_t sample_queue_mutex, sampler_mutex;
	pthread_cond_t sample_queue_cond, worker_ready_cond;

	Vector SphereDistribute(int i, int n, Float extent, const Vector &normal);
	Colour PhongShader(const Shape *shape,
		const Vector &P, const Vector &N, const Vector &V);
	void lightScatter(const Ray &ray, const Shape *shape, int depth,
		const Vector &P, const Vector &normal, bool from_inside, Colour &col);

#ifndef NO_SIMD
	VectorPacket PhongShader_packet(const Shape* const* shapes,
		const VectorPacket &P, const VectorPacket &N, const VectorPacket &V);
	void raytracePacket(RayPacket &rays, Colour *results);
#endif

	NORETURN static void *raytrace_worker(void *d);

public:
	Raytracer(): top(NULL), camera(NULL), lights(), bg_colour(0., 0., 0.),
		ao_samples(0), num_threads(4), max_depth(3), use_packets(true)
	{
		pthread_mutex_init(&sample_queue_mutex, NULL);
		pthread_mutex_init(&sampler_mutex, NULL);
		pthread_cond_init (&sample_queue_cond, NULL);
		pthread_cond_init (&worker_ready_cond, NULL);
	};
	~Raytracer()
	{
		pthread_mutex_destroy(&sample_queue_mutex);
		pthread_mutex_destroy(&sampler_mutex);
		pthread_cond_destroy (&sample_queue_cond);
		pthread_cond_destroy (&worker_ready_cond);
	}

	void render();
	Colour raytrace(Ray &ray, int depth, const Shape *origin_shape);
	void addShape(Shape *shape) { top->addShape(shape); };
	void addLight(Light *light) { lights.push_back(light); };
	void setSampler(Sampler *sampl) { sampler = sampl; };
	Sampler *&getSampler() { return sampler; };
	void setCamera(Camera *cam) { camera = cam; };
	Camera *&getCamera() { return camera; };
	void setTop(Container *atop) { top = atop; };
	Container *&getTop() { return top; };

	void setBgColour(const Colour &bg) { bg_colour = bg; };
	void setMaxDepth(int newdepth) { max_depth = newdepth; };

	void ambientOcclusion(int samples, Float distance, Float angle);
	void setThreads(int num) { num_threads = num; };
};

#endif