-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraytracer.h
144 lines (115 loc) · 4.56 KB
/
raytracer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/***********************************************************
Starter code for Assignment 3
This code was originally written by Jack Wang for
CSC418, SPRING 2005
This file contains the interface and
datastructures of the raytracer.
Simple traversal and addition code to
the datastructures are given to you.
***********************************************************/
#include "util.h"
#include "scene_object.h"
#include "light_source.h"
// Linked list containing light sources in the scene.
struct LightListNode {
LightListNode() : light(NULL), next(NULL) {}
LightListNode( LightSource* light, LightListNode* next = NULL ) :
light(light), next(next) {}
~LightListNode() {
if (!light) delete light;
}
LightSource* light;
LightListNode* next;
};
// The scene graph, containing objects in the scene.
struct SceneDagNode {
SceneDagNode() :
obj(NULL), mat(NULL),
next(NULL), parent(NULL), child(NULL) {
}
SceneDagNode( SceneObject* obj, Material* mat ) :
obj(obj), mat(mat), next(NULL), parent(NULL), child(NULL) {
}
~SceneDagNode() {
if (!obj) delete obj;
if (!mat) delete mat;
}
// Pointer to geometry primitive, used for intersection.
SceneObject* obj;
// Pointer to material of the object, used in shading.
Material* mat;
// Each node maintains a transformation matrix, which maps the
// geometry from object space to world space and the inverse.
Matrix4x4 trans;
Matrix4x4 invtrans;
// Internal structure of the tree, you shouldn't have to worry
// about them.
SceneDagNode* next;
SceneDagNode* parent;
SceneDagNode* child;
};
class Raytracer {
public:
Raytracer();
~Raytracer();
// Renders an image fileName with width and height and a camera
// positioned at eye, with view vector view, up vector up, and
// field of view fov.
void render( int width, int height, Point3D eye, Vector3D view,
Vector3D up, double fov, char* fileName );
// Add an object into the scene, with material mat. The function
// returns a handle to the object node you just added, use the
// handle to apply transformations to the object.
SceneDagNode* addObject( SceneObject* obj, Material* mat ) {
return addObject(_root, obj, mat);
}
// Add an object into the scene with a specific parent node,
// don't worry about this unless you want to do hierarchical
// modeling. You could create nodes with NULL obj and mat,
// in which case they just represent transformations.
SceneDagNode* addObject( SceneDagNode* parent, SceneObject* obj,
Material* mat );
// Add a light source.
LightListNode* addLightSource( LightSource* light );
// Transformation functions are implemented by right-multiplying
// the transformation matrix to the node's transformation matrix.
// Apply rotation about axis 'x', 'y', 'z' angle degrees to node.
void rotate( SceneDagNode* node, char axis, double angle );
// Apply translation in the direction of trans to node.
void translate( SceneDagNode* node, Vector3D trans );
// Apply scaling about a fixed point origin.
void scale( SceneDagNode* node, Point3D origin, double factor[3] );
private:
// Allocates and initializes the pixel buffer for rendering, you
// could add an interesting background to your scene by modifying
// this function.
void initPixelBuffer();
// Saves the pixel buffer to a file and deletes the buffer.
void flushPixelBuffer(char *file_name);
// Return the colour of the ray after intersection and shading, call
// this function recursively for reflection and refraction.
Colour shadeRay( Ray3D& ray );
// Constructs a view to world transformation matrix based on the
// camera parameters.
Matrix4x4 initInvViewMatrix( Point3D eye, Vector3D view, Vector3D up );
// Traversal code for the scene graph, the ray is transformed into
// the object space of each node where intersection is performed.
void traverseScene( SceneDagNode* node, Ray3D& ray );
// After intersection, calculate the colour of the ray by shading it
// with all light sources in the scene.
void computeShading( Ray3D& ray );
// Width and height of the viewport.
int _scrWidth;
int _scrHeight;
// Light list and scene graph.
LightListNode *_lightSource;
SceneDagNode *_root;
// Pixel buffer.
unsigned char* _rbuffer;
unsigned char* _gbuffer;
unsigned char* _bbuffer;
// Maintain global transformation matrices similar to OpenGL's matrix
// stack. These are used during scene traversal.
Matrix4x4 _modelToWorld;
Matrix4x4 _worldToModel;
};