Shader material ECS example

This commit is contained in:
2026-04-10 12:48:05 -05:00
parent 37cfdde1ee
commit 673d8e0a18
20 changed files with 349 additions and 64 deletions
+32 -6
View File
@@ -20,6 +20,8 @@ errorret_t shaderInitGL(shadergl_t *shader, const shaderdefinitiongl_t *def) {
assertNotNull(def, "Shader definition cannot be null");
memoryZero(shader, sizeof(shadergl_t));
shader->definition = def;
#ifdef DUSK_OPENGL_LEGACY
glm_mat4_identity(shader->view);
glm_mat4_identity(shader->proj);
@@ -31,8 +33,6 @@ errorret_t shaderInitGL(shadergl_t *shader, const shaderdefinitiongl_t *def) {
assertNotNull(def->vert, "Vertex shader source cannot be null");
assertNotNull(def->frag, "Fragment shader source cannot be null");
shader->setTexture = def->setTexture;
// Create vertex shader
shader->vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
errorret_t err = errorGLCheck();
@@ -237,10 +237,36 @@ errorret_t shaderSetTextureGL(
errorChain(errorGLCheck());
#else
if(shader->setTexture == NULL) {
assertUnreachable("Shader does not support setting textures.");
}
errorChain(shader->setTexture(shader, name, texture));
assertNotNull(shader->definition, "Shader definition cannot be null");
assertNotNull(shader->definition->setTexture, "Shader cannot do textures.");
errorChain(shader->definition->setTexture(shader, name, texture));
#endif
errorOk();
}
errorret_t shaderSetColorGL(
shadergl_t *shader,
const char_t *name,
color_t color
) {
assertNotNull(shader, "Shader cannot be null");
assertStrLenMin(name, 1, "Uniform name cannot be empty");
#ifdef DUSK_OPENGL_LEGACY
glColor4ub(color.r, color.g, color.b, color.a);
errorChain(errorGLCheck());
#else
GLint location;
errorChain(shaderParamGetLocationGL(shader, name, &location));
glUniform4f(
location,
color.r / 255.0f,
color.g / 255.0f,
color.b / 255.0f,
color.a / 255.0f
);
errorChain(errorGLCheck());
#endif
errorOk();
+21 -17
View File
@@ -10,6 +10,7 @@
#include "display/texture/texture.h"
typedef struct shadergl_s shadergl_t;
typedef union shadermaterial_u shadermaterial_t;
typedef errorret_t (*shadersettexturefn_t)(
shadergl_t *,
@@ -17,7 +18,22 @@ typedef errorret_t (*shadersettexturefn_t)(
texture_t *
);
typedef struct {
errorret_t (*setMaterial)(shadergl_t *, const shadermaterial_t *);
#ifdef DUSK_OPENGL_LEGACY
void *nothing;
#else
errorret_t (*setTexture)(shadergl_t *, const char_t *, texture_t *);
const char_t *vert;
const char_t *frag;
#endif
} shaderdefinitiongl_t;
typedef struct shadergl_s {
const shaderdefinitiongl_t *definition;
#ifdef DUSK_OPENGL_LEGACY
mat4 view;
mat4 proj;
@@ -26,21 +42,9 @@ typedef struct shadergl_s {
GLuint shaderProgramId;
GLuint vertexShaderId;
GLuint fragmentShaderId;
shadersettexturefn_t setTexture;
#endif
} shadergl_t;
typedef struct {
#ifdef DUSK_OPENGL_LEGACY
void *nothing;
#else
const char_t *vert;
const char_t *frag;
shadersettexturefn_t setTexture;
#endif
} shaderdefinitiongl_t;
#if DUSK_OPENGL_LEGACY
typedef struct {
shadergl_t *boundShader;
@@ -121,11 +125,11 @@ errorret_t shaderSetTextureGL(
* @param color The color data to set.
* @return An errorret_t indicating success or failure.
*/
// errorret_t shaderSetColorGL(
// shadergl_t *shader,
// const char_t *name,
// color_t color
// );
errorret_t shaderSetColorGL(
shadergl_t *shader,
const char_t *name,
color_t color
);
/**
* Disposes of a shader, freeing any associated resources.
+1 -1
View File
@@ -15,5 +15,5 @@ typedef shaderdefinitiongl_t shaderdefinitionplatform_t;
#define shaderBindPlatform shaderBindGL
#define shaderSetMatrixPlatform shaderSetMatrixGL
#define shaderSetTexturePlatform shaderSetTextureGL
// #define shaderSetColorPlatform shaderSetColorGL
#define shaderSetColorPlatform shaderSetColorGL
#define shaderDisposePlatform shaderDisposeGL
+21 -13
View File
@@ -9,7 +9,10 @@
#include "assert/assertgl.h"
#ifdef DUSK_OPENGL_LEGACY
shaderdefinition_t SHADER_UNLIT_DEFINITION = { 0 };
shaderdefinition_t SHADER_UNLIT_DEFINITION = {
.platform = { 0 },
.upload = shaderUnlitUpload
};
#else
errorret_t shaderUnlitSetTextureGL(
shadergl_t *shader,
@@ -75,7 +78,12 @@
errorOk();
}
shaderdefinition_t SHADER_UNLIT_DEFINITION = {
.setMaterial = shaderUnlitSetMaterial,
.setTexture = shaderUnlitSetTextureGL,
.vert =
#ifdef DUSK_OPENGL_ES
"#version 300 es\n"
@@ -115,6 +123,8 @@
" v_TexCoord = a_TexCoord;\n"
"}\n",
#endif
.frag =
#ifdef DUSK_OPENGL_ES
"#version 300 es\n"
@@ -124,6 +134,7 @@
"uniform int u_TextureType;\n"
"uniform uint u_Colors[256];\n"// For paletted textures.
"uniform int u_ColorCount;\n"
"uniform vec4 u_Color;\n"
// Fragment shader inputs
"in vec4 v_Color;\n"
"in vec2 v_TexCoord;\n"
@@ -131,11 +142,11 @@
"out vec4 FragColor;\n"
"void main() {\n"
" if(u_TextureType == 0) {\n"// No texture
" FragColor = v_Color;\n"
" FragColor = v_Color * u_Color;\n"
" return;\n"
" }\n"
" if(u_TextureType == 1) {\n"// Regular texture
" FragColor = texture(u_Texture, v_TexCoord) * v_Color;\n"
" FragColor = texture(u_Texture, v_TexCoord) * v_Color * u_Color;\n"
" return;\n"
" }\n"
" if(u_TextureType == 2) {\n"// Paletted texture
@@ -146,11 +157,10 @@
" float g = float((palColor >> 16) & 0xFFu) / 255.0;\n"
" float b = float((palColor >> 8) & 0xFFu) / 255.0;\n"
" float a = float((palColor >> 0) & 0xFFu) / 255.0;\n"
" vec4 paletteColor = vec4(r, g, b, a);\n"
" FragColor = paletteColor;\n"
" FragColor = vec4(r, g, b, a) * u_Color;\n"
" return;\n"
" }\n"
" FragColor = v_Color;\n"// Unknown texture type?
" FragColor = v_Color * u_Color;\n"// Unknown texture type?
"}\n",
#else
"#version 330 core\n"
@@ -159,6 +169,7 @@
"uniform int u_TextureType;\n"
"uniform uint u_Colors[256];\n"// For paletted textures.
"uniform int u_ColorCount;\n"
"uniform vec4 u_Color;\n"
// Fragment shader inputs
"in vec4 v_Color;\n"
"in vec2 v_TexCoord;\n"
@@ -166,11 +177,11 @@
"out vec4 FragColor;\n"
"void main() {\n"
" if(u_TextureType == 0) {\n"// No texture
" FragColor = v_Color;\n"
" FragColor = v_Color * u_Color;\n"
" return;\n"
" }\n"
" if(u_TextureType == 1) {\n"// Regular texture
" FragColor = texture(u_Texture, v_TexCoord) * v_Color;\n"
" FragColor = texture(u_Texture, v_TexCoord) * v_Color * u_Color;\n"
" return;\n"
" }\n"
" if(u_TextureType == 2) {\n"// Paletted texture
@@ -181,14 +192,11 @@
" float g = float((palColor >> 16) & 0xFFu) / 255.0;\n"
" float b = float((palColor >> 8) & 0xFFu) / 255.0;\n"
" float a = float((palColor >> 0) & 0xFFu) / 255.0;\n"
" vec4 paletteColor = vec4(r, g, b, a);\n"
" FragColor = paletteColor;\n"
" FragColor = vec4(r, g, b, a) * u_Color;\n"
" return;\n"
" }\n"
" FragColor = v_Color;\n"// Unknown texture type?
" FragColor = v_Color * u_Color;\n"// Unknown texture type?
"}\n",
#endif
.setTexture = shaderUnlitSetTextureGL
};
#endif