--- /dev/null
+uniform vec3 horiz_color, mid_color, zenith_color, fog_color;
+
+const float mid_point = 0.25;
+const float horiz_scale = 20.0;
+
+vec3 sky_grad(float t, float mid);
+float pnoise(float p, float rep);
+float fbm(float p, int octaves, float rep);
+
+void main()
+{
+ float tx = gl_TexCoord[0].x;
+ float ty = 1.0 - gl_TexCoord[0].y;
+ vec3 color = sky_grad(ty, mid_point);
+
+ float horiz = ty + fbm(tx * horiz_scale, 4, horiz_scale) * 0.035;
+ color = mix(fog_color, color, step(0.035, horiz));
+
+ gl_FragColor.rgb = color;
+ gl_FragColor.a = 1.0;
+}
+
+vec3 sky_grad(float t, float mid)
+{
+ float t_low = min(t / mid, 1.0);
+ float t_high = min((1.0 - t) / (1.0 - mid), 1.0);
+
+ return mix(zenith_color, mix(horiz_color, mid_color, t_low), t_high);
+}
+
+float pnoise(vec2 p, vec2 rep);
+
+float pnoise(float p, float rep)
+{
+ return pnoise(vec2(p, 0.0), vec2(rep, rep));
+}
+
+float fbm(float p, int octaves, float rep)
+{
+ float res = 0.0;
+ float freq = 1.0;
+ float scale = 1.0;
+
+ for(int i=0; i<octaves; i++) {
+ res += pnoise(p * freq, rep * freq) * scale;
+ freq *= 2.0;
+ scale *= 0.5;
+ }
+ return res;
+}
+
+//
+// GLSL textureless classic 2D noise "cnoise",
+// with an RSL-style periodic variant "pnoise".
+// Author: Stefan Gustavson (stefan.gustavson@liu.se)
+// Version: 2011-08-22
+//
+// Many thanks to Ian McEwan of Ashima Arts for the
+// ideas for permutation and gradient selection.
+//
+// Copyright (c) 2011 Stefan Gustavson. All rights reserved.
+// Distributed under the MIT license. See LICENSE file.
+// https://github.com/stegu/webgl-noise
+//
+
+vec4 mod289(vec4 x)
+{
+ return x - floor(x * (1.0 / 289.0)) * 289.0;
+}
+
+vec4 permute(vec4 x)
+{
+ return mod289(((x*34.0)+1.0)*x);
+}
+
+vec4 taylor_inv_sqrt(vec4 r)
+{
+ return 1.79284291400159 - 0.85373472095314 * r;
+}
+
+vec2 fade(vec2 t) {
+ return t*t*t*(t*(t*6.0-15.0)+10.0);
+}
+
+// Classic perlin noise
+float cnoise(vec2 p)
+{
+ vec4 pi = floor(p.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
+ vec4 pf = fract(p.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
+ pi = mod289(pi); // To avoid truncation effects in permutation
+ vec4 ix = pi.xzxz;
+ vec4 iy = pi.yyww;
+ vec4 fx = pf.xzxz;
+ vec4 fy = pf.yyww;
+
+ vec4 i = permute(permute(ix) + iy);
+
+ vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
+ vec4 gy = abs(gx) - 0.5 ;
+ vec4 tx = floor(gx + 0.5);
+ gx = gx - tx;
+
+ vec2 g00 = vec2(gx.x,gy.x);
+ vec2 g10 = vec2(gx.y,gy.y);
+ vec2 g01 = vec2(gx.z,gy.z);
+ vec2 g11 = vec2(gx.w,gy.w);
+
+ vec4 norm = taylor_inv_sqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
+ g00 *= norm.x;
+ g01 *= norm.y;
+ g10 *= norm.z;
+ g11 *= norm.w;
+
+ float n00 = dot(g00, vec2(fx.x, fy.x));
+ float n10 = dot(g10, vec2(fx.y, fy.y));
+ float n01 = dot(g01, vec2(fx.z, fy.z));
+ float n11 = dot(g11, vec2(fx.w, fy.w));
+
+ vec2 fade_xy = fade(pf.xy);
+ vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
+ float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
+ return 2.3 * n_xy;
+}
+
+// Classic perlin noise, periodic variant
+float pnoise(vec2 p, vec2 rep)
+{
+ vec4 pi = floor(p.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
+ vec4 pf = fract(p.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
+ pi = mod(pi, rep.xyxy); // To create noise with explicit period
+ pi = mod289(pi); // To avoid truncation effects in permutation
+ vec4 ix = pi.xzxz;
+ vec4 iy = pi.yyww;
+ vec4 fx = pf.xzxz;
+ vec4 fy = pf.yyww;
+
+ vec4 i = permute(permute(ix) + iy);
+
+ vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
+ vec4 gy = abs(gx) - 0.5 ;
+ vec4 tx = floor(gx + 0.5);
+ gx = gx - tx;
+
+ vec2 g00 = vec2(gx.x,gy.x);
+ vec2 g10 = vec2(gx.y,gy.y);
+ vec2 g01 = vec2(gx.z,gy.z);
+ vec2 g11 = vec2(gx.w,gy.w);
+
+ vec4 norm = taylor_inv_sqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
+ g00 *= norm.x;
+ g01 *= norm.y;
+ g10 *= norm.z;
+ g11 *= norm.w;
+
+ float n00 = dot(g00, vec2(fx.x, fy.x));
+ float n10 = dot(g10, vec2(fx.y, fy.y));
+ float n01 = dot(g01, vec2(fx.z, fy.z));
+ float n11 = dot(g11, vec2(fx.w, fy.w));
+
+ vec2 fade_xy = fade(pf.xy);
+ vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
+ float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
+ return 2.3 * n_xy;
+}
--- /dev/null
+#include "opengl.h"
+#include "gmath/gmath.h"
+#include "sdr.h"
+#include "texture.h"
+#include "mesh.h"
+#include "meshgen.h"
+#include "backdrop.h"
+
+static unsigned int sdr_grid;
+static Texture *tex_grid;
+
+static Mesh *mesh_skydome;
+static unsigned int sdr_skydome;
+
+static const Vec3 grid_color = Vec3(1.0, 0.3, 1.0);
+static const Vec3 mid_color = Vec3(0.4, 0.1, 0.8);
+static const Vec3 horiz_color = Vec3(0.8, 0.1, 1.0);
+static const Vec3 zenith_color = Vec3(0.2, 0.2, 0.2);
+static const Vec3 fog_color = Vec3(0.12, 0.12, 0.12);
+
+bool init_backdrop()
+{
+ if(!(sdr_grid = create_program_load("sdr/grid.v.glsl", "sdr/grid.p.glsl"))) {
+ return false;
+ }
+ set_uniform_float3(sdr_grid, "grid_color", grid_color.x, grid_color.y, grid_color.z);
+ set_uniform_float3(sdr_grid, "fog_color", fog_color.x, fog_color.y, fog_color.z);
+
+ if(!(sdr_skydome = create_program_load("sdr/skydome.v.glsl", "sdr/skydome.p.glsl"))) {
+ return false;
+ }
+ set_uniform_float3(sdr_skydome, "zenith_color", zenith_color.x, zenith_color.y, zenith_color.z);
+ set_uniform_float3(sdr_skydome, "mid_color", mid_color.x, mid_color.y, mid_color.z);
+ set_uniform_float3(sdr_skydome, "horiz_color", horiz_color.x, horiz_color.y, horiz_color.z);
+ set_uniform_float3(sdr_skydome, "fog_color", fog_color.x, fog_color.y, fog_color.z);
+
+ if(!(tex_grid = load_texture("data/grid2.png"))) {
+ delete tex_grid;
+ return false;
+ }
+
+ mesh_skydome = new Mesh;
+ gen_sphere(mesh_skydome, 100.0, 32, 16, 1, 0.5);
+ mesh_skydome->flip();
+
+ return true;
+}
+
+void cleanup_backdrop()
+{
+ delete mesh_skydome;
+ delete tex_grid;
+ free_program(sdr_grid);
+}
+
+void draw_backdrop()
+{
+ // draw sky
+ bind_program(sdr_skydome);
+ mesh_skydome->draw();
+
+ // draw grid
+ Mat4 xform;
+ xform.scaling(500.0);
+ glPushMatrix();
+ glMultMatrixf(xform[0]);
+
+ bind_program(sdr_grid);
+ bind_texture(tex_grid);
+
+ glBegin(GL_QUADS);
+ glNormal3f(0, 1, 0);
+ glVertex3f(-1, 0, 1);
+ glVertex3f(1, 0, 1);
+ glVertex3f(1, 0, -1);
+ glVertex3f(-1, 0, -1);
+ glEnd();
+
+ glPopMatrix();
+}