From: John Tsiombikas Date: Tue, 26 Jul 2016 02:55:44 +0000 (+0300) Subject: cyberenv X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=vrfileman;a=commitdiff_plain;h=5854b2eec65e297e1b21e8e1716c02516a3bac4a cyberenv --- diff --git a/sdr/grid.p.glsl b/sdr/grid.p.glsl index 825cdc6..3dc1230 100644 --- a/sdr/grid.p.glsl +++ b/sdr/grid.p.glsl @@ -1,14 +1,17 @@ uniform sampler2D tex; +uniform vec3 fog_color, grid_color; 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; } diff --git a/sdr/skydome.p.glsl b/sdr/skydome.p.glsl new file mode 100644 index 0000000..62a3475 --- /dev/null +++ b/sdr/skydome.p.glsl @@ -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; idraw(); - - 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); diff --git a/src/backdrop.cc b/src/backdrop.cc new file mode 100644 index 0000000..33f4c89 --- /dev/null +++ b/src/backdrop.cc @@ -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 index 0000000..b89d040 --- /dev/null +++ b/src/backdrop.h @@ -0,0 +1,9 @@ +#ifndef BACKDROP_H_ +#define BACKDROP_H_ + +bool init_backdrop(); +void cleanup_backdrop(); + +void draw_backdrop(); + +#endif // BACKDROP_H_ diff --git a/src/meshgen.cc b/src/meshgen.cc index 412f763..b6ae0c8 100644 --- a/src/meshgen.cc +++ b/src/meshgen.cc @@ -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) { + if(urange == 0.0 || vrange == 0.0) return; + 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)); - *uvarr++ = Vec2(u * urange, v * vrange); + *uvarr++ = Vec2(u / urange, v / vrange); if(i < usub && j < vsub) { int idx = i * vverts + j;