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

add starfield rendering

parent 16cbeda1
...@@ -2,6 +2,59 @@ ...@@ -2,6 +2,59 @@
out vec4 frag_color; out vec4 frag_color;
uniform vec2 resolution;
uniform vec2 inverse_resolution;
uniform vec3 origin;
uniform mat2 rotation;
uniform float zoom;
#define iterations 15
#define volsteps 6
#define sparsity 0.5f
#define stepsize 0.3f
#define frequency_variation 1.3f
#define brightness 0.0018f
#define distfading 0.68f
void main() { void main() {
frag_color = vec4(1.f); vec2 uv = gl_FragCoord.xy * inverse_resolution - 0.5f;
uv.y *= resolution.y * inverse_resolution.x;
vec3 direction = vec3(uv * zoom, 1.f);
direction.xy *= rotation;
float s = 0.1f;
float fade = 0.01f;
frag_color = vec4(0.f);
for (int r = 0; r < volsteps; ++r) {
vec3 p = origin + direction * (s * 0.5f);
p = abs(vec3(frequency_variation) - mod(p, vec3(frequency_variation * 2.f)));
float prevlen = 0.f;
float a = 0.f;
for (int i = 0; i < iterations; ++i) {
p = abs(p);
p = p * (1.f / dot(p, p)) + (-sparsity);
float len = length(p);
a += abs(len - prevlen);
prevlen = len;
}
a *= a * a;
frag_color.rgb += (vec3(s, s * s, s * s * s) * a * brightness + 1.f) * fade;
fade *= distfading;
s += stepsize;
}
frag_color.rgb = min(frag_color.rgb, vec3(1.2f));
float intensity = min(frag_color.r + frag_color.g + frag_color.b, 0.7f);
ivec2 sgn = (ivec2(gl_FragCoord.xy) & 1) * 2 - 1;
vec2 gradient = vec2(dFdx(intensity) * sgn.x, dFdy(intensity) * sgn.y);
float cutoff = max(max(gradient.x, gradient.y) - 0.1f, 0.f);
frag_color.rgb *= max(1.f - cutoff * 0.6f, 0.3f);
frag_color.a = 1.f;
} }
...@@ -835,6 +835,19 @@ namespace gdw { ...@@ -835,6 +835,19 @@ namespace gdw {
void rendering_system::render_starfield(float delta_time, camera_component& camera) { void rendering_system::render_starfield(float delta_time, camera_component& camera) {
const static auto model = glm::scale(glm::mat4(1.f), glm::vec3(500.f)); const static auto model = glm::scale(glm::mat4(1.f), glm::vec3(500.f));
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));
composition_buffer_.bind(); composition_buffer_.bind();
...@@ -847,6 +860,11 @@ namespace gdw { ...@@ -847,6 +860,11 @@ namespace gdw {
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("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);
starfield_->draw(starfield_program_); starfield_->draw(starfield_program_);
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
......
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