X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgraphics%2Fshader.cpp;h=5540dea4ef4b9454279332d17e33435ee7aed1f5;hb=2e3774eb3f2d5d23a08731175b168566457e2192;hp=938dc9772b1d4a5cd05fe4830846c897a78a77dd;hpb=7bb75960dbf9bfdee9ac865384aca81791b3da5c;p=blank.git diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index 938dc97..5540dea 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -1,10 +1,13 @@ #include "BlendedSprite.hpp" #include "BlockLighting.hpp" #include "DirectionalLighting.hpp" +#include "PlainColor.hpp" #include "Program.hpp" #include "Shader.hpp" +#include "SkyBoxShader.hpp" #include "ArrayTexture.hpp" +#include "CubeMap.hpp" #include "Texture.hpp" #include "../app/init.hpp" @@ -492,4 +495,140 @@ void BlendedSprite::SetBG(const glm::vec4 &v) noexcept { program.Uniform(bg_handle, v); } + +SkyBoxShader::SkyBoxShader() +: program() +, vp(1.0f) +, 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 VP;\n" + "out vec3 vtx_viewspace;\n" + "void main() {\n" + "gl_Position = VP * vec4(vtx_position, 1);\n" + "gl_Position.z = gl_Position.w;\n" + "vtx_viewspace = vtx_position;\n" + "}\n" + ); + program.LoadShader( + GL_FRAGMENT_SHADER, + "#version 330 core\n" + "in vec3 vtx_viewspace;\n" + "uniform samplerCube tex_sampler;\n" + "out vec3 color;\n" + "void main() {\n" + "color = texture(tex_sampler, vtx_viewspace).rgb;\n" + //"color = vec3(1,0,0);\n" + "}\n" + ); + program.Link(); + if (!program.Linked()) { + program.Log(std::cerr); + throw std::runtime_error("link program"); + } + + vp_handle = program.UniformLocation("VP"); + sampler_handle = program.UniformLocation("tex_sampler"); +} + + +void SkyBoxShader::Activate() noexcept { + program.Use(); +} + +void SkyBoxShader::SetTexture(CubeMap &tex) noexcept { + glActiveTexture(GL_TEXTURE0); + tex.Bind(); + program.Uniform(sampler_handle, GLint(0)); +} + +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 { + view = v; + view[0].w = 0.0f; + view[1].w = 0.0f; + view[2].w = 0.0f; + view[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); + vp = projection * view; + program.Uniform(vp_handle, vp); +} + +void SkyBoxShader::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept { + projection = p; + SetView(v); +} + + +PlainColor::PlainColor() +: program() +, vp(1.0f) +, mvp_handle(0) { + program.LoadShader( + GL_VERTEX_SHADER, + "#version 330 core\n" + "layout(location = 0) in vec3 vtx_position;\n" + "layout(location = 1) in vec3 vtx_color;\n" + "uniform mat4 MVP;\n" + "out vec3 frag_color;\n" + "void main() {\n" + "gl_Position = MVP * vec4(vtx_position, 1);\n" + "frag_color = vtx_color;\n" + "}\n" + ); + program.LoadShader( + GL_FRAGMENT_SHADER, + "#version 330 core\n" + "in vec3 frag_color;\n" + "out vec3 color;\n" + "void main() {\n" + "color = frag_color;\n" + "}\n" + ); + program.Link(); + if (!program.Linked()) { + program.Log(std::cerr); + throw std::runtime_error("link program"); + } + + mvp_handle = program.UniformLocation("MVP"); +} + + +void PlainColor::Activate() noexcept { + program.Use(); +} + +void PlainColor::SetM(const glm::mat4 &m) noexcept { + program.Uniform(mvp_handle, vp * m); +} + +void PlainColor::SetProjection(const glm::mat4 &p) noexcept { + projection = p; + vp = p * view; +} + +void PlainColor::SetView(const glm::mat4 &v) noexcept { + view = v; + vp = projection * v; +} + +void PlainColor::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept { + projection = p; + view = v; + vp = p * v; +} + +void PlainColor::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept { + SetVP(v, p); + SetM(m); +} + }