skybox working
authorEleni Maria Stea <estea@igalia.com>
Sun, 30 Jul 2017 20:24:40 +0000 (23:24 +0300)
committerEleni Maria Stea <estea@igalia.com>
Sun, 30 Jul 2017 20:24:40 +0000 (23:24 +0300)
Makefile
gl_shaders/sky.f.glsl [new file with mode: 0644]
gl_shaders/sky.v.glsl [new file with mode: 0644]
src/gfxapi.cc
src/gfxapi.h
src/main.cc
src/opengl/mesh-gl.cc
src/opengl/opengl.cc
src/renderer.cc
src/renderer.h

index 8d6f87e..fce6d9a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ bin = demo
 
 dbg = -g
 opt = -O0
-inc = -Isrc -Iexternal/gmath/src -I/usr/include -Iexternal/libimago/src 
+inc = -Isrc -Iexternal/gmath/src -I/usr/include -Iexternal/libimago/src -I/usr/local/include
 libs = -lGL -lGLEW -lglfw -lvulkan -lassimp -lgmath -limago
 
 CXX = g++
diff --git a/gl_shaders/sky.f.glsl b/gl_shaders/sky.f.glsl
new file mode 100644 (file)
index 0000000..d5009c0
--- /dev/null
@@ -0,0 +1,14 @@
+#version 450
+
+uniform samplerCube stex;
+
+in vec3 normal;
+out vec4 color;
+
+void main()
+{
+       vec4 texel = textureCube(stex, normalize(normal));
+
+       color.rgb = texel.rgb;
+       color.a = 1.0;
+}
\ No newline at end of file
diff --git a/gl_shaders/sky.v.glsl b/gl_shaders/sky.v.glsl
new file mode 100644 (file)
index 0000000..84097a0
--- /dev/null
@@ -0,0 +1,14 @@
+#version 450
+
+uniform mat4 mviewproj;
+
+layout(location = 1) in vec3 attr_pos;
+layout(location = 2) in vec3 attr_normal;
+
+out vec3 normal;
+
+void main()
+{
+       gl_Position = mviewproj * vec4(attr_pos, 1.0);
+       normal = attr_normal;
+}
\ No newline at end of file
index 3bda079..2987a59 100644 (file)
@@ -14,6 +14,8 @@ static Gfx_API api;
 
 void (*gfx_clear)(float r, float g, float b);
 void (*gfx_viewport)(int x, int y, int width, int height);
+void (*gfx_zbuffer)(bool enable);
+void (*gfx_cull_face)(Gfx_cull_face cf);
 
 
 bool gfx_init(Gfx_API api)
index d3dcd4d..1e6cd2b 100644 (file)
@@ -11,8 +11,16 @@ enum Gfx_API {
        GFX_VK
 };
 
+enum Gfx_cull_face {
+       GFX_CULL_NONE,
+       GFX_CULL_FRONT,
+       GFX_CULL_BACK
+};
+
 extern void (*gfx_clear)(float r, float g, float b);
 extern void (*gfx_viewport)(int x, int y, int width, int height);
+extern void (*gfx_zbuffer)(bool enable);
+extern void (*gfx_cull_face)(Gfx_cull_face cf);
 
 bool gfx_init(Gfx_API api);
 void gfx_cleanup();
index 6d7f4e7..7061bb6 100644 (file)
@@ -16,6 +16,7 @@
 #include "object.h"
 #include "renderer.h"
 #include "scene.h"
+#include "texture.h"
 
 #include "opengl/opengl.h"
 #include "vulkan/vk.h"
@@ -57,6 +58,7 @@ static Renderer *rcow;
 
 static Scene *scene_ground;
 static Renderer *rground; // default renderer
+static Texture *gskybox;
 
 /* *** */
 
@@ -135,6 +137,10 @@ static bool init(Gfx_API api)
                return false;
        }
 
+       gskybox = gfx_create_texture();
+       gskybox->load("data/cubemap/cubemap.jpg");
+       rground->set_sky_tex(gskybox);
+
        rcow = new Renderer;
        rcow->camera = camera;
        rcow->scene = scene_cow;
index 119d043..4707f11 100644 (file)
@@ -1,4 +1,5 @@
 #include <GL/glew.h>
+#include <GL/gl.h>
 #include <gmath/gmath.h>
 
 #include "mesh-gl.h"
index ffa1d47..367e283 100644 (file)
@@ -10,6 +10,8 @@ extern int win_w;
 
 static void clear(float r, float g, float b);
 static void viewport(int x, int y, int width, int height);
+static void zbuffer(bool enable);
+static void cull_face(Gfx_cull_face cf);
 
 bool init_opengl()
 {
@@ -36,6 +38,8 @@ bool init_opengl()
 
        gfx_clear = clear;
        gfx_viewport = viewport;
+       gfx_zbuffer = zbuffer;
+       gfx_cull_face = cull_face;
 
        return true;
 }
@@ -57,4 +61,29 @@ static void clear(float r, float g, float b)
 static void viewport(int x, int y, int width, int height)
 {
        glViewport(x, y, width, height);
+}
+
+static void zbuffer(bool enable)
+{
+       if(enable)
+               glEnable(GL_DEPTH_TEST);
+       else
+               glDisable(GL_DEPTH_TEST);
+}
+
+static void cull_face(Gfx_cull_face cf)
+{
+       switch(cf) {
+       case GFX_CULL_NONE:
+               glDisable(GL_CULL_FACE);
+               break;
+       case GFX_CULL_FRONT:
+               glEnable(GL_CULL_FACE);
+               glCullFace(GL_FRONT);
+               break;
+       case GFX_CULL_BACK:
+               glEnable(GL_CULL_FACE);
+               glCullFace(GL_BACK);
+               break;
+       }
 }
\ No newline at end of file
index 10dd766..e11b973 100644 (file)
@@ -77,6 +77,10 @@ void Renderer::draw() const
        if(!camera || !scene)
                return;
 
+       if(skytex) {
+               draw_skybox();
+       }
+
        if(!sprog->link())
                return;
 
@@ -123,7 +127,7 @@ void Renderer::set_sky_tex(Texture *stex)
        if(!skyprog) {
                if(!(skyprog = sdr_man->create_shader_program("sky.v.glsl", "sky.f.glsl")))
                        return;
-               if((viewproj_loc = skyprog->get_uniform_location("viewproj") == -1))
+               if((viewproj_loc = skyprog->get_uniform_location("mviewproj") == -1))
                        return;
        }
        skytex = stex;
@@ -134,11 +138,14 @@ void Renderer::set_diffuse_sky_tex(Texture *dstex)
        dskytex = dstex;
 }
 
-void Renderer::draw_skybox()
+void Renderer::draw_skybox() const
 {
        if(!skymesh || !skytex)
                return;
 
+       gfx_zbuffer(false);
+       gfx_cull_face(GFX_CULL_NONE);
+
        skytex->bind();
        skyprog->use();
 
@@ -147,4 +154,7 @@ void Renderer::draw_skybox()
        skyprog->set_uniform_matrix(viewproj_loc, mviewproj);
 
        skymesh->draw();
+
+       gfx_cull_face(GFX_CULL_BACK);
+       gfx_zbuffer(true);
 }
\ No newline at end of file
index 07db322..a218dab 100644 (file)
@@ -19,7 +19,7 @@ protected:
        Texture *skytex, *dskytex;
 
        virtual void draw_object(Object *object) const;
-       virtual void draw_skybox();
+       virtual void draw_skybox() const;
 
 public:
        Scene *scene;