rendering_system.cpp 42.9 KB
Newer Older
Georg Schaefer's avatar
Georg Schaefer committed
1
2
#include <stdexcept>

3
#include <glm/gtc/matrix_inverse.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
4
#include <glm/gtc/matrix_transform.hpp>
5
#include <glm/gtc/random.hpp>
6
#include <glm/gtx/compatibility.hpp>
7
#include <glm/gtx/string_cast.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
8
9

#include <core/engine.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
10
#include <ecs/entity.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
11
12
13
#include <graphics/graphics_system.hpp>
#include <graphics/shader.hpp>
#include <graphics/shader_manager.hpp>
14
#include <graphics/texture_manager.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
15
#include <rendering/camera_component.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
16
#include <rendering/directional_light_component.hpp>
17
#include <rendering/light_attributes.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
18
#include <rendering/light_component.hpp>
19
#include <rendering/instanced_staticmesh_component.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
20
21
22
#include <rendering/mesh.hpp>
#include <rendering/mesh_vertex.hpp>
#include <rendering/rendering_system.hpp>
23
#include <rendering/screen_quad.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
24
#include <rendering/shadow_component.hpp>
25
#include <rendering/star_vertex.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
26
27
#include <rendering/staticmesh_component.hpp>
#include <util/component_helper.hpp>
28
#include <util/config.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
29
#include <util/logger.hpp>
30
#include <util/make_unique.hpp>
Georg Schaefer's avatar
Georg Schaefer committed
31
32
33

