include/shapes.h
author Radek Brich <radek.brich@devl.cz>
Mon, 19 May 2008 22:59:04 +0200
branchpyrit
changeset 98 64638385798a
parent 95 ca7d4c665531
child 100 c005054bf4c1
permissions -rw-r--r--
add sections about demos to README update doxygen comments
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
     1
/**
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
     2
 * @file  shapes.h
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
     3
 * @brief Shape classes: Box, Sphere, Triangle and helpers
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
 *
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
 * This file is part of Pyrit Ray Tracer.
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
 *
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
 * Copyright 2006, 2007, 2008  Radek Brich
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
 *
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
 * of this software and associated documentation files (the "Software"), to deal
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
 * in the Software without restriction, including without limitation the rights
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
 * copies of the Software, and to permit persons to whom the Software is
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
 * furnished to do so, subject to the following conditions:
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
 *
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
 * The above copyright notice and this permission notice shall be included in
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
 * all copies or substantial portions of the Software.
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
 *
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
 * THE SOFTWARE.
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
 */
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
#ifndef SHAPES_H
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
#define SHAPES_H
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
#include "common.h"
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
#include "scene.h"
79
062b1c4143f7 material and texture classes moved to material.(cc,h)
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
    33
#include "material.h"
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
/*
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
triangle intersection alghoritm
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
options are:
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
TRI_PLUCKER
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
TRI_BARI
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
TRI_BARI_PRE
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
*/
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
#if !defined(TRI_PLUCKER) && !defined(TRI_BARI) && !defined(TRI_BARI_PRE)
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
#	define TRI_BARI_PRE
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
#endif
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
/**
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    47
 * abstract shape class
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
 */
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
class Shape
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
public:
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
	Material *material;
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    53
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
	Shape() {};
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
	virtual ~Shape() {};
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    57
	/**
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    58
	 * intersect ray with sphere
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    59
	 * @param[in] ray	the ray
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    60
	 * @param[in] dist	maximum allowed distance of intersection
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    61
	 * @param[out] dist	distance of the intersection if found, unchanged otherwise
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    62
	 * @return true if ray intersects the sphere
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    63
	 */
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
	virtual bool intersect(const Ray &ray, Float &dist) const = 0;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    66
	/**
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    67
	 * same as intersect, but for ray packets
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
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
#ifndef NO_SIMD
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    70
	virtual mfloat4 intersect_packet(const RayPacket &rays, mfloat4 &dists) const
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 82
diff changeset
    71
	{
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    72
		mfloat4 results;
85
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    73
		((int*)&results)[0] = intersect(rays[0], ((float*)&dists)[0]) ? -1 : 0;
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    74
		((int*)&results)[1] = intersect(rays[1], ((float*)&dists)[1]) ? -1 : 0;
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    75
		((int*)&results)[2] = intersect(rays[2], ((float*)&dists)[2]) ? -1 : 0;
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    76
		((int*)&results)[3] = intersect(rays[3], ((float*)&dists)[3]) ? -1 : 0;
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    77
		return results;
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 82
diff changeset
    78
	};
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    79
#endif
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 82
diff changeset
    80
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    81
	/** get all intersections -- not needed nor used currently */
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
	virtual bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const = 0;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    84
	/** test intersection with bounding box */
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
	virtual bool intersect_bbox(const BBox &bbox) const = 0;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    87
	/** get surface normal at point P */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    88
	virtual const Vector normal(const Vector &P) const = 0;
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    90
	/** get bounding box of this shape */
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
	virtual BBox get_bbox() const = 0;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    93
	/** write textual representation of the shape to stream */
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    94
	virtual ostream & dump(ostream &st) const = 0;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    95
};
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
/**
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
 * list of shapes
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
 */
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
   100
typedef vector<const Shape*> ShapeList;
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
/**
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   103
 * sphere shape
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
 */
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   105
class Sphere: public Shape
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   106
{
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   107
	Vector center;
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   108
	Float radius;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   109
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   110
	Float sqr_radius;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   111
	Float inv_radius;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   112
public:
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   113
	Sphere(const Vector &acenter, const Float aradius, Material *amaterial):
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   114
		center(acenter), radius(aradius),
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   115
		sqr_radius(aradius*aradius), inv_radius(1.0f/aradius)
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   116
		{ material = amaterial; }
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   117
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   118
	bool intersect(const Ray &ray, Float &dist) const;
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   119
#ifndef NO_SIMD
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   120
	mfloat4 intersect_packet(const RayPacket &rays, mfloat4 &dists) const;
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   121
#endif
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   122
	bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   123
	bool intersect_bbox(const BBox &bbox) const;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   124
	const Vector normal(const Vector &P) const { return (P - center) * inv_radius; };
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   125
	BBox get_bbox() const;
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   126
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   127
	const Vector getCenter() const { return center; };
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 82
diff changeset
   128
	Float getRadius() const { return radius; };
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   129
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   130
	ostream & dump(ostream &st) const;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   131
};
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   132
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   133
/**
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   134
 * box shape
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   135
 */
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   136
class Box: public Shape
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   137
{
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   138
	Vector L;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   139
	Vector H;
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   140
public:
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   141
	Box(const Vector &aL, const Vector &aH, Material *amaterial): L(aL), H(aH)
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   142
	{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   143
		for (int i = 0; i < 3; i++)
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   144
			if (L[i] > H[i])
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   145
				swap(L[i], H[i]);
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   146
		material = amaterial;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   147
	};
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   148
	bool intersect(const Ray &ray, Float &dist) const;
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   149
#ifndef NO_SIMD
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   150
	mfloat4 intersect_packet(const RayPacket &rays, mfloat4 &dists) const;
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   151
#endif
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   152
	bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const { return false; };
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   153
	bool intersect_bbox(const BBox &bbox) const;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   154
	const Vector normal(const Vector &P) const;
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   155
	BBox get_bbox() const { return BBox(L, H); };
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   156
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   157
	const Vector getL() const { return L; };
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   158
	const Vector getH() const { return H; };
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   159
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   160
	ostream & dump(ostream &st) const;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   161
};
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   162
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   163
/**
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   164
 * triangle vertex
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   165
 */
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   166
class Vertex
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   167
{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   168
public:
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   169
	Vector P;
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   170
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   171
	Vertex(const Vector &aP): P(aP) {};
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 79
diff changeset
   172
	virtual ~Vertex() {};
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   173
	virtual ostream & dump(ostream &st) const;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   174
};
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   175
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   176
/**
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   177
 * triangle vertex with normal
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   178
 */
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   179
class NormalVertex: public Vertex
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   180
{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   181
public:
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   182
	Vector N;
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   183
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   184
	NormalVertex(const NormalVertex *v): Vertex(v->P), N(v->N) {};
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   185
	NormalVertex(const Vector &aP): Vertex(aP) {};
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   186
	NormalVertex(const Vector &aP, const Vector &aN): Vertex(aP), N(aN) {};
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   187
	const Vector &getNormal() { return N; };
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   188
	void setNormal(const Vector &aN) { N = aN; };
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   189
	ostream & dump(ostream &st) const;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   190
};
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   191
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   192
/**
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   193
 * triangle shape
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   194
 */
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   195
class Triangle: public Shape
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   196
{
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   197
	Vector N;
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   198
#ifdef TRI_BARI_PRE
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   199
	Float nu, nv, nd;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   200
	Float bnu, bnv;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   201
	Float cnu, cnv;
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   202
	int k; // dominant axis
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   203
#endif
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   204
#ifdef TRI_BARI
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   205
	int k; // dominant axis
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   206
#endif
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   207
#ifdef TRI_PLUCKER
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   208
	Float pla[6], plb[6], plc[6];
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   209
#endif
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   210
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   211
	const Vector smooth_normal(const Vector &P) const
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   212
	{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   213
#ifdef TRI_BARI_PRE
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   214
		const Vector &NA = static_cast<NormalVertex*>(A)->N;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   215
		const Vector &NB = static_cast<NormalVertex*>(B)->N;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   216
		const Vector &NC = static_cast<NormalVertex*>(C)->N;
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   217
		static const int modulo3[5] = {0,1,2,0,1};
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   218
		register const int ku = modulo3[k+1];
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   219
		register const int kv = modulo3[k+2];
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   220
		const Float pu = P[ku] - A->P[ku];
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   221
		const Float pv = P[kv] - A->P[kv];
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   222
		const Float u = pv * bnu + pu * bnv;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   223
		const Float v = pu * cnv + pv * cnu;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   224
		Vector n = NA + u * (NB - NA) + v * (NC - NA);
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   225
		n.normalize();
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   226
		return n;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   227
#else
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   228
		return N; // not implemented for other algorithms
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   229
#endif
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   230
	};
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   231
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   232
public:
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   233
	Vertex *A, *B, *C;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   234
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   235
	Triangle() {};
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   236
	Triangle(Vertex *aA, Vertex *aB, Vertex *aC, Material *amaterial);
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   237
	bool intersect(const Ray &ray, Float &dist) const;
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   238
#if !defined(NO_SIMD) && defined(TRI_BARI_PRE)
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   239
	mfloat4 intersect_packet(const RayPacket &rays, mfloat4 &dists) const;
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   240
#endif
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   241
	bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const {return false;};
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   242
	bool intersect_bbox(const BBox &bbox) const;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   243
	const Vector normal(const Vector &P) const { return (material->smooth ? smooth_normal(P) : N); };
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   244
	BBox get_bbox() const;
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   245
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   246
	/** get real normal of the triangle */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   247
	const Vector getNormal() const { return N; };
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   248
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   249
	ostream & dump(ostream &st) const;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   250
};
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   251
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   252
/** template for triangle arrays, currently not used */
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   253
template <class T> class Array
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   254
{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   255
	T *array;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   256
public:
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   257
	Array(int n) { array = new T[n]; };
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   258
	~Array() { delete[] array; };
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   259
	const T &operator[](int i) const { return array[i]; };
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   260
};
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   261
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   262
typedef Array<Vertex> VertexArray;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   263
typedef Array<NormalVertex> NormalVertexArray;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   264
typedef Array<Triangle> TriangleArray;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   265
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   266
#endif