From a52f54b3e62ea9465444cd9d4b762bdcc2be28f2 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sun, 2 Apr 2023 19:00:51 -0700 Subject: [PATCH] idk shaders --- src/dawnopengl/display/shader/CMakeLists.txt | 1 - ...rogram.cpp => SimpleBillboardedShader.cpp} | 48 ++-------- .../shader/SimpleBillboardedShader.hpp | 34 +++++++ .../display/shader/SimpleTexturedShader.cpp | 89 +++++++++++++++++++ .../display/shader/SimpleTexturedShader.hpp | 13 ++- .../shader/SimpleTexturedShaderProgram.hpp | 21 ----- .../display/shader/UIShaderProgram.hpp | 2 +- 7 files changed, 142 insertions(+), 66 deletions(-) rename src/dawnopengl/display/shader/{SimpleTexturedShaderProgram.cpp => SimpleBillboardedShader.cpp} (53%) create mode 100644 src/dawnopengl/display/shader/SimpleBillboardedShader.hpp delete mode 100644 src/dawnopengl/display/shader/SimpleTexturedShaderProgram.hpp diff --git a/src/dawnopengl/display/shader/CMakeLists.txt b/src/dawnopengl/display/shader/CMakeLists.txt index 3aa65e2a..7e90d7bb 100644 --- a/src/dawnopengl/display/shader/CMakeLists.txt +++ b/src/dawnopengl/display/shader/CMakeLists.txt @@ -8,5 +8,4 @@ target_sources(${DAWN_TARGET_NAME} PRIVATE ShaderProgram.cpp SimpleTexturedShader.cpp - SimpleTexturedShaderProgram.cpp ) \ No newline at end of file diff --git a/src/dawnopengl/display/shader/SimpleTexturedShaderProgram.cpp b/src/dawnopengl/display/shader/SimpleBillboardedShader.cpp similarity index 53% rename from src/dawnopengl/display/shader/SimpleTexturedShaderProgram.cpp rename to src/dawnopengl/display/shader/SimpleBillboardedShader.cpp index 7a50cdbb..92d343df 100644 --- a/src/dawnopengl/display/shader/SimpleTexturedShaderProgram.cpp +++ b/src/dawnopengl/display/shader/SimpleBillboardedShader.cpp @@ -3,11 +3,11 @@ // This software is released under the MIT License. // https://opensource.org/licenses/MIT -#include "SimpleTexturedShaderProgram.hpp" +#include "SimpleBillboardedShader.hpp" using namespace Dawn; -void SimpleTexturedShaderProgram::compile() { +void SimpleBillboardedShaderProgram::compile() { #if DAWN_OPENGL_GLSL this->compileShader( { @@ -25,7 +25,10 @@ void SimpleTexturedShaderProgram::compile() { "out vec2 o_TextCoord;\n" "void main() {\n" - "gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n" + "vec3 camRight = vec3(view[0][0], view[1][0], view[2][0]);\n" + "vec3 camUp = vec3(view[0][1], view[1][1], view[2][1]);\n" + "vec3 billboardPos = view[3].xyz + position.x * camRight + position.y * camUp;\n" + "gl_Position = u_Proj * u_View * u_Model * vec4(billboardPos, 1.0);\n" "o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n" "}", @@ -45,45 +48,6 @@ void SimpleTexturedShaderProgram::compile() { "}\n" "}\n" ); - #elif DAWN_OPENGL_HLSL - this->compileShader( - { - { "aPos", 0 }, - { "aTexCoord", 1 } - }, - // Vertex Shader - "uniform float4x4 u_Proj;\n" - "uniform float4x4 u_View;\n" - "uniform float4x4 u_Model;\n" - "void main(" - "float3 aPos,\n" - "float2 aTexCoord,\n" - "float2 out o_TextCoord : TEXCOORD0,\n" - "float4 out gl_Position : POSITION\n" - ") {\n" - "o_TextCoord = aTexCoord;\n" - "gl_Position = mul(mul(mul(float4(aPos, 1.0), u_Model), u_View), u_Proj);\n" - "}", - - // Fragment Shader - "uniform float4 u_Color;\n" - "uniform bool u_HasTexture;\n" - "uniform sampler2D u_Text : TEXUNIT0;\n" - - "float4 main(\n" - "float2 o_TextCoord : TEXCOORD0\n" - ") {\n" - "float4 o_Color;\n" - "if(u_HasTexture) {\n" - "o_Color = mul(tex2D(u_Text, o_TextCoord), u_Color);\n" - "} else {\n" - "o_Color = u_Color;\n" - "}\n" - "return o_Color;\n" - "}\n" - ); - #else - #error Shader Type must be either GLSL or HLSL #endif this->paramProjection = this->getParameterByName("u_Proj"); diff --git a/src/dawnopengl/display/shader/SimpleBillboardedShader.hpp b/src/dawnopengl/display/shader/SimpleBillboardedShader.hpp new file mode 100644 index 00000000..ef041323 --- /dev/null +++ b/src/dawnopengl/display/shader/SimpleBillboardedShader.hpp @@ -0,0 +1,34 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "display/shader/Shader.hpp" + +namespace Dawn { + class SimpleBillboardedShader : public ShaderProgram { + public: + shaderparameter_t paramProjection; + shaderparameter_t paramView; + shaderparameter_t paramModel; + shaderparameter_t paramColor; + shaderparameter_t paramTexture; + shaderparameter_t paramHasTexture; + + void compile() override; + }; + + class SimpleBillboardedShader : public Shader { + public: + SimpleBillboardedShaderProgram program; + + void compile() override; + + std::vector getPassItems( + Mesh *mesh, + Material *material, + Camera *camera + ) override; + }; +} \ No newline at end of file diff --git a/src/dawnopengl/display/shader/SimpleTexturedShader.cpp b/src/dawnopengl/display/shader/SimpleTexturedShader.cpp index 78acefcb..baac27ab 100644 --- a/src/dawnopengl/display/shader/SimpleTexturedShader.cpp +++ b/src/dawnopengl/display/shader/SimpleTexturedShader.cpp @@ -9,6 +9,95 @@ using namespace Dawn; +void SimpleTexturedShaderProgram::compile() { + #if DAWN_OPENGL_GLSL + this->compileShader( + { + { "aPos", 0 }, + { "aTexCoord", 1 } + }, + // Vertex Shader + "#version 330 core\n" + "layout (location = 0) in vec3 aPos;\n" + "layout (location = 1) in vec2 aTexCoord;\n" + + "uniform mat4 u_Proj;\n" + "uniform mat4 u_View;\n" + "uniform mat4 u_Model;\n" + + "out vec2 o_TextCoord;\n" + "void main() {\n" + "gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n" + "o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n" + "}", + + // Fragment Shader + "#version 330 core\n" + "in vec2 o_TextCoord;\n" + "out vec4 o_Color;\n" + "uniform vec4 u_Color;\n" + "uniform bool u_HasTexture;\n" + "uniform sampler2D u_Text;\n" + + "void main() {\n" + "if(u_HasTexture) {\n" + "o_Color = texture(u_Text, o_TextCoord) * u_Color;\n" + "} else {\n" + "o_Color = u_Color;" + "}\n" + "}\n" + ); + #elif DAWN_OPENGL_HLSL + this->compileShader( + { + { "aPos", 0 }, + { "aTexCoord", 1 } + }, + // Vertex Shader + "uniform float4x4 u_Proj;\n" + "uniform float4x4 u_View;\n" + "uniform float4x4 u_Model;\n" + "void main(" + "float3 aPos,\n" + "float2 aTexCoord,\n" + "float2 out o_TextCoord : TEXCOORD0,\n" + "float4 out gl_Position : POSITION\n" + ") {\n" + "o_TextCoord = aTexCoord;\n" + "gl_Position = mul(mul(mul(float4(aPos, 1.0), u_Model), u_View), u_Proj);\n" + "}", + + // Fragment Shader + "uniform float4 u_Color;\n" + "uniform bool u_HasTexture;\n" + "uniform sampler2D u_Text : TEXUNIT0;\n" + + "float4 main(\n" + "float2 o_TextCoord : TEXCOORD0\n" + ") {\n" + "float4 o_Color;\n" + "if(u_HasTexture) {\n" + "o_Color = mul(tex2D(u_Text, o_TextCoord), u_Color);\n" + "} else {\n" + "o_Color = u_Color;\n" + "}\n" + "return o_Color;\n" + "}\n" + ); + #else + #error Shader Type must be either GLSL or HLSL + #endif + + this->paramProjection = this->getParameterByName("u_Proj"); + this->paramView = this->getParameterByName("u_View"); + this->paramModel = this->getParameterByName("u_Model"); + this->paramColor = this->getParameterByName("u_Color"); + this->paramTexture = this->getParameterByName("u_Text"); + this->paramHasTexture = this->getParameterByName("u_HasTexture"); +} + + + void SimpleTexturedShader::compile() { this->program.compile(); } diff --git a/src/dawnopengl/display/shader/SimpleTexturedShader.hpp b/src/dawnopengl/display/shader/SimpleTexturedShader.hpp index 0ab4370a..055b2c75 100644 --- a/src/dawnopengl/display/shader/SimpleTexturedShader.hpp +++ b/src/dawnopengl/display/shader/SimpleTexturedShader.hpp @@ -5,10 +5,21 @@ #pragma once #include "display/shader/Shader.hpp" -#include "SimpleTexturedShaderProgram.hpp" #include "scene/components/display/material/SimpleTexturedMaterial.hpp" namespace Dawn { + class SimpleTexturedShaderProgram : public ShaderProgram { + public: + shaderparameter_t paramProjection; + shaderparameter_t paramView; + shaderparameter_t paramModel; + shaderparameter_t paramColor; + shaderparameter_t paramTexture; + shaderparameter_t paramHasTexture; + + void compile() override; + }; + class SimpleTexturedShader : public Shader { public: SimpleTexturedShaderProgram program; diff --git a/src/dawnopengl/display/shader/SimpleTexturedShaderProgram.hpp b/src/dawnopengl/display/shader/SimpleTexturedShaderProgram.hpp deleted file mode 100644 index 1179b35c..00000000 --- a/src/dawnopengl/display/shader/SimpleTexturedShaderProgram.hpp +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) 2022 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "display/shader/Shader.hpp" - -namespace Dawn { - class SimpleTexturedShaderProgram : public ShaderProgram { - public: - shaderparameter_t paramProjection; - shaderparameter_t paramView; - shaderparameter_t paramModel; - shaderparameter_t paramColor; - shaderparameter_t paramTexture; - shaderparameter_t paramHasTexture; - - void compile() override ; - }; -} \ No newline at end of file diff --git a/src/dawnopengl/display/shader/UIShaderProgram.hpp b/src/dawnopengl/display/shader/UIShaderProgram.hpp index a055b1f5..73ea1a7b 100644 --- a/src/dawnopengl/display/shader/UIShaderProgram.hpp +++ b/src/dawnopengl/display/shader/UIShaderProgram.hpp @@ -4,7 +4,7 @@ // https://opensource.org/licenses/MIT #pragma once -#include "SimpleTexturedShaderProgram.hpp" +#include "SimpleTexturedShader.hpp" #define UI_SHADER_PROGRAM_PRIORITY 100