-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
310 lines (262 loc) · 8.73 KB
/
main.cpp
File metadata and controls
310 lines (262 loc) · 8.73 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <cmath>
#include <iostream>
#include <math.h>
#include "Camera.h"
#include "vec3.h"
#include "OrthographicCam.h"
#include "LightIntensity.h"
#include "Renderer.h"
#include "ray.h"
#include "sphere.h"
#include "mat3x3.h"
#include "triangle.h"
#include "plane.h"
#include "objectParser.h"
#include "PerspectiveCam.h"
#include "Light/Light.h"
#include "Light/DirectionalLight.h"
#include "Light/PointLight.h"
using namespace std;
using namespace math;
using namespace display;
using namespace lighting;
int main() {
/*
//Zadanie 1 i 2
vec3 v1(0, 2, 3);
vec3 v2(4, 5, 6);
vec3 v3 = v1.add(v2);
vec3 v4 = v2.add(v1);
cout <<"naprzemienność wektorow:" << endl;
cout<< "dodawanie v1 + v2: " << v3.x << " " << v3.y << " " << v3.z << endl;
cout<< "dodawanie v2 + v1: " << v4.x << " " << v4.y << " " << v4.z << endl;
cout << endl;
cout << "--------------------------------" << endl;
//Zadanie 3 - kąt między wektorami
vec3 v5(0, 3, 0);
vec3 v6(5, 5, 0);
float dotv5v6 = v5.dotProduct(v6);
dotv5v6 = dotv5v6 / (v5.length() * v6.length());
float angle = acos(dotv5v6) * 180 / M_PI;
cout << "Kat miedzy wektorami: " << angle << endl;
cout << endl;
cout << "--------------------------------" << endl;
//Zadanie 4 - Wektor prostopadły
vec3 v7(4, 5, 1);
vec3 v8(4, 1, 3);
vec3 crossv7v8 = v7.crossProduct(v8);
cout << "Wektor prostopadly do podanych wektorow: " << crossv7v8.x << " " << crossv7v8.y << " " << crossv7v8.z << endl;
cout << endl;
cout << "--------------------------------" << endl;
//Zadanie 5 - Normalizacja wektora
vec3 normalized = crossv7v8.normalize();
cout << "Normalizacja wektora: " << normalized.x << " " << normalized.y << " " << normalized.z << endl;
cout << endl;
cout << "--------------------------------" << endl;
//Zadanie 6 i 7 - Sfera
vec3 center(0, 0, 0);
sphere s(center, 10);
//Zadanie 8 - Promien r1
vec3 r1o(0,0,-20);
vec3 dirr1 = center.subtract(r1o).normalize();
ray r1(r1o, dirr1);
math::vec3 ress = r1o.crossProduct(dirr1);
//Zadanie 9 - Promien r2 rownolegle do osi y
vec3 dirr2(0,1,0);
ray r2(r1o, dirr2);
//Zadanie 10 i 11 - Przeciecie sfery z promieniami r1 i r2
cout << "r1: " << endl;
bool intersection = s.intersection(r1, 0, 10);
cout << "r2: " << endl;
bool intersection2 = s.intersection(r2, 0, 1000);
cout << endl;
cout << "--------------------------------" << endl;
//Zadanie 12 - Promien R3 przecinajacy w jednym punkcie
vec3 dirr3(0,0,1);
vec3 r3o (0,10,-20);
ray r3(r3o, dirr3);
cout << "r3 przeciecie: " << endl;
bool intersection3 = s.intersection(r3, 0, 1000);
cout << endl;
cout << "--------------------------------" << endl;
//Zadanie 12 i 13: Płaszczyzna P i przecięcie z promieniem R2
vec3 pPoint(0, 0, 0);
vec3 pNormal(0, 1, 1);
plane P(pPoint, pNormal);
vec3 r2Origin(0, 0, -20);
vec3 r2Direction(0, 1, 0);
ray R2(r2Origin, r2Direction);
float t_plane;
bool planeHit = P.intersection(R2, 0.0f, 1000.0f, t_plane);
if (planeHit) {
vec3 interPoint = R2.pointAtParameter(t_plane);
cout << "Punkt przeciecia plaszczyzny P z promieniem R2: "
<< interPoint.x << ", " << interPoint.y << ", " << interPoint.z << endl;
} else {
cout << "Brak przeciecia plaszczyzny P z promieniem R2" << endl;
}
cout << "--------------------------------" << endl;
//Zadanie 15: Sprawdzenie przecięcia linii z trójkątem
vec3 A(0, 0, 0);
vec3 B(1, 0, 0);
vec3 C(0, 1, 0);
triangle tri(A, B, C);
vec3 interSectionPt;
// Przypadek 1: Linia przechodząca przez trójkąt.
// P1: (-1, 0.5, 0), P2: (1, 0.5, 0) -> true
vec3 P1_case1(-1, 0.5, 0);
vec3 P2_case1(1, 0.5, 0);
bool case1 = tri.intersectsLine(P1_case1, P2_case1, interSectionPt);
cout << "Przypadek 1 (linia przechodzaca przez trojkat): "
<< (case1 ? "true" : "false") << endl;
// Przypadek 2: Linia leżąca na plaszczyźnie trójkąta, ale poza nim.
// P1: (2, -1, 0), P2: (2, 2, 0) -> false
vec3 P1_case2(2, -1, 0);
vec3 P2_case2(2, 2, 0);
bool case2 = tri.intersectsLine(P1_case2, P2_case2, interSectionPt);
cout << "Przypadek 2 (linia na plaszczyznie, ale poza trojkatem): "
<< (case2 ? "true" : "false") << endl;
// Przypadek 3: Linia nieprzecinajaca trójkąta.
// P1: (0, 0, -1), P2: (0, 0, 1) -> false
vec3 P1_case3(0, 0, -1);
vec3 P2_case3(0, 0, 1);
bool case3 = tri.intersectsLine(P1_case3, P2_case3, interSectionPt);
cout << "Przypadek 3 (linia przecinajaca plaszczyzne poza trojkatem): "
<< (case3 ? "true" : "false") << endl;
//.obj parser
objectParser parser;
parser.loadObj("G:\\RayTracer\\cube.obj");
*/
//ortagonalna
OrthographicCam OrthoCam(
vec3(0, 0, 0),
vec3(0, 0, -1),
vec3(0, 1, 0),
0.1f,
1000.0f);
Material sphere1Mat (lightIntensity(0.0, 0, 0),
lightIntensity(1.0, 0.0, 0.0),
lightIntensity(0.7, 0.7, 0.7),
10,
0.0,
1.0,
false
);
Material sphere2Mat (lightIntensity(0, 0, 0.01),
lightIntensity(0.0, 0.0, 1.0),
lightIntensity(1.0, 1.0, 1.0),
5,
0.0,
1.0,
false
);
Material planeMat (lightIntensity(0, 0.01, 0),
lightIntensity(0.0, 1.0, 0.0),
lightIntensity(0.7, 0.7, 0.7),
50,
0.0,
1.0,
false
);
Material rightWall (lightIntensity(0, 0.01, 0.01),
lightIntensity(0.0, 0.3, 0.3),
lightIntensity(0.7, 1.0, 0.7),
50,
0.0,
1.0,
false
);
Material backWall (lightIntensity(0.01, 0.01, 0),
lightIntensity(0.3, 0.5, 0.7),
lightIntensity(0.7, 0.7, 0.7),
50,
0.0,
1.0,
false
);
Material glassMat(
lightIntensity(0.1f, 0.1f, 0.1f),
lightIntensity(0.2f, 0.2f, 0.2f),
lightIntensity(1.0f, 1.0f, 1.0f),
128.0,
0.1,
1.5,
true
);
Material mirrorMat(
lightIntensity(0.0, 0.0, 0.0),
lightIntensity(0.0, 0.0, 0.0),
lightIntensity(1.0, 1.0, 1.0),
256.0,
0.9,
1.0,
false
);
vec3 sphere1Center = vec3(0, 0, -30);
lightIntensity sphere1Col(0, 0, 1);
sphere s1(sphere1Center, 0.5f, sphere1Mat);
lightIntensity bgColor(0.1,0.1,0.1);
DirectionalLight light1(lightIntensity(0.7,0.7,0.7), vec3(-0.2,-0.5,-0.5));
PointLight pointLight(lightIntensity(3.0, 3.0, 3.0), vec3(3, 6, -3));
vector<primitive*> primitives;
vector<Light*> lights;
primitives.push_back(&s1);
//lights.push_back(&light1);
lights.push_back(&pointLight);
Renderer renderer(&OrthoCam, lights, primitives, &bgColor, 20);
renderer.render(300, 300);
//perspektywiczna
PerspectiveCam PerspCam(
vec3(0, 0, -1),
vec3(0, 0, -1.5),
vec3(0, 1, 0),
0.1f,
1000.0f,
90.0f);
vec3 sphere2Center = vec3(-3, 0, -10);
sphere s2(sphere2Center, 2.0f, glassMat);
vec3 sphere3Center = vec3(-1, 3, -8);
sphere s3(sphere3Center, 1.5f, mirrorMat);
vec3 sphere4Center = vec3(3, 0, -10);
sphere s4(sphere4Center, 2.0f, sphere1Mat);
vec3 sphere5Center = vec3(3, 3, -8);
sphere s5(sphere5Center, 1.0f, sphere2Mat);
//floor
vec3 p1Normal(0, 1, 0); // Normal pointing up
vec3 p1Center(0, -10, -10);
plane P(p1Center, p1Normal, 10.0f, 10.0f, planeMat);
//right wall
vec3 p2Normal(-1,0,0);
vec3 p2Center(10, 0, -10);
plane P2(p2Center, p2Normal, 10.0f, 10.0f, rightWall);
//backwall
vec3 p3Normal(0,0,1);
vec3 p3Center(0, 0, -20);
plane P3(p3Center, p3Normal, 10.0f, 10.0f, backWall);
//roof
vec3 p4Normal(0,-1,0);
vec3 p4Center(0, 10, -10);
plane P4(p4Center, p4Normal, 10.0f, 10.0f, backWall);
//leftwall
vec3 p5Normal(1,0,0);
vec3 p5Center(-10, 0, -10);
plane P5(p5Center, p5Normal, 10.0f, 10.0f, rightWall);
//frontwall
vec3 p6Normal(0,0,-1);
vec3 p6Center(0, 0, 0);
plane P6(p6Center, p6Normal, 10.0f, 10.0f, backWall);
vector<primitive*> primitivesPerspective;
primitivesPerspective.push_back(&s2);
primitivesPerspective.push_back(&s3);
primitivesPerspective.push_back(&s4);
primitivesPerspective.push_back(&s5);
primitivesPerspective.push_back(&P);
primitivesPerspective.push_back(&P2);
primitivesPerspective.push_back(&P3);
primitivesPerspective.push_back(&P4);
primitivesPerspective.push_back(&P5);
primitivesPerspective.push_back(&P6);
Renderer perspRenderer(&PerspCam,lights, primitivesPerspective, &bgColor, 10);
perspRenderer.render(500, 500);
return 0;
}