This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raytracr.hpp
275 lines (222 loc) · 8.12 KB
/
raytracr.hpp
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#ifndef SNIPPETS_RAYTRACER_HPP
#define SNIPPETS_RAYTRACER_HPP
#include <algorithm>
#include <cmath>
#include <memory>
#include <vector>
// Depending on the platform, double may be faster than float.
// #define float double
class Raytracer
{
public:
#include "_raytracr/color.hpp"
protected:
#include "_raytracr/matrix.hpp"
#include "_raytracr/vector3d.hpp"
#include "_raytracr/matrix3d.hpp"
#include "_raytracr/plane3d.hpp"
#include "_raytracr/sphere3d.hpp"
#include "_raytracr/ray3d.hpp"
#include "_raytracr/hit3d.hpp"
#include "_raytracr/object.hpp"
#include "_raytracr/triangle.hpp"
#include "_raytracr/plane.hpp"
#include "_raytracr/sphere.hpp"
#include "_raytracr/light.hpp"
#include "_raytracr/camera.hpp"
protected:
typedef std::vector< std::shared_ptr< CObject > > OBJECTVECTOR;
typedef std::vector< std::shared_ptr< CLight > > LIGHTVECTOR;
typedef std::vector< CColor > PIXELVECTOR;
typedef bool (*PROGRESSCB)(const float nR, const float nG, const float nB, const int nX, const int nY, void* lpContext);
protected:
static const float PI;
#ifndef FLD_MAX
#define FLD_MAX 1e37f
#endif /* FLD_MAX */
protected:
int m_nImageW;
int m_nImageH;
unsigned int m_uBounceDepth;
unsigned int m_uAntiAliasing;
CVector3d m_ovecEye;
CVector3d m_rvecLookAt;
CVector3d m_rvecUp;
PIXELVECTOR m_aclrPixels;
OBJECTVECTOR m_aobjObjects;
LIGHTVECTOR m_aobjLights;
PROGRESSCB m_lpfnProgress;
public:
Raytracer()
: m_nImageW(0)
, m_nImageH(0)
, m_uBounceDepth(0)
, m_uAntiAliasing(0)
, m_ovecEye(0.0f, 0.0f, 0.0f)
, m_rvecLookAt(0.0f, 0.0f, 0.0f)
, m_rvecUp(0.0f, 0.0f, 0.0f)
, m_lpfnProgress(nullptr)
{
}
template< typename T > void AddObject(const T& objObject)
{
m_aobjObjects.emplace_back(new T(objObject));
}
template< typename T > void AddLight(const T& objLight)
{
m_aobjLights.emplace_back(new T(objLight));
}
void SetImageSize(const int nImageW, const int nImageH)
{
m_nImageW = nImageW;
m_nImageH = nImageH;
}
void SetBounceDepth(const unsigned int uBounceDepth)
{//HACK: Should be defined by the object material's reflectivity
m_uBounceDepth = uBounceDepth;
}
void SetAntiAliasing(const unsigned int uLevel)
{
m_uAntiAliasing = uLevel;
}
void SetCamera(const CVector3d& ovecEye, const CVector3d& rvecLookAt, const CVector3d& rvecUp)
{
m_ovecEye = ovecEye;
m_rvecLookAt = rvecLookAt;
m_rvecUp = rvecUp;
}
void SetCallback(PROGRESSCB lpfnProgress)
{
m_lpfnProgress = lpfnProgress;
}
CHit3d HitTest(const CRay3d& Ray, const float nDistanceMin, const float nDistanceMax, const bool bSingleHit = false, const unsigned int uDepth = 0)
{
CHit3d ResultHit;
for(size_t uIdx = 0; uIdx<m_aobjObjects.size(); uIdx++)
{
const CObject& objObject = *m_aobjObjects[uIdx];
const CHit3d Hit = objObject.HitTest(Ray, nDistanceMin, nDistanceMax);
if(Hit && (!ResultHit || ResultHit.GetDistance()>Hit.GetDistance()))
{
if(bSingleHit)
{
return Hit;
}
ResultHit = Hit;
}
}
if(ResultHit)
{
CColor clrLight(0, 0, 0); // shadow
const CVector3d ovecHitPoint = Ray.GetPointAt(ResultHit.GetDistance());
for(size_t uIdx = 0; uIdx<m_aobjLights.size(); uIdx++)
{
const CLight& Light = *m_aobjLights[uIdx];
const CVector3d rvecDirLight = Light.GetOrigin()-ovecHitPoint;
if(rvecDirLight.Magnitude()>Light.GetRange())
{// this light has no effect
continue;
}
const CRay3d ShadowRay(ovecHitPoint, rvecDirLight.UnitVector());
const CHit3d ShadowHit = HitTest(ShadowRay, 0.001f, rvecDirLight.Magnitude(), true);
if(!ShadowHit)
{
clrLight+= Light.GetAttenuatedColor(rvecDirLight.Magnitude());
}
}
if(uDepth>0U)
{
const CHit3d SecondaryHit = HitTest(CRay3d(ovecHitPoint, CPlane3d(ovecHitPoint, ResultHit.GetSurfaceNormal()).Reflection(Ray.GetLookAt()).UnitVector()), 0.001f, FLD_MAX, false, uDepth-1U);
if(SecondaryHit)
{
clrLight = clrLight+SecondaryHit.GetColor()/4.0f;
}
}
ResultHit = CHit3d(ResultHit.GetDistance(), clrLight-~ResultHit.GetColor(), ResultHit.GetSurfaceNormal());
}
return ResultHit;
}
CColor RenderColorAt(const CCamera& Cam, const int nImageX, const int nImageY)
{
CColor clrHit(0, 0, 0);
if(m_uAntiAliasing)
{
const float nPoints = m_uAntiAliasing+1.0f;
const float nInc = 2.0f/float(m_uAntiAliasing);
float nT = -1.0f;
for(unsigned int uIdx = 0; uIdx<=m_uAntiAliasing; uIdx++)
{
const float nSubX = cos((nPoints)*nT);
const float nSubY = sin((nPoints-1.0f)*nT);
clrHit+= HitTest(Cam.GetRay(nImageX, nImageY, nSubX, nSubY), 0.0f, FLD_MAX, false, m_uBounceDepth).GetColor();
nT+= nInc;
}
clrHit/= nPoints;
}
else
{
clrHit = HitTest(Cam.GetRay(nImageX, nImageY), 0.0f, FLD_MAX, false, m_uBounceDepth).GetColor();
}
return clrHit;
}
void Render(void* const lpContext = nullptr)
{
CCamera Cam(m_ovecEye, m_rvecLookAt, m_rvecUp, CCamera::TYPE_PERSPECTIVE, 45.0f, m_nImageW, m_nImageH);
// initialize pixel buffer
m_aclrPixels.assign(m_nImageW*m_nImageH, CColor(0.0f, 0.0f, 0.0f));
for(int nImageY = 0; nImageY<m_nImageH; nImageY++)
{
for(int nImageX = 0; nImageX<m_nImageW; nImageX++)
{
const CColor clrPixel = RenderColorAt(Cam, nImageX, nImageY);
m_aclrPixels[m_nImageW*nImageY+nImageX] = clrPixel;
if(m_lpfnProgress!=nullptr && !m_lpfnProgress(clrPixel.R(), clrPixel.G(), clrPixel.B(), nImageX, nImageY, lpContext))
{
break;
}
}
}
}
const PIXELVECTOR& GetResult() const
{
return m_aclrPixels;
}
int GetImageWidth() const
{
return m_nImageW;
}
int GetImageHeight() const
{
return m_nImageH;
}
static CTriangle CreateTriangle(const CVector3d& ovecA, const CVector3d& ovecB, const CVector3d& ovecC, const CColor& clrColor)
{
return CTriangle(ovecA, ovecB, ovecC, clrColor);
}
static CPlane CreatePlane(const CVector3d& ovecOrigin, const CVector3d& rvecNormal, const CColor& clrColor)
{
return CPlane(ovecOrigin, rvecNormal, clrColor);
}
static CSphere CreateSphere(const CVector3d& ovecOrigin, const float nRadius, const CColor& clrColor)
{
return CSphere(ovecOrigin, nRadius, clrColor);
}
static CLight CreateLight(const CVector3d& ovecOrigin, const CColor& clrColor, const float nRange, const float nAtt0 = 1.0f, const float nAtt1 = 0.1f, const float nAtt2 = 0.01f)
{
return CLight(ovecOrigin, clrColor, nRange, nAtt0, nAtt1, nAtt2);
}
static CVector3d CreateVector3d(const float nX, const float nY, const float nZ)
{
return CVector3d(nX, nY, nZ);
}
static CMatrix3d CreateMatrix3d()
{
return CMatrix3d();
}
static CColor CreateColor(const float nR, const float nG, const float nB)
{
return CColor(nR, nG, nB);
}
};
const float Raytracer::PI = acos(0.0f)*2.0f;
#endif /* SNIPPETS_RAYTRACER_HPP */