fixed color linearity, and tweaked for VR
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Tue, 26 Jul 2016 15:04:14 +0000 (18:04 +0300)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Tue, 26 Jul 2016 15:04:14 +0000 (18:04 +0300)
sdr/grid.p.glsl
sdr/grid.v.glsl
src/app.cc
src/app.h
src/backdrop.cc
src/opengl.c
src/opengl.h
src/texture.cc
src/texture.h

index 3dc1230..d41d17f 100644 (file)
@@ -6,10 +6,10 @@ varying vec3 vpos;
 void main()
 {
        vec3 texel = texture2D(tex, gl_TexCoord[0].st).xyz;
-       vec3 color = pow(texel * grid_color, vec3(0.75)) * 2.0;
+       vec3 color = pow(texel * grid_color, vec3(0.76)) * 5.0;
 
        float dist = abs(vpos.z);
-       float fog = 1.0 - exp(-dist * 0.11);
+       float fog = 1.0 - exp(-dist * 0.12);
 
        float t = clamp(fog, 0.0, 1.0);
        gl_FragColor.xyz = mix(color, fog_color, t);
index d6bda6e..5e239dd 100644 (file)
@@ -4,5 +4,5 @@ void main()
 {
        gl_Position = ftransform();
        vpos = (gl_ModelViewMatrix * gl_Vertex).xyz;
-       gl_TexCoord[0].xy = gl_Vertex.xz * 500.0;
+       gl_TexCoord[0].xy = gl_Vertex.xz * 330.0;
 }
index d8aa144..0dc88f0 100644 (file)
@@ -13,6 +13,7 @@ static bool parse_args(int argc, char **argv);
 int win_width, win_height;
 float win_aspect;
 long time_msec;
+Mat4 view_matrix;
 
 static bool use_vr;
 static bool should_swap;
@@ -39,6 +40,8 @@ bool app_init(int argc, char **argv)
        glGetIntegerv(GL_SAMPLES, &aasamples);
        printf("got %d samples per pixel\n", aasamples);
 
+       printf("Max anisotropy: %d\n", glcaps.max_aniso);
+
        glEnable(GL_CULL_FACE);
        glEnable(GL_DEPTH_TEST);
        //glEnable(GL_LIGHTING);
@@ -93,13 +96,13 @@ void app_draw()
                        glMatrixMode(GL_PROJECTION);
                        glLoadMatrixf(goatvr_projection_matrix(i, 0.5, 1000.0));
 
-                       Mat4 view_mat = goatvr_view_matrix(i);
-                       view_mat.pre_rotate_x(deg_to_rad(cam_phi));
-                       view_mat.pre_rotate_y(deg_to_rad(cam_theta));
-                       view_mat.pre_translate(0, -cam_height, 0);
+                       view_matrix = goatvr_view_matrix(i);
+                       view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
+                       view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
+                       view_matrix.pre_translate(0, -cam_height, 0);
 
                        glMatrixMode(GL_MODELVIEW);
-                       glLoadMatrixf(view_mat[0]);
+                       glLoadMatrixf(view_matrix[0]);
 
                        draw_backdrop();
                }
@@ -114,13 +117,13 @@ void app_draw()
                // regular monoscopic mode
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-               Mat4 view_mat;
-               view_mat.pre_rotate_x(deg_to_rad(cam_phi));
-               view_mat.pre_rotate_y(deg_to_rad(cam_theta));
-               view_mat.pre_translate(0, -cam_height, 0);
+               view_matrix = Mat4::identity;
+               view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
+               view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
+               view_matrix.pre_translate(0, -cam_height, 0);
 
                glMatrixMode(GL_MODELVIEW);
-               glLoadMatrixf(view_mat[0]);
+               glLoadMatrixf(view_matrix[0]);
 
                draw_backdrop();
 
index bfe96cb..a95e117 100644 (file)
--- a/src/app.h
+++ b/src/app.h
@@ -1,10 +1,15 @@
 #ifndef APP_H_
 #define APP_H_
 
+#include "gmath/gmath.h"
+
 extern int win_width, win_height;
 extern float win_aspect;
 extern long time_msec;
 
+extern Mat4 view_matrix;
+
+
 bool app_init(int argc, char **argv);
 void app_cleanup();
 
index f9dbe22..b81a04a 100644 (file)
@@ -5,6 +5,7 @@
 #include "mesh.h"
 #include "meshgen.h"
 #include "backdrop.h"
+#include "app.h"
 
 static unsigned int sdr_grid;
 static Texture *tex_grid;
@@ -12,11 +13,11 @@ 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);
+static const Vec3 grid_color = Vec3(1.0, 0.07, 1.0);
+static const Vec3 mid_color = Vec3(0.133, 0.006, 0.612);
+static const Vec3 horiz_color = Vec3(0.612, 0.006, 1.0);
+static const Vec3 zenith_color = Vec3(0.029, 0.029, 0.029);
+static const Vec3 fog_color = Vec3(0.01, 0.01, 0.01);
 
 bool init_backdrop()
 {
@@ -38,11 +39,14 @@ bool init_backdrop()
                delete tex_grid;
                return false;
        }
