Make color optional

This commit is contained in:
2026-04-13 12:29:06 -05:00
parent c8abd374fe
commit c9cd91cbd8
16 changed files with 242 additions and 82 deletions
+12 -10
View File
@@ -57,16 +57,18 @@ errorret_t assetMeshLoader(assetfile_t *file) {
// Fix endianess of of data
for(uint8_t j = 0; j < 3; j++) {
verts[i * 3 + j].color.r = (uint8_t)(endianLittleToHostFloat(
triData.normal[0]
) * 255.0f);
verts[i * 3 + j].color.g = (uint8_t)(endianLittleToHostFloat(
triData.normal[1]
) * 255.0f);
verts[i * 3 + j].color.b = (uint8_t)(endianLittleToHostFloat(
triData.normal[2]
) * 255.0f);
verts[i * 3 + j].color.a = 0xFF;
#if MESH_ENABLE_COLOR
verts[i * 3 + j].color.r = (uint8_t)(endianLittleToHostFloat(
triData.normal[0]
) * 255.0f);
verts[i * 3 + j].color.g = (uint8_t)(endianLittleToHostFloat(
triData.normal[1]
) * 255.0f);
verts[i * 3 + j].color.b = (uint8_t)(endianLittleToHostFloat(
triData.normal[2]
) * 255.0f);
verts[i * 3 + j].color.a = 0xFF;
#endif
verts[i * 3 + j].uv[0] = 0.0f; // No UV data in STL, just set to 0
verts[i * 3 + j].uv[1] = 0.0f;
+16 -7
View File
@@ -25,13 +25,22 @@ errorret_t cubeInit() {
}
// Helper macro: set one vertex position, UV and color.
#define CUBE_VERT(i, px, py, pz, u, v) \
vertices[i].pos[0] = (px); \
vertices[i].pos[1] = (py); \
vertices[i].pos[2] = (pz); \
vertices[i].uv[0] = (u); \
vertices[i].uv[1] = (v); \
vertices[i].color = color
#if MESH_ENABLE_COLOR
#define CUBE_VERT(i, px, py, pz, u, v) \
vertices[i].color = color; \
vertices[i].pos[0] = (px); \
vertices[i].pos[1] = (py); \
vertices[i].pos[2] = (pz); \
vertices[i].uv[0] = (u); \
vertices[i].uv[1] = (v);
#else
#define CUBE_VERT(i, px, py, pz, u, v) \
vertices[i].pos[0] = (px); \
vertices[i].pos[1] = (py); \
vertices[i].pos[2] = (pz); \
vertices[i].uv[0] = (u); \
vertices[i].uv[1] = (v);
#endif
void cubeBuffer(
meshvertex_t *vertices,
+8 -1
View File
@@ -9,11 +9,18 @@
#include "dusk.h"
#include "display/color.h"
#ifndef MESH_ENABLE_COLOR
#define MESH_ENABLE_COLOR 0
#endif
#define MESH_VERTEX_UV_SIZE 2
#define MESH_VERTEX_POS_SIZE 3
typedef struct {
color_t color;
#if MESH_ENABLE_COLOR
color_t color;
#endif
float_t uv[MESH_VERTEX_UV_SIZE];
float_t pos[MESH_VERTEX_POS_SIZE];
} meshvertex_t;
+90 -20
View File
@@ -10,13 +10,55 @@
mesh_t QUAD_MESH_SIMPLE;
meshvertex_t QUAD_MESH_SIMPLE_VERTICES[QUAD_VERTEX_COUNT] = {
{ .color = COLOR_WHITE_4B, .uv = { 0.0f, 0.0f }, .pos = { 0.0f, 0.0f, 0.0f } },
{ .color = COLOR_WHITE_4B, .uv = { 1.0f, 0.0f }, .pos = { 1.0f, 0.0f, 0.0f } },
{ .color = COLOR_WHITE_4B, .uv = { 1.0f, 1.0f }, .pos = { 1.0f, 1.0f, 0.0f } },
{
#if MESH_ENABLE_COLOR
.color = COLOR_WHITE_4B,
#endif
{ .color = COLOR_WHITE_4B, .uv = { 0.0f, 0.0f }, .pos = { 0.0f, 0.0f, 0.0f } },
{ .color = COLOR_WHITE_4B, .uv = { 1.0f, 1.0f }, .pos = { 1.0f, 1.0f, 0.0f } },
{ .color = COLOR_WHITE_4B, .uv = { 0.0f, 1.0f }, .pos = { 0.0f, 1.0f, 0.0f } }
.uv = { 0.0f, 0.0f },
.pos = { 0.0f, 0.0f, 0.0f }
},
{
#if MESH_ENABLE_COLOR
.color = COLOR_WHITE_4B,
#endif
.uv = { 1.0f, 0.0f },
.pos = { 1.0f, 0.0f, 0.0f }
},
{
#if MESH_ENABLE_COLOR
.color = COLOR_WHITE_4B,
#endif
.uv = { 1.0f, 1.0f },
.pos = { 1.0f, 1.0f, 0.0f }
},
{
#if MESH_ENABLE_COLOR
.color = COLOR_WHITE_4B,
#endif
.uv = { 0.0f, 0.0f },
.pos = { 0.0f, 0.0f, 0.0f }
},
{
#if MESH_ENABLE_COLOR
.color = COLOR_WHITE_4B,
#endif
.uv = { 1.0f, 1.0f },
.pos = { 1.0f, 1.0f, 0.0f }
},
{
#if MESH_ENABLE_COLOR
.color = COLOR_WHITE_4B,
#endif
.uv = { 0.0f, 1.0f },
.pos = { 0.0f, 1.0f, 0.0f }
}
};
errorret_t quadInit() {
@@ -35,7 +77,9 @@ void quadBuffer(
const float_t minY,
const float_t maxX,
const float_t maxY,
const color_t color,
#if MESH_ENABLE_COLOR
const color_t color,
#endif
const float_t u0,
const float_t v0,
const float_t u1,
@@ -45,21 +89,27 @@ void quadBuffer(
assertNotNull(vertices, "Vertices cannot be NULL");
// First triangle
vertices[0].color = color;
#if MESH_ENABLE_COLOR
vertices[0].color = color;
#endif
vertices[0].uv[0] = u0;
vertices[0].uv[1] = v1;
vertices[0].pos[0] = minX;
vertices[0].pos[1] = maxY;
vertices[0].pos[2] = z;
vertices[1].color = color;
#if MESH_ENABLE_COLOR
vertices[1].color = color;
#endif
vertices[1].uv[0] = u1;
vertices[1].uv[1] = v0;
vertices[1].pos[0] = maxX;
vertices[1].pos[1] = minY;
vertices[1].pos[2] = z;
vertices[2].color = color;
#if MESH_ENABLE_COLOR
vertices[2].color = color;
#endif
vertices[2].uv[0] = u0;
vertices[2].uv[1] = v0;
vertices[2].pos[0] = minX;
@@ -67,21 +117,27 @@ void quadBuffer(
vertices[2].pos[2] = z;
// Second triangle
vertices[3].color = color;
#if MESH_ENABLE_COLOR
vertices[3].color = color;
#endif
vertices[3].uv[0] = u0;
vertices[3].uv[1] = v1;
vertices[3].pos[0] = minX;
vertices[3].pos[1] = maxY;
vertices[3].pos[2] = z;
vertices[4].color = color;
#if MESH_ENABLE_COLOR
vertices[4].color = color;
#endif
vertices[4].uv[0] = u1;
vertices[4].uv[1] = v1;
vertices[4].pos[0] = maxX;
vertices[4].pos[1] = maxY;
vertices[4].pos[2] = z;
vertices[5].color = color;
#if MESH_ENABLE_COLOR
vertices[5].color = color;
#endif
vertices[5].uv[0] = u1;
vertices[5].uv[1] = v0;
vertices[5].pos[0] = maxX;
@@ -93,7 +149,9 @@ void quadBuffer3D(
meshvertex_t *vertices,
const vec3 min,
const vec3 max,
const color_t color,
#if MESH_ENABLE_COLOR
const color_t color,
#endif
const vec2 uvMin,
const vec2 uvMax
) {
@@ -104,21 +162,27 @@ void quadBuffer3D(
assertNotNull(uvMax, "UV Max vector cannot be NULL");
// First triangle
vertices[0].color = color;
#if MESH_ENABLE_COLOR
vertices[0].color = color;
#endif
vertices[0].uv[0] = uvMin[0];
vertices[0].uv[1] = uvMin[1];
vertices[0].pos[0] = min[0];
vertices[0].pos[1] = min[1];
vertices[0].pos[2] = min[2];
vertices[1].color = color;
#if MESH_ENABLE_COLOR
vertices[1].color = color;
#endif
vertices[1].uv[0] = uvMax[0];
vertices[1].uv[1] = uvMin[1];
vertices[1].pos[0] = max[0];
vertices[1].pos[1] = min[1];
vertices[1].pos[2] = min[2];
vertices[2].color = color;
#if MESH_ENABLE_COLOR
vertices[2].color = color;
#endif
vertices[2].uv[0] = uvMax[0];
vertices[2].uv[1] = uvMax[1];
vertices[2].pos[0] = max[0];
@@ -126,21 +190,27 @@ void quadBuffer3D(
vertices[2].pos[2] = min[2];
// Second triangle
vertices[3].color = color;
#if MESH_ENABLE_COLOR
vertices[3].color = color;
#endif
vertices[3].uv[0] = uvMin[0];
vertices[3].uv[1] = uvMin[1];
vertices[3].pos[0] = min[0];
vertices[3].pos[1] = min[1];
vertices[3].pos[2] = min[2];
vertices[4].color = color;
#if MESH_ENABLE_COLOR
vertices[4].color = color;
#endif
vertices[4].uv[0] = uvMax[0];
vertices[4].uv[1] = uvMax[1];
vertices[4].pos[0] = max[0];
vertices[4].pos[1] = max[1];
vertices[4].pos[2] = min[2];
vertices[5].color = color;
#if MESH_ENABLE_COLOR
vertices[5].color = color;
#endif
vertices[5].uv[0] = uvMin[0];
vertices[5].uv[1] = uvMax[1];
vertices[5].pos[0] = min[0];
+6 -2
View File
@@ -42,7 +42,9 @@ void quadBuffer(
const float_t minY,
const float_t maxX,
const float_t maxY,
const color_t color,
#if MESH_ENABLE_COLOR
const color_t color,
#endif
const float_t u0,
const float_t v0,
const float_t u1,
@@ -63,7 +65,9 @@ void quadBuffer3D(
meshvertex_t *vertices,
const vec3 min,
const vec3 max,
const color_t color,
#if MESH_ENABLE_COLOR
const color_t color,
#endif
const vec2 uvMin,
const vec2 uvMax
);
+6 -2
View File
@@ -26,7 +26,9 @@ errorret_t screenInit() {
SCREEN.frameBufferMeshVertices,
0.0f, 0.0f,
1.0f, 1.0f,
COLOR_WHITE,
#if MESH_ENABLE_COLOR
COLOR_WHITE,
#endif
0.0f, 0.0f,
1.0f, 1.0f
);
@@ -361,7 +363,9 @@ errorret_t screenRender() {
SCREEN.frameBufferMeshVertices,
centerX - fbWidth * 0.5f, centerY + fbHeight * 0.5f, // top-left
centerX + fbWidth * 0.5f, centerY - fbHeight * 0.5f, // bottom-right
COLOR_WHITE,
#if MESH_ENABLE_COLOR
COLOR_WHITE,
#endif
0.0f, 0.0f,
1.0f, 1.0f
);
+14 -4
View File
@@ -29,7 +29,9 @@ errorret_t spriteBatchPush(
const float_t minY,
const float_t maxX,
const float_t maxY,
const color_t color,
#if MESH_ENABLE_COLOR
const color_t color,
#endif
const float_t u0,
const float_t v0,
const float_t u1,
@@ -38,7 +40,9 @@ errorret_t spriteBatchPush(
return spriteBatchPush3D(
(vec3){ minX, minY, 0 },
(vec3){ maxX, maxY, 0 },
color,
#if MESH_ENABLE_COLOR
color,
#endif
(vec2){ u0, v0 },
(vec2){ u1, v1 }
);
@@ -47,7 +51,9 @@ errorret_t spriteBatchPush(
errorret_t spriteBatchPush3D(
const vec3 min,
const vec3 max,
const color_t color,
#if MESH_ENABLE_COLOR
const color_t color,
#endif
const vec2 uv0,
const vec2 uv1
) {
@@ -62,7 +68,11 @@ errorret_t spriteBatchPush3D(
) * QUAD_VERTEX_COUNT;
quadBuffer3D(
&SPRITEBATCH_VERTICES[vertexOffset],
min, max, color, uv0, uv1
min, max,
#if MESH_ENABLE_COLOR
color,
#endif
uv0, uv1
);
SPRITEBATCH.spriteCount++;
errorOk();
+6 -2
View File
@@ -57,7 +57,9 @@ errorret_t spriteBatchPush(
const float_t minY,
const float_t maxX,
const float_t maxY,
const color_t color,
#if MESH_ENABLE_COLOR
const color_t color,
#endif
const float_t u0,
const float_t v0,
const float_t u1,
@@ -78,7 +80,9 @@ errorret_t spriteBatchPush(
errorret_t spriteBatchPush3D(
const vec3 min,
const vec3 max,
const color_t color,
#if MESH_ENABLE_COLOR
const color_t color,
#endif
const vec2 uvMin,
const vec2 uvMax
);
+16 -4
View File
@@ -33,7 +33,9 @@ errorret_t textDrawChar(
const float_t x,
const float_t y,
const char_t c,
const color_t color,
#if MESH_ENABLE_COLOR
const color_t color,
#endif
const tileset_t *tileset,
texture_t *texture
) {
@@ -55,7 +57,9 @@ errorret_t textDrawChar(
x, y,
x + tileset->tileWidth,
y + tileset->tileHeight,
color,
#if MESH_ENABLE_COLOR
color,
#endif
uv[0], uv[1], uv[2], uv[3]
));
errorOk();
@@ -66,7 +70,9 @@ errorret_t textDraw(
const float_t x,
const float_t y,
const char_t *text,
const color_t color,
#if MESH_ENABLE_COLOR
const color_t color,
#endif
const tileset_t *tileset,
texture_t *texture
) {
@@ -100,7 +106,13 @@ errorret_t textDraw(
continue;
}
errorChain(textDrawChar(posX, posY, c, color, tileset, texture));
errorChain(textDrawChar(
posX, posY, c,
#if MESH_ENABLE_COLOR
color,
#endif
tileset, texture
));
posX += tileset->tileWidth;
}
errorOk();
+6 -2
View File
@@ -44,7 +44,9 @@ errorret_t textDrawChar(
const float_t x,
const float_t y,
const char_t c,
const color_t color,
#if MESH_ENABLE_COLOR
const color_t color,
#endif
const tileset_t *tileset,
texture_t *texture
);
@@ -64,7 +66,9 @@ errorret_t textDraw(
const float_t x,
const float_t y,
const char_t *text,
const color_t color,
#if MESH_ENABLE_COLOR
const color_t color,
#endif
const tileset_t *tileset,
texture_t *texture
);
+6 -4
View File
@@ -52,22 +52,24 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
// FOF
entityid_t cam = entityManagerAdd();
componentid_t camPos = entityAddComponent(cam, COMPONENT_TYPE_POSITION);
float_t distance = 200.0f;
float_t up = 50.0f;
entityPositionLookAt(
cam,
camPos,
(vec3){ 0.0f, 20.0f, 0.0f },
(vec3){ 0.0f, up, 0.0f },
(vec3){ 0.0f, 1.0f, 0.0f },
(vec3){ 50.0f, 70.0f, 50.0f }
(vec3){ distance, distance + up, distance }
);
componentid_t camCam = entityAddComponent(cam, COMPONENT_TYPE_CAMERA);
entityCameraSetZFar(cam, camCam, 500.0f);
entityCameraSetZFar(cam, camCam, distance * 5.0f);
ent1 = entityManagerAdd();
ent1Pos = entityAddComponent(ent1, COMPONENT_TYPE_POSITION);
ent1Mesh = entityAddComponent(ent1, COMPONENT_TYPE_MESH);
ent1Mat = entityAddComponent(ent1, COMPONENT_TYPE_MATERIAL);
errorChain(assetMeshLoad("test/RosaTh.stl", &loadedMesh, &loadedVertices));
errorChain(assetMeshLoad("test/test.stl", &loadedMesh, &loadedVertices));
entityMeshSetMesh(ent1, ent1Mesh, &loadedMesh);
shadermaterial_t *mat = entityMaterialGetShaderMaterial(ent1, ent1Mat);
@@ -84,7 +84,9 @@ int moduleSpriteBatchPush(lua_State *L) {
minY,
maxX,
maxY,
color == NULL ? COLOR_WHITE : *color,
#if MESH_ENABLE_COLOR
color == NULL ? COLOR_WHITE : *color,
#endif
u0,
v0,
u1,
+3 -1
View File
@@ -52,7 +52,9 @@ int moduleTextDraw(lua_State *L) {
x,
y,
text,
color == NULL ? COLOR_WHITE : *color,
#if MESH_ENABLE_COLOR
color == NULL ? COLOR_WHITE : *color,
#endif
&DEFAULT_FONT_TILESET,
&DEFAULT_FONT_TEXTURE
);
+7
View File
@@ -8,12 +8,19 @@
#include "memory.h"
#include "assert/assert.h"
#include "util/math.h"
#include "log/log.h"
size_t memoryGetAllocatedCount(void) {
return MEMORY_POINTERS_IN_USE;
}
void * memoryAllocate(const size_t size) {
logDebug(
"Attempt to allocate %u bytes (%.2fKB) of memory\n",
size,
size / 1024.0f
);
assertTrue(size > 0, "Cannot allocate 0 bytes of memory.");
void *ptr = malloc(size);
assertNotNull(ptr, "Memory allocation failed.");
+28 -19
View File
@@ -26,8 +26,11 @@ errorret_t meshInitGL(
#ifdef DUSK_OPENGL_LEGACY
// Nothing needed.
glEnableClientState(GL_COLOR_ARRAY);
errorChain(errorGLCheck());
#if MESH_ENABLE_COLOR
glEnableClientState(GL_COLOR_ARRAY);
errorChain(errorGLCheck());
#endif
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
errorChain(errorGLCheck());
glEnableClientState(GL_VERTEX_ARRAY);
@@ -79,17 +82,19 @@ errorret_t meshInitGL(
glEnableVertexAttribArray(1);
errorChain(errorGLCheck());
glVertexAttribPointer(
2,
sizeof(color_t) / sizeof(GLubyte),
GL_UNSIGNED_BYTE,
GL_TRUE,
sizeof(meshvertex_t),
(const GLvoid*)offsetof(meshvertex_t, color)
);
errorChain(errorGLCheck());
glEnableVertexAttribArray(2);
errorChain(errorGLCheck());
#if MESH_ENABLE_COLOR
glVertexAttribPointer(
2,
sizeof(color_t) / sizeof(GLubyte),
GL_UNSIGNED_BYTE,
GL_TRUE,
sizeof(meshvertex_t),
(const GLvoid*)offsetof(meshvertex_t, color)
);
errorChain(errorGLCheck());
glEnableVertexAttribArray(2);
errorChain(errorGLCheck());
#endif
// Unbind VAO and VBO to prevent accidental modification
glBindBuffer(GL_ARRAY_BUFFER, 0);
@@ -133,18 +138,22 @@ errorret_t meshDrawGL(
// Legacy pointer style rendering
const GLsizei stride = sizeof(meshvertex_t);
glColorPointer(
sizeof(color4b_t),
GL_UNSIGNED_BYTE,
stride,
(const GLvoid*)&mesh->vertices[offset].color
);
#if MESH_ENABLE_COLOR
glColorPointer(
sizeof(color4b_t),
GL_UNSIGNED_BYTE,
stride,
(const GLvoid*)&mesh->vertices[offset].color
);
#endif
glTexCoordPointer(
MESH_VERTEX_UV_SIZE,
GL_FLOAT,
stride,
(const GLvoid*)&mesh->vertices[offset].uv[0]
);
glVertexPointer(
MESH_VERTEX_POS_SIZE,
GL_FLOAT,
+15 -3
View File
@@ -90,7 +90,9 @@
// Attributes
"layout(location = 0) in vec3 a_Pos;\n"
"layout(location = 1) in vec2 a_TexCoord;\n"
"layout(location = 2) in vec4 a_Color;\n"
#if MESH_ENABLE_COLOR
"layout(location = 2) in vec4 a_Color;\n"
#endif
// Uniforms
"uniform mat4 u_Proj;\n"
"uniform mat4 u_View;\n"
@@ -100,7 +102,11 @@
"out vec2 v_TexCoord;\n"
"void main() {\n"
" gl_Position = u_Proj * u_View * u_Model * vec4(a_Pos, 1.0);\n"
#if MESH_ENABLE_COLOR
" v_Color = a_Color;\n"
#else
" v_Color = vec4(1.0);\n"
#endif
" v_TexCoord = a_TexCoord;\n"
"}\n",
#else
@@ -108,7 +114,9 @@
// Attributes
"layout(location = 0) in vec3 a_Pos;\n"
"layout(location = 1) in vec2 a_TexCoord;\n"
"layout(location = 2) in vec4 a_Color;\n"
#if MESH_ENABLE_COLOR
"layout(location = 2) in vec4 a_Color;\n"
#endif
// Uniforms
"uniform mat4 u_Proj;\n"
"uniform mat4 u_View;\n"
@@ -118,7 +126,11 @@
"out vec2 v_TexCoord;\n"
"void main() {\n"
" gl_Position = u_Proj * u_View * u_Model * vec4(a_Pos, 1.0);\n"
" v_Color = a_Color;\n"
#if MESH_ENABLE_COLOR
" v_Color = a_Color;\n"
#else
" v_Color = vec4(1.0);\n"
#endif
" v_TexCoord = a_TexCoord;\n"
"}\n",
#endif