-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.h
231 lines (166 loc) · 4.17 KB
/
object.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#ifndef OBJECT_H
#define OBJECT_H
#include "ray.h"
#include "bbox.h"
#include "vector.h"
///
/// struct material
///
struct material
{
material()
{
for(int i = 0; i < 3; i ++)
{
specColor[i] = 0;
diffColor[i] = 0;
ambiColor[i] = 0;
}
fShininess = 1.0f;
}
vect3d specColor;
vect3d diffColor;
vect3d ambiColor;
float fShininess;
};
//
enum ObjType {TRI_CPU, SQU_CPU, SPH_CPU, CUBE_CPU, OBJ_CPU, NONE_CPU};
///
/// class Object
///
class Object
{
public:
Object(float fReflR = 1.f, float fRefrR = 0.f, float fRefrK = 1.f, float fEmitR = 1.0);
virtual ~Object(){};
virtual ObjType getObjType() = 0;
// Intersection
virtual bool isHit(Ray &ray, vect3d &pNormal, float *pt, vect3d *pTexColor = NULL) = 0;
float getReflectionRatio() { return _fReflectionRatio; }
float getRefractionRatio() { return _fRefractionRatio; }
float getRefractionK() { return _fRefractionK; }
float getEmissionRatio() { return _fEmitRatio; }
// Get info
void setMaterial(vect3d &specColor, vect3d &diffColor, vect3d &ambiColor, float fShininess);
material & getMaterial() { return _mat; }
unsigned _id;
//Ray *_pLastVisitingRay;
IdType _nLastVisitingRay;
public:
BBox * getBBox(){ return &_bbox; }
protected:
virtual void updateBBox() = 0;
protected:
BBox _bbox;
// Obj color itself occupies (1 - _fReflectionRatio - _fRefractionRatio)
float _fReflectionRatio; // how much light contribution due to the reflection?
float _fRefractionRatio; // how much light contribution due to the refraction?
float _fRefractionK;
float _fEmitRatio;
public:
material _mat;
};
///
/// class PrimaryObject
///
class PrimaryObject : public Object
{
public:
PrimaryObject(float fReflR = 1.f, float fRefrR = 0.f, float fRefrK = 1.f, float fEmitR = 1.0)
:Object(fReflR, fRefrR, fRefrK, fEmitR)
{}
};
///
/// Primary - Sphere
///
bool isTriangleHit( vect3d [3], Ray &ray,
float *pt, float *pu, float *pv);
class Triangle : public PrimaryObject
{
public:
Triangle( float vVertices[3][3], float *facetNormal,
float fReflR = 1.f, float fRefrR = 0.f, float fRefrK = 1.f, float fEmitR = 1.0);
ObjType getObjType()
{ return TRI_CPU; }
void setSmooth(bool bSmooth)
{ _bSmooth = bSmooth; }
bool isHit(Ray &ray, vect3d &pNormal, float *pt, vect3d *pTexColor = NULL);
void setVNorm(float vnorm[3][3]);
void updateBBox();
//private:
vect3d _vertices[3];
vect3d _normal;
vect3d _vnormal[3];
//protected:
bool _bSmooth;
bool _bHasVNorm;
};
///\ Square Object class
///
class Square : public PrimaryObject
{
public:
// WARNING: 1. the passed in vec. should be normalized !!
// 2. Dude, normal and WidthVec should be perpendicular to each other.
// you need to promise it.
Square(vect3d &pCenter, vect3d &pNormal, vect3d &pHeadVec, float nWidth, float nHeight,
float fReflR = 1.f, float fRefrR = 0.f, float fRefrK = 1.f, float fEmitR = 1.0);
ObjType getObjType()
{ return SQU_CPU; }
bool isHit(Ray &ray, vect3d &pNormal, float *pt, vect3d *pTexColor = NULL);
protected:
void updateBBox();
//protected:
public:
// Directions
vect3d _vNormal;
vect3d _vWidthVec;
// Positions
vect3d _vCenter;
float _nWidth;
float _nHeight;
// For Calc.
//
vect3d _v2HeightVec;
vect3d _v2WidthVec;
};
///\ Cube Object class
///
class Cube : public PrimaryObject
{
public:
/// WARNING: all vectors should be normalized !!!
Cube( float fLen, float fWidth, float fHeight,
vect3d &pCenterPos,
vect3d &pVerticalVec,
vect3d &pHorizonVec,
float fReflR = 1.f, float fRefrR = 0.f, float fRefrK = 1.f, float fEmitR = 1.0
);
~Cube();
ObjType getObjType()
{ return CUBE_CPU; }
bool isHit(Ray &ray, vect3d &pNormal, float *pt, vect3d *pTexColor = NULL);
void setColor( vect3d &pAmbColor,
vect3d &pDiffColor,
vect3d &pSpecColor,
float fShininess)
{
for(int i = 0; i < 6; i++)
{
_vs[i]->setMaterial(pSpecColor, pDiffColor, pAmbColor, fShininess);
}
PrimaryObject::setMaterial( pSpecColor, pDiffColor, pAmbColor, fShininess);
}
protected:
void updateBBox();
private:
float _fLength;
float _fWidth;
float _fHeight;
vect3d _vCenterPos;
vect3d _verticalVec;
vect3d _horizonVec;
public:
Square* _vs[6];
};
#endif