namespace gdw {
	rendering_system::rendering_system(engine& engine)
Georg Schaefer's avatar
Georg Schaefer committed
34
	: engine_(engine), mesh_manager_(engine), active_camera_(0), debug_mode_(false), point_light_instance_data_(GL_DYNAMIC_DRAW),
35
	shadow_resolution_(256),simple_render_vbo_(GL_STATIC_DRAW),simple_render_element_size_(0), baked_(true) {
Georg Schaefer's avatar
Georg Schaefer committed
36
37
38
39
40
41
		staticmesh_layout_.emplace_back("_position", 3, GL_FLOAT, false, sizeof(mesh_vertex), offsetof(mesh_vertex, position));
		staticmesh_layout_.emplace_back("_texcoord", 2, GL_FLOAT, false, sizeof(mesh_vertex), offsetof(mesh_vertex, texcoord));
		staticmesh_layout_.emplace_back("_normal", 3, GL_FLOAT, false, sizeof(mesh_vertex), offsetof(mesh_vertex, normal));
		staticmesh_layout_.emplace_back("_tangent", 3, GL_FLOAT, false, sizeof(mesh_vertex), offsetof(mesh_vertex, tangent));

		auto& graphics_system = engine_.graphics_system();
42
43
44
45

		auto width = graphics_system.width();
		auto height = graphics_system.height();

Georg Schaefer's avatar
Georg Schaefer committed
46
47
48
49
50
51
52
53
54
55
56
57
		auto& shader_manager = graphics_system.shader_manager();

		auto vertex_shader = shader_manager.load("shader/staticmesh.vs", GL_VERTEX_SHADER);
		if (!vertex_shader) {
			throw std::runtime_error("could not load shader shader/staticmesh.vs");
		}
		staticmesh_program_.attach_shader(vertex_shader);
		auto fragment_shader = shader_manager.load("shader/staticmesh.fs", GL_FRAGMENT_SHADER);
		if (!fragment_shader) {
			throw std::runtime_error("could not load shader shader/staticmesh.fs");
		}
		staticmesh_program_.attach_shader(fragment_shader);
58
59
60
		staticmesh_layout_.setup_program(staticmesh_program_, "albedo_color");
		staticmesh_program_.bind_frag_data_location("normal_color", 1);
		staticmesh_program_.bind_frag_data_location("specular_color", 2);
Georg Schaefer's avatar
Georg Schaefer committed
61
		staticmesh_program_.link();
62

63
64
65
		auto config = engine_.config_manager().load("config/settings");
		if (config) {
			debug_mode_ = config->get<bool>("debug_render", false);
Georg Schaefer's avatar
Georg Schaefer committed
66
			shadow_resolution_ = config->get<size_t>("shadow_resolution", 256);
67
			shadows_ = config->get<bool>("shadows", true);
68
69
70
71
		}

		ortho_projection_ = glm::ortho(0.f, static_cast<float>(width), 0.f, static_cast<float>(height));

72
		g_buffer_.attach(GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, GL_DEPTH_COMPONENT32F, width, height, GL_DEPTH_COMPONENT, GL_FLOAT);
73
74
75
		g_buffer_.attach(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, GL_RGBA32F, width, height, GL_RGBA, GL_FLOAT);
		g_buffer_.attach(GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, GL_RGBA32F, width, height, GL_RGBA, GL_FLOAT);
		g_buffer_.attach(GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, GL_RGBA32F, width, height, GL_RGBA, GL_FLOAT);
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

		g_buffer_.bind();
		glDrawBuffers(4, buf_);
		g_buffer_.bind_default();

		vertex_shader = shader_manager.load("shader/screen_quad.vs", GL_VERTEX_SHADER);
		if (!vertex_shader) {
			throw std::runtime_error("could not load shader/screen_quad.vs");
		}
		screen_quad_program_.attach_shader(vertex_shader);
		fragment_shader = shader_manager.load("shader/screen_quad.fs", GL_FRAGMENT_SHADER);
		if (!fragment_shader) {
			throw std::runtime_error("could not load shader/screen_quad.fs");
		}
		screen_quad_program_.attach_shader(fragment_shader);
		staticmesh_layout_.setup_program(screen_quad_program_, "frag_color");
		screen_quad_program_.link();

		for (auto i = 0; i < 4; ++i) {
			debug_screen_quads_.emplace_back(make_unique<screen_quad>(
				staticmesh_layout_,
				glm::vec2((i * width / 4.f) + 0.01f * width, (1 * height / 4.f) + 0.01f * height),
				glm::vec2(((i + 1) * width / 4.f) - 0.01f * width, 0.01f * height)
			));
		}
101
102
103
104
105
106
107
108
109
110

		light_volume_layout_.emplace_back("_position", 3, GL_FLOAT, GL_FALSE, sizeof(mesh_vertex), offsetof(mesh_vertex, position));
		light_volume_layout_.emplace_back("_texcoord", 2, GL_FLOAT, GL_FALSE, sizeof(mesh_vertex), offsetof(mesh_vertex, texcoord));
		light_volume_layout_.emplace_back("_normal", 3, GL_FLOAT, GL_FALSE, sizeof(mesh_vertex), offsetof(mesh_vertex, normal));
		light_volume_layout_.emplace_back("_tangent", 3, GL_FLOAT, GL_FALSE, sizeof(mesh_vertex), offsetof(mesh_vertex, tangent));
		light_volume_layout_.emplace_back("position", 3, GL_FLOAT, GL_FALSE, sizeof(light_attributes), offsetof(light_attributes, position), 1, 1);
		light_volume_layout_.emplace_back("color", 3, GL_FLOAT, GL_FALSE, sizeof(light_attributes), offsetof(light_attributes, color), 1, 1);
		light_volume_layout_.emplace_back("radius", 1, GL_FLOAT, GL_FALSE, sizeof(light_attributes), offsetof(light_attributes, radius), 1, 1);
		light_volume_layout_.emplace_back("exponent", 1, GL_FLOAT, GL_FALSE, sizeof(light_attributes), offsetof(light_attributes, exponent), 1, 1);

Georg Schaefer's avatar
Georg Schaefer committed
111
		point_light_volume_ = mesh_manager_.load("mesh/light_volume.msh", staticmesh_layout_);
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
		if (!point_light_volume_) {
			throw std::runtime_error("could not load mesh/light_volume.msh");
		}

		const vertex_buffer* light_buffer[2] = {
			&point_light_volume_->vbo(),
			&point_light_instance_data_
		};
		light_volume_layout_.setup_layout(point_light_volume_->vao(), light_buffer);

		vertex_shader = shader_manager.load("shader/point_light.vs", GL_VERTEX_SHADER);
		if (!vertex_shader) {
			throw std::runtime_error("could not load point_light.vs");
		}
		point_light_program_.attach_shader(vertex_shader);
		fragment_shader = shader_manager.load("shader/point_light.fs", GL_FRAGMENT_SHADER);
		if (!fragment_shader) {
			throw std::runtime_error("could not load point_light.fs");
		}
		point_light_program_.attach_shader(fragment_shader);
		light_volume_layout_.setup_program(point_light_program_, "frag_color");
		point_light_program_.link();
Georg Schaefer's avatar
Georg Schaefer committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

		screen_quad_ = make_unique<screen_quad>(staticmesh_layout_, glm::vec2(0.f), glm::vec2(width, height));

		vertex_shader = shader_manager.load("shader/directional_light.vs", GL_VERTEX_SHADER);
		if (!vertex_shader) {
			throw std::runtime_error("could not load shader/directional_light.vs");
		}
		directional_light_program_.attach_shader(vertex_shader);
		fragment_shader = shader_manager.load("shader/directional_light.fs", GL_FRAGMENT_SHADER);
		if (!fragment_shader) {
			throw std::runtime_error("could not load shader/directional_light.fs");
		}
		directional_light_program_.attach_shader(fragment_shader);
		light_volume_layout_.setup_program(directional_light_program_, "frag_color");
		directional_light_program_.link();
Georg Schaefer's avatar
Georg Schaefer committed
149

150
151
152
153
154
155
156
		composition_buffer_.attach(GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, GL_DEPTH_COMPONENT32F, width, height, GL_DEPTH_COMPONENT, GL_FLOAT);
		composition_buffer_.attach(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, GL_RGBA32F, width, height, GL_RGBA, GL_FLOAT);
		const GLenum shadow_buffers[2] = { GL_COLOR_ATTACHMENT0, GL_NONE };
		composition_buffer_.bind();
		glDrawBuffers(2, shadow_buffers);
		framebuffer::bind_default();

Georg Schaefer's avatar
Georg Schaefer committed
157
		shadow_map_.attach(GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, GL_DEPTH_COMPONENT32F, shadow_resolution_, shadow_resolution_, GL_DEPTH_COMPONENT, GL_FLOAT);
158
		shadow_map_.attach(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, GL_RG32F, shadow_resolution_, shadow_resolution_, GL_RG, GL_FLOAT);
Georg Schaefer's avatar
Georg Schaefer committed
159
160
161
162
163
164
165
		shadow_map_.attachment(GL_COLOR_ATTACHMENT0).bind(0);
		glGenerateMipmap(GL_TEXTURE_2D);
		shadow_map_.attachment(GL_COLOR_ATTACHMENT0).apply_settings();
		shadow_map_.bind();
		glDrawBuffers(2, shadow_buffers);
		shadow_map_.bind_default();

166
167
168
169
170
171
172
173
174
		baked_shadow_map_.attach(GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, GL_DEPTH_COMPONENT32F, shadow_resolution_, shadow_resolution_, GL_DEPTH_COMPONENT, GL_FLOAT);
		baked_shadow_map_.attach(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, GL_RG32F, shadow_resolution_, shadow_resolution_, GL_RG, GL_FLOAT);
		baked_shadow_map_.attachment(GL_COLOR_ATTACHMENT0).bind(0);
		glGenerateMipmap(GL_TEXTURE_2D);
		baked_shadow_map_.attachment(GL_COLOR_ATTACHMENT0).apply_settings();
		baked_shadow_map_.bind();
		glDrawBuffers(2, shadow_buffers);
		baked_shadow_map_.bind_default();

Georg Schaefer's avatar
Georg Schaefer committed
175
176
177
178
179
180
181
182
183
184
185
186
		vertex_shader = shader_manager.load("shader/staticmesh.vs", GL_VERTEX_SHADER);
		if (!vertex_shader) {
			throw std::runtime_error("could not load shader/staticmesh.vs");
		}
		shadow_staticmesh_program_.attach_shader(vertex_shader);
		fragment_shader = shader_manager.load("shader/shadow.fs", GL_FRAGMENT_SHADER);
		if (!fragment_shader) {
			throw std::runtime_error("could not load shader/shadow.fs");
		}
		shadow_staticmesh_program_.attach_shader(fragment_shader);
		staticmesh_layout_.setup_program(shadow_staticmesh_program_, "frag_color");
		shadow_staticmesh_program_.link();
Georg Schaefer's avatar
Georg Schaefer committed
187
188

		shadow_map_blured_.attach(GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, GL_DEPTH_COMPONENT32F, shadow_resolution_, shadow_resolution_, GL_DEPTH_COMPONENT, GL_FLOAT);
189
		shadow_map_blured_.attach(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, GL_RG32F, shadow_resolution_, shadow_resolution_, GL_RG, GL_FLOAT);
Georg Schaefer's avatar
Georg Schaefer committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
		glGenerateMipmap(GL_TEXTURE_2D);
		shadow_map_.attachment(GL_COLOR_ATTACHMENT0).apply_settings();
		shadow_map_blured_.bind();
		glDrawBuffers(2, shadow_buffers);
		shadow_map_blured_.bind_default();

		vertex_shader = shader_manager.load("shader/screen_quad.vs", GL_VERTEX_SHADER);
		if (!vertex_shader) {
			throw std::runtime_error("could not load shader/creen_quad.vs");
		}
		shadow_blur_program_.attach_shader(vertex_shader);
		fragment_shader = shader_manager.load("shader/gaussian_blur.fs", GL_FRAGMENT_SHADER);
		if (!fragment_shader) {
			throw std::runtime_error("could not load gaussian_blur.fs");
		}
		shadow_blur_program_.attach_shader(fragment_shader);
		staticmesh_layout_.setup_program(shadow_blur_program_, "frag_color");
		shadow_blur_program_.link();
Jerry's avatar
Jerry committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226


		//simple meshrendering
		auto stride = sizeof(float)*3+sizeof(float)*4;
		simple_render_layout_.emplace_back("_position", 3, GL_FLOAT, false, stride , 0);
		simple_render_layout_.emplace_back("_col", 4, GL_FLOAT, false, stride , sizeof(float)*3);


		vertex_shader = shader_manager.load("shader/simple_shader.vs", GL_VERTEX_SHADER);
		if (!vertex_shader) {
			throw std::runtime_error("could not load shader/simple_shader.vs");
		}
		simple_render_program_.attach_shader(vertex_shader);
		fragment_shader = shader_manager.load("shader/simple_shader.fs", GL_FRAGMENT_SHADER);
		if (!fragment_shader) {
			throw std::runtime_error("could not load shader/simple_shader.fs");
		}
		simple_render_program_.attach_shader(fragment_shader);
		simple_render_layout_.setup_program(simple_render_program_,"_color");
Jerry's avatar
Jerry committed
227
		simple_render_layout_.setup_layout(simple_render_vao_,&simple_render_vbo_);
Jerry's avatar
Jerry committed
228
		simple_render_program_.link();
229

Georg Schaefer's avatar
Georg Schaefer committed
230
231
232
233
		instance_layout_.emplace_back("_position", 3, GL_FLOAT, false, sizeof(mesh_vertex), offsetof(mesh_vertex, position));
		instance_layout_.emplace_back("_texcoord", 2, GL_FLOAT, false, sizeof(mesh_vertex), offsetof(mesh_vertex, texcoord));
		instance_layout_.emplace_back("_normal", 3, GL_FLOAT, false, sizeof(mesh_vertex), offsetof(mesh_vertex, normal));
		instance_layout_.emplace_back("_tangent", 3, GL_FLOAT, false, sizeof(mesh_vertex), offsetof(mesh_vertex, tangent));
Georg Schaefer's avatar
Georg Schaefer committed
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
		instance_layout_.emplace_back("model", 4, GL_FLOAT, false, sizeof(instance_attributes), offsetof(instance_attributes, model), sizeof(glm::mat4), 4, 1, 1);
		instance_layout_.emplace_back("mit", 4, GL_FLOAT, false, sizeof(instance_attributes), offsetof(instance_attributes, mit), sizeof(glm::mat4), 4, 1, 1);

		vertex_shader = shader_manager.load("shader/instanced_staticmesh.vs", GL_VERTEX_SHADER);
		if (!vertex_shader) {
			throw std::runtime_error("could not load shader/instanced_staticmesh.vs");
		}
		instanced_staticmesh_program_.attach_shader(vertex_shader);
		fragment_shader = shader_manager.load("shader/staticmesh.fs", GL_FRAGMENT_SHADER);
		if (!fragment_shader) {
			throw std::runtime_error("could not load shader/staticmesh.fs");
		}
		instanced_staticmesh_program_.attach_shader(fragment_shader);
		instance_layout_.setup_program(instanced_staticmesh_program_, "frag_color");
		instanced_staticmesh_program_.link();

		vertex_shader = shader_manager.load("shader/instanced_staticmesh.vs", GL_VERTEX_SHADER);
		if (!vertex_shader) {
			throw std::runtime_error("could not load shader/instanced_staticmesh.vs");
		}
		shadow_instanced_staticmesh_program_.attach_shader(vertex_shader);
		fragment_shader = shader_manager.load("shader/shadow.fs", GL_FRAGMENT_SHADER);
		if (!fragment_shader) {
			throw std::runtime_error("could not load shader/shadow.fs");
		}
		shadow_instanced_staticmesh_program_.attach_shader(fragment_shader);
		instance_layout_.setup_program(shadow_instanced_staticmesh_program_, "frag_color");
		shadow_instanced_staticmesh_program_.link();
Georg Schaefer's avatar
Georg Schaefer committed
262
263
264
265
266
267

		traktor_beam_ = mesh_manager_.load("mesh/beam.msh", staticmesh_layout_);
		if (!traktor_beam_) {
			throw std::runtime_error("could not load mesh/beam.msh");
		}

268
269
270
271
272
		traktor_beam_stripes_ = mesh_manager_.load("mesh/beam_stripes.msh", staticmesh_layout_);
		if (!traktor_beam_stripes_) {
			throw std::runtime_error("could not load mesh/beam_stripes.msh");
		}

Georg Schaefer's avatar
Georg Schaefer committed
273
274
275
276
277
278
279
280
281
282
		vertex_shader = shader_manager.load("shader/beam.vs", GL_VERTEX_SHADER);
		if (!vertex_shader) {
			throw std::runtime_error("could not load shader/beam.vs");
		}
		traktor_beam_program_.attach_shader(vertex_shader);
		fragment_shader = shader_manager.load("shader/beam.fs", GL_FRAGMENT_SHADER);
		if (!fragment_shader) {
			throw std::runtime_error("could not load shader/beam.fs");
		}
		traktor_beam_program_.attach_shader(fragment_shader);
283
		staticmesh_layout_.setup_program(traktor_beam_program_, "frag_color");
Georg Schaefer's avatar
Georg Schaefer committed
284
		traktor_beam_program_.link();
285

286
287
288
289
		starfield_ = mesh_manager_.load("mesh/starfield.msh", staticmesh_layout_);
		if (!starfield_) {
			throw std::runtime_error("could not load mesh/starfield.msh");
		}
290
291
292
293
294
295
296
297
298
299
300

		vertex_shader = shader_manager.load("shader/starfield.vs", GL_VERTEX_SHADER);
		if (!vertex_shader) {
			throw std::runtime_error("could not load shader/starfield.vs");
		}
		starfield_program_.attach_shader(vertex_shader);
		fragment_shader = shader_manager.load("shader/starfield.fs", GL_FRAGMENT_SHADER);
		if (!fragment_shader) {
			throw std::runtime_error("could not load shader/starfield.fs");
		}
		starfield_program_.attach_shader(fragment_shader);
301
		staticmesh_layout_.setup_program(starfield_program_, "frag_color");
302
303
		starfield_program_.link();

Georg Schaefer's avatar
Georg Schaefer committed
304
305
306
307
308
309
310
		planet_ = mesh_manager_.load("mesh/planet.msh", staticmesh_layout_);
		if (!planet_) {
			throw std::runtime_error("could not load mesh/planet.msh");
		}

		planet_model_ = glm::translate(glm::mat4(1.f), glm::vec3(80.f, -50.f, -100.f));
		planet_model_ *= glm::scale(glm::mat4(1.f), glm::vec3(100.f));
311
312
		athmosphere_model_ = glm::translate(glm::mat4(1.f), glm::vec3(80.f, -50.f, -100.f));
		athmosphere_model_ *= glm::scale(glm::mat4(1.f), glm::vec3(102.5f));
Georg Schaefer's avatar
Georg Schaefer committed
313
314
315
316
317
318
319
320
321
322
323
324
325

		vertex_shader = shader_manager.load("shader/planet.vs", GL_VERTEX_SHADER);
		if (!vertex_shader) {
			throw std::runtime_error("could not load shader/planet.vs");
		}
		planet_program_.attach_shader(vertex_shader);
		fragment_shader = shader_manager.load("shader/planet.fs", GL_FRAGMENT_SHADER);
		if (!fragment_shader) {
			throw std::runtime_error("could not load shader/planet.fs");
		}
		planet_program_.attach_shader(fragment_shader);
		staticmesh_layout_.setup_program(planet_program_, "frag_color");
		planet_program_.link();
326
327
328
329
330
331
332
333
334
335
336
337
338

		vertex_shader = shader_manager.load("shader/athmosphere.vs", GL_VERTEX_SHADER);
		if (!vertex_shader) {
			throw std::runtime_error("could not load shader/athmosphere.vs");
		}
		athmosphere_program_.attach_shader(vertex_shader);
		fragment_shader = shader_manager.load("shader/athmosphere.fs", GL_FRAGMENT_SHADER);
		if (!fragment_shader) {
			throw std::runtime_error("could not load shader/athmosphere.fs");
		}
		athmosphere_program_.attach_shader(fragment_shader);
		staticmesh_layout_.setup_program(athmosphere_program_, "frag_color");
		athmosphere_program_.link();
Georg Schaefer's avatar
Georg Schaefer committed
339
340
341
	}