+       tex_grid->set_anisotropy(glcaps.max_aniso);
 
        mesh_skydome = new Mesh;
        gen_sphere(mesh_skydome, 500.0, 32, 16, 1, 0.5);
        mesh_skydome->flip();
 
+       glClearColor(fog_color.x, fog_color.y, fog_color.z, 1.0);
+
        return true;
 }
 
@@ -56,9 +60,21 @@ void cleanup_backdrop()
 void draw_backdrop()
 {
        // draw sky
+       Mat4 view_mat_inf = view_matrix.upper3x3();
+       glPushMatrix();
+       glLoadMatrixf(view_mat_inf[0]);
+
+       glDisable(GL_DEPTH_TEST);
+       glDepthMask(0);
+
        bind_program(sdr_skydome);
        mesh_skydome->draw();
 
+       glEnable(GL_DEPTH_TEST);
+       glDepthMask(1);
+
+       glPopMatrix();
+
        // draw grid
        Mat4 xform;
        xform.scaling(500.0);
index 78b5591..5285d4b 100644 (file)
@@ -1,9 +1,22 @@
+#include <string.h>
 #include "opengl.h"
 
+struct GLCaps glcaps;
+
 int init_opengl(void)
 {
 #ifdef __glew_h__
        glewInit();
 #endif
+
+       memset(&glcaps, 0, sizeof glcaps);
+       glcaps.shaders = GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader;
+       glcaps.fsaa = GLEW_ARB_multisample;
+       glcaps.fbo = GLEW_ARB_framebuffer_object;
+       glcaps.shadow = GLEW_ARB_shadow || GLEW_SGIX_shadow;
+       if(GLEW_EXT_texture_filter_anisotropic) {
+               glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &glcaps.max_aniso);
+       }
+
        return 0;
 }
index 9acd561..29bfdf9 100644 (file)
 #define GL_DEPTH24_STENCIL8    GL_DEPTH24_STENCIL8_OES
 #endif
 
+struct GLCaps {
+       int shaders;
+       int fsaa;
+       int fbo;
+       int shadow;
+       int max_aniso;
+};
+extern struct GLCaps glcaps;
+
+
 #ifdef __cplusplus
 extern "C" {
 #endif
index 95d34e3..f332efa 100644 (file)
@@ -110,6 +110,14 @@ void Texture::set_filtering(unsigned int min_filt, unsigned int mag_filt)
        glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filt);
 }
 
+void Texture::set_anisotropy(int aniso)
+{
+       if(GLEW_EXT_texture_filter_anisotropic) {
+               glBindTexture(target, id);
+               glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
+       }
+}
+
 unsigned int Texture::get_format() const
 {
        return texfmt;
@@ -345,14 +353,17 @@ static int glfmt_from_ifmt(unsigned int ifmt)
        switch(ifmt) {
        case GL_LUMINANCE16F:
        case GL_LUMINANCE32F:
+       case GL_SLUMINANCE:
                return GL_LUMINANCE;
 
        case GL_RGB16F:
        case GL_RGB32F:
+       case GL_SRGB:
                return GL_RGB;
 
        case GL_RGBA16F:
        case GL_RGBA32F:
+       case GL_SRGB_ALPHA:
                return GL_RGBA;
 
        default:
@@ -385,17 +396,23 @@ static int glifmt_from_imgfmt(Image::Format fmt)
 {
        switch(fmt) {
        case Image::FMT_GREY:
-               return GL_LUMINANCE;
+               return GLEW_EXT_texture_sRGB ? GL_SLUMINANCE : GL_LUMINANCE;
+
        case Image::FMT_GREY_FLOAT:
                return GL_LUMINANCE16F;
+
        case Image::FMT_RGB:
-               return GL_RGB;
+               return GLEW_EXT_texture_sRGB ? GL_SRGB : GL_RGB;
+
        case Image::FMT_RGB_FLOAT:
                return GL_RGB16F;
+
        case Image::FMT_RGBA:
-               return GL_RGBA;
+               return GLEW_EXT_texture_sRGB ? GL_SRGB_ALPHA : GL_RGBA;
+
        case Image::FMT_RGBA_FLOAT:
                return GL_RGBA16F;
+
        default:
                break;
        }
@@ -406,14 +423,17 @@ static Image::Format imgfmt_from_glifmt(unsigned int ifmt)
 {
        switch(ifmt) {
        case GL_LUMINANCE:
+       case GL_SLUMINANCE:
                return Image::FMT_GREY;
        case GL_LUMINANCE16F:
                return Image::FMT_GREY_FLOAT;
        case GL_RGB:
+       case GL_SRGB:
                return Image::FMT_RGB;
        case GL_RGB16F:
                return Image::FMT_RGB_FLOAT;
        case GL_RGBA:
+       case GL_SRGB_ALPHA:
                return Image::FMT_RGBA;
        case GL_RGBA16F:
                return Image::FMT_RGBA_FLOAT;
index e39efe0..b1adf52 100644 (file)
@@ -31,6 +31,7 @@ public:
        void set_wrapping(unsigned int wrap);
        void set_filtering(unsigned int filt);
        void set_filtering(unsigned int min_filt, unsigned int mag_filt);
+       void set_anisotropy(int aniso);
 
        unsigned int get_format() const;