-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
185 lines (148 loc) · 4.74 KB
/
util.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/***********************************************************
Starter code for Assignment 3
This code was originally written by Jack Wang for
CSC418, SPRING 2005
utility functions and structures
(based on code from CGL, University of Waterloo),
modify this file as you see fit.
***********************************************************/
#ifndef _UTIL_
#define _UTIL_
#include <iostream>
#include <cmath>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
class Point3D {
public:
Point3D();
Point3D(double x, double y, double z);
Point3D(const Point3D& other);
Point3D& operator =(const Point3D& other);
double& operator[](int i);
double operator[](int i) const;
private:
double m_data[3];
};
class Vector3D {
public:
Vector3D();
Vector3D(double x, double y, double z);
Vector3D(const Vector3D& other);
Vector3D& operator =(const Vector3D& other);
double& operator[](int i);
double operator[](int i) const;
double length() const;
double normalize();
double dot(const Vector3D& other) const;
Vector3D cross(const Vector3D& other) const;
private:
double m_data[3];
};
// standard operators on points and vectors
Vector3D operator *(double s, const Vector3D& v);
Vector3D operator +(const Vector3D& u, const Vector3D& v);
Point3D operator +(const Point3D& u, const Vector3D& v);
Vector3D operator -(const Point3D& u, const Point3D& v);
Vector3D operator -(const Vector3D& u, const Vector3D& v);
Vector3D operator -(const Vector3D& u);
Point3D operator -(const Point3D& u, const Vector3D& v);
Vector3D cross(const Vector3D& u, const Vector3D& v);
std::ostream& operator <<(std::ostream& o, const Point3D& p);
std::ostream& operator <<(std::ostream& o, const Vector3D& v);
class Vector4D {
public:
Vector4D();
Vector4D(double w, double x, double y, double z);
Vector4D(const Vector4D& other);
Vector4D& operator =(const Vector4D& other);
double& operator[](int i);
double operator[](int i) const;
private:
double m_data[4];
};
class Matrix4x4 {
public:
Matrix4x4();
Matrix4x4(const Matrix4x4& other);
Matrix4x4& operator=(const Matrix4x4& other);
Vector4D getRow(int row) const;
double *getRow(int row);
Vector4D getColumn(int col) const;
Vector4D operator[](int row) const;
double *operator[](int row);
Matrix4x4 transpose() const;
private:
double m_data[16];
};
Matrix4x4 operator *(const Matrix4x4& M, const Matrix4x4& N);
Vector3D operator *(const Matrix4x4& M, const Vector3D& v);
Point3D operator *(const Matrix4x4& M, const Point3D& p);
// Multiply n by the transpose of M, useful for transforming normals.
// Recall that normals should be transformed by the inverse transpose
// of the matrix.
Vector3D transNorm(const Matrix4x4& M, const Vector3D& n);
std::ostream& operator <<(std::ostream& os, const Matrix4x4& M);
class Colour {
public:
Colour();
Colour(double r, double g, double b);
Colour(const Colour& other);
Colour& operator =(const Colour& other);
Colour operator *(const Colour& other);
double& operator[](int i);
double operator[](int i) const;
void clamp();
private:
double m_data[3];
};
Colour operator *(double s, const Colour& c);
Colour operator +(const Colour& u, const Colour& v);
std::ostream& operator <<(std::ostream& o, const Colour& c);
struct Material {
Material( Colour ambient, Colour diffuse, Colour specular, double exp ) :
ambient(ambient), diffuse(diffuse), specular(specular),
specular_exp(exp) {}
// Ambient components for Phong shading.
Colour ambient;
// Diffuse components for Phong shading.
Colour diffuse;
// Specular components for Phong shading.
Colour specular;
// Specular expoent.
double specular_exp;
};
struct Intersection {
// Location of intersection.
Point3D point;
// Normal at the intersection.
Vector3D normal;
// Material at the intersection.
Material* mat;
// Position of the intersection point on your ray.
// (i.e. point = ray.origin + t_value * ray.dir)
// This is used when you need to intersect multiply objects and
// only want to keep the nearest intersection.
double t_value;
// Set to true when no intersection has occured.
bool none;
};
// Ray structure.
struct Ray3D {
Ray3D() {
intersection.none = true;
}
Ray3D( Point3D p, Vector3D v ) : origin(p), dir(v) {
intersection.none = true;
}
// Origin and direction of the ray.
Point3D origin;
Vector3D dir;
// Intersection status, should be computed by the intersection
// function.
Intersection intersection;
// Current colour of the ray, should be computed by the shading
// function.
Colour col;
};
#endif