// Copyright (c) 2023 Dominic Masters // // This software is released under the MIT License. // https://opensource.org/licenses/MIT #include "SimpleTexturedShader.hpp" using namespace Dawn; void SimpleTexturedShader::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"); }