-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
329 lines (273 loc) · 8.96 KB
/
main.cpp
File metadata and controls
329 lines (273 loc) · 8.96 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//TODOs (big ones):
/*
/ 1. Collection of ready to draw 3D-primitives
/ 2. Transparency with Depth Peeling
/ 4. Physical Simulation with Transform Feedback (OpenGL Superbible Example)
/ 5. Shadow Volumes en.wikipedia.org/wiki/Shadow_volume
/ 6. Unified Particle Physics for Real-Time Applications with compute shader
/ 7. Indirect Illumination with Voxel Cone Tracing
/ 8. Heightmap generator for microstructure materials (e.g. textiles), render with normal mapping
/ 9. Include V8 and manipulate the scene at runtime using JavaScript.
*/
#include <GL/glew.h>
#include "GLFW/glfw3.h"
#include <glm.hpp>
#include <gtc/matrix_transform.hpp>
#include <gtx/rotate_vector.hpp>
#include <gtc/matrix_inverse.hpp>
#include <gtc/matrix_access.hpp>
#include <gtc/quaternion.hpp>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <stdexcept>
#include <random>
#include "config.h"
#include "ModelType.h"
#include "Shader.h"
#include "FragmentShader.h"
#include "VertexShader.h"
#include "Module.h"
#include "Model.h"
#include "Factory.h"
#include "World.h"
#include "User.h"
#include "Modules/Camera.h"
#include "Modules/VolumetricLightScatteringMitchell.h"
#include "Modules/ShadowMapping.h"
#include "Modules/SunShaftsSousa.h"
#include "Modules/OBJLoader.h"
#include "ParticipatingMediaToth.h"
int main() {
Camera cam(glm::vec3(0,0,10),
glm::vec3(0,0,0),
glm::vec3(0,1,0),
45,
glm::vec2(1920,1080),
0.1f,
800.0f);
Light light({ });
GLuint triShader = Shader::link({
VertexShader("tri.vert"),
FragmentShader("tri.frag")
});
ModelType triModel(0, GL_TRIANGLES, triShader,
{VertexAttribute("normal")}, {
Material(),
Light::Emitter()
});
ModelType pointModel(1, GL_POINTS, Shader::link({
VertexShader("point.vert"), FragmentShader("point.frag")
}), { }, {
Light::Emitter()
});
Model cube(0, &triModel, factory.cube());
for(int x = 0; x < 50; x+=5) {
for(int y = 0; y < 50; y+=5) {
cube.use()->translate(x,y,0);
}
}
Model lightsource(1, &pointModel, {{{10,10,10}}});
lightsource.emit(1, light.l(700.0f, 1.0f, 1.0f));
lightsource.use()->translateX(10)->emit(1, light.l(400, 1, 1));
World world({&cube, &lightsource}, [](Model* m){}, {});
world.extend(&cam);
world.extend(&light);
User user(&world);
((CameraInteraction*)user.use(&cam))->simple();
glClearColor(0,0,0,1);
glClearDepth(1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CW);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_POINT_SPRITE);
glEnable(GL_PROGRAM_POINT_SIZE);
while(cam.is_on()) {
cam.shoot(&world);
glfwPollEvents();
}
exit(EXIT_SUCCESS);
}
/*
int main(void) {
using namespace glm;
// Camera creates GLFW window and thus OpenGL context
Camera camera(
vec3(10,2,44),//vec3(0,3,-12),//glm::vec3(22,6,-21),//vec3(1,-16,2),//vec3(-2,2,9),
vec3(10,0,0),//glm::vec3(-1,7,6),//vec3(1,4,0), //vec3(-2,8,0 -2,5,0),
glm::vec3(0,1,0),
45,//25,
glm::vec2(800,600),
0.1f,
800.0f);
printf("* Hello.\n* This is OpenGL %s.\n* The shading language is GLSL %s.\n* Your hardware is %s.\n",
glGetString(GL_VERSION),
glGetString(GL_SHADING_LANGUAGE_VERSION),
glGetString(GL_RENDERER));
Light light({
// {{650.0f, 0.0f, 0.3f}, {-15,0,0}}
// {{650.0f, 0.0f, 0.8f}, {3,10,-60}}
});
GLuint shader1 = Shader::link({VertexShader("triangle.vert"), FragmentShader("triangle.frag")});
GLuint shader2 = Shader::link({VertexShader("point.vert"), FragmentShader("point.frag")});
ModelType type1(1, GL_TRIANGLES, shader1, {VertexAttribute("normal")}, {
Material(),
Light::Emitter()
});
ModelType type2(2, GL_POINTS, shader2, {}, {});
Model cube(1, &type1, factory.cube());
Model sun(2, &type2, {{{0, 4, 0}}}); // sun as point sprite
Model tetrahedron(3, &type1, factory.tetrahedron());
Model octahedron(4, &type1, factory.octahedron());
cube.attr(1, Material( //TODO how to get rid of the index?
0.0f, 0.0f, 0.0f,
730.0f, 1.0f, 0.1f,
0.0f, 0.0f
));
cube.sub({
//links
cube.use()->translate(-4,0,2)->units(0.2,4,0.2),
cube.use()->translate(-4,0,-2)->units(0.2,4,0.2),
cube.use()->translate(-4,0,0)->units(0.1,4,4),
//rechts
cube.use()->translate(4,0,2)->units(0.2,4,0.2),
cube.use()->translate(4,0,-2)->units(0.2,4,0.2),
cube.use()->translate(4,0,0)->units(0.1,4,4),
//hinten
cube.use()->translate(-2.25,0,-2)->units(3.5,4,0.1),//->attr(1, Material(0,0,0, 800,0,1, 0,0)),
cube.use()->translate(2.25,0,-2)->units(3.5,4,0.1),
cube.use()->translate(0,1.5,-2)->units(1,1,0.1),
//Dach
cube.use()->translate(-2.25,2,0)->units(3.5,0.1,4),
cube.use()->translate(2.25,2,0)->units(3.5,0.1,4),
cube.use()->translate(0,2,-1.25)->units(1,0.1,1.5),
//Staebe
cube.use()->translate(0,2,0)->units(1,0.05,0.05),
cube.use()->translate(0,2,0.5)->units(1,0.05,0.05),
cube.use()->translate(0,2,1)->units(1,0.05,0.05),
//Querstaebe
cube.use()->translate(-0.25,2,0.5)->units(0.05,0.05,2),
cube.use()->translate(0.25,2,0.5)->units(0.05,0.05,2),
//Zu Dach
cube.use()->translate(0,2,1.75)->units(1,0.1,0.5)
});
cube.attr(1, Material( //TODO how to get rid of the index?
0.0f, 0.0f, 0.0f,
730.0f, 0.2f, 0.5f,
0.0f, 0.0f
));
// ModelInstance* lightcube = cube.use();
// lightcube->translate(13 -4, 8, -8);
// lightcube->units(0.8f, 0.8f, 0.8f);
// lightcube->emit(2, light.l(720.0f, 0.0f, 1.0f)); //TODO light is calculated even if the model is not in the world
// cube.use()->translateZ(5)->emit(2, light.l(700.0f, 0.0f, 0.9f));
Model skyscraper(6, &type1, factory.cube());
skyscraper.translateX(16)->units(1, 6, 1)->translateY(2);
skyscraper.attr(1, Material(
0,0,0,
1000, 0, 1,
0,0
));
skyscraper.use()->translateY(4)->units(0.5, 3, 0.5);
skyscraper.use()->units(5,2,5)->translate(2,-2,2);
for(int i = 0; i < 10; i+=1) {
for(int j = 0; j < 10; j+=1) {
int min = 0, max = 40;
int r = rand()%(max-min + 1) + min;
skyscraper.use()->units(0.5,(float)r/10.0f,0.5)->translateX(6+i)->translateY(-3)->translateZ(j)->attr(1, Material(
0,0,0,
0, 1, 0.1,
0,0
));
}
}
Model skybox(7, &type1, factory.infacing_cube());
skybox.attr(1, Material(0.0695, 0.118, 0.244));
skybox.scaleX(200 18)->scaleY(100)->scaleZ(100);
Model ground(5, &type1, factory.cube());
ground.scaleX(1000)->scaleY(1)->scaleZ(1000)->translateY(-16.4);
((Material*)ground.attr(1))->reflectRGB(0.1f, 0.1f, 0.1f);
// OBJLoader obj;
// Model sibenik(0, &type1, obj.load("C:/Users/mk/Desktop/sibenik.obj"));
// sibenik.attr(1, Material(0.5f, 0.2f, 0.1f));
// Model dragon(1, &type1, obj.load("C:/Users/mk/Desktop/dragon.obj"));
// dragon.attr(1, Material(0.5f, 0.2f, 0.1f));
// dragon.scale(3)->translateY(5.3f)->translateX(15)->rotateY(-90);
Model lightcube(9, &type1, factory.cube());
// lightcube.translate(-15, 20, 60); // SIBENIK SCENE OUTDOOR
// lightcube.translate(60,20,0); // SIBENIK INDOOR
lightcube.emit(2, light.l(0.0f, 0.0f, 1.0f));
// UFO SCENE START
// Model ufo(0, &type1, obj.load("C:/Users/mk/Desktop/ufo.obj"));
// ufo.attr(1, Material(0.2f,0.2f,0.2f));
// ufo.translate(-1,5,0);
lightcube.translate(0,10,15);
ground.translateY(-1.0f)->scaleZ(0.05);
Model tower(10, &type1, factory.cube());
tower.scaleY(10)->translateZ(-20);
// Model wall(10, &type1, factory.cube());
// wall.position(-3,0,0)->units(1,10,40);
// UFO SCENE END
//UFO DRAGON SCENE START
// lightcube.translateY(20);
// ufo.translateY(5);
// dragon.scale(4)->rotateY(180);
World world({&cube}, [&](Model* m){
// if(m->id()==7) m->scaleZ(1.001);
}, {
Uniform("modelID", [](Uniform* u, Model* m) {
u->update(m->id());
})
});
world.extend(&camera);
world.extend(&light);
// VolumetricLightScatteringMitchell modLightScattering(camera.post_processor(), lightcube.position_world_space());
// world.extend(&modLightScattering);
// SunShaftsSousa modSunShafts(camera.post_processor(), lightcube.position_world_space());
// world.extend(&modSunShafts);
// ShadowMapping modShadowMapping(lightcube.position_world_space(), glm::vec3(0,0,4));
// world.extend(&modShadowMapping);
// ParticipatingMediaToth toth;
// world.extend(&toth);
User user(&world);
((CameraInteraction*) user.use(&camera))->simple();
// user.use(&modLightScattering);
// user.use(&modSunShafts);
// user.use(&toth);
//+// Point primitives with shader-specified size
glEnable(GL_POINT_SPRITE);
glEnable(GL_PROGRAM_POINT_SIZE);
//+// Background color and depth values
glClearColor(0.0695*4, 0.118*4, 0.244*4, 1.0f);
glClearDepth(1.0);
//+// Backface culling
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CW);
//+// Alpha channel and blending
//glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//+// Depth test
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
// double t[100];
// unsigned int i = 0;
// rendering loop
while(camera.is_on()) {
// double t1 = glfwGetTime();
camera.shoot(&world);
// double t2 = glfwGetTime();
// t[i%100] = t2-t1;
// i++;
// if(i%100 == 0) {
// double sum = 0.0;
// for(int j = 0; j < 100; j++) sum += t[j];
// printf("%f\n", sum/100.0);
// }
glfwPollEvents();
}
exit(EXIT_SUCCESS);
}
*/