-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
189 lines (148 loc) · 5.78 KB
/
Camera.cpp
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
#include "Camera.h"
#include "Scene.h"
#include <climits>
bool intersectsWithSphere(Ray lightray)
{
RayHitInfo hitinfo;
hitinfo.golgeflag = 1;
for(vector<Sphere>::iterator it_sph = CurrentScene->_spheres.begin(); it_sph!=CurrentScene->_spheres.end(); ++it_sph)
{
if ((*it_sph).Intersect(lightray, hitinfo))
return true;
}
return false;
}
bool intersectsWithMesh(Ray lightray)
{
RayHitInfo hitinfo;
hitinfo.golgeflag = 1;
for(vector<Mesh>::iterator it_mesh = CurrentScene->_meshes.begin(); it_mesh!=CurrentScene->_meshes.end(); ++it_mesh)
{
for(vector<Triangle>::iterator it_tri = (*it_mesh).triangles.begin(); it_tri!=(*it_mesh).triangles.end(); ++it_tri)
{
if ((*it_tri).Intersect(lightray, hitinfo))
return true;
}
}
return false;
}
inline Color computeColor(RayHitInfo& hitinfo, Vector3 v)
{
float totallight_R = 0.0;
float totallight_G = 0.0;
float totallight_B = 0.0;
//lighttan noktaya distance.
// normal (N -unitvector)
Vector3 _N = hitinfo.Normal;
// kamera-nokta (V -unitvector)
Vector3 _V = v;
// if (_N*_V < 0) {_N = _N * -1;}
totallight_R += CurrentScene->_materials[hitinfo.material_id-1].ambient.R() * CurrentScene->_ambient.R();
totallight_G += CurrentScene->_materials[hitinfo.material_id-1].ambient.G() * CurrentScene->_ambient.G();
totallight_B += CurrentScene->_materials[hitinfo.material_id-1].ambient.B() * CurrentScene->_ambient.B();
for (const auto& mylight : CurrentScene->Lights())
{
// distance between light and hitpos
float dlight = vectorDistance(mylight.light_position,hitinfo.Position);
Ray lightray(hitinfo.Position+(mylight.light_position-hitinfo.Position)*0.0001, mylight.light_position-hitinfo.Position);
if (intersectsWithMesh(lightray) || intersectsWithSphere(lightray))
{
continue;
}
float dlight4314 = 12.56*dlight*dlight;
// light-nokta (L -unitvector)
Vector3 _L = normalize(mylight.light_position-hitinfo.Position);
// H (Phong Yansıması) (V+L -unitvector)
Vector3 _H = normalize(_V+_L);
// Intensity = int_katsayisi / 4*pi*dkare
float __Ir = mylight.light_intensity.R() / dlight4314;
float __Ig = mylight.light_intensity.G() / dlight4314;
float __Ib = mylight.light_intensity.B() / dlight4314;
// diffuse coefficients (r,g,b)
float __kdr = CurrentScene->_materials[hitinfo.material_id-1].diffuse.R();
float __kdg = CurrentScene->_materials[hitinfo.material_id-1].diffuse.G();
float __kdb = CurrentScene->_materials[hitinfo.material_id-1].diffuse.B();
/* Lambertian = diffuse coef * Intensity * (N.L) */
float NL = _N*_L;
totallight_R += __kdr * __Ir * max(0.0f, NL);
totallight_G += __kdg * __Ig * max(0.0f, NL);
totallight_B += __kdb * __Ib * max(0.0f, NL);
/* Phong = specular coef * Intensity * (N.H)^spec */
totallight_R += CurrentScene->_materials[hitinfo.material_id-1].specular.R() * __Ir * pow(max(0.0f,(_N*_H)),CurrentScene->_materials[hitinfo.material_id-1].specexp);
totallight_G += CurrentScene->_materials[hitinfo.material_id-1].specular.G() * __Ig * pow(max(0.0f,(_N*_H)),CurrentScene->_materials[hitinfo.material_id-1].specexp);
totallight_B += CurrentScene->_materials[hitinfo.material_id-1].specular.B() * __Ib * pow(max(0.0f,(_N*_H)),CurrentScene->_materials[hitinfo.material_id-1].specexp);
}
Color totallight(totallight_R, totallight_G, totallight_B);
return totallight;
}
Color rayTrace(Ray& cameraray, int refl_count)
{
RayHitInfo hitinfo;
hitinfo.golgeflag = 0;
hitinfo.Parameter = LONG_MAX;
for(vector<Sphere>::iterator it_sph = CurrentScene->_spheres.begin(); it_sph!=CurrentScene->_spheres.end(); ++it_sph)
{
(*it_sph).Intersect(cameraray, hitinfo);
}
for(vector<Mesh>::iterator it_mesh = CurrentScene->_meshes.begin(); it_mesh!=CurrentScene->_meshes.end(); ++it_mesh)
{
(*it_mesh).Intersect(cameraray, hitinfo);
}
if (hitinfo.Parameter < LONG_MAX)
{
Vector3 d = normalize(cameraray.Direction());
if (refl_count==0 || (CurrentScene->_materials[hitinfo.material_id-1].reflectance.R() == 0 &&
CurrentScene->_materials[hitinfo.material_id-1].reflectance.G() == 0 &&
CurrentScene->_materials[hitinfo.material_id-1].reflectance.B() == 0) )
{
return computeColor(hitinfo, d*(-1));
}
else
{
Vector3 reflvec = normalize(d-hitinfo.Normal*2*(d*hitinfo.Normal));
Ray recursiveray(hitinfo.Position + (reflvec)*(sinus(hitinfo.Normal, d)/70), reflvec);
return computeColor(hitinfo,d*(-1)) + CurrentScene->_materials[hitinfo.material_id-1].reflectance*rayTrace(recursiveray, refl_count-1);
}
}
else
{
// cout << "B";
return CurrentScene->_background;
}
}
Image Camera::Render() const
{
// width = width, height = height
int width = _imagePlane.Width;
int height = _imagePlane.Height;
// r,l,t,b
float r = _imagePlane.Right;
float l = _imagePlane.Left;
float t = _imagePlane.Top;
float b = _imagePlane.Bottom;
float u,v;
int refl_count = CurrentScene->_rayReflect;
Image img(width, height, CurrentScene->_background);
// direction = -d.w + Right.u + Up.v
float tbheight = (t-b)/height;
float rlwidth = (r-l)/width;
u = l + (r-l)*(0.5)/width;
for (int i=0; i<width; i++)
{
v = b + (t-b)*(0.5)/height;
for (int j=0; j<height; j++)
{
//-------------------------------------------
Vector3 direction = (_space.Forward * _imagePlane.Distance) + (_space.Right * u) + (_space.Up * v);
Ray myray(_position, direction);
//*********************RAYTRACE BEGINS
Color totallight = rayTrace(myray, CurrentScene->_rayReflect);
img.Pixel(height-1-j,i)._channels[0] = totallight.R();
img.Pixel(height-1-j,i)._channels[1] = totallight.G();
img.Pixel(height-1-j,i)._channels[2] = totallight.B();
v = v + tbheight;
}
u = u + rlwidth;
}
return img;
}