Commit 16cbeda1 authored by Georg Schaefer's avatar Georg Schaefer
Browse files

replace sprite based starfield with skybox for generating starfiled via fragment shader

parent 4f754c79
#version 330 #version 330
in vec2 texcoord_;
in vec3 color_;
out vec4 frag_color; out vec4 frag_color;
uniform sampler2D albedo_texture;
void main() { void main() {
vec4 color = texture(albedo_texture, texcoord_); frag_color = vec4(1.f);
if (color.a <= 0.f) {
discard;
}
frag_color = color * vec4(color_, 1.f);
} }
#version 330 #version 330
in vec3 _position; in vec3 _position;
in float _size;
in vec3 _color;
out float _size_;
out vec3 _color_;
uniform mat4 projection;
uniform mat4 view; uniform mat4 view;
uniform mat4 model; uniform mat4 model;
void main() { void main() {
vec4 position = view * model * vec4(_position, 1.f); gl_Position = projection * view * model * vec4(_position, 1.f);
gl_Position = position;
_size_ = _size;
_color_ = _color;
} }
...@@ -80,13 +80,8 @@ namespace gdw { ...@@ -80,13 +80,8 @@ namespace gdw {
vertex_array simple_render_vao_; vertex_array simple_render_vao_;
int simple_render_element_size_; int simple_render_element_size_;
vertex_layout starfield_vertex_layout_;
program starfield_program_; program starfield_program_;
vertex_buffer starfield_point_; std::shared_ptr<const mesh> starfield_;
vertex_buffer starfield_vbo_;
vertex_array starfield_vao_;
unsigned int starfield_size_ = 0;
std::shared_ptr<const texture> star_texture_;
std::shared_ptr<const mesh> planet_; std::shared_ptr<const mesh> planet_;
program planet_program_; program planet_program_;
...@@ -158,7 +153,6 @@ namespace gdw { ...@@ -158,7 +153,6 @@ namespace gdw {
void render_non_opauqe_geometry(float delta_time, camera_component& camera); void render_non_opauqe_geometry(float delta_time, camera_component& camera);
void render_navMesh(camera_component &camera); void render_navMesh(camera_component &camera);
void render_final_image(); void render_final_image();
void generate_starfield();
void render_starfield(float delta_time, camera_component& camera); void render_starfield(float delta_time, camera_component& camera);
void render_planet(camera_component& camera); void render_planet(camera_component& camera);
......
...@@ -9,17 +9,17 @@ deathstar.dds : 20 ...@@ -9,17 +9,17 @@ deathstar.dds : 20
dome_material.dds : 18 dome_material.dds : 18
beam_stripes.dds : 13 beam_stripes.dds : 13
weighted_companion_cube_export_material.dds : 3 weighted_companion_cube_export_material.dds : 3
piratenschiff_diffuse.dds : 5
Meteor_normal_2.dds : 21 Meteor_normal_2.dds : 21
piratenschiff_diffuse.dds : 5
beam.dds : 12 beam.dds : 12
piratenschiff_normal.dds : 6 piratenschiff_normal.dds : 6
cannon.dds : 11
victim_diffuse.dds : 15
Texture_Meteor.dds : 9 Texture_Meteor.dds : 9
weighted_companion_cube_export.dds : 1 weighted_companion_cube_export.dds : 1
Meteor_LOW_Normals.dds : 10 Meteor_LOW_Normals.dds : 10
cannon.dds : 11
victim_diffuse.dds : 15
greyscale_normal.dds : 2
black.dds : 7
piratenschiff_material.dds : 4 piratenschiff_material.dds : 4
planet_normal.dds : 24
dome_diffuse.dds : 17 dome_diffuse.dds : 17
black.dds : 7 planet_normal.dds : 24
greyscale_normal.dds : 2
...@@ -32,8 +32,7 @@ ...@@ -32,8 +32,7 @@
namespace gdw { namespace gdw {
rendering_system::rendering_system(engine& engine) rendering_system::rendering_system(engine& engine)
: engine_(engine), mesh_manager_(engine), active_camera_(0), debug_mode_(false), point_light_instance_data_(GL_DYNAMIC_DRAW), : 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), starfield_vbo_(GL_STATIC_DRAW), shadow_resolution_(256),simple_render_vbo_(GL_STATIC_DRAW),simple_render_element_size_(0), baked_(true) {
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("_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("_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("_normal", 3, GL_FLOAT, false, sizeof(mesh_vertex), offsetof(mesh_vertex, normal));
...@@ -284,43 +283,24 @@ namespace gdw { ...@@ -284,43 +283,24 @@ namespace gdw {
staticmesh_layout_.setup_program(traktor_beam_program_, "frag_color"); staticmesh_layout_.setup_program(traktor_beam_program_, "frag_color");
traktor_beam_program_.link(); traktor_beam_program_.link();
starfield_vertex_layout_.emplace_back("_point", 3, GL_FLOAT, false, sizeof(glm::vec3), 0); starfield_ = mesh_manager_.load("mesh/starfield.msh", staticmesh_layout_);
starfield_vertex_layout_.emplace_back("_position", 3, GL_FLOAT, false, sizeof(star_vertex), offsetof(star_vertex, position), 1, 1); if (!starfield_) {
starfield_vertex_layout_.emplace_back("_size", 1, GL_FLOAT, false, sizeof(star_vertex), offsetof(star_vertex, size), 1, 1); throw std::runtime_error("could not load mesh/starfield.msh");
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); vertex_shader = shader_manager.load("shader/starfield.vs", GL_VERTEX_SHADER);
if (!vertex_shader) { if (!vertex_shader) {
throw std::runtime_error("could not load shader/starfield.vs"); throw std::runtime_error("could not load shader/starfield.vs");
} }
starfield_program_.attach_shader(vertex_shader); starfield_program_.attach_shader(vertex_shader);
auto geometry_shader = shader_manager.load("shader/starfield.gs", GL_GEOMETRY_SHADER);
if (!geometry_shader) {
throw std::runtime_error("could not load shader/starfield.gs");
}
starfield_program_.attach_shader(geometry_shader);
fragment_shader = shader_manager.load("shader/starfield.fs", GL_FRAGMENT_SHADER); fragment_shader = shader_manager.load("shader/starfield.fs", GL_FRAGMENT_SHADER);
if (!fragment_shader) { if (!fragment_shader) {
throw std::runtime_error("could not load shader/starfield.fs"); throw std::runtime_error("could not load shader/starfield.fs");
} }
starfield_program_.attach_shader(fragment_shader); starfield_program_.attach_shader(fragment_shader);
starfield_vertex_layout_.setup_program(starfield_program_, "frag_color"); staticmesh_layout_.setup_program(starfield_program_, "frag_color");
starfield_program_.link(); 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);
star_texture_ = graphics_system.texture_manager().load("texture/star.dds");
if (!star_texture_) {
throw std::runtime_error("could not load texture/star.dds");
}
generate_starfield();
planet_ = mesh_manager_.load("mesh/planet.msh", staticmesh_layout_); planet_ = mesh_manager_.load("mesh/planet.msh", staticmesh_layout_);
if (!planet_) { if (!planet_) {
throw std::runtime_error("could not load mesh/planet.msh"); throw std::runtime_error("could not load mesh/planet.msh");
...@@ -366,8 +346,8 @@ namespace gdw { ...@@ -366,8 +346,8 @@ namespace gdw {
render_gbuffer(delta_time, *camera); render_gbuffer(delta_time, *camera);
render_lights(*camera); render_lights(*camera);
render_planet(*camera);
render_starfield(delta_time, *camera); render_starfield(delta_time, *camera);
render_planet(*camera);
render_non_opauqe_geometry(delta_time, *camera); render_non_opauqe_geometry(delta_time, *camera);
render_final_image(); render_final_image();
if(engine_.getConfig().get("navDebug",true)) if(engine_.getConfig().get("navDebug",true))
...@@ -853,47 +833,24 @@ namespace gdw { ...@@ -853,47 +833,24 @@ namespace gdw {
tractor_beams_.emplace_back(beam); tractor_beams_.emplace_back(beam);
} }
void rendering_system::generate_starfield() {
static std::vector<star_vertex> stars;
for (auto i = 0; i < 10000; ++i) {
static const auto white = glm::vec3(1.f);
static const auto yellow = glm::vec3(1.f, 1.f, 0.8f);
auto alpha = glm::gaussRand(0.f, 1.f);
star_vertex v = {glm::sphericalRand(500.f), glm::gaussRand(1.f, 2.f), glm::lerp(white, yellow, alpha)};
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(float delta_time, camera_component& camera) { void rendering_system::render_starfield(float delta_time, camera_component& camera) {
static auto alpha = 0.f; const static auto model = glm::scale(glm::mat4(1.f), glm::vec3(500.f));
static const auto planet_acceleration_ = 0.005f;
static const auto planet_center = glm::vec3(80.f, -20.f, -100.f); composition_buffer_.bind();
alpha += planet_acceleration_ * delta_time;
if (alpha >= 2.f * glm::pi<float>()) { glEnable(GL_DEPTH_TEST);
alpha = 0.f; glEnable(GL_CULL_FACE);
} glCullFace(GL_BACK);
auto model = glm::translate(glm::mat4(1.f), planet_center);
model *= glm::toMat4(glm::angleAxis(alpha, glm::vec3(0.f, 1.f, 0.f)));
model *= glm::translate(glm::mat4(1.f), -planet_center);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
starfield_vao_.bind();
starfield_program_.use(); starfield_program_.use();
starfield_program_.uniform("projection", false, camera.projection()); starfield_program_.uniform("projection", false, camera.projection());
starfield_program_.uniform("view", false, camera.view()); starfield_program_.uniform("view", false, camera.view());
starfield_program_.uniform("model", false, model); starfield_program_.uniform("model", false, model);
starfield_program_.uniform("albedo_texture", 0); starfield_->draw(starfield_program_);
star_texture_->bind(0);
glDrawArraysInstanced(GL_POINTS, 0, 1, starfield_size_); glDisable(GL_CULL_FACE);
glDepthMask(GL_TRUE);
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
} }
void rendering_system::render_planet(camera_component& camera) { void rendering_system::render_planet(camera_component& camera) {
......
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