src/container.cc
author Radek Brich <radek.brich@devl.cz>
Sat, 10 May 2008 14:29:37 +0200
branchpyrit
changeset 95 ca7d4c665531
parent 92 9af5c039b678
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:
34
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
     1
/*
44
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
     2
 * container.cc: Container class
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
     3
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
     4
 * This file is part of Pyrit Ray Tracer.
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
     5
 *
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
     6
 * Copyright 2007, 2008  Radek Brich
34
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
     7
 *
44
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
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: 34
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: 34
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: 34
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: 34
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: 34
diff changeset
    13
 * furnished to do so, subject to the following conditions:
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    14
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
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: 34
diff changeset
    16
 * all copies or substantial portions of the Software.
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    17
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
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: 34
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: 34
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: 34
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: 34
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: 34
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: 34
diff changeset
    24
 * THE SOFTWARE.
34
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
    25
 */
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
    26
24
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
#include "common.h"
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
#include "container.h"
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    29
#include "serialize.h"
24
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    31
void Container::addShape(const Shape* aShape)
24
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
{
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    33
	const Float e = Eps;
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    34
	if (shapes.size() == 0) {
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    35
		/* initialize bounding box */
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    36
		bbox = aShape->get_bbox();
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    37
		const Vector E(e, e, e);
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    38
		bbox = BBox(bbox.L - E, bbox.H + E);
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    39
	} else {
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    40
		/* adjust bounding box */
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    41
		BBox shapebb = aShape->get_bbox();
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    42
		if (shapebb.L.x - e < bbox.L.x)  bbox.L.x = shapebb.L.x - e;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    43
		if (shapebb.L.y - e < bbox.L.y)  bbox.L.y = shapebb.L.y - e;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    44
		if (shapebb.L.z - e < bbox.L.z)  bbox.L.z = shapebb.L.z - e;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    45
		if (shapebb.H.x + e > bbox.H.x)  bbox.H.x = shapebb.H.x + e;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    46
		if (shapebb.H.y + e > bbox.H.y)  bbox.H.y = shapebb.H.y + e;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    47
		if (shapebb.H.z + e > bbox.H.z)  bbox.H.z = shapebb.H.z + e;
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    48
	}
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    49
	shapes.push_back(aShape);
24
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
};
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    52
const Shape *Container::nearest_intersection(const Shape *origin_shape, const Ray &ray,
24
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
        Float &nearest_distance)
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
{
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    55
	const Shape *nearest_shape = NULL;
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    56
	ShapeList::iterator shape;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    57
	for (shape = shapes.begin(); shape != shapes.end(); shape++)
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    58
		if (*shape != origin_shape && (*shape)->intersect(ray, nearest_distance))
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    59
			nearest_shape = *shape;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    60
	return nearest_shape;
24
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
}
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    62
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    63
#ifndef NO_SIMD
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    64
void Container::packet_intersection(const Shape* const* origin_shapes, const RayPacket &rays,
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    65
	Float *nearest_distances, const Shape **nearest_shapes)
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
    66
{
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
    67
	for (int i = 0; i < 4; i++)
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
    68
		nearest_shapes[i] = nearest_intersection(origin_shapes[i], rays[i],
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
    69
			nearest_distances[i]);
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
    70
}
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    71
#endif
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
    72
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    73
ostream & Container::dump(ostream &st) const
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    74
{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    75
	st << "(container," << shapes.size();
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    76
	ShapeList::const_iterator shape;
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    77
	for (shape = shapes.begin(); shape != shapes.end(); shape++)
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    78
	{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    79
		int idx;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    80
		if (!shape_index.get(*shape, idx))
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    81
			st << "," << **shape;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    82
	}
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    83
	return st << ")";
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    84
}