Commit 012056a8 authored by Georg Schaefer's avatar Georg Schaefer
Browse files

Closes #31. Replace skydome with generated starfield

parent 9aec792b
#version 330
in vec3 color_;
out vec4 frag_color;
void main() {
frag_color = vec4(color_, 1.f);
}
#version 330
in vec3 _point;
in vec3 _position;
in float _size;
in vec3 _color;
out vec3 color_;
uniform mat4 projection;
uniform mat4 view;
void main() {
gl_Position = projection * view * vec4(_point + _position, 1.f);
gl_PointSize = _size;
color_ = _color;
}
......@@ -80,6 +80,13 @@ namespace gdw {
vertex_array simple_render_vao_;
int simple_render_element_size_;
vertex_layout starfield_vertex_layout_;
program starfield_program_;
vertex_buffer starfield_point_;
vertex_buffer starfield_vbo_;
vertex_array starfield_vao_;
unsigned int starfield_size_ = 0;
std::unordered_map<unsigned long long, camera_component*> camera_components_;
std::vector<directional_light_component*> directional_light_components_;
std::vector<light_component*> light_components_;
......@@ -144,6 +151,8 @@ namespace gdw {
void render_non_opauqe_geometry(float delta_time, camera_component& camera);
void render_navMesh(camera_component &camera);
void render_final_image();
void generate_starfield();
void render_starfield(camera_component& camera);
void register_component(camera_component* component);
......
#ifndef __GDW_STAR_VERTEX_HPP__
#define __GDW_STAR_VERTEX_HPP__
#include <glm/glm.hpp>
namespace gdw {
struct star_vertex {
glm::vec3 position;
float size;
glm::vec3 color;
};
}
#endif
......@@ -2,6 +2,7 @@
#include <glm/gtc/matrix_inverse.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/random.hpp>
#include <glm/gtx/string_cast.hpp>
#include <core/engine.hpp>
......@@ -19,6 +20,7 @@
#include <rendering/rendering_system.hpp>
#include <rendering/screen_quad.hpp>
#include <rendering/shadow_component.hpp>
#include <rendering/star_vertex.hpp>
#include <rendering/staticmesh_component.hpp>
#include <util/component_helper.hpp>
#include <util/config.hpp>
......@@ -28,7 +30,8 @@
namespace gdw {
rendering_system::rendering_system(engine& engine)
: engine_(engine), mesh_manager_(engine), active_camera_(0), debug_mode_(false), point_light_instance_data_(GL_DYNAMIC_DRAW),
shadow_resolution_(256),simple_render_vbo_(GL_STATIC_DRAW),simple_render_element_size_(0), baked_(true) {
shadow_resolution_(256),simple_render_vbo_(GL_STATIC_DRAW),simple_render_element_size_(0), baked_(true), starfield_vbo_(GL_STATIC_DRAW),
starfield_point_(GL_STATIC_DRAW) {
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));
......@@ -278,6 +281,33 @@ namespace gdw {
traktor_beam_program_.attach_shader(fragment_shader);
staticmesh_layout_.setup_program(traktor_beam_program_, "frag_color");
traktor_beam_program_.link();
starfield_vertex_layout_.emplace_back("_point", 3, GL_FLOAT, false, sizeof(glm::vec3), 0);
starfield_vertex_layout_.emplace_back("_position", 3, GL_FLOAT, false, sizeof(star_vertex), offsetof(star_vertex, position), 1, 1);
starfield_vertex_layout_.emplace_back("_size", 1, GL_FLOAT, false, sizeof(star_vertex), offsetof(star_vertex, size), 1, 1);
starfield_vertex_layout_.emplace_back("_color", 3, GL_FLOAT, false, sizeof(star_vertex), offsetof(star_vertex, color), 1, 1);
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);
starfield_vertex_layout_.setup_program(starfield_program_, "frag_color");
starfield_program_.link();
auto point = glm::vec3(0.f);
starfield_point_.bind();
starfield_point_.data(sizeof(glm::vec3), &point);
const vertex_buffer* buff[2] = {&starfield_point_, &starfield_vbo_};
starfield_vertex_layout_.setup_layout(starfield_vao_, buff);
generate_starfield();
}
void rendering_system::update(float delta_time) {
......@@ -288,6 +318,7 @@ namespace gdw {
render_gbuffer(delta_time, *camera);
render_lights(*camera);
render_starfield(*camera);
render_non_opauqe_geometry(delta_time, *camera);
render_final_image();
if(engine_.getConfig().get("navDebug",true))
......@@ -420,6 +451,13 @@ namespace gdw {
glDisable(GL_CULL_FACE);
glDisable(GL_BLEND);
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);
}
void rendering_system::render_directional_lights(camera_component& camera) {
......@@ -642,11 +680,7 @@ namespace gdw {
auto width = engine_.graphics_system().width();
auto height = engine_.graphics_system().height();
g_buffer_.bind_read();
composition_buffer_.bind_draw();
auto error = GL_NO_ERROR;
while((error = glGetError()) != GL_NO_ERROR);
glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
composition_buffer_.bind();
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
......@@ -770,6 +804,29 @@ namespace gdw {
tractor_beams_.emplace_back(beam);
}
void rendering_system::generate_starfield() {
static std::vector<star_vertex> stars;
for (auto i = 0; i < 100000; ++i) {
star_vertex v = {glm::sphericalRand(500.f), glm::gaussRand(0.05f, 0.1f), glm::vec3(glm::gaussRand(0.1f, 1.f))};
stars.emplace_back(v);
}
starfield_vbo_.bind();
starfield_vbo_.data(stars.size() * sizeof(star_vertex), stars.data());
starfield_size_ = stars.size();
}
void rendering_system::render_starfield(camera_component& camera) {
glEnable(GL_DEPTH_TEST);
glEnable(GL_PROGRAM_POINT_SIZE);
starfield_vao_.bind();
starfield_program_.use();
starfield_program_.uniform("projection", false, camera.projection());
starfield_program_.uniform("view", false, camera.view());
glDrawArraysInstanced(GL_POINTS, 0, 1, starfield_size_);
glDisable(GL_PROGRAM_POINT_SIZE);
glDisable(GL_DEPTH_TEST);
}
void rendering_system::register_component(camera_component* component) {
camera_components_.emplace(std::make_pair(component->owner().id(), component));
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment