Dawn/src/dawnopengl/display/shader/SimpleTexturedShader.cpp
2024-11-25 22:12:03 -06:00

104 lines
2.5 KiB
C++

// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "display/shader/SimpleTexturedShader.hpp"
using namespace Dawn;
void SimpleTexturedShader::getStages(
const enum ShaderOpenGLVariant variant,
const struct SimpleTexturedShaderData *rel,
std::vector<std::shared_ptr<ShaderStage>> &stages,
std::vector<struct ShaderParameter> &parameters,
std::vector<struct IShaderStructure> &structures
) {
// Stages
std::shared_ptr<ShaderStage> vertex;
std::shared_ptr<ShaderStage> fragment;
switch(variant) {
case ShaderOpenGLVariant::GLSL_330_CORE:
vertex = std::make_shared<ShaderStage>(
ShaderStageType::VERTEX,R"(
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
uniform mat4 u_Projection;
uniform mat4 u_View;
uniform mat4 u_Model;
out vec2 o_TextCoord;
void main() {
gl_Position = u_Projection * u_View * u_Model * vec4(aPos, 1.0);
o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);
}
)"
);
fragment = std::make_shared<ShaderStage>(
ShaderStageType::FRAGMENT,R"(
#version 330 core
in vec2 o_TextCoord;
out vec4 o_Color;
uniform vec4 u_Color;
uniform bool u_HasTexture;
uniform sampler2D u_Texture;
void main() {
if(u_HasTexture) {
o_Color = texture(u_Texture, o_TextCoord);
} else {
o_Color = u_Color;
}
if(o_Color.a == 0) discard;
}
)"
);
break;
default:
assertUnreachable("Unsupported ShaderOpenGLVariant");
}
// Add stages
stages.push_back(vertex);
stages.push_back(fragment);
// Parameters
parameters.push_back(ShaderParameter(
"u_Projection",
&rel->projection,
ShaderParameterType::MAT4
));
parameters.push_back(ShaderParameter(
"u_View",
&rel->view,
ShaderParameterType::MAT4
));
parameters.push_back(ShaderParameter(
"u_Model",
&rel->model,
ShaderParameterType::MAT4
));
parameters.push_back(ShaderParameter(
"u_Color",
&rel->color,
ShaderParameterType::COLOR
));
parameters.push_back(ShaderParameter(
"u_HasTexture",
&rel->hasTexture,
ShaderParameterType::BOOLEAN
));
parameters.push_back(ShaderParameter(
"u_Texture",
&rel->texture,
ShaderParameterType::TEXTURE
));
}