Commit e889b4e0 authored by Georg Schaefer's avatar Georg Schaefer
Browse files

add point sprites for starfield

parent 012056a8
......@@ -4,6 +4,12 @@ in vec3 color_;
out vec4 frag_color;
uniform sampler2D albedo_texture;
void main() {
frag_color = vec4(color_, 1.f);
vec4 color = texture(albedo_texture, gl_PointCoord);
if (color.a <= 0.f) {
discard;
}
frag_color = color * vec4(color_, 1.f);
}
#version 330
in vec3 _point;
in vec3 _position;
in float _size;
in vec3 _color;
......@@ -11,7 +10,8 @@ uniform mat4 projection;
uniform mat4 view;
void main() {
gl_Position = projection * view * vec4(_point + _position, 1.f);
gl_PointSize = _size;
vec4 position = projection * view * vec4(_position, 1.f);
gl_Position = position;
gl_PointSize = _size / position.z;
color_ = _color;
}
......@@ -86,6 +86,7 @@ namespace gdw {
vertex_buffer starfield_vbo_;
vertex_array starfield_vao_;
unsigned int starfield_size_ = 0;
std::shared_ptr<const texture> star_texture_;
std::unordered_map<unsigned long long, camera_component*> camera_components_;
std::vector<directional_light_component*> directional_light_components_;
......
......@@ -3,6 +3,7 @@
#include <glm/gtc/matrix_inverse.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/random.hpp>
#include <glm/gtx/compatibility.hpp>
#include <glm/gtx/string_cast.hpp>
#include <core/engine.hpp>
......@@ -10,6 +11,7 @@
#include <graphics/graphics_system.hpp>
#include <graphics/shader.hpp>
#include <graphics/shader_manager.hpp>
#include <graphics/texture_manager.hpp>
#include <rendering/camera_component.hpp>
#include <rendering/directional_light_component.hpp>
#include <rendering/light_attributes.hpp>
......@@ -307,7 +309,16 @@ namespace gdw {
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();
glEnable(GL_PROGRAM_POINT_SIZE);
glEnable(GL_POINT_SPRITE);
glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
}
void rendering_system::update(float delta_time) {
......@@ -806,8 +817,11 @@ namespace gdw {
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))};
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(10.f, 50.f), glm::lerp(white, yellow, alpha)};
stars.emplace_back(v);
}
starfield_vbo_.bind();
......@@ -817,14 +831,19 @@ namespace gdw {
void rendering_system::render_starfield(camera_component& camera) {
glEnable(GL_DEPTH_TEST);
glEnable(GL_PROGRAM_POINT_SIZE);
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
starfield_vao_.bind();
starfield_program_.use();
starfield_program_.uniform("projection", false, camera.projection());
starfield_program_.uniform("view", false, camera.view());
starfield_program_.uniform("albedo_texture", 0);
star_texture_->bind(0);
glDrawArraysInstanced(GL_POINTS, 0, 1, starfield_size_);
glDisable(GL_PROGRAM_POINT_SIZE);
glDepthMask(GL_TRUE);
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
}
void rendering_system::register_component(camera_component* 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