-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
152 lines (134 loc) · 5.39 KB
/
Camera.cpp
File metadata and controls
152 lines (134 loc) · 5.39 KB
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
#include "Camera.h"
#include "Scene.h"
#include <climits>
/*
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();
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);
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
{
int width = _imagePlane.Width;
int height = _imagePlane.Height;
// cout << "Renderdayim. W-H: " << width << "-" << height << endl;
Image img(width, height, CurrentScene->_background);
// cout << "OLUSTURDUGUM X Y : " << width << " " << height << endl;
/*CurrentScene->cameraTransformation(camera_id);
// cout << "camera transformation ended" << endl;
// cout << "AFTER CAMERA TRANSFORMATION !!!!!" << endl;
// CurrentScene->sceneData();
CurrentScene->cull(camera_id);
// cout << "cull ended" << endl;
CurrentScene->projectionTransformation(camera_id);
// cout << "AFTER PROJ TRANSFORMATION !!!!!" << endl;
// CurrentScene->sceneData();
// cout << "AFTER CULL!" << endl;
// CurrentScene->sceneData();
// cout << "projection transformation ended" << endl;
CurrentScene->viewportTransformation(camera_id);
// cout << "viewport transformation ended" << endl;
// cout << "###########AFTER WP TRANSFORMATION !!!!!";
// cout << "###########_________AFTER WP TRANSFORMATION !!!!!";
// CurrentScene->sceneData();
CurrentScene->rasterize(camera_id, img);
// cout << "rasterize ended" << endl;
// CurrentScene->sceneData();
*/
return img;
}