cyberenv
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Tue, 26 Jul 2016 02:55:44 +0000 (05:55 +0300)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Tue, 26 Jul 2016 02:55:44 +0000 (05:55 +0300)
sdr/grid.p.glsl
sdr/skydome.p.glsl [new file with mode: 0644]
sdr/skydome.v.glsl [new file with mode: 0644]
src/app.cc
src/backdrop.cc [new file with mode: 0644]
src/backdrop.h [new file with mode: 0644]
src/meshgen.cc

index 825cdc6..3dc1230 100644 (file)
@@ -1,14 +1,17 @@
 uniform sampler2D tex;
 uniform sampler2D tex;
+uniform vec3 fog_color, grid_color;
 
 varying vec3 vpos;
 
 void main()
 {
 
 varying vec3 vpos;
 
 void main()
 {
-       const vec3 bg_color = vec3(0.5, 0.1, 1.0);
-       vec3 grid_color = texture2D(tex, gl_TexCoord[0].st).xyz;
+       vec3 texel = texture2D(tex, gl_TexCoord[0].st).xyz;
+       vec3 color = pow(texel * grid_color, vec3(0.75)) * 2.0;
 
 
-       float fog = min(abs(vpos.z) * 0.05, 1.0);
+       float dist = abs(vpos.z);
+       float fog = 1.0 - exp(-dist * 0.11);
 
 
-       gl_FragColor.xyz = mix(grid_color, bg_color, fog);
+       float t = clamp(fog, 0.0, 1.0);
+       gl_FragColor.xyz = mix(color, fog_color, t);
        gl_FragColor.a = 1.0;
 }
        gl_FragColor.a = 1.0;
 }
diff --git a/sdr/skydome.p.glsl b/sdr/skydome.p.glsl
new file mode 100644 (file)
index 0000000..62a3475
--- /dev/null
@@ -0,0 +1,164 @@
+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;
+}
diff --git a/sdr/skydome.v.glsl b/sdr/skydome.v.glsl
new file mode 100644 (file)
index 0000000..b446ef6
--- /dev/null
@@ -0,0 +1,5 @@
+void main()
+{
+       gl_Position = ftransform();
+       gl_TexCoord[0] = gl_MultiTexCoord0;
+}
index f99e034..859e6f2 100644 (file)
@@ -5,8 +5,7 @@
 #include "gmath/gmath.h"
 #include "mesh.h"
 #include "meshgen.h"
 #include "gmath/gmath.h"
 #include "mesh.h"
 #include "meshgen.h"
-#include "sdr.h"
-#include "texture.h"
+#include "backdrop.h"
 
 int win_width, win_height;
 float win_aspect;
 
 int win_width, win_height;
 float win_aspect;
@@ -14,13 +13,10 @@ long time_msec;
 
 static float cam_theta, cam_phi;
 static Mesh *mesh_torus;
 
 static float cam_theta, cam_phi;
 static Mesh *mesh_torus;
-static Texture *tex_grid;
 
 static bool bnstate[16];
 static int prev_x, prev_y;
 
 
 static bool bnstate[16];
 static int prev_x, prev_y;
 
-static unsigned int sdr_grid;
-
 bool app_init(int argc, char **argv)
 {
        if(init_opengl() == -1) {
 bool app_init(int argc, char **argv)
 {
        if(init_opengl() == -1) {
@@ -43,11 +39,7 @@ bool app_init(int argc, char **argv)
        mesh_torus = new Mesh;
        gen_torus(mesh_torus, 1.0, 0.25, 32, 32);
 
        mesh_torus = new Mesh;
        gen_torus(mesh_torus, 1.0, 0.25, 32, 32);
 
-       if(!(sdr_grid = create_program_load("sdr/grid.v.glsl", "sdr/grid.p.glsl"))) {
-               return false;
-       }
-       if(!(tex_grid = load_texture("data/purple_grid.png"))) {
-               delete tex_grid;
+       if(!init_backdrop()) {
                return false;
        }
 
                return false;
        }
 
@@ -56,6 +48,8 @@ bool app_init(int argc, char **argv)
 
 void app_cleanup()
 {
 
 void app_cleanup()
 {
+       delete mesh_torus;
+       cleanup_backdrop();
 }
 
 void app_draw()
 }
 
 void app_draw()
@@ -70,27 +64,7 @@ void app_draw()
        glLoadMatrixf(view_mat[0]);
 
        //mesh_torus->draw();
        glLoadMatrixf(view_mat[0]);
 
        //mesh_torus->draw();
-
-       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();
-
-       bind_texture(0);
-       bind_program(0);
-
-       glPopMatrix();
+       draw_backdrop();
 
        app_swap_buffers();
        assert(glGetError() == GL_NO_ERROR);
 
        app_swap_buffers();
        assert(glGetError() == GL_NO_ERROR);
diff --git a/src/backdrop.cc b/src/backdrop.cc
new file mode 100644 (file)
index 0000000..33f4c89
--- /dev/null
@@ -0,0 +1,80 @@
+#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();
+}
diff --git a/src/backdrop.h b/src/backdrop.h
new file mode 100644 (file)
index 0000000..b89d040
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef BACKDROP_H_
+#define BACKDROP_H_
+
+bool init_backdrop();
+void cleanup_backdrop();
+
+void draw_backdrop();
+
+#endif // BACKDROP_H_
index 412f763..b6ae0c8 100644 (file)
@@ -16,6 +16,8 @@ static Vec3 sphvec(float theta, float phi)
 
 void gen_sphere(Mesh *mesh, float rad, int usub, int vsub, float urange, float vrange)
 {
 
 void gen_sphere(Mesh *mesh, float rad, int usub, int vsub, float urange, float vrange)
 {
+       if(urange == 0.0 || vrange == 0.0) return;
+
        if(usub < 4) usub = 4;
        if(vsub < 2) vsub = 2;
 
        if(usub < 4) usub = 4;
        if(vsub < 2) vsub = 2;
 
@@ -49,7 +51,7 @@ void gen_sphere(Mesh *mesh, float rad, int usub, int vsub, float urange, float v
                        *varr++ = pos * rad;
                        *narr++ = pos;
                        *tarr++ = normalize(sphvec(theta + 0.1f, (float)M_PI / 2.0f) - sphvec(theta - 0.1f, (float)M_PI / 2.0f));
                        *varr++ = pos * rad;
                        *narr++ = pos;
                        *tarr++ = normalize(sphvec(theta + 0.1f, (float)M_PI / 2.0f) - sphvec(theta - 0.1f, (float)M_PI / 2.0f));
-                       *uvarr++ = Vec2(u * urange, v * vrange);
+                       *uvarr++ = Vec2(u / urange, v / vrange);
 
                        if(i < usub && j < vsub) {
                                int idx = i * vverts + j;
 
                        if(i < usub && j < vsub) {
                                int idx = i * vverts + j;