include/scene.h
author Radek Brich <radek.brich@devl.cz>
Wed, 26 Mar 2008 14:29:21 +0100
branchpyrit
changeset 47 320d5d466864
parent 46 6493fb65f0b1
child 49 558fde7da82a
permissions -rw-r--r--
move Sampler classes to sampler.cc fix demos, make whole project compilable again
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
/*
44
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
     2
 * scene.h: classes for objects in scene
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
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
#ifndef SCENE_H
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
#define SCENE_H
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
#include <vector>
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
    31
#include <typeinfo>
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
    32
47
320d5d466864 move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    33
#include "sampler.h"
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
#include "noise.h"
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    35
#include "vector.h"
34
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
    36
#include "quaternion.h"
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    38
/*
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    39
triangle intersection alghoritm
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    40
chooses are:
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    41
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
    42
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
    43
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
    44
*/
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    45
#if !defined(TRI_PLUCKER) && !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
    46
#	define 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
    47
#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
    48
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
using namespace std;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    51
class Ray
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    52
{
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    53
public:
15
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    54
	Vector3 o, dir;
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    55
	Ray(const Vector3 &ao, const Vector3 &adir):
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    56
		o(ao), dir(adir) {};
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    57
};
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    58
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    59
/**
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    60
 * a camera
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    61
 */
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    62
class Camera
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    63
{
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    64
public:
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    65
	Vector3 eye, p, u, v;
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    66
	Float f;
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    67
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    68
	Camera(): eye(0,0,10), p(0,0,-1), u(-1,0,0), v(0,1,0), f(3.14/4.0) {};
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    69
	Camera(const Vector3 &C, const Vector3 &ap, const Vector3 &au, const Vector3 &av):
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    70
		eye(C), p(ap), u(au), v(av), f(3.14/4.0) {};
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    71
	void setEye(const Vector3 &aeye) { eye = aeye; };
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    72
	void setFocalLength(const Float af) { f = af; };
20
f22952603f29 new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents: 19
diff changeset
    73
	void rotate(const Quaternion &q);
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    74
	void move(const Float fw, const Float left, const Float up);
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    75
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    76
	Ray makeRay(Sample *samp);
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    77
};
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    78
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    79
/**
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    80
 * axis-aligned bounding box
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    81
 */
7
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    82
class BBox
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    83
{
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    84
public:
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    85
	Vector3 L;
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
    86
	Vector3 H;
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
    87
	BBox(): L(), H() {};
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
    88
	BBox(const Vector3 aL, const Vector3 aH): L(aL), H(aH) {};
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    89
	Float w() { return H.x-L.x; };
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    90
	Float h() { return H.y-L.y; };
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    91
	Float d() { return H.z-L.z; };
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    92
	bool intersect(const Ray &ray, Float &a, Float &b);
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
    93
};
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
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    95
/**
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    96
 * light object
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    97
 */
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
class Light
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
{
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
public:
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
	Vector3 pos;
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
	Colour colour;
21
79b516a3803d naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents: 20
diff changeset
   103
	bool cast_shadows;
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
   104
40
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   105
	Light():
929aad02c5f2 Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   106
		pos(Vector3(0,0,0)), colour(Colour(1,1,1)), cast_shadows(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
   107
	Light(const Vector3 &position, const Colour &acolour):
21
79b516a3803d naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents: 20
diff changeset
   108
		pos(position), colour(acolour), cast_shadows(true) {};
79b516a3803d naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents: 20
diff changeset
   109
	void castShadows(bool cast) { cast_shadows = cast; };
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
   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
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   112
/**
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   113
 * texture
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   114
 */
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
   115
class Texture
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
{
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
public:
47
320d5d466864 move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   118
	virtual ~Texture() {};
42
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
   119
	virtual Colour evaluate(Vector3 point) = 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
   120
};
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   121
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   122
/**
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   123
 * material
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   124
 */
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
   125
class Material
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   126
{
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   127
public:
42
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
   128
	Colour colour;
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   129
	Float ambient, diffuse, specular, shininess; // Phong constants
31
b4e09433934a refraction
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   130
	Float reflectivity; // how much reflective is the surface
b4e09433934a refraction
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   131
	Float transmissivity, refract_index; // part of light which can be refracted; index of refraction
42
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
   132
	Texture *texture;
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
   133
42
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
   134
	Material(const Colour &acolour): colour(acolour), texture(NULL)
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
   135
	{
31
b4e09433934a refraction
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   136
		ambient = 0.2;
b4e09433934a refraction
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   137
		diffuse = 0.8;
b4e09433934a refraction
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   138
		specular = 0.2;
20
f22952603f29 new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents: 19
diff changeset
   139
		shininess = 0.5;
31
b4e09433934a refraction
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   140
		reflectivity = 0.2;
b4e09433934a refraction
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   141
		transmissivity = 0.0;
b4e09433934a refraction
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   142
		refract_index = 1.3;
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   143
	}
31
b4e09433934a refraction
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   144
b4e09433934a refraction
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   145
	void setPhong(const Float amb, const Float dif, const Float spec, const Float shin)
b4e09433934a refraction
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   146
		{ ambient = amb; diffuse = dif; specular = spec; shininess = shin; };
b4e09433934a refraction
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   147
	void setReflectivity(const Float refl) { reflectivity = refl; };
b4e09433934a refraction
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   148
	void setTransmissivity(const Float trans, const Float rindex)
b4e09433934a refraction
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   149
		{ transmissivity = trans; refract_index = rindex; };
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   150
};
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   151
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   152
/**
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   153
 * shape
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   154
 */
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
   155
class Shape
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   156
{
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   157
public:
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   158
	Material *material;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   159
	Shape() {};
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   160
	virtual ~Shape() {};
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   161
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   162
	// first intersection point
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   163
	virtual bool intersect(const Ray &ray, Float &dist) const = 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
   164
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   165
	// all intersections (only for CSG)
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   166
	virtual bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const = 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
   167
36
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   168
	// intersection with AABB
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   169
	virtual bool intersect_bbox(const BBox &bbox) const = 0;
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   170
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
   171
	// normal at point P
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   172
	virtual const Vector3 normal(const Vector3 &P) const = 0;
7
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   173
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   174
	virtual BBox get_bbox() const = 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
   175
};
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   176
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   177
/**
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   178
 * list of shapes
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   179
 */
24
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
   180
class ShapeList: public vector<Shape*>
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
   181
{
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
   182
};
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
   183
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   184
/**
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   185
 * sphere shape
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   186
 */
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
   187
class Sphere: public Shape
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   188
{
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   189
	Float sqr_radius;
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   190
	Float inv_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
   191
public:
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   192
	Vector3 center;
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   193
	Float 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
   194
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   195
	Sphere(const Vector3 &acenter, const Float aradius, Material *amaterial):
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
   196
		sqr_radius(aradius*aradius), inv_radius(1.0f/aradius),
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   197
		center(acenter), radius(aradius) { 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
   198
	bool intersect(const Ray &ray, Float &dist) const;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   199
	bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const;
38
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   200
	bool intersect_bbox(const BBox &bbox) const;
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   201
	const Vector3 normal(const Vector3 &P) const { return (P - center) * inv_radius; };
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   202
	BBox get_bbox() 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
   203
};
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   204
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   205
/**
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   206
 * box shape
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   207
 */
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
   208
class Box: public Shape
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   209
{
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
   210
	Vector3 L;
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
   211
	Vector3 H;
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   212
public:
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
   213
	Box(const Vector3 &aL, const Vector3 &aH, Material *amaterial): L(aL), H(aH)
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
   214
	{
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
   215
		for (int i = 0; i < 3; i++)
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
   216
			if (L.cell[i] > H.cell[i])
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
   217
				swap(L.cell[i], H.cell[i]);
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
   218
		material = amaterial;
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
   219
	};
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   220
	bool intersect(const Ray &ray, Float &dist) const;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   221
	bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const { return false; };
38
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   222
	bool intersect_bbox(const BBox &bbox) const;
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   223
	const Vector3 normal(const Vector3 &P) const;
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   224
	BBox get_bbox() const { return BBox(L, H); };
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   225
};
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   226
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   227
/**
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   228
 * triangle vertex
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   229
 */
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   230
class Vertex
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   231
{
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   232
public:
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   233
	Vector3 P;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   234
	Vertex(const Vector3 &aP): P(aP) {};
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   235
};
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   236
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   237
/**
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   238
 * triangle vertex with normal
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   239
 */
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   240
class NormalVertex: public Vertex
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   241
{
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   242
public:
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   243
	Vector3 N;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   244
	NormalVertex(const Vector3 &aP): Vertex(aP) {};
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   245
	NormalVertex(const Vector3 &aP, const Vector3 &aN): Vertex(aP), N(aN) {};
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   246
	const Vector3 &getNormal() { return N; };
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   247
	void setNormal(const Vector3 &aN) { N = aN; };
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   248
};
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   249
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   250
/**
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   251
 * triangle shape
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   252
 */
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
   253
class Triangle: public Shape
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   254
{
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   255
#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
   256
	Float nu, nv, nd;
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
   257
	int k; // dominant axis
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   258
	Float bnu, bnv;
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   259
	Float cnu, cnv;
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   260
#endif
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   261
#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
   262
	int k; // dominant axis
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   263
#endif
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   264
#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
   265
	Float pla[6], plb[6], plc[6];
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   266
#endif
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   267
	Vector3 N;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   268
	bool smooth;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   269
	const Vector3 smooth_normal(const Vector3 &P) const
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   270
	{
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   271
#ifdef TRI_BARI_PRE
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   272
		const Vector3 &NA = static_cast<NormalVertex*>(A)->N;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   273
		const Vector3 &NB = static_cast<NormalVertex*>(B)->N;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   274
		const Vector3 &NC = static_cast<NormalVertex*>(C)->N;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   275
		static const int modulo3[5] = {0,1,2,0,1};
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   276
		register const int ku = modulo3[k+1];
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   277
		register const int kv = modulo3[k+2];
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   278
		const Float pu = P[ku] - A->P[ku];
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   279
		const Float pv = P[kv] - A->P[kv];
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   280
		const Float u = pv * bnu + pu * bnv;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   281
		const Float v = pu * cnv + pv * cnu;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   282
		Vector3 n = NA + u * (NB - NA) + v * (NC - NA);
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   283
		n.normalize();
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   284
		return n;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   285
#else
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   286
		return N; // not implemented for other algorithms
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   287
#endif
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   288
	};
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
   289
public:
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   290
	Vertex *A, *B, *C;
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
   291
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   292
	Triangle(Vertex *aA, Vertex *aB, Vertex *aC, 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
   293
	bool intersect(const Ray &ray, Float &dist) const;
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   294
	bool intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const {return false;};
36
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   295
	bool intersect_bbox(const BBox &bbox) const;
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   296
	const Vector3 normal(const Vector3 &P) const { return (smooth ? smooth_normal(P) : N); };
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   297
	const Vector3 getNormal() const { return N; };
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   298
	void setSmooth() { smooth = true; };//(typeid(*A) == typeid(*B) == typeid(*C) == typeid(NormalVertex)); };
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   299
	void setFlat() { smooth = false; };
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   300
	bool getSmooth() const { return smooth; };
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
	BBox get_bbox() 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
   302
};
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   303
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
#endif