src/shapes.cc
author Radek Brich <radek.brich@devl.cz>
Sat, 10 May 2008 14:29:37 +0200
branchpyrit
changeset 95 ca7d4c665531
parent 93 96d65f841791
permissions -rw-r--r--
build script fixes, add ldflags build option update and enhance demos fix bug in 4x grid oversampling warn if writePNG called while compiled without libpng make shapes in ShapeList const and add many other const needed due to snowball effect slightly optimize Camera::makeRayPacket using _mm_shuffle_ps make Vector SIMD vectorization disabled by default (causes problems) fix bug in implicit reflection of transmissive surfaces, when surface's reflection parameter is set to zero
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
/*
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
     2
 * shapes.cc: shape classes
44
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
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
    27
#include "shapes.h"
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
    28
#include "serialize.h"
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
    29
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    30
bool Sphere::intersect(const Ray &ray, Float &dist) const
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
{
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    32
	Vector V = ray.o - center;
56
d4481fc43952 fix sphere transmissivity, rename demo.py to spheres_glass.py
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
    33
	register Float d = -dot(V, ray.dir);
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    34
	register Float Det = d * d - (dot(V,V) - sqr_radius);
56
d4481fc43952 fix sphere transmissivity, rename demo.py to spheres_glass.py
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
    35
	register Float t1,t2;
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
	if (Det > 0) {
56
d4481fc43952 fix sphere transmissivity, rename demo.py to spheres_glass.py
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
    37
		Det = sqrtf(Det);
d4481fc43952 fix sphere transmissivity, rename demo.py to spheres_glass.py
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
    38
		t1 = d - Det;
d4481fc43952 fix sphere transmissivity, rename demo.py to spheres_glass.py
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
    39
		t2 = d + Det;
d4481fc43952 fix sphere transmissivity, rename demo.py to spheres_glass.py
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
    40
		if (t1 > 0)
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
    41
		{
56
d4481fc43952 fix sphere transmissivity, rename demo.py to spheres_glass.py
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
    42
			if (t1 < dist)
d4481fc43952 fix sphere transmissivity, rename demo.py to spheres_glass.py
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
    43
			{
d4481fc43952 fix sphere transmissivity, rename demo.py to spheres_glass.py
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
    44
				dist = t1;
d4481fc43952 fix sphere transmissivity, rename demo.py to spheres_glass.py
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
    45
				return true;
d4481fc43952 fix sphere transmissivity, rename demo.py to spheres_glass.py
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
    46
			}
d4481fc43952 fix sphere transmissivity, rename demo.py to spheres_glass.py
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
    47
		}
d4481fc43952 fix sphere transmissivity, rename demo.py to spheres_glass.py
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
    48
		else if (t2 > 0 && t2 < dist)
d4481fc43952 fix sphere transmissivity, rename demo.py to spheres_glass.py
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
    49
		{
d4481fc43952 fix sphere transmissivity, rename demo.py to spheres_glass.py
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
    50
			dist = t2;
21
79b516a3803d naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents: 20
diff changeset
    51
			return true;
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
		}
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
	return false;
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
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    57
#ifndef NO_SIMD
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    58
mfloat4 Sphere::intersect_packet(const RayPacket &rays, mfloat4 &dists) const
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    59
{
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    60
	VectorPacket V = rays.o - VectorPacket(center);
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    61
	mfloat4 d = msub(mZero, dot(V, rays.dir));
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    62
	mfloat4 Det = msub(mmul(d, d), msub(dot(V,V), mset1(sqr_radius)));
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    63
	mfloat4 t1, t2, mask;
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    64
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    65
	mask = mcmpgt(Det, mZero);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    66
	if (!mmovemask(mask))
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    67
		return mask;
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    68
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    69
	Det = msqrt(Det);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    70
	t1 = msub(d, Det);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    71
	t2 = madd(d, Det);
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    72
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    73
	mask = mand(mask, mcmpgt(t2, mZero));
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    74
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    75
	const mfloat4 cond1 = mand(mcmpgt(t1, mZero), mcmplt(t1, dists));
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    76
	const mfloat4 cond2 = mand(mcmple(t1, mZero), mcmplt(t2, dists));
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    77
	const mfloat4 newdists = mor(mand(cond1, t1), mand(cond2, t2));
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    78
	mask = mand(mask, mor(cond1, cond2));
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    79
	dists = mselect(mask, newdists, dists);
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    80
	return mask;
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    81
}
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    82
#endif
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    83
40
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    84
/* if there should be CSG sometimes, this may be needed... */
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    85
bool Sphere::intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const
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
    86
{
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    87
	//allts = new vector<Float>();
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
    88
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    89
	Vector V = ((Ray)ray).o - center;
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    90
	Float Vd = - dot(V, ray.dir);
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    91
	Float Det = Vd * Vd - (dot(V,V) - sqr_radius);
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
    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
	if (Det > 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
    94
		Det = sqrtf(Det);
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    95
		Float t1 = Vd - Det;
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    96
		Float t2 = Vd + Det;
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
		if (t1 < 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
    98
		{
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
			if (t2 > 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
   100
			{
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
				// ray from inside of the sphere
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
				allts.push_back(0.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
				allts.push_back(t2);
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
				return true;
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
			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
   107
				return false;
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
		}
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
		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
   110
		{
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
			allts.push_back(t1);
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
			allts.push_back(t2);
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
			return true;
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
		}
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
	}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   116
	return false;
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
}
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
38
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   119
bool Sphere::intersect_bbox(const BBox &bbox) const
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   120
{
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   121
	register float dmin = 0;
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   122
	for (int i = 0; i < 3; i++)
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   123
	{
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   124
		if (center[i] < bbox.L[i])
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   125
			dmin += (center[i] - bbox.L[i])*(center[i] - bbox.L[i]);
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   126
		else
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   127
		if (center[i] > bbox.H[i])
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   128
			dmin += (center[i] - bbox.H[i])*(center[i] - bbox.H[i]);
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   129
	}
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   130
	if (dmin <= sqr_radius)
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   131
		return true;
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   132
	return false;
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   133
};
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   134
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   135
BBox Sphere::get_bbox() const
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
   136
{
38
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   137
	return BBox(center - radius, center + radius);
8
e6567b740c5e fixed virtual method get_bbox() for all shapes, default thread num changed to 4
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   138
}
e6567b740c5e fixed virtual method get_bbox() for all shapes, default thread num changed to 4
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   139
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   140
bool Box::intersect(const Ray &ray, Float &dist) const
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
   141
{
40
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   142
	register Float tnear = -Inf;
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   143
	register Float tfar = Inf;
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   144
	register Float t1, t2, t;
40
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   145
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   146
	for (int i = 0; i < 3; i++)
21
79b516a3803d naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents: 20
diff changeset
   147
	{
40
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   148
		if (ray.dir[i] == 0) {
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   149
			/* ray is parallel to these planes */
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   150
			if (ray.o[i] < L[i] || ray.o[i] > H[i])
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   151
				return false;
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   152
		}
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   153
		else
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   154
		{
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   155
			/* compute the intersection distance of the planes */
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   156
			t1 = (L[i] - ray.o[i]) / ray.dir[i];
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   157
			t2 = (H[i] - ray.o[i]) / ray.dir[i];
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   158
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   159
			if (t1 > t2)
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   160
			{
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   161
				t = t1;
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   162
				t1 = t2;
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   163
				t2 = t;
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   164
			}
40
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   165
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   166
			if (t1 > tnear)
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   167
				tnear = t1; /* want largest Tnear */
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   168
			if (t2 < tfar)
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   169
				tfar = t2; /* want smallest Tfar */
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   170
			if (tnear > tfar || tfar < 0)
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   171
				return false; /* box missed; box is behind ray */
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   172
		}
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   173
	}
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   174
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   175
	if (tnear < dist)
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   176
	{
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   177
		dist = tnear;
21
79b516a3803d naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents: 20
diff changeset
   178
		return true;
79b516a3803d naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents: 20
diff changeset
   179
	}
40
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   180
	return false;
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
   181
}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   182
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   183
#ifndef NO_SIMD
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   184
mfloat4 Box::intersect_packet(const RayPacket &rays, mfloat4 &dists) const
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   185
{
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   186
	mfloat4 origin = rays.o.ma[0];
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   187
	mfloat4 invdir = rays.invdir.ma[0];
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   188
	mfloat4 t1 = mmul(msub(mset1(L[0]), origin), invdir);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   189
	mfloat4 t2 = mmul(msub(mset1(H[0]), origin), invdir);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   190
	mfloat4 tmin = mmin(t1, t2);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   191
	mfloat4 tmax = mmax(t1, t2);
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   192
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   193
	origin = rays.o.ma[1];
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   194
	invdir = rays.invdir.ma[1];
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   195
	t1 = mmul(msub(mset1(L[1]), origin), invdir);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   196
	t2 = mmul(msub(mset1(H[1]), origin), invdir);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   197
	tmin = mmax(mmin(t1, t2), tmin);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   198
	tmax = mmin(mmax(t1, t2), tmax);
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   199
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   200
	origin = rays.o.ma[2];
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   201
	invdir = rays.invdir.ma[2];
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   202
	t1 = mmul(msub(mset1(L[2]), origin), invdir);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   203
	t2 = mmul(msub(mset1(H[2]), origin), invdir);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   204
	tmin = mmax(mmin(t1, t2), tmin);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   205
	tmax = mmin(mmax(t1, t2), tmax);
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   206
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   207
	mfloat4 mask = mand(mand(mcmplt(tmin, tmax), mcmpgt(tmax, mZero)), mcmplt(tmin, dists));
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   208
	dists = mselect(mask, tmin, dists);
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   209
	return mask;
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   210
}
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   211
#endif
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   212
38
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   213
bool Box::intersect_bbox(const BBox &bbox) const
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   214
{
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   215
	return (
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   216
	H.x > bbox.L.x && L.x < bbox.H.x &&
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   217
	H.y > bbox.L.y && L.y < bbox.H.y &&
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   218
	H.z > bbox.L.z && L.z < bbox.H.z);
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   219
}
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   220
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   221
const Vector Box::normal(const Vector &P) const
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
   222
{
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   223
	Vector l = P - L;
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   224
	Vector h = H - P;
40
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   225
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   226
	if (l.x < h.x)
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   227
		h.x = -1;
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   228
	else
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   229
	{
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   230
		l.x = h.x;
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   231
		h.x = +1;
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   232
	}
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   233
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   234
	if (l.y < h.y)
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   235
		h.y = -1;
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   236
	else
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
   237
	{
40
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   238
		l.y = h.y;
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   239
		h.y = +1;
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   240
	}
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   241
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   242
	if (l.z < h.z)
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   243
		h.z = -1;
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   244
	else
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   245
	{
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   246
		l.z = h.z;
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   247
		h.z = +1;
15
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
   248
	}
40
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   249
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   250
	if (l.x > l.y)
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   251
	{
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   252
		h.x = 0;
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   253
		if (l.y > l.z)
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   254
			h.y = 0;
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   255
		else
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   256
			h.z = 0;
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   257
	}
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   258
	else
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   259
	{
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   260
		h.y = 0;
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   261
		if (l.x > l.z)
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   262
			h.x = 0;
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   263
		else
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   264
			h.z = 0;
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   265
	}
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   266
	return h;
8
e6567b740c5e fixed virtual method get_bbox() for all shapes, default thread num changed to 4
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   267
}
e6567b740c5e fixed virtual method get_bbox() for all shapes, default thread num changed to 4
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   268
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   269
#ifdef TRI_PLUCKER
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   270
inline void Plucker(const Vector &p, const Vector &q, Float* pl)
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   271
{
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   272
    pl[0] = p.x*q.y - q.x*p.y;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   273
    pl[1] = p.x*q.z - q.x*p.z;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   274
    pl[2] = p.x - q.x;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   275
    pl[3] = p.y*q.z - q.y*p.z;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   276
    pl[4] = p.z - q.z;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   277
    pl[5] = q.y - p.y;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   278
}
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   279
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   280
inline Float Side(const Float* pla, const Float* plb)
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   281
{
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   282
    return pla[0]*plb[4] + pla[1]*plb[5] + pla[2]*plb[3] + pla[4]*plb[0] + pla[5]*plb[1] + pla[3]*plb[2];
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   283
}
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   284
#endif
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   285
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   286
Triangle::Triangle(Vertex *aA, Vertex *aB, Vertex *aC, Material *amaterial)
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 56
diff changeset
   287
	: A(aA), B(aB), C(aC)
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
   288
{
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   289
	material = amaterial;
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   290
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   291
	const Vector c = B->P - A->P;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   292
	const Vector b = C->P - A->P;
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   293
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   294
	N = cross(c, b);
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   295
	N.normalize();
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
   296
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   297
#ifdef TRI_PLUCKER
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   298
	Plucker(B->P,C->P,pla);
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   299
	Plucker(C->P,A->P,plb);
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   300
	Plucker(A->P,B->P,plc);
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   301
#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
   302
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   303
#if defined(TRI_BARI) || defined(TRI_BARI_PRE)
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
   304
	if (fabsf(N.x) > fabsf(N.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
   305
	{
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   306
		if (fabsf(N.x) > fabsf(N.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
   307
			k = 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
   308
		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
   309
			k = 2;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   310
	}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   311
	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
   312
	{
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   313
		if (fabsf(N.y) > fabsf(N.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
   314
			k = 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
   315
		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
   316
			k = 2;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   317
	}
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   318
#endif
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   319
#ifdef TRI_BARI_PRE
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
   320
	int u = (k + 1) % 3;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   321
	int v = (k + 2) % 3;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   322
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   323
	Float krec = 1.0f / N[k];
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
   324
	nu = N[u] * krec;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   325
	nv = N[v] * krec;
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   326
	nd = dot(N, A->P) * krec;
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
   327
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   328
	// first line equation
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   329
	Float reci = 1.0f / (b[u] * c[v] - b[v] * c[u]);
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
   330
	bnu = b[u] * reci;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   331
	bnv = -b[v] * reci;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   332
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   333
	// second line equation
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   334
	cnu = -c[u] * reci;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   335
	cnv = c[v] * reci;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   336
#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
   337
}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   338
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   339
bool Triangle::intersect(const Ray &ray, Float &dist) const
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
   340
{
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   341
#ifdef TRI_PLUCKER
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   342
	Float plr[6];
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   343
	Plucker(ray.o, ray.o+ray.dir, plr);
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   344
	const bool side0 = Side(plr, pla) >= 0.0;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   345
	const bool side1 = Side(plr, plb) >= 0.0;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   346
	if (side0 != side1)
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   347
		return false;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   348
	const bool side2 = Side(plr, plc) >= 0.0;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   349
	if (side0 != side2)
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   350
		return false;
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   351
	const Float t = - dot( (ray.o - A->P), N) / dot(ray.dir,N);
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   352
	if(t <= Eps || t >= dist)
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   353
		return false;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   354
	dist = t;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   355
	return true;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   356
#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
   357
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   358
#if defined(TRI_BARI) || defined(TRI_BARI_PRE)
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   359
	static const int modulo3[5] = {0,1,2,0,1};
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   360
	const Vector &O = ray.o;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   361
	const Vector &D = ray.dir;
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   362
	register const int u = modulo3[k+1];
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   363
	register const int v = modulo3[k+2];
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   364
#endif
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   365
#ifdef TRI_BARI_PRE
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   366
	const Float t = (nd - O[k] - nu * O[u] - nv * O[v]) / (D[k] + nu * D[u] + nv * D[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
   367
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   368
	if (t >= dist || t < Eps)
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
   369
		return false;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   370
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   371
	const Float hu = O[u] + t * D[u] - A->P[u];
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   372
	const Float hv = O[v] + t * D[v] - A->P[v];
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   373
	const Float beta = hv * bnu + hu * bnv;
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
   374
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   375
	if (beta < 0.)
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
   376
		return false;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   377
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   378
	const Float gamma = hu * cnv + hv * cnu;
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   379
	if (gamma < 0. || beta + gamma > 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
   380
		return false;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   381
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   382
	dist = 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
   383
	return true;
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   384
#endif
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   385
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   386
#ifdef TRI_BARI
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   387
	// original barycentric coordinates based intesection
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   388
	// not optimized, just for reference
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   389
	const Vector c = B - A;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   390
	const Vector b = C - A;
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   391
	// distance test
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   392
	const Float t = - dot( (O-A), N) / dot(D,N);
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   393
	if (t < Eps || t > dist)
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   394
		return false;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   395
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   396
	// calc hitpoint
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   397
	const Float Hu = O[u] + t * D[u] - A[u];
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   398
	const Float Hv = O[v] + t * D[v] - A[v];
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   399
	const Float beta = (b[u] * Hv - b[v] * Hu) / (b[u] * c[v] - b[v] * c[u]);
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   400
	if (beta < 0)
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   401
		return false;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   402
	const Float gamma = (c[v] * Hu - c[u] * Hv) / (b[u] * c[v] - b[v] * c[u]);
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   403
	if (gamma < 0)
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   404
		return false;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   405
	if (beta+gamma > 1)
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   406
		return false;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   407
	dist = t;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   408
	return true;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   409
#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
   410
}
7
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   411
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   412
#if !defined(NO_SIMD) && defined(TRI_BARI_PRE)
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   413
mfloat4 Triangle::intersect_packet(const RayPacket &rays, mfloat4 &dists) const
85
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   414
{
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   415
	static const int modulo3[5] = {0,1,2,0,1};
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   416
	register const int u = modulo3[k+1];
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   417
	register const int v = modulo3[k+2];
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   418
	mfloat4 mask;
85
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   419
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   420
	const mfloat4 t = mdiv(
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   421
		msub(msub(
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   422
		msub(mset1(nd), rays.o.ma[k]),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   423
		mmul(mset1(nu), rays.o.ma[u])
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   424
		), mmul(mset1(nv), rays.o.ma[v])),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   425
		madd(rays.dir.ma[k],
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   426
		madd(mmul(mset1(nu), rays.dir.ma[u]),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   427
		mmul(mset1(nv), rays.dir.ma[v])))
85
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   428
		);
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   429
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   430
	mask = mand(mcmplt(t, dists), mcmpge(t, mEps));
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   431
	if (!mmovemask(mask))
85
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   432
		return mask;
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   433
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   434
	const mfloat4 hu = msub(madd(rays.o.ma[u],
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   435
		mmul(t, rays.dir.ma[u])), mset1(A->P[u]));
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   436
	const mfloat4 hv = msub(madd(rays.o.ma[v],
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   437
		mmul(t, rays.dir.ma[v])), mset1(A->P[v]));
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   438
	const mfloat4 beta = madd(mmul(hv, mset1(bnu)),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   439
		mmul(hu, mset1(bnv)));
85
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   440
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   441
	mask = mand(mask, mcmpge(beta, mZero));
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   442
	if (!mmovemask(mask))
85
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   443
		return mask;
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   444
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   445
	const mfloat4 gamma = madd(mmul(hu, mset1(cnv)),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   446
		mmul(hv, mset1(cnu)));
85
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   447
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   448
	mask = mand(mask, mand(mcmpge(gamma, mZero),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   449
		mcmple(madd(beta, gamma), mOne)));
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   450
	if (!mmovemask(mask))
85
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   451
		return mask;
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   452
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   453
	dists = mselect(mask, t, dists);
85
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   454
	return mask;
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   455
}
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   456
#endif
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   457
36
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   458
bool Triangle::intersect_bbox(const BBox &bbox) const
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   459
{
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   460
	const Vector boxcenter = (bbox.L+bbox.H)*0.5;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   461
	const Vector boxhalfsize = (bbox.H-bbox.L)*0.5;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   462
	const Vector v0 = A->P - boxcenter;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   463
	const Vector v1 = B->P - boxcenter;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   464
	const Vector v2 = C->P - boxcenter;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   465
	const Vector e0 = v1-v0;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   466
	const Vector e1 = v2-v1;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   467
	const Vector e2 = v0-v2;
36
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   468
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   469
	Float fex = fabsf(e0.x);
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   470
	Float fey = fabsf(e0.y);
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   471
	Float fez = fabsf(e0.z);
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   472
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   473
	Float p0,p1,p2,min,max,rad;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   474
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   475
	p0 = e0.z*v0.y - e0.y*v0.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   476
	p2 = e0.z*v2.y - e0.y*v2.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   477
	if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;}
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   478
	rad = fez * boxhalfsize.y + fey * boxhalfsize.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   479
	if(min>rad || max<-rad) return false;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   480
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   481
	p0 = -e0.z*v0.x + e0.x*v0.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   482
	p2 = -e0.z*v2.x + e0.x*v2.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   483
	if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;}
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   484
	rad = fez * boxhalfsize.x + fex * boxhalfsize.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   485
	if(min>rad || max<-rad) return false;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   486
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   487
	p1 = e0.y*v1.x - e0.x*v1.y;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   488
	p2 = e0.y*v2.x - e0.x*v2.y;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   489
	if(p2<p1) {min=p2; max=p1;} else {min=p1; max=p2;}
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   490
	rad = fey * boxhalfsize.x + fex * boxhalfsize.y;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   491
	if(min>rad || max<-rad) return false;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   492
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   493
	fex = fabsf(e1.x);
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   494
	fey = fabsf(e1.y);
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   495
	fez = fabsf(e1.z);
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   496
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   497
	p0 = e1.z*v0.y - e1.y*v0.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   498
	p2 = e1.z*v2.y - e1.y*v2.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   499
	if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;}
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   500
	rad = fez * boxhalfsize.y + fey * boxhalfsize.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   501
	if(min>rad || max<-rad) return false;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   502
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   503
	p0 = -e1.z*v0.x + e1.x*v0.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   504
	p2 = -e1.z*v2.x + e1.x*v2.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   505
	if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;}
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   506
	rad = fez * boxhalfsize.x + fex * boxhalfsize.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   507
	if(min>rad || max<-rad) return false;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   508
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   509
	p0 = e1.y*v0.x - e1.x*v0.y;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   510
	p1 = e1.y*v1.x - e1.x*v1.y;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   511
	if(p0<p1) {min=p0; max=p1;} else {min=p1; max=p0;}
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   512
	rad = fey * boxhalfsize.x + fex * boxhalfsize.y;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   513
	if(min>rad || max<-rad) return false;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   514
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   515
	fex = fabsf(e2.x);
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   516
	fey = fabsf(e2.y);
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   517
	fez = fabsf(e2.z);
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   518
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   519
	p0 = e2.z*v0.y - e2.y*v0.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   520
	p1 = e2.z*v1.y - e2.y*v1.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   521
	if(p0<p1) {min=p0; max=p1;} else {min=p1; max=p0;}
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   522
	rad = fez * boxhalfsize.y + fey * boxhalfsize.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   523
	if(min>rad || max<-rad) return false;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   524
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   525
	p0 = -e2.z*v0.x + e2.x*v0.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   526
	p1 = -e2.z*v1.x + e2.x*v1.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   527
	if(p0<p1) {min=p0; max=p1;} else {min=p1; max=p0;}
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   528
	rad = fez * boxhalfsize.x + fex * boxhalfsize.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   529
	if(min>rad || max<-rad) return false;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   530
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   531
	p1 = e2.y*v1.x - e2.x*v1.y;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   532
	p2 = e2.y*v2.x - e2.x*v2.y;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   533
	if(p2<p1) {min=p2; max=p1;} else {min=p1; max=p2;}
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   534
	rad = fey * boxhalfsize.x + fex * boxhalfsize.y;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   535
	if(min>rad || max<-rad) return false;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   536
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   537
	/* test overlap in the {x,y,z}-directions */
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   538
	/* test in X-direction */
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   539
	min = v0.x;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   540
	if (v1.x < min) min = v1.x;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   541
	if (v2.x < min) min = v2.x;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   542
	max = v0.x;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   543
	if (v1.x > max) max = v1.x;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   544
	if (v2.x > max) max = v2.x;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   545
	if(min>boxhalfsize.x || max<-boxhalfsize.x) return false;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   546
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   547
	/* test in Y-direction */
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   548
	min = v0.y;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   549
	if (v1.y < min) min = v1.y;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   550
	if (v2.y < min) min = v2.y;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   551
	max = v0.y;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   552
	if (v1.y > max) max = v1.y;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   553
	if (v2.y > max) max = v2.y;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   554
	if(min>boxhalfsize.y || max<-boxhalfsize.y) return false;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   555
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   556
	/* test in Z-direction */
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   557
	min = v0.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   558
	if (v1.z < min) min = v1.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   559
	if (v2.z < min) min = v2.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   560
	max = v0.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   561
	if (v1.z > max) max = v1.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   562
	if (v2.z > max) max = v2.z;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   563
	if(min>boxhalfsize.z || max<-boxhalfsize.z) return false;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   564
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   565
	/*  test if the box intersects the plane of the triangle */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   566
	Vector vmin,vmax;
36
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   567
	Float v;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   568
	for(int q=0;q<3;q++)
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   569
	{
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   570
		v=v0[q];
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   571
		if(N[q]>0.0f)
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   572
		{
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   573
			vmin[q]=-boxhalfsize[q] - v;
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   574
			vmax[q]= boxhalfsize[q] - v;
36
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   575
		}
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   576
		else
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   577
		{
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   578
			vmin[q]= boxhalfsize[q] - v;
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   579
			vmax[q]=-boxhalfsize[q] - v;
36
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   580
		}
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   581
	}
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   582
	if(dot(N,vmin)>0.0f) return false;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   583
	if(dot(N,vmax)>=0.0f) return true;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   584
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   585
	return false;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   586
}
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   587
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   588
BBox Triangle::get_bbox() const
7
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   589
{
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   590
	BBox bbox = BBox();
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   591
	bbox.L = A->P;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   592
	if (B->P.x < bbox.L.x)  bbox.L.x = B->P.x;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   593
	if (C->P.x < bbox.L.x)  bbox.L.x = C->P.x;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   594
	if (B->P.y < bbox.L.y)  bbox.L.y = B->P.y;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   595
	if (C->P.y < bbox.L.y)  bbox.L.y = C->P.y;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   596
	if (B->P.z < bbox.L.z)  bbox.L.z = B->P.z;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   597
	if (C->P.z < bbox.L.z)  bbox.L.z = C->P.z;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   598
	bbox.H = A->P;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   599
	if (B->P.x > bbox.H.x)  bbox.H.x = B->P.x;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   600
	if (C->P.x > bbox.H.x)  bbox.H.x = C->P.x;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   601
	if (B->P.y > bbox.H.y)  bbox.H.y = B->P.y;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   602
	if (C->P.y > bbox.H.y)  bbox.H.y = C->P.y;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   603
	if (B->P.z > bbox.H.z)  bbox.H.z = B->P.z;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   604
	if (C->P.z > bbox.H.z)  bbox.H.z = C->P.z;
7
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   605
	return bbox;
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   606
}
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   607
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   608
ostream & Sphere::dump(ostream &st) const
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   609
{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   610
	return st << "(sphere," << center << "," << radius << ")";
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   611
}
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   612
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   613
ostream & Box::dump(ostream &st) const
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   614
{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   615
	return st << "(box," << L << "," << H << ")";
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   616
}
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   617
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   618
ostream & Vertex::dump(ostream &st) const
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   619
{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   620
	return st << "(v," << P << ")";
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   621
}
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   622
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   623
ostream & NormalVertex::dump(ostream &st) const
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   624
{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   625
	return st << "(vn," << P << "," << N << ")";
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   626
}
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   627
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   628
ostream & Triangle::dump(ostream &st) const
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   629
{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   630
	int idxA, idxB, idxC;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   631
	if (!vertex_index.get(A, idxA))
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   632
		st << *A << "," << endl;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   633
	if (!vertex_index.get(B, idxB))
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   634
		st << *B << "," << endl;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   635
	if (!vertex_index.get(C, idxC))
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   636
		st << *C << "," << endl;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   637
	return st << "(t," << idxA << "," << idxB << "," << idxC << ")";
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
   638
}