Dawn/src/dawnopengl/display/shaders/simpletexturedshader.c
2024-10-04 21:51:38 -05:00

120 lines
3.3 KiB
C

/**
* Copyright (c) 2023 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "simpletexturedshader.h"
shader_t SIMPLE_TEXTURED_SHADER;
void simpleTexturedShaderInit() {
shaderInit(&SIMPLE_TEXTURED_SHADER);
shaderCompile(&SIMPLE_TEXTURED_SHADER,
// Vertex Shader
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec2 aTexCoord;\n"
"layout (location = 2) in vec4 aColor;\n"
"uniform mat4 u_Projection;\n"
"uniform mat4 u_View;\n"
"uniform mat4 u_Model;\n"
"out vec2 o_TextCoord;\n"
"out vec4 v_Color;\n"
"void main() {\n"
"gl_Position = u_Projection * u_View * u_Model * vec4(aPos, 1.0);\n"
"o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
"v_Color = aColor;\n"
"}",
// Fragment Shader
"#version 330 core\n"
"in vec2 o_TextCoord;\n"
"in vec4 v_Color;\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"
"vec4 texColor = texture(u_Text, o_TextCoord);\n"
"if(texColor.r < 0.1) discard;\n"
"o_Color = u_Color * v_Color * vec4(1.0, 1.0, 1.0, texColor.r);\n"
"} else {\n"
"o_Color = u_Color;"
"}\n"
"}\n"
);
shaderBind(&SIMPLE_TEXTURED_SHADER);
simpleTexturedShaderSetColor(COLOR4F_WHITE);
simpleTexturedShaderSetTexture(NULL);
mat4 projection;
glm_mat4_identity(projection);
glm_perspective(45.0f, 16.0f/9.0f, 0.01f, 1000.0f, projection);
simpleTexturedShaderSetProjection(projection);
mat4 view;
glm_mat4_identity(view);
glm_lookat((vec3_t){ 5,5,5 }, (vec3_t){ 0, 0, 0 }, (vec3_t){ 0, 1, 0 }, view);
simpleTexturedShaderSetView(view);
mat4 model;
glm_mat4_identity(model);
glm_translate(model, (vec3_t){ 0, 0, 0 });
simpleTexturedShaderSetModel(model);
}
void simpleTexturedShaderBind() {
shaderBind(&SIMPLE_TEXTURED_SHADER);
}
void simpleTexturedShaderSetModel(const mat4_t model) {
shaderparameter_t param = shaderGetParameterByName(
&SIMPLE_TEXTURED_SHADER, "u_Model"
);
shaderSetMatrix(param, model);
}
void simpleTexturedShaderSetView(const mat4_t view) {
shaderparameter_t param = shaderGetParameterByName(
&SIMPLE_TEXTURED_SHADER, "u_View"
);
shaderSetMatrix(param, view);
}
void simpleTexturedShaderSetProjection(const mat4_t projection) {
shaderparameter_t param = shaderGetParameterByName(
&SIMPLE_TEXTURED_SHADER, "u_Projection"
);
shaderSetMatrix(param, projection);
}
void simpleTexturedShaderSetColor(const color4f_t color) {
shaderparameter_t param = shaderGetParameterByName(
&SIMPLE_TEXTURED_SHADER, "u_Color"
);
shaderSetVector4(param, color);
}
void simpleTexturedShaderSetTexture(const texture_t *texture) {
shaderparameter_t paramHas = shaderGetParameterByName(
&SIMPLE_TEXTURED_SHADER, "u_HasTexture"
);
if(texture == NULL) {
shaderSetBoolean(paramHas, false);
return;
}
shaderparameter_t param = shaderGetParameterByName(
&SIMPLE_TEXTURED_SHADER, "u_Text"
);
textureBind(texture, 0);
shaderSetTexture(param, 0);
shaderSetBoolean(paramHas, true);
}
void simpleTexturedShaderDispose() {
shaderDispose(&SIMPLE_TEXTURED_SHADER);
}