From: Daniel Karbach Date: Wed, 23 Sep 2015 16:21:48 +0000 (+0200) Subject: some skybox experiments X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=e821b85cf13376b11d125bdf5da4f16b20acf2e2;p=blank.git some skybox experiments it's soooooo small :( --- diff --git a/assets b/assets index 7f1ebb8..b965c83 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 7f1ebb8448f866ac28ea14b0dd8792edebedcf5a +Subproject commit b965c835cce06d762c9741f42dee34809bbf6a08 diff --git a/src/app/WorldState.cpp b/src/app/WorldState.cpp index b4f8e64..590f6ac 100644 --- a/src/app/WorldState.cpp +++ b/src/app/WorldState.cpp @@ -25,6 +25,7 @@ WorldState::WorldState( , chunk_renderer(*interface.GetPlayer().chunks) , skeletons() , spawner(world, skeletons, gc.seed) +, sky(env.loader.LoadCubeMap("skybox")) , preload(env, chunk_loader, chunk_renderer) , unload(env, world.Chunks(), save) { TextureIndex tex_index; @@ -94,6 +95,8 @@ void WorldState::Render(Viewport &viewport) { viewport.WorldPosition(player.Transform(player.ChunkCoords())); chunk_renderer.Render(viewport); world.Render(viewport); + sky.Render(viewport); + interface.Render(viewport); } diff --git a/src/app/WorldState.hpp b/src/app/WorldState.hpp index d5eac19..b48d1b9 100644 --- a/src/app/WorldState.hpp +++ b/src/app/WorldState.hpp @@ -5,6 +5,7 @@ #include "State.hpp" #include "UnloadState.hpp" #include "../ai/Spawner.hpp" +#include "../graphics/SkyBox.hpp" #include "../model/Skeletons.hpp" #include "../ui/Interface.hpp" #include "../world/BlockTypeRegistry.hpp" @@ -50,6 +51,8 @@ private: Skeletons skeletons; Spawner spawner; + SkyBox sky; + PreloadState preload; UnloadState unload; diff --git a/src/graphics/SkyBox.hpp b/src/graphics/SkyBox.hpp new file mode 100644 index 0000000..1254fe2 --- /dev/null +++ b/src/graphics/SkyBox.hpp @@ -0,0 +1,27 @@ +#ifndef BLANK_GRAPHICS_SKYBOX_HPP_ +#define BLANK_GRAPHICS_SKYBOX_HPP_ + +#include "CubeMap.hpp" +#include "../model/SkyBoxModel.hpp" + + +namespace blank { + +class Viewport; + +class SkyBox { + +public: + explicit SkyBox(CubeMap &&); + + void Render(Viewport &) noexcept; + +private: + CubeMap texture; + SkyBoxModel model; + +}; + +} + +#endif diff --git a/src/graphics/SkyBoxShader.hpp b/src/graphics/SkyBoxShader.hpp index 752298e..20adef7 100644 --- a/src/graphics/SkyBoxShader.hpp +++ b/src/graphics/SkyBoxShader.hpp @@ -15,11 +15,9 @@ public: void SetTexture(CubeMap &) noexcept; - void SetM(const glm::mat4 &m) noexcept; void SetProjection(const glm::mat4 &p) noexcept; void SetView(const glm::mat4 &v) noexcept; void SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept; - void SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept; const glm::mat4 &Projection() const noexcept { return projection; } const glm::mat4 &View() const noexcept { return view; } @@ -32,9 +30,7 @@ private: glm::mat4 view; glm::mat4 vp; - GLuint m_handle; - GLuint mv_handle; - GLuint mvp_handle; + GLuint vp_handle; GLuint sampler_handle; }; diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index 62a9c63..98da25a 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -499,22 +499,18 @@ void BlendedSprite::SetBG(const glm::vec4 &v) noexcept { SkyBoxShader::SkyBoxShader() : program() , vp(1.0f) -, m_handle(0) -, mv_handle(0) -, mvp_handle(0) +, vp_handle(0) , sampler_handle(0) { program.LoadShader( GL_VERTEX_SHADER, "#version 330 core\n" "layout(location = 0) in vec3 vtx_position;\n" - "uniform mat4 M;\n" - "uniform mat4 MV;\n" - "uniform mat4 MVP;\n" + "uniform mat4 VP;\n" "out vec3 vtx_viewspace;\n" "void main() {\n" - "gl_Position = MVP * vec4(vtx_position, 1);\n" + "gl_Position = VP * vec4(vtx_position, 1);\n" "gl_Position.z = gl_Position.w;\n" - "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n" + "vtx_viewspace = vtx_position;\n" "}\n" ); program.LoadShader( @@ -533,9 +529,7 @@ SkyBoxShader::SkyBoxShader() throw std::runtime_error("link program"); } - m_handle = program.UniformLocation("M"); - mv_handle = program.UniformLocation("MV"); - mvp_handle = program.UniformLocation("MVP"); + vp_handle = program.UniformLocation("VP"); sampler_handle = program.UniformLocation("tex_sampler"); } @@ -544,17 +538,6 @@ void SkyBoxShader::Activate() noexcept { program.Use(); } -void SkyBoxShader::SetM(const glm::mat4 &M) noexcept { - glm::mat4 m(M); - m[0].w = 0.0f; - m[1].w = 0.0f; - m[2].w = 0.0f; - m[3] = { 0.0f, 0.0f, 0.0f, 1.0f }; - program.Uniform(m_handle, m); - program.Uniform(mv_handle, view * m); - program.Uniform(mvp_handle, vp * m); -} - void SkyBoxShader::SetTexture(CubeMap &tex) noexcept { glActiveTexture(GL_TEXTURE0); tex.Bind(); @@ -564,6 +547,7 @@ void SkyBoxShader::SetTexture(CubeMap &tex) noexcept { void SkyBoxShader::SetProjection(const glm::mat4 &p) noexcept { projection = p; vp = p * view; + program.Uniform(vp_handle, vp); } void SkyBoxShader::SetView(const glm::mat4 &v) noexcept { @@ -571,8 +555,9 @@ void SkyBoxShader::SetView(const glm::mat4 &v) noexcept { view[0].w = 0.0f; view[1].w = 0.0f; view[2].w = 0.0f; - view[3] = { 0.0f, 0.0f, 0.0f, 1.0f }; + view[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); vp = projection * v; + program.Uniform(vp_handle, vp); } void SkyBoxShader::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept { @@ -580,11 +565,6 @@ void SkyBoxShader::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept { SetView(v); } -void SkyBoxShader::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept { - SetVP(v, p); - SetM(m); -} - PlainColor::PlainColor() : program() diff --git a/src/graphics/viewport.cpp b/src/graphics/viewport.cpp index 875da78..181c97c 100644 --- a/src/graphics/viewport.cpp +++ b/src/graphics/viewport.cpp @@ -1,5 +1,6 @@ #include "Camera.hpp" #include "Canvas.hpp" +#include "SkyBox.hpp" #include "Viewport.hpp" #include "../app/init.hpp" @@ -73,6 +74,19 @@ void Canvas::UpdateProjection() noexcept { } +SkyBox::SkyBox(CubeMap &&tex) +: texture(std::move(tex)) +, model() { + model.LoadUnitBox(); +} + +void SkyBox::Render(Viewport &viewport) noexcept { + SkyBoxShader &prog = viewport.SkyBoxProgram(); + prog.SetTexture(texture); + model.Draw(); +} + + Viewport::Viewport() : cam() , canv() @@ -140,6 +154,7 @@ void Viewport::Resize(int w, int h) noexcept { } else { entity_prog.SetProjection(Perspective()); } + sky_prog.SetProjection(Perspective()); sprite_prog.SetProjection(Ortho()); } @@ -228,6 +243,7 @@ SkyBoxShader &Viewport::SkyBoxProgram() noexcept { if (active_prog != SKY_BOX) { sky_prog.Activate(); DisableBlending(); + DisableBackfaceCulling(); EqualDepthTest(); active_prog = SKY_BOX; } @@ -247,6 +263,7 @@ BlendedSprite &Viewport::SpriteProgram() noexcept { void Viewport::WorldPosition(const glm::mat4 &t) noexcept { cam.View(glm::inverse(t)); chunk_prog.SetView(cam.View()); + sky_prog.SetView(cam.View()); } }