src/raytracer.cc
author Radek Brich <radek.brich@devl.cz>
Thu, 15 May 2008 00:07:25 +0200
branchpyrit
changeset 96 9eb71e76c7fd
parent 95 ca7d4c665531
child 100 c005054bf4c1
permissions -rw-r--r--
added Python binding for material.h classes added raytracermodule.h header file for declarations updated car.py demo added texture.py demo (based on spheres_glass.py) all remaining 'centre' changed to more common 'center' added some more const's to material.h
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
/*
44
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
     2
 * raytracer.cc: Raytracer class
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
     3
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
     4
 * This file is part of Pyrit Ray Tracer.
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
     5
 *
47
320d5d466864 move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
     6
 * Copyright 2006, 2007, 2008  Radek Brich
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
     7
 *
44
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
     8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
     9
 * of this software and associated documentation files (the "Software"), to deal
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    10
 * in the Software without restriction, including without limitation the rights
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    12
 * copies of the Software, and to permit persons to whom the Software is
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    13
 * furnished to do so, subject to the following conditions:
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    14
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    15
 * The above copyright notice and this permission notice shall be included in
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    16
 * all copies or substantial portions of the Software.
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    17
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    24
 * THE SOFTWARE.
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
 */
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
4
c73bc405ee7a multi-threaded rendering via pthreads
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    27
#include <pthread.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
    28
#include <stdio.h>
67
249553e1d4fe new option to choose single or double precision floats
Radek Brich <radek.brich@devl.cz>
parents: 56
diff changeset
    29
#include <stdlib.h>
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    30
#include <assert.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
    31
#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
    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
// 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
    34
// http://www.cse.cuhk.edu.hk/~ttwong/papers/udpoint/udpoints.html
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    35
Vector Raytracer::SphereDistribute(int i, int n, Float extent, const Vector &normal)
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
    36
{
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    37
	Float p, t, st, phi, phirad;
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
    38
	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
    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
	t = 0;
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    41
	for (p=0.5f, kk=i; kk; p*=0.5f, kk>>=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
    42
	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
    43
		t += p;
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    44
	t = 1.0f + (t - 1.0f)*extent;
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
    45
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    46
	phi = (i + 0.5f) / n;
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    47
	phirad =  phi * 2.0f * PI;
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
    48
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    49
	st = sqrt(1.0f - t*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
    50
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    51
	Float x, y, z, xx, yy, zz, q;
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
    52
	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
    53
	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
    54
	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
    55
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
	// 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
    57
	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
    58
	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
    59
	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
    60
	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
    61
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
	// 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
    63
	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
    64
	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
    65
	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
    66
	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
    67
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    68
	return Vector(x, y, z);
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
    69
}
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
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
/*
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
 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
    73
 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
    74
 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
    75
 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
    76
*/
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    77
Colour Raytracer::PhongShader(const Shape *shape,
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    78
	const Vector &P, const Vector &N, const Vector &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
    79
{
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    80
	Colour col, acc;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    81
	Material * const &mat = shape->material;
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
    82
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    83
	if (mat->texture)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    84
		col = mat->texture->evaluate(P);
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    85
	else
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    86
		col = mat->colour;
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
    87
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    88
	// ambient
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    89
	acc = mat->ambient * col;
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    90
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    91
	vector<Light*>::iterator light;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    92
	for (light = lights.begin(); light != lights.end(); light++)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    93
	{
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    94
		const Vector L = normalize((*light)->pos - P); // direction vector to light
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    95
		const Float L_dot_N = dot(L, N);
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    96
		if (L_dot_N > 0)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    97
		{
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    98
			// test if this light is occluded (sharp shadows)
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    99
			if ((*light)->cast_shadows) {
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   100
				const Ray shadow_ray = Ray(P, L);
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   101
				Float dist = FLT_MAX;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   102
				if (top->nearest_intersection(shape, shadow_ray, dist))
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   103
					continue;
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   104
			}
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   105
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   106
			const Vector R = L - 2.0f * L_dot_N * N;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   107
			const Float R_dot_V = dot(R, V);
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   108
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   109
			// diffuse
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   110
			acc += mat->diffuse * col * (*light)->colour * L_dot_N;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   111
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   112
			// specular
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   113
			if (R_dot_V > 0)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   114
				acc += mat->specular * (*light)->colour * powf(R_dot_V, mat->shininess);
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   115
		}
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   116
	}
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   117
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   118
	return acc;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   119
}
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   120
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   121
#ifndef NO_SIMD
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   122
VectorPacket Raytracer::PhongShader_packet(const Shape* const* shapes,
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   123
	const VectorPacket &P, const VectorPacket &N, const VectorPacket &V)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   124
{
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   125
	VectorPacket acc, colour;
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   126
	union { mfloat4 ambient; float ambient_f[4]; };
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   127
	union { mfloat4 diffuse; float diffuse_f[4]; };
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   128
	union { mfloat4 specular; float specular_f[4]; };
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   129
	union { mfloat4 shininess; float shininess_f[4]; };
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   130
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   131
	for (int i = 0; i < 4; i++)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   132
		if (shapes[i] == NULL)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   133
		{
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   134
			ambient_f[i] = 0;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   135
			diffuse_f[i] = 0;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   136
			specular_f[i] = 0;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   137
			shininess_f[i] = 0;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   138
		}
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   139
		else
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   140
		{
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   141
			Material * const &mat = shapes[i]->material;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   142
			if (mat->texture)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   143
				colour.setVector(i, mat->texture->evaluate(P.getVector(i)));
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   144
			else
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   145
				colour.setVector(i, mat->colour);
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   146
			ambient_f[i] = mat->ambient;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   147
			diffuse_f[i] = mat->diffuse;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   148
			specular_f[i] = mat->specular;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   149
			shininess_f[i] = mat->shininess;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   150
		}
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   151
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   152
	// ambient
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   153
	acc = colour * ambient;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   154
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   155
	const Shape *shadow_shapes[4];
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   156
	vector<Light*>::iterator light;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   157
	for (light = lights.begin(); light != lights.end(); light++)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   158
	{
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   159
		 // direction vector to light
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   160
		VectorPacket L = VectorPacket((*light)->pos) - P;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   161
		L.normalize();
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   162
		const mfloat4 L_dot_N = dot(L, N);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   163
		mfloat4 valid = mcmpgt(L_dot_N, mZero);
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   164
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   165
		// test if this light is occluded (sharp shadows)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   166
		if ((*light)->cast_shadows)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   167
		{
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   168
			const RayPacket shadow_rays = RayPacket(P, L);
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   169
			union { mfloat4 dists; float dists_f[4]; };
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   170
			dists = mInf;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   171
			top->packet_intersection(shapes, shadow_rays,
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   172
				dists_f, shadow_shapes);
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   173
			valid = mand(valid, mcmpeq(dists, mInf));
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   174
		}
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   175
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   176
		const VectorPacket R = L - N * mmul(mTwo, L_dot_N);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   177
		const mfloat4 R_dot_V = dot(R, V);
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   178
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   179
		// diffuse
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   180
		acc.selectiveAdd(valid,
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   181
			colour * VectorPacket((*light)->colour) * mmul(diffuse, L_dot_N));
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   182
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   183
		// specular
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   184
		valid = mand(valid, mcmpgt(R_dot_V, mZero));
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   185
		mfloat4 spec = mmul(mmul(specular, mset1((*light)->colour.r)),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   186
			mfastpow(R_dot_V, shininess));
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   187
		acc.selectiveAdd(valid, spec);
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   188
	}
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   189
	return acc;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   190
}
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   191
#endif
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   192
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   193
void Raytracer::lightScatter(const Ray &ray, const Shape *shape, int depth,
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   194
	const Vector &P, const Vector &normal, bool from_inside, Colour &col)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   195
{
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   196
	if (depth < max_depth)
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   197
	{
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   198
		Colour trans_col, refl_col;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   199
		Float trans = shape->material->transmissivity;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   200
		Float refl = shape->material->reflectivity;
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   201
		const Float cos_i = - dot(normal, ray.dir);
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   202
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   203
		// refraction
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   204
		if (trans > 0.01)
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   205
		{
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   206
			Float n, n1, n2;
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   207
			if (from_inside)
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   208
			{
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   209
				n1 = shape->material->refract_index;
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   210
				n2 = 1.0;
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   211
				n = n1;
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   212
			}
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   213
			else
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   214
			{
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   215
				n1 = 1.0f;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   216
				n2 = shape->material->refract_index;
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   217
				n = 1.0f / n2;
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   218
			}
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   219
			const Float sin2_t = n*n * (1 - cos_i*cos_i);
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   220
			if (sin2_t >= 1.0)
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   221
			{
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   222
					// totally reflected
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   223
					refl += trans;
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   224
					trans = 0;
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   225
			}
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   226
			else
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   227
			{
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   228
				const Float cos_t = sqrtf(1 - sin2_t);
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   229
				const Float Rdiv = 1.0f / (n1*cos_i + n2*cos_t);
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   230
				const Float Rper = (n1*cos_i - n2*cos_t)*Rdiv;
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   231
				const Float Rpar = (n2*cos_i - n1*cos_t)*Rdiv;
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   232
				const Float R = (Rper*Rper + Rpar*Rpar)/2;
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   233
				refl += R*trans;
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   234
				trans = (1-R)*trans;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   235
				Vector newdir = n * ray.dir + (n*cos_i - cos_t) * normal;
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   236
				Ray newray = Ray(P + 0.001f*newdir, newdir);
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   237
				trans_col = raytrace(newray, depth + 1, NULL);
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   238
			}
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   239
		}
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   240
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   241
		// reflection
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   242
		if (refl > 0.01)
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   243
		{
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   244
			Vector newdir = ray.dir + 2.0f * cos_i * normal;
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   245
			Ray newray = Ray(P, newdir);
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   246
			refl_col = raytrace(newray, depth + 1, shape);
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   247
		}
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   248
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   249
		col = (1-refl-trans)*col + refl*refl_col + trans*trans_col;
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   250
	}
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   251
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   252
	// ambient occlusion
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   253
	if (ao_samples && !from_inside)
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   254
	{
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   255
		Float miss = 0;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   256
		for (int i = 0; i < ao_samples; i++)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   257
		{
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   258
			Vector dir = SphereDistribute(i, ao_samples, ao_angle, normal);
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   259
			Ray ao_ray = Ray(P, dir);
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   260
			Float dist = ao_distance;
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   261
			const Shape *shape_in_way = top->nearest_intersection(shape, ao_ray, dist);
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   262
			if (shape_in_way == NULL)
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   263
				miss += 1.0;
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   264
			else
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   265
				miss += dist / ao_distance;
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   266
		}
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   267
		Float ao_intensity = miss / ao_samples;
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   268
		col = col * ao_intensity;
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   269
	}
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   270
}
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   271
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   272
Colour Raytracer::raytrace(Ray &ray, int depth, const Shape *origin_shape)
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
   273
{
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   274
	Float nearest_distance = Inf;
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   275
	const 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
   276
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   277
	if (nearest_shape == NULL)
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
   278
		return bg_colour;
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   279
	else
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   280
	{
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   281
		const Vector P = ray.o + ray.dir * nearest_distance; // point of intersection
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   282
		Vector normal = nearest_shape->normal(P);
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   283
		bool from_inside = false;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   284
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   285
		// make shapes double sided
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   286
		if (dot(normal, ray.dir) > 0.0)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   287
		{
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   288
			normal = - normal;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   289
			from_inside = true;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   290
		}
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   291
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   292
		// shading function
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   293
		Colour col = PhongShader(nearest_shape, P, normal, ray.dir);
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   294
		lightScatter(ray, nearest_shape, depth, P, normal, from_inside, col);
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   295
		return col;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   296
	}
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   297
}
31
b4e09433934a refraction
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
   298
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   299
#ifndef NO_SIMD
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   300
void Raytracer::raytracePacket(RayPacket &rays, Colour *results)
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   301
{
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   302
	union {
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   303
		float nearest_distances[4];
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   304
		mfloat4 m_nearest_distances;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   305
	};
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   306
	mfloat4 mask;
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   307
	const Shape *nearest_shapes[4];
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   308
	static const Shape *origin_shapes[4] = {NULL, NULL, NULL, NULL};
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   309
	m_nearest_distances = mInf;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   310
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   311
	top->packet_intersection(origin_shapes, rays, nearest_distances, nearest_shapes);
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
   312
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   313
	mask = mcmpneq(m_nearest_distances, mInf);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   314
	if (!mmovemask(mask))
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   315
	{
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   316
		for (int i = 0; i < 4; i++)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   317
			results[i] = bg_colour;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   318
		return;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   319
	}
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   320
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   321
	const VectorPacket P = rays.o + rays.dir * m_nearest_distances; // point of intersection
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   322
	VectorPacket normal;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   323
	for (int i = 0; i < 4; i++)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   324
		if (nearest_shapes[i] != NULL)
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   325
		{
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   326
			const Vector Pvecti = P.getVector(i);
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   327
			normal.setVector(i, nearest_shapes[i]->normal(Pvecti));
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   328
		}
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   329
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   330
	// make shapes double sided
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   331
	mfloat4 from_inside = mcmpgt(dot(normal, rays.dir), mZero);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   332
	normal.mx = mselect(from_inside, msub(mZero, normal.mx), normal.mx);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   333
	normal.my = mselect(from_inside, msub(mZero, normal.my), normal.my);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   334
	normal.mz = mselect(from_inside, msub(mZero, normal.mz), normal.mz);
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   335
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   336
	// shading function
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   337
	VectorPacket pres = PhongShader_packet(nearest_shapes, P, normal, rays.dir);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   338
	//pres.mx = mselect(mask, pres.mx, mset1(bg_colour.r));
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   339
	//pres.my = mselect(mask, pres.my, mset1(bg_colour.g));
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   340
	//pres.mz = mselect(mask, pres.mz, mset1(bg_colour.b));
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   341
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   342
	for (int i = 0; i < 4; i++)
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   343
		if (nearest_shapes[i] != NULL)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   344
		{
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   345
			results[i] = pres.getVector(i);
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   346
			lightScatter(rays[i], nearest_shapes[i], 0,
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   347
				P.getVector(i), normal.getVector(i), (mmovemask(from_inside)>>i)&1,
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   348
				results[i]);
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   349
		}
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   350
		else
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   351
			results[i] = bg_colour;
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
   352
}
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   353
#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
   354
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   355
NORETURN void *Raytracer::raytrace_worker(void *d)
50
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   356
{
55
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   357
	static const int my_queue_size = 256;
50
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   358
	Raytracer *rt = (Raytracer*)d;
54
dbe3c7a4e0f0 more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   359
	Sample my_queue[my_queue_size];
dbe3c7a4e0f0 more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   360
	Colour my_colours[my_queue_size];
53
228cb8bfdd54 slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents: 52
diff changeset
   361
	int my_count;
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   362
	Ray ray;
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   363
#ifndef NO_SIMD
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   364
	RayPacket rays;
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   365
	const bool can_use_packets = (rt->use_packets && rt->sampler->packetableSamples());
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   366
#endif
50
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   367
	for (;;)
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   368
	{
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   369
		pthread_mutex_lock(&rt->sample_queue_mutex);
52
a6413a3d741d new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents: 51
diff changeset
   370
		while (rt->sample_queue_count == 0)
a6413a3d741d new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents: 51
diff changeset
   371
		{
54
dbe3c7a4e0f0 more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   372
			if (rt->end_of_samples)
50
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   373
			{
52
a6413a3d741d new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents: 51
diff changeset
   374
				dbgmsg(4, "T thread [%d] exiting\n", pthread_self());
50
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   375
				pthread_mutex_unlock(&rt->sample_queue_mutex);
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   376
				pthread_exit(NULL);
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   377
			}
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   378
			pthread_cond_wait(&rt->sample_queue_cond, &rt->sample_queue_mutex);
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   379
		}
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   380
54
dbe3c7a4e0f0 more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   381
		if (rt->sample_queue_count >= my_queue_size)
dbe3c7a4e0f0 more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   382
			my_count = my_queue_size;
53
228cb8bfdd54 slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents: 52
diff changeset
   383
		else
228cb8bfdd54 slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents: 52
diff changeset
   384
			my_count = rt->sample_queue_count;
228cb8bfdd54 slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents: 52
diff changeset
   385
		rt->sample_queue_count -= my_count;
228cb8bfdd54 slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents: 52
diff changeset
   386
54
dbe3c7a4e0f0 more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   387
		// copy samples to local queue
55
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   388
		if (rt->sample_queue_pos + my_count >= rt->sample_queue_size)
53
228cb8bfdd54 slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents: 52
diff changeset
   389
		{
55
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   390
			register int c = rt->sample_queue_size - rt->sample_queue_pos;
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   391
			memcpy(my_queue, rt->sample_queue + rt->sample_queue_pos, c*sizeof(Sample));
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   392
			memcpy(my_queue + c, rt->sample_queue, (my_count - c)*sizeof(Sample));
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   393
			rt->sample_queue_pos = my_count - c;
53
228cb8bfdd54 slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents: 52
diff changeset
   394
		}
55
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   395
		else
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   396
		{
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   397
			memcpy(my_queue, rt->sample_queue + rt->sample_queue_pos,
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   398
				my_count*sizeof(Sample));
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   399
			rt->sample_queue_pos += my_count;
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   400
		}
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   401
		if (rt->sample_queue_count <= my_queue_size*2)
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   402
			pthread_cond_signal(&rt->worker_ready_cond);
50
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   403
		pthread_mutex_unlock(&rt->sample_queue_mutex);
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   404
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   405
		// do the work
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   406
#ifndef NO_SIMD
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   407
		if (can_use_packets)
53
228cb8bfdd54 slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents: 52
diff changeset
   408
		{
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   409
			// packet ray tracing
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   410
			assert((my_count & 3) == 0);
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   411
			for (int i = 0; i < (my_count >> 2); i++)
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   412
			{
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   413
				rt->camera->makeRayPacket(my_queue + (i<<2), rays);
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   414
				rt->raytracePacket(rays, my_colours + (i<<2));
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   415
			}
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   416
		}
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   417
		else
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
   418
#endif
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   419
		{
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   420
			// single ray tracing
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   421
			for (int i = 0; i < my_count; i++)
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   422
			{
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   423
				ray = rt->camera->makeRay(my_queue[i]);
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   424
				my_colours[i] = rt->raytrace(ray, 0, NULL);
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   425
			}
53
228cb8bfdd54 slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents: 52
diff changeset
   426
		}
51
89fec8668768 remove debug messages - algorithm seems stable, but it's slow
Radek Brich <radek.brich@devl.cz>
parents: 50
diff changeset
   427
53
228cb8bfdd54 slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents: 52
diff changeset
   428
		// save the results
50
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   429
		pthread_mutex_lock(&rt->sampler_mutex);
53
228cb8bfdd54 slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents: 52
diff changeset
   430
		for (int i = 0; i < my_count; i++)
228cb8bfdd54 slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents: 52
diff changeset
   431
			rt->sampler->saveSample(my_queue[i], my_colours[i]);
50
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   432
		pthread_mutex_unlock(&rt->sampler_mutex);
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   433
	}
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   434
}
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   435
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   436
void Raytracer::render()
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
   437
{
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   438
	if (!sampler || !camera || !top)
20
f22952603f29 new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents: 19
diff changeset
   439
		return;
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
   440
55
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   441
	static const int my_count_max = 256;
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   442
	sample_queue_size = my_count_max*2*num_threads;
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   443
	sample_queue = new Sample [sample_queue_size];
52
a6413a3d741d new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents: 51
diff changeset
   444
	sample_queue_count = 0;
77
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   445
	int my_count, my_pos;
54
dbe3c7a4e0f0 more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   446
	int sampnum = 0, sampdone;
77
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   447
	int phase = 1;
54
dbe3c7a4e0f0 more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   448
	bool more_samples;
50
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   449
55
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   450
	sampler->init();
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   451
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   452
	// create workers
77
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   453
	dbgmsg(1, "* using %d threads\n", num_threads);
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   454
	pthread_t *threads = new pthread_t[num_threads];
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   455
51
89fec8668768 remove debug messages - algorithm seems stable, but it's slow
Radek Brich <radek.brich@devl.cz>
parents: 50
diff changeset
   456
	dbgmsg(1, "* raytracing...\n");
89fec8668768 remove debug messages - algorithm seems stable, but it's slow
Radek Brich <radek.brich@devl.cz>
parents: 50
diff changeset
   457
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   458
	while ( (sampnum = sampler->initSampleSet()) > 0 )
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   459
	{
77
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   460
		my_pos = 0;
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   461
		sample_queue_pos = 0;
51
89fec8668768 remove debug messages - algorithm seems stable, but it's slow
Radek Brich <radek.brich@devl.cz>
parents: 50
diff changeset
   462
		sampdone = 0;
77
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   463
		end_of_samples = false;
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   464
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   465
		for (int t = 0; t < num_threads; t++)
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   466
		{
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   467
			int rc = pthread_create(&threads[t], NULL, raytrace_worker, (void*)this);
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   468
			if (rc) {
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   469
				dbgmsg(0, "\nE pthread_create unsuccessful, return code was %d\n", rc);
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   470
				exit(1);
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   471
			}
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   472
		}
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   473
90
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   474
		dbgmsg(2, "- phase %d:  0%% done", phase);
77
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   475
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   476
		pthread_mutex_lock(&sampler_mutex);
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   477
52
a6413a3d741d new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents: 51
diff changeset
   478
		for (;;)
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   479
		{
52
a6413a3d741d new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents: 51
diff changeset
   480
			my_count = 0;
53
228cb8bfdd54 slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents: 52
diff changeset
   481
			while ( more_samples = sampler->nextSample(&sample_queue[my_pos++]) )
52
a6413a3d741d new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents: 51
diff changeset
   482
			{
a6413a3d741d new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents: 51
diff changeset
   483
				my_count++;
55
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   484
				if (my_pos >= sample_queue_size)
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   485
					my_pos = 0;
54
dbe3c7a4e0f0 more raytrace_worker optimization and cleaning
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   486
				if (my_count >= my_count_max)
52
a6413a3d741d new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents: 51
diff changeset
   487
					break;
a6413a3d741d new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents: 51
diff changeset
   488
			}
55
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   489
			if (!more_samples && !my_count)
52
a6413a3d741d new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents: 51
diff changeset
   490
				break;
a6413a3d741d new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents: 51
diff changeset
   491
50
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   492
			pthread_mutex_unlock(&sampler_mutex);
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   493
			pthread_mutex_lock(&sample_queue_mutex);
52
a6413a3d741d new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents: 51
diff changeset
   494
			sample_queue_count += my_count;
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   495
51
89fec8668768 remove debug messages - algorithm seems stable, but it's slow
Radek Brich <radek.brich@devl.cz>
parents: 50
diff changeset
   496
			// wait for workers if there is enough samples ready on queue
55
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   497
			while (sample_queue_count > (2*num_threads-1)*my_count_max)
52
a6413a3d741d new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents: 51
diff changeset
   498
				pthread_cond_wait(&worker_ready_cond, &sample_queue_mutex);
55
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   499
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   500
			pthread_cond_signal(&sample_queue_cond);
f6d75ae82c88 fix bugs
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   501
			pthread_mutex_unlock(&sample_queue_mutex);
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   502
52
a6413a3d741d new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents: 51
diff changeset
   503
			sampdone += my_count;
83
e3a2a5b26abb vectorize makeRayPacket() using SSE intrinsics
Radek Brich <radek.brich@devl.cz>
parents: 82
diff changeset
   504
			dbgmsg(2, "\b\b\b\b\b\b\b\b%2d%% done", ((long)sampdone - sample_queue_count)*100/sampnum);
51
89fec8668768 remove debug messages - algorithm seems stable, but it's slow
Radek Brich <radek.brich@devl.cz>
parents: 50
diff changeset
   505
50
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   506
			pthread_mutex_lock(&sampler_mutex);
77
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   507
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   508
			if (!more_samples)
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   509
				break;
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   510
		}
51
89fec8668768 remove debug messages - algorithm seems stable, but it's slow
Radek Brich <radek.brich@devl.cz>
parents: 50
diff changeset
   511
		dbgmsg(2, "\b\b\b\b\b\b\b\b100%% done\n");
77
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   512
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   513
		pthread_mutex_unlock(&sampler_mutex);
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   514
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   515
		// wait for workers
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   516
		dbgmsg(2, "- waiting for threads to finish\n");
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   517
77
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   518
		pthread_mutex_lock(&sample_queue_mutex);
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   519
		end_of_samples = true;
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   520
		while (sample_queue_count)
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   521
		{
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   522
			pthread_cond_broadcast(&sample_queue_cond);
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   523
			pthread_mutex_unlock(&sample_queue_mutex);
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   524
			pthread_mutex_lock(&sample_queue_mutex);
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   525
		}
50
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   526
		pthread_cond_broadcast(&sample_queue_cond);
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   527
		pthread_mutex_unlock(&sample_queue_mutex);
77
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   528
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   529
		for (int t = 0; t < num_threads; t++)
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   530
			pthread_join(threads[t], NULL);
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   531
dbe8438d5dca rewrite subsampling from old code to DefaultSampler
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   532
		phase ++;
50
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   533
	}
14a727b70d07 rewritten threads with heavy debug information
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   534
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   535
	delete[] threads;
52
a6413a3d741d new implementation of sample_queue
Radek Brich <radek.brich@devl.cz>
parents: 51
diff changeset
   536
	delete[] sample_queue;
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
   537
}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   538
75
20dee9819b17 unify capitalization of method names in C++ and Python
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   539
void Raytracer::ambientOcclusion(int samples, Float distance, Float angle)
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
   540
{
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   541
	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
   542
	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
   543
	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
   544
	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
   545
		/* 0 ==> Inf */
53
228cb8bfdd54 slighly optimized raytrace_worker
Radek Brich <radek.brich@devl.cz>
parents: 52
diff changeset
   546
		ao_distance = Inf;
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
   547
}