	void rendering_system::update(float delta_time) {
Georg Schaefer's avatar
Georg Schaefer committed
342
343
344
345
346
347
		auto camera = active_camera();
		if (!camera) {
			return;
		}

		render_gbuffer(delta_time, *camera);
348
		render_lights(*camera);
Georg Schaefer's avatar
Georg Schaefer committed
349
		render_starfield(delta_time, *camera);
350
		render_planet(*camera);
351
		render_non_opauqe_geometry(delta_time, *camera);
352
		render_final_image();
353
		if(engine_.getConfig().get("navDebug",true))
354
			render_navMesh(*camera);
355
356
357
358

		if (debug_mode_) {
			render_debug_screen_quads(*camera);
		}
Georg Schaefer's avatar
Georg Schaefer committed
359
	}
Georg Schaefer's avatar
Georg Schaefer committed
360

Georg Schaefer's avatar
Georg Schaefer committed
361
	void rendering_system::render_gbuffer(float delta_time, camera_component& camera) {
362
363
364
		g_buffer_.bind();
		glClearColor(0.f, 0.f, 0.f, 0.f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Georg Schaefer's avatar
Georg Schaefer committed
365
		glEnable(GL_DEPTH_TEST);
Georg Schaefer's avatar
Georg Schaefer committed
366

Georg Schaefer's avatar
Georg Schaefer committed
367
368
369
		auto projection = camera.projection();
		auto view = camera.view();

Georg Schaefer's avatar
Georg Schaefer committed
370
		staticmesh_program_.use();
Georg Schaefer's avatar
Georg Schaefer committed
371
372
		staticmesh_program_.uniform("projection", false, projection);
		staticmesh_program_.uniform("view", false, view);
373
		for (auto& sm : staticmesh_components_) {
Georg Schaefer's avatar
Georg Schaefer committed
374
			staticmesh_program_.uniform("model", false, sm->owner().transform());
375
			staticmesh_program_.uniform("mit", false, glm::inverseTranspose(sm->owner().transform()));
Georg Schaefer's avatar
Georg Schaefer committed
376
			sm->mesh().draw(staticmesh_program_);
Georg Schaefer's avatar
Georg Schaefer committed
377
378
		}

379
		for (auto& sm : staticmesh_components_with_baked_shadows_) {
Georg Schaefer's avatar
Georg Schaefer committed
380
			staticmesh_program_.uniform("model", false, sm->owner().transform());
381
			staticmesh_program_.uniform("mit", false, glm::inverseTranspose(sm->owner().transform()));
Georg Schaefer's avatar
Georg Schaefer committed
382
			sm->mesh().draw(staticmesh_program_);
Georg Schaefer's avatar
Georg Schaefer committed
383
384
		}

Georg Schaefer's avatar
Georg Schaefer committed
385
		instanced_staticmesh_program_.use();
Georg Schaefer's avatar
Georg Schaefer committed
386
387
		instanced_staticmesh_program_.uniform("projection", false, projection);
		instanced_staticmesh_program_.uniform("view", false, view);
388
		for (auto& ism : instanced_staticmesh_components_) {
Georg Schaefer's avatar
Georg Schaefer committed
389
390
391
392
393
394
395
			if (ism.second.update_per_frame || !ism.second.updated) {
				auto& instance_data = ism.second.instance_data;
				instance_data.clear();

				for (auto& instance : ism.second.components) {
					auto& owner = instance->owner();
					instance_attributes attr {
Georg Schaefer's avatar
Georg Schaefer committed
396
397
						owner.transform(),
						glm::inverseTranspose(owner.transform())
Georg Schaefer's avatar
Georg Schaefer committed
398
399
400
401
402
403
404
405
406
					};
					instance_data.emplace_back(attr);
				}

				ism.second.instance_buffer.bind();
				ism.second.instance_buffer.data(instance_data.size() * sizeof(instance_attributes), instance_data.data());
				ism.second.updated = true;
			}

Georg Schaefer's avatar
Georg Schaefer committed
407
408
409
			auto& components = ism.second.components;
			if (!components.empty()) {
				components.front()->mesh().draw_instanced(instanced_staticmesh_program_, components.size());
410
			}
Georg Schaefer's avatar
Georg Schaefer committed
411
412
		}

Georg Schaefer's avatar
Georg Schaefer committed
413
		glDisable(GL_DEPTH_TEST);
414
415
416
		g_buffer_.bind_default();
	}

Jerry's avatar
Jerry committed
417
	void rendering_system::render_navMesh(camera_component& camera) {
Jerry's avatar
Jerry committed
418
419
420
		//g_buffer_.bind();
		framebuffer::bind_default();
		//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Jerry's avatar
Jerry committed
421
		//glDisable(GL_CULL_FACE);
Jerry's avatar
Jerry committed
422
		//glCullFace(GL_NONE);
Jerry's avatar
Jerry committed
423
424
425
426
427
428
		glEnable(GL_DEPTH_TEST);


		auto projection = camera.projection();
		auto view = camera.view();

Jerry's avatar
Jerry committed
429
430
		//simple_render_vbo_.bind();
		simple_render_vao_.bind();
Jerry's avatar
Jerry committed
431
432
433
		simple_render_program_.use();
		simple_render_program_.uniform("projection", false, projection);
		simple_render_program_.uniform("view", false, view);
Jerry's avatar
Jerry committed
434
435
		//glDrawArrays(GL_TRIANGLES,0,simple_render_element_size_);
		glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
Jerry's avatar
Jerry committed
436
		glDrawArrays(GL_TRIANGLES,0,simple_render_element_size_);
Jerry's avatar
Jerry committed
437
		glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
Jerry's avatar
Jerry committed
438
439
440

		glDisable(GL_DEPTH_TEST);
		//simple_render_vbo_.bind_default();
Jerry's avatar
Jerry committed
441
		//g_buffer_.bind_default();
Jerry's avatar
Jerry committed
442
443
	}

444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
	void rendering_system::render_debug_screen_quads(camera_component& camera) {
		auto z_near = camera.z_near();
		auto z_far = camera.z_far();

		screen_quad_program_.use();
		screen_quad_program_.uniform("projection", false, ortho_projection_);
		screen_quad_program_.uniform("z_near", z_near);
		screen_quad_program_.uniform("z_far", z_far);
		screen_quad_program_.uniform("albedo_texture", 0);

		for (auto i = 0; i < 4; ++i) {
			g_buffer_.attachment(attachments_[i]).bind(0);

			screen_quad_program_.uniform("linearize", false);
			if (attachments_[i] == GL_DEPTH_ATTACHMENT) {
				screen_quad_program_.uniform("linearize", true);
			}

			debug_screen_quads_[i]->draw();
		}
Georg Schaefer's avatar
Georg Schaefer committed
464
	}
Georg Schaefer's avatar
Georg Schaefer committed
465

466
	void rendering_system::render_lights(camera_component& camera) {
467
468
		composition_buffer_.bind();
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Georg Schaefer's avatar
Georg Schaefer committed
469
470
		render_directional_lights(camera);

471
472
473
474
475
476
477
478
479
480
481
482
		glEnable(GL_BLEND);
		glBlendEquation(GL_FUNC_ADD);
		glBlendFunc(GL_ONE, GL_ONE);

		glCullFace(GL_FRONT);
		glEnable(GL_CULL_FACE);

		render_point_lights(camera);

		glDisable(GL_CULL_FACE);

		glDisable(GL_BLEND);
483
484
485
486
487
488
489

		auto width = static_cast<float>(engine_.graphics_system().width());
		auto height = static_cast<float>(engine_.graphics_system().height());

		g_buffer_.bind_read();
		composition_buffer_.bind_draw();
		glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
490
491
	}

Georg Schaefer's avatar
Georg Schaefer committed
492
	void rendering_system::render_directional_lights(camera_component& camera) {
Georg Schaefer's avatar
Georg Schaefer committed
493
494
495
496
		for (auto i = 0; i < 4; ++i) {
			g_buffer_.attachment(attachments_[i]).bind(i);
		}

Georg Schaefer's avatar
Georg Schaefer committed
497
498
499
500
501
502
		auto inv_view_projection = glm::inverse(camera.projection_view());

		auto width = static_cast<float>(engine_.graphics_system().width());
		auto height = static_cast<float>(engine_.graphics_system().height());

		for (auto& dl : directional_light_components_) {
Georg Schaefer's avatar
Georg Schaefer committed
503
			auto sc = dl->owner().component<shadow_component>();
504
			if (shadows_ && sc) {
Georg Schaefer's avatar
Georg Schaefer committed
505
				render_shadow_map(*sc);
Georg Schaefer's avatar
Georg Schaefer committed
506
				blur_shadow_map();
Georg Schaefer's avatar
Georg Schaefer committed
507
508
509
510
511
512
				directional_light_program_.use();
				auto shadow_projection = sc->projection();
				auto shadow_view = glm::lookAt(dl->owner().position(), glm::vec3(0.f), glm::vec3(0.f, 1.f, 0.f));
				directional_light_program_.uniform("shadow_casting", true);
				directional_light_program_.uniform("shadow_projection", false, shadow_projection);
				directional_light_program_.uniform("shadow_view", false, shadow_view);
Georg Schaefer's avatar
Georg Schaefer committed
513
			} else {
Georg Schaefer's avatar
Georg Schaefer committed
514
515
516
517
518
519
520
521
				directional_light_program_.use();
				directional_light_program_.uniform("shadow_casting", false);
			}

			for (auto i = 0; i < 4; ++i) {
				g_buffer_.attachment(attachments_[i]).bind(i);
			}

522
523
			composition_buffer_.bind();

Georg Schaefer's avatar
Georg Schaefer committed
524
525
526
527
528
529
530
531
532
533
			directional_light_program_.uniform("albedo_texture", 0);
			directional_light_program_.uniform("normal_texture", 1);
			directional_light_program_.uniform("specular_texture", 2);
			directional_light_program_.uniform("depth_texture", 3);
			directional_light_program_.uniform("shadow_texture", 4);
			directional_light_program_.uniform("projection", false, ortho_projection_);
			directional_light_program_.uniform("inv_view_projection", false, inv_view_projection);
			directional_light_program_.uniform("view_vector", camera.owner().position());
			directional_light_program_.uniform("ambient_term", 0.1f);
			directional_light_program_.uniform("resolution", glm::vec2(width, height));
Georg Schaefer's avatar
Georg Schaefer committed
534
535
536
537
538
539
540
541
542

			directional_light_program_.uniform("directional_light_direction", dl->owner().position());
			directional_light_program_.uniform("directional_light_color", dl->color());
			directional_light_program_.uniform("directional_light_energy", dl->energy());

			screen_quad_->draw();
		}
	}

543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
	void rendering_system::render_point_lights(camera_component& camera) {
		auto inv_view_projection = glm::inverse(camera.projection_view());

		std::vector<light_attributes> instance_data;
		for (auto& pl : light_components_) {
			auto scale = glm::scale(glm::mat4(1.f), glm::vec3(2.f * pl->radius()));
			auto rotation = glm::toMat4(camera.owner().rotation());
			auto translation = glm::translate(glm::mat4(1.f), pl->owner().position());
			auto model_matrix = translation * rotation * scale;
			instance_data.emplace_back(
				light_attributes{
					pl->owner().position(),
					pl->color(),
					pl->radius(),
					pl->exponent()
				}
			);
		}
		point_light_instance_data_.bind();
		point_light_instance_data_.data(instance_data.size() * sizeof(light_attributes), instance_data.data());

		auto width = static_cast<float>(engine_.graphics_system().width());
		auto height = static_cast<float>(engine_.graphics_system().height());

		point_light_program_.use();
		point_light_program_.uniform("albedo_texture", 0);
		point_light_program_.uniform("normal_texture", 1);
		point_light_program_.uniform("specular_texture", 2);
		point_light_program_.uniform("depth_texture", 3);
		point_light_program_.uniform("inv_view_projection", false, inv_view_projection);
		point_light_program_.uniform("view_projection", false, camera.projection_view());
		point_light_program_.uniform("view_vector", camera.owner().position());
		point_light_program_.uniform("resolution", glm::vec2(width, height));
		point_light_volume_->vao().bind();
		glDrawElementsInstanced(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0, light_components_.size());
	}

580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
	void rendering_system::render_baked_shadow_map(shadow_component& shadow) {
		auto& owner = shadow.owner();
		auto width = engine_.graphics_system().width();
		auto height = engine_.graphics_system().height();

		glEnable(GL_DEPTH_TEST);
		glEnable(GL_DEPTH_CLAMP);
		glFrontFace(GL_CCW);
		glCullFace(GL_BACK);
		glEnable(GL_CULL_FACE);
		glViewport(0, 0, shadow_resolution_, shadow_resolution_);

		auto projection = shadow.projection();
		auto view = glm::lookAt(owner.position(), glm::vec3(0.f), glm::vec3(0.f, 1.f, 0.f));

		shadow_staticmesh_program_.use();
		shadow_staticmesh_program_.uniform("projection", false, projection);
		shadow_staticmesh_program_.uniform("view", false, view);
		shadow_staticmesh_program_.uniform("mit", false, glm::mat4(1.f));
		for (auto& s : staticmesh_components_with_baked_shadows_) {
			auto& owner = s->owner();
			auto model = owner.transform();
			shadow_staticmesh_program_.uniform("model", false, model);
			s->mesh().draw(shadow_staticmesh_program_);
		}

606
		baked_shadow_map_.attachment(GL_COLOR_ATTACHMENT0).bind(4);
607
608
609
610
611
612
613
		glGenerateMipmap(GL_TEXTURE_2D);

		glDisable(GL_DEPTH_TEST);
		glDisable(GL_CULL_FACE);
		glViewport(0, 0, width, height);
	}

Georg Schaefer's avatar
Georg Schaefer committed
614
615
616
617
618
619
620
621
622
623
624
625
	void rendering_system::render_shadow_map(shadow_component& shadow)  {
		auto& owner = shadow.owner();
		auto width = engine_.graphics_system().width();
		auto height = engine_.graphics_system().height();

		glEnable(GL_DEPTH_TEST);
		glEnable(GL_DEPTH_CLAMP);
		glFrontFace(GL_CCW);
		glCullFace(GL_BACK);
		glEnable(GL_CULL_FACE);
		glViewport(0, 0, shadow_resolution_, shadow_resolution_);

626
627
628
		baked_shadow_map_.bind_read();
		shadow_map_.bind_draw();
		glBlitFramebuffer(0, 0, shadow_resolution_, shadow_resolution_, 0, 0, shadow_resolution_, shadow_resolution_, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST);
Georg Schaefer's avatar
Georg Schaefer committed
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
		shadow_map_.bind();

		auto projection = shadow.projection();
		auto view = glm::lookAt(owner.position(), glm::vec3(0.f), glm::vec3(0.f, 1.f, 0.f));

		shadow_staticmesh_program_.use();
		shadow_staticmesh_program_.uniform("projection", false, projection);
		shadow_staticmesh_program_.uniform("view", false, view);
		shadow_staticmesh_program_.uniform("mit", false, glm::mat4(1.f));
		for (auto& s : staticmesh_components_) {
			auto& owner = s->owner();
			auto model = owner.transform();
			shadow_staticmesh_program_.uniform("model", false, model);
			s->mesh().draw(shadow_staticmesh_program_);
		}

Georg Schaefer's avatar
Georg Schaefer committed
645
646
647
648
649
650
		shadow_instanced_staticmesh_program_.use();
		shadow_instanced_staticmesh_program_.uniform("projection", false, projection);
		shadow_instanced_staticmesh_program_.uniform("view", false, view);
		for (auto& ism : instanced_staticmesh_components_) {
			auto& components = ism.second.components;
			if (!components.empty()) {
Georg Schaefer's avatar
Georg Schaefer committed
651
				components.front()->mesh().draw_instanced(shadow_instanced_staticmesh_program_, components.size());
Georg Schaefer's avatar
Georg Schaefer committed
652
653
			}
		}
Georg Schaefer's avatar
Georg Schaefer committed
654
655
656
657
658
659
660
661
662

		shadow_map_.attachment(GL_COLOR_ATTACHMENT0).bind(4);
		glGenerateMipmap(GL_TEXTURE_2D);

		glDisable(GL_DEPTH_TEST);
		glDisable(GL_CULL_FACE);
		glViewport(0, 0, width, height);
	}

Georg Schaefer's avatar
Georg Schaefer committed
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
	void rendering_system::blur_shadow_map() {
		auto width = engine_.graphics_system().width();
		auto height = engine_.graphics_system().height();

		glViewport(0, 0, shadow_resolution_, shadow_resolution_);
		shadow_map_blured_.bind();
		glClear(GL_COLOR_BUFFER_BIT);

		shadow_blur_program_.use();
		shadow_blur_program_.uniform("projection", false, ortho_projection_);
		shadow_blur_program_.uniform("shadow_texture", 0);
		shadow_blur_program_.uniform("blur_scale", glm::vec2(1.f / shadow_resolution_, 0.f));
		shadow_map_.attachment(GL_COLOR_ATTACHMENT0).bind(0);
		screen_quad_->draw();

		shadow_map_.attachment(GL_COLOR_ATTACHMENT0).bind(0);
		glGenerateMipmap(GL_TEXTURE_2D);

		shadow_map_.bind();
		glClear(GL_COLOR_BUFFER_BIT);

		shadow_blur_program_.use();
		shadow_blur_program_.uniform("projection", false, ortho_projection_);
		shadow_blur_program_.uniform("shadow_texture", 0);
		shadow_blur_program_.uniform("blur_scale", glm::vec2(0.f, 1.f / shadow_resolution_));
		shadow_map_blured_.attachment(GL_COLOR_ATTACHMENT0).bind(0);
		screen_quad_->draw();

		shadow_map_blured_.attachment(GL_COLOR_ATTACHMENT0).bind(0);
		glGenerateMipmap(GL_TEXTURE_2D);

		glViewport(0, 0, width, height);
	}

Jerry's avatar
Jerry committed
697
	void rendering_system::set_render_navMesh(std::vector<float> vertinfo){
Jerry's avatar
Jerry committed
698
		/*for(int i =0; i < vertinfo.size(); i+=7 ){
Jerry's avatar
Jerry committed
699
			log << "vertpos(" <<  vertinfo[i] << "/" << vertinfo[i+1] << "/" << vertinfo[i+2] << ")" << std::endl;
Jerry's avatar
Jerry committed
700
701
			log << "vertcol(" <<  vertinfo[i+3] << "/" << vertinfo[i+4] << "/" << vertinfo[i+5] << "/" << vertinfo[i+6] << ")" << std::endl;
		}*/
Jerry's avatar
Jerry committed
702

Jerry's avatar
Jerry committed
703
		simple_render_vbo_.bind();
Jerry's avatar
Jerry committed
704
		simple_render_vbo_.data(vertinfo.size()* sizeof(float),vertinfo.data(),GL_STATIC_DRAW);
Jerry's avatar
Jerry committed
705
		simple_render_element_size_ = vertinfo.size()/7;
Jerry's avatar
Jerry committed
706
707
	}

708
	void rendering_system::render_non_opauqe_geometry(float delta_time, camera_component& camera) {
709
710
711
		auto width = engine_.graphics_system().width();
		auto height = engine_.graphics_system().height();

712
		composition_buffer_.bind();
713
714
715

		glEnable(GL_DEPTH_TEST);
		glDepthMask(GL_FALSE);
716
717
		glEnable(GL_CULL_FACE);
		glCullFace(GL_FRONT);
718
719
720
721
722
723
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

		auto projection = camera.projection();
		auto view = camera.view();

724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
		static float accumulated_time = 0.f;
		accumulated_time += delta_time;
		if (accumulated_time / 2.f >= 4.f) {
			accumulated_time = 0.f;
		}

		static float accumulated_time_stripes = 0.f;
		accumulated_time_stripes += delta_time;
		if (accumulated_time_stripes >= 4.f) {
			accumulated_time_stripes = 0.f;
		}

		traktor_beam_program_.use();
		traktor_beam_program_.uniform("projection", false, projection);
		traktor_beam_program_.uniform("view", false, view);

		for (auto& beam : tractor_beams_) {
741
			float sign = 1.f;
742
			if (beam.invert_beam) {
743
744
				sign = -1.f;
			}
745
			glCullFace(GL_FRONT);
746

747
748
			traktor_beam_program_.uniform("model", false, beam.beam_model);
			traktor_beam_program_.uniform("beam_length", beam.beam_scale);
749
			traktor_beam_program_.uniform("offset", sign * accumulated_time / 2.f);
750
			traktor_beam_program_.uniform("albedo_texture", 0);
751
752
			traktor_beam_program_.uniform("color", beam.color);
			traktor_beam_program_.uniform("invert", beam.invert_beam);
753
754
			traktor_beam_->draw(traktor_beam_program_);

755
756
			glCullFace(GL_BACK);

757
			traktor_beam_program_.uniform("offset", sign * accumulated_time / 2.f);
758
759
			traktor_beam_->draw(traktor_beam_program_);

760
761
762
763
764
765
766
767
768
769
			glCullFace(GL_FRONT);

			traktor_beam_program_.use();
			traktor_beam_program_.uniform("offset", sign * accumulated_time_stripes);
			traktor_beam_stripes_->draw(traktor_beam_program_);

			glCullFace(GL_BACK);

			traktor_beam_program_.uniform("offset", sign * accumulated_time_stripes);
			traktor_beam_stripes_->draw(traktor_beam_program_);
770
		}
771
		tractor_beams_.clear();
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790

		glDisable(GL_BLEND);
		glDepthMask(GL_TRUE);
		glDisable(GL_DEPTH_TEST);
		glDisable(GL_CULL_FACE);
	}

	void rendering_system::render_final_image() {
		framebuffer::bind_default();
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		screen_quad_program_.use();
		screen_quad_program_.uniform("projection", false, ortho_projection_);
		screen_quad_program_.uniform("albedo_texture", 0);
		screen_quad_program_.uniform("linearize", false);
		composition_buffer_.attachment(GL_COLOR_ATTACHMENT0).bind(0);
		screen_quad_->draw();
	}

Georg Schaefer's avatar
Georg Schaefer committed
791
792
793
794
795
796
797
798
799
800
801
802
803
804
	void rendering_system::active_camera(unsigned long long id) {
		auto it = camera_components_.find(id);
		if (it != camera_components_.end()) {
			active_camera_ = id;
		}
	}
	camera_component* rendering_system::active_camera() noexcept {
		auto it = camera_components_.find(active_camera_);
		if (it != camera_components_.end()) {
			return it->second;
		}
		return nullptr;
	}

Georg Schaefer's avatar
Georg Schaefer committed
805
806
807
808
809
810
811
	void rendering_system::update_instance_per_frame(unsigned long long id, bool per_frame) {
		auto it = instanced_staticmesh_components_.find(id);
		if (it == instanced_staticmesh_components_.end()) {
			instanced_staticmesh_components_[id].update_per_frame = per_frame;
		}
	}

812
	void rendering_system::rebake_shadows() {
813
814
815
816
817
818
819
820
		baked_shadow_map_.bind();
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		if (!staticmesh_components_with_baked_shadows_.empty()) {
			render_baked_shadow_map(*shadow_components_.front());
		}

		framebuffer::bind_default();
821
822
	}

823
824
825
826
827
828
829
830
831
832
833
	void rendering_system::render_trakor_beam(const glm::vec3& position, const glm::quat& rotation, float length, float radius, bool invert, const glm::vec3& color) {
		tractor_beam beam;
		beam.beam_scale = radius / length * 4.f;
		beam.beam_length = length;
		beam.beam_model = glm::mat4(1.f);
		beam.beam_model *= glm::translate(glm::mat4(1.f), position);
		beam.beam_model *= glm::toMat4(rotation);
		beam.beam_model *= glm::scale(glm::mat4(1.f), glm::vec3(radius, radius, length));
		beam.invert_beam = invert;
		beam.color = color;
		tractor_beams_.emplace_back(beam);
Georg Schaefer's avatar
Georg Schaefer committed
834
835
	}

Georg Schaefer's avatar
Georg Schaefer committed
836
	void rendering_system::render_starfield(float delta_time, camera_component& camera) {
837
		const static auto model = glm::scale(glm::mat4(1.f), glm::vec3(500.f));
Georg Schaefer's avatar
Georg Schaefer committed
838
839
840
841
842
843
844
845
846
847
848
849
850
		const static auto width = static_cast<float>(engine_.graphics_system().width());
		const static auto height = static_cast<float>(engine_.graphics_system().height());

		static auto origin = glm::vec3(width / 2.f, height / 2.f, -100.f);
		origin.x += delta_time / 1000.f;

		static auto alpha = 0.f;
		alpha += delta_time / 100.f;
		if (alpha >= 2.f * glm::pi<float>()) {
			alpha = 0.f;
		}
		auto rotation = glm::mat2(glm::cos(alpha), -glm::sin(alpha), glm::sin(alpha), glm::cos(alpha));

851
852
853
854
855
856

		composition_buffer_.bind();

		glEnable(GL_DEPTH_TEST);
		glEnable(GL_CULL_FACE);
		glCullFace(GL_BACK);
Georg Schaefer's avatar
Georg Schaefer committed
857

858
859
860
861
		glEnable(GL_DEPTH_TEST);
		starfield_program_.use();
		starfield_program_.uniform("projection", false, camera.projection());
		starfield_program_.uniform("view", false, camera.view());
Georg Schaefer's avatar
Georg Schaefer committed
862
		starfield_program_.uniform("model", false, model);
Georg Schaefer's avatar
Georg Schaefer committed
863
864
865
866
867
		starfield_program_.uniform("resolution", glm::vec2(width, height));
		starfield_program_.uniform("inverse_resolution", 1.f / glm::vec2(width, height));
		starfield_program_.uniform("origin", origin);
		starfield_program_.uniform("rotation", false, rotation);
		starfield_program_.uniform("zoom", 1.f);
868
869
870
		starfield_->draw(starfield_program_);

		glDisable(GL_CULL_FACE);
871
872
873
		glDisable(GL_DEPTH_TEST);
	}

Georg Schaefer's avatar
Georg Schaefer committed
874
	void rendering_system::render_planet(camera_component& camera) {
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
		if (directional_light_components_.empty()) {
			return;
		}

		static const auto wavelength = glm::vec3(0.65f, 0.57f, 0.475f);
		static const auto wavelength_high_four = glm::vec3(glm::pow(wavelength.r, 4.f), glm::pow(wavelength.g, 4.f), glm::pow(wavelength.b, 4.f));
		static const auto inner_radius = 100.f;
		static const auto outer_radius = 102.5f;
		static const auto sun_energy = 20.f;
		static const auto rayleigh_constant = 0.0025f;
		static const auto rayleigh_times_energy = rayleigh_constant * sun_energy;
		static const auto rayleigh_times_four_pi = rayleigh_constant *  4.f * glm::pi<float>();
		static const auto mie_constant = 0.001f;
		static const auto mie_times_energy = mie_constant * sun_energy;
		static const auto mie_times_four_pi = mie_constant *  4.f * glm::pi<float>();
		static const auto scale = 1.f / (outer_radius - inner_radius);
		static const auto scale_depth = 0.25f;
892
		static const auto g = -0.99f;
893
894
		auto camera_position = camera.owner().position();

Georg Schaefer's avatar
Georg Schaefer committed
895
896
897
898
899
900
901
902
903
904
		composition_buffer_.bind();

		glEnable(GL_DEPTH_TEST);
		glEnable(GL_CULL_FACE);
		glCullFace(GL_BACK);

		planet_program_.use();
		planet_program_.uniform("projection", false, camera.projection());
		planet_program_.uniform("view", false, camera.view());
		planet_program_.uniform("model", false, planet_model_);
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
		planet_program_.uniform("camera_position", 405.f * glm::normalize(camera_position));
		planet_program_.uniform("light_direction", directional_light_components_.front()->owner().position());
		planet_program_.uniform("inverse_wave_length", 1.f / wavelength_high_four);
		planet_program_.uniform("camera_height", glm::length(camera_position));
		planet_program_.uniform("camera_height_squared", glm::length2(camera_position));
		planet_program_.uniform("outer_radius", outer_radius);
		planet_program_.uniform("outer_radius_squared", outer_radius * outer_radius);
		planet_program_.uniform("inner_radius", inner_radius);
		planet_program_.uniform("inner_radius_sqaured", inner_radius * inner_radius);
		planet_program_.uniform("rayleigh_times_energy", rayleigh_times_energy);
		planet_program_.uniform("mie_times_energy", mie_times_energy);
		planet_program_.uniform("rayleigh_times_four_pi", rayleigh_times_four_pi);
		planet_program_.uniform("mie_times_four_pi", mie_times_four_pi);
		planet_program_.uniform("scale", scale);
		planet_program_.uniform("scale_depth", scale_depth);
		planet_program_.uniform("scale_over_scale_depth", scale / scale_depth);
Georg Schaefer's avatar
Georg Schaefer committed
921
922
923
		planet_program_.uniform("albedo_texture", 0);
		planet_->draw(planet_program_);

924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE);
		glCullFace(GL_FRONT);

		athmosphere_program_.use();
		athmosphere_program_.uniform("projection", false, camera.projection());
		athmosphere_program_.uniform("view", false, camera.view());
		athmosphere_program_.uniform("model", false, athmosphere_model_);
		athmosphere_program_.uniform("camera_position", 405.f * glm::normalize(camera_position));
		athmosphere_program_.uniform("light_direction", directional_light_components_.front()->owner().position());
		athmosphere_program_.uniform("inverse_wave_length", 1.f / wavelength_high_four);
		athmosphere_program_.uniform("camera_height", glm::length(camera_position));
		athmosphere_program_.uniform("camera_height_squared", glm::length2(camera_position));
		athmosphere_program_.uniform("outer_radius", outer_radius);
		athmosphere_program_.uniform("outer_radius_squared", outer_radius * outer_radius);
		athmosphere_program_.uniform("inner_radius", inner_radius);
		athmosphere_program_.uniform("inner_radius_sqaured", inner_radius * inner_radius);
		athmosphere_program_.uniform("rayleigh_times_energy", rayleigh_times_energy);
		athmosphere_program_.uniform("mie_times_energy", mie_times_energy);
		athmosphere_program_.uniform("rayleigh_times_four_pi", rayleigh_times_four_pi);
		athmosphere_program_.uniform("mie_times_four_pi", mie_times_four_pi);
		athmosphere_program_.uniform("scale", scale);
		athmosphere_program_.uniform("scale_depth", scale_depth);
		athmosphere_program_.uniform("scale_over_scale_depth", scale / scale_depth);
		athmosphere_program_.uniform("g", g);
		athmosphere_program_.uniform("g_squared", g * g);
		planet_->draw(athmosphere_program_);

		glCullFace(GL_BACK);

		athmosphere_program_.use();
		athmosphere_program_.uniform("projection", false, camera.projection());
		athmosphere_program_.uniform("view", false, camera.view());
		athmosphere_program_.uniform("model", false, athmosphere_model_);
		athmosphere_program_.uniform("camera_position", 405.f * glm::normalize(camera_position));
		athmosphere_program_.uniform("light_direction", directional_light_components_.front()->owner().position());
		athmosphere_program_.uniform("inverse_wave_length", 1.f / wavelength_high_four);
		athmosphere_program_.uniform("camera_height", glm::length(camera_position));
		athmosphere_program_.uniform("camera_height_squared", glm::length2(camera_position));
		athmosphere_program_.uniform("outer_radius", outer_radius);
		athmosphere_program_.uniform("outer_radius_squared", outer_radius * outer_radius);
		athmosphere_program_.uniform("inner_radius", inner_radius);
		athmosphere_program_.uniform("inner_radius_sqaured", inner_radius * inner_radius);
		athmosphere_program_.uniform("rayleigh_times_energy", rayleigh_times_energy);
		athmosphere_program_.uniform("mie_times_energy", mie_times_energy);
		athmosphere_program_.uniform("rayleigh_times_four_pi", rayleigh_times_four_pi);
		athmosphere_program_.uniform("mie_times_four_pi", mie_times_four_pi);
		athmosphere_program_.uniform("scale", scale);
		athmosphere_program_.uniform("scale_depth", scale_depth);
		athmosphere_program_.uniform("scale_over_scale_depth", scale / scale_depth);
		athmosphere_program_.uniform("g", g);
		athmosphere_program_.uniform("g_squared", g * g);
		planet_->draw(athmosphere_program_);

		glDisable(GL_BLEND);
Georg Schaefer's avatar
Georg Schaefer committed
979
980
981
982
		glDisable(GL_DEPTH_TEST);
		glDisable(GL_CULL_FACE);
	}

Georg Schaefer's avatar
Georg Schaefer committed
983
984
985
986
987
988
989
990
991
992
993
	void rendering_system::register_component(camera_component* component) {
		camera_components_.emplace(std::make_pair(component->owner().id(), component));
	}

	void rendering_system::unregister_component(camera_component* component) {
		auto it = camera_components_.find(component->owner().id());
		if (it != camera_components_.end()) {
			camera_components_.erase(it);
		}
	}

Georg Schaefer's avatar
Georg Schaefer committed
994
995
996
997
998
999
1000
	void rendering_system::register_component(directional_light_component* component) {
		directional_light_components_.emplace_back(component);
	}

	void rendering_system::unregister_component(directional_light_component* component) {
		remove_component(directional_light_components_, component);
	}