src/raytracer.cc
author Radek Brich <radek.brich@devl.cz>
Mon, 26 Nov 2007 23:12:40 +0100
branchpyrit
changeset 19 4e0955fca797
parent 16 20bceb605f48
child 20 f22952603f29
permissions -rw-r--r--
added Camera, currently w/o Python binding new #define option: OVERSAMPLING fixed all demos to work with new camera (they had inverted z axis)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
/*
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
 * C++ RayTracer
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
 * file: raytracer.cc
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
 *
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
 * Radek Brich, 2006
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
 */
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
     8
#ifdef PTHREADS
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
     9
#include <pthread.h>
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    10
#endif
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    11
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
#include <stdio.h>
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
#include <malloc.h>
14
fc18ac4833f2 replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents: 12
diff changeset
    14
#include "common.h"
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
#include "raytracer.h"
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
// Hammersley spherical point distribution
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
// http://www.cse.cuhk.edu.hk/~ttwong/papers/udpoint/udpoints.html
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
Vector3 Raytracer::SphereDistribute(int i, int n, float extent, Vector3 &normal)
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
{
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
	float p, t, st, phi, phirad;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
	int kk;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
	t = 0;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
	for (p=0.5, kk=i; kk; p*=0.5, kk>>=1)
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
	if (kk & 1)
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
		t += p;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
	t = 1.0 + (t - 1.0)*extent;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
	phi = (i + 0.5) / n;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
	phirad =  phi * 2.0 * M_PI;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
	st = sqrt(1.0 - t*t);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
	float x, y, z, xx, yy, zz, q;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
	x = st * cos(phirad);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
	y = st * sin(phirad);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
	z = t;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
	// rotate against Y axis
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
	q = acos(normal.z);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
	zz = z*cos(q) - x*sin(q);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
	xx = z*sin(q) + x*cos(q);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
	yy = y;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
	// rotate against Z axis
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
	q = atan2f(normal.y, normal.x);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
	x = xx*cos(q) - yy*sin(q);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
	y = xx*sin(q) + yy*cos(q);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
	z = zz;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
	return Vector3(x, y, z);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
// ---- tyto dve funkce budou v budouci verzi metody objektu PhongShader
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
// calculate shader function
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
// P is point of intersection, N normal in this point
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
Colour PhongShader_ambient(Material &mat, Vector3 &P)
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
{
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
	Colour col = mat.texture.colour; //mat.texture.evaluate(P);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
	// ambient
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
	return mat.ambient * col;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
/*
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
 P is point of intersection,
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
 N normal in this point,
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
 R direction of reflected ray,
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
 V direction to the viewer
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
*/
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
Colour PhongShader_calculate(Material &mat, Vector3 &P, Vector3 &N, Vector3 &R, Vector3 &V,
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
	Light &light)
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
{
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
	Colour I = Colour();
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
	Vector3 L = light.pos - P;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
	L.normalize();
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
	float L_dot_N = dot(L, N);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    80
	float R_dot_V = dot(R, V);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    81
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
	Colour col = mat.texture.colour; //mat.texture.evaluate(P);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
	// diffuse
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
	I = mat.diffuse * col * light.colour * L_dot_N;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    87
	// specular
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
	if (R_dot_V > 0)
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
		I += mat.specular * light.colour * powf(R_dot_V, mat.shininess);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
	return I;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    93
Colour Raytracer::raytrace(Ray &ray, int depth, Shape *origin_shape)
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    94
{
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    95
	float nearest_distance = FLT_MAX; //Infinity
11
4d192e13ee84 move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 10
diff changeset
    96
	Shape *nearest_shape = top->nearest_intersection(origin_shape, ray, nearest_distance);
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
	if (nearest_shape == NULL) {
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
		return bg_colour;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
	} else {
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
		Colour acc = Colour();
15
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
   102
		Vector3 P = ray.o + ray.dir * nearest_distance; // point of intersection
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   103
		Vector3 normal = nearest_shape->normal(P);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
		acc = PhongShader_ambient(*nearest_shape->material, P);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   105
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   106
		vector<Light*>::iterator light;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   107
		for (light = lights.begin(); light != lights.end(); light++) {
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   108
			Vector3 jo, L = (*light)->pos - P; // direction vector to light
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   109
			L.normalize();
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   110
			float L_dot_N = dot(L, normal);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   111
			if (L_dot_N > 0) {
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   112
				// test if this light is occluded (sharp shadows)
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   113
				if ((*light)->shadows) {
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   114
					Ray shadow_ray = Ray(P, L);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   115
					float dist = FLT_MAX;
11
4d192e13ee84 move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 10
diff changeset
   116
					if (top->nearest_intersection(nearest_shape, shadow_ray, dist))
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   117
						continue;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   118
				}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   119
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   120
				// shading function
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   121
				Vector3 R = L - 2.0 * L_dot_N * normal;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   122
				acc += PhongShader_calculate(*nearest_shape->material,
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   123
					P, normal, R, ray.dir, **light);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   124
			}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   125
		}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   126
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   127
		// reflection
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   128
		int trace_max_depth = 4;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   129
		Vector3 newdir = ray.dir - 2.0 * dot(ray.dir, normal) * normal;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   130
		if (depth < trace_max_depth && nearest_shape->material->reflection > 0.01) {
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   131
			Ray newray = Ray(P, newdir);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   132
			Colour refl_col = raytrace(newray, depth + 1, nearest_shape);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   133
			acc += nearest_shape->material->reflection * refl_col;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   134
		}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   135
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   136
		// refraction
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   137
		/* ... */
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   138
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   139
		// ambient occlusion
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   140
		if (ao_samples)
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   141
		{
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   142
			float miss = 0;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   143
			for (int i = 0; i < ao_samples; i++) {
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   144
				Vector3 dir = SphereDistribute(i, ao_samples, ao_angle, normal);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   145
				Ray ao_ray = Ray(P, dir);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   146
				float dist = ao_distance;
11
4d192e13ee84 move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 10
diff changeset
   147
				Shape *shape_in_way = top->nearest_intersection(nearest_shape, ao_ray, dist);
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   148
				if (shape_in_way == NULL)
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   149
					miss += 1.0;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   150
				else
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   151
					miss += dist / ao_distance;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   152
			}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   153
			float ao_intensity = miss / ao_samples;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   154
			acc = acc * ao_intensity;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   155
		}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   156
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   157
		return acc;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   158
	}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   159
}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   160
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   161
static void *renderrow(void *data)
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   162
{
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   163
	RenderrowData *d = (RenderrowData*) data;
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   164
	Vector3 dir = d->dfix;
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   165
	for (int x = 0; x < d->w; x++) {
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   166
		// generate a ray from eye passing through this pixel
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   167
#if OVERSAMPLING
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   168
		// 5x oversampling
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   169
		Colour c = Colour();
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   170
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   171
		for (int i = 0; i < 5; i++)
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   172
		{
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   173
			float osax[] = {0.0, -0.4, +0.4, +0.4, -0.4};
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   174
			float osay[] = {0.0, -0.4, -0.4, +0.4, +0.4};
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   175
			Vector3 tmpdir = dir + osax[i]*d->dx + osay[i]*d->dy;
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   176
			tmpdir.normalize();
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   177
			Ray ray(d->eye, tmpdir);
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   178
			c += d->rt->raytrace(ray, 0, NULL);
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   179
		}
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   180
		c = c * (1./5);
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   181
#else
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   182
		// no oversampling
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   183
		dir.normalize();
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   184
		Ray ray(d->eye, dir);
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   185
		Colour c = d->rt->raytrace(ray, 0, NULL);
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   186
#endif
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   187
		*(d->iter++) = c.r;
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   188
		*d->iter++ = c.g;
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   189
		*d->iter++ = c.b;
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   190
		dir += d->dx;
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   191
	}
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   192
#ifdef PTHREADS
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   193
	pthread_exit((void *)d);
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   194
#endif
6
d8d596d26f25 pthreads and other fixes for Windows
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   195
	return (void *)d;
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   196
}
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   197
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   198
float *Raytracer::render(int w, int h)
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   199
{
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   200
	if (!camera || !top)
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   201
		return NULL;
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   202
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   203
	float *data;
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   204
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   205
	data = (float *) malloc(w*h*3*sizeof(float));
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   206
	if (!data)
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   207
		return NULL;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   208
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   209
	RenderrowData *d;
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   210
10
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
   211
	printf("* building kd-tree\n");
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   212
	top->optimize();
10
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
   213
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   214
	float S = 0.5/w;
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   215
	Vector3 dfix = camera->u*(-w/2.0*S/camera->f)
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   216
		+ camera->v*(h/2.0*S/camera->f) + camera->p;
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   217
	Vector3 dx = camera->u * (S/camera->f);
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   218
	Vector3 dy = camera->v * (-S/camera->f);
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   219
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   220
#ifdef PTHREADS
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   221
	printf("* pthreads enabled, using %d threads\n", num_threads);
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   222
	pthread_t threads[num_threads];
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   223
	for (int t = 0; t < num_threads; t++)
6
d8d596d26f25 pthreads and other fixes for Windows
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   224
		threads[t] = pthread_self();
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   225
	int t = 0;
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   226
#endif
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   227
15
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
   228
	/* for each pixel... */
5
2d97ea5e711a resolve warnings from current g++
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   229
	printf("* rendering row   0 (  0%% done)");
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   230
	for (int y = 0; y < h; y++) {
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   231
		d = (RenderrowData*) malloc(sizeof(RenderrowData));
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   232
		d->rt = this;
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   233
		d->w = w;
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   234
		d->eye = camera->eye;
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   235
		d->dfix = dfix;
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   236
		d->dx = dx;
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   237
#if OVERSAMPLING
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   238
		d->dy = dy;
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   239
#endif
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   240
		d->iter = data + y*3*w;
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   241
#ifdef PTHREADS
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   242
		/* create new thread and increase 't' */
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   243
		int rc = pthread_create(&threads[t++], NULL, renderrow, (void *)d);
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   244
		if (rc) {
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   245
			printf("\nERROR: return code from pthread_create() is %d\n", rc);
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   246
			exit(1);
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   247
		}
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   248
		/* when 't' owerflows, reset it */
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   249
		if (t >= num_threads)
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   250
			t = 0;
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   251
		/* wait for next thread in fifo queue, so the descriptor can be reused;
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   252
		   this also limits number of running threads */
6
d8d596d26f25 pthreads and other fixes for Windows
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
   253
		if (!pthread_equal(threads[t], pthread_self()))
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   254
			if (pthread_join(threads[t], (void**)&d) == 0)
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   255
				free(d);
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   256
#else
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   257
		renderrow((void *)d);
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   258
		free(d);
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   259
#endif
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   260
		dfix += dy;
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   261
		printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b%4d (%2d%% done)", y, y*100/(h-1));
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   262
	}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   263
	printf("\n");
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   264
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   265
#ifdef PTHREADS
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   266
	printf("* waiting for threads to finish\n");
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   267
	for (t = 0; t < num_threads; t++)
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   268
		if (pthread_join(threads[t], (void**)&d) == 0)
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   269
			free(d);
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   270
#endif
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   271
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   272
	return data;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   273
}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   274
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   275
void Raytracer::addlight(Light *light)
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   276
{
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   277
	lights.push_back(light);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   278
}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   279
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   280
void Raytracer::ambientocclusion(int samples, float distance, float angle)
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   281
{
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   282
	ao_samples = samples;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   283
	ao_distance = distance;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   284
	ao_angle = angle;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   285
	if (ao_distance == 0)
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   286
		/* 0 ==> Inf */
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   287
		ao_distance = FLT_MAX;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   288
}