Added vertex color shading to improve readability.

This commit is contained in:
Jake 2025-05-30 20:40:15 -04:00
parent 701c72a2a5
commit 77cba9260a

View File

@ -11,6 +11,19 @@
#define ATLAS_SIZE 256 #define ATLAS_SIZE 256
#define ATLAS_TILES_PER_ROW (ATLAS_SIZE / TILE_SIZE) #define ATLAS_TILES_PER_ROW (ATLAS_SIZE / TILE_SIZE)
// For my basic Minecraft style vertex color shading.
static Color getFaceBrightness(int dir) {
switch (dir) {
case 0: return (Color){ 127, 127, 127, 255 }; // -X
case 1: return (Color){ 191, 191, 191, 255 }; // +X
case 2: return (Color){ 80, 80, 80, 255 }; // -Y (darker underside)
case 3: return (Color){ 255, 255, 255, 255 }; // +Y (top, fully lit)
case 4: return (Color){ 159, 159, 159, 255 }; // -Z
case 5: return (Color){ 223, 223, 223, 255 }; // +Z
default: return WHITE;
}
}
// Returns the UV coordinate for a given tile index and corner index (03), // Returns the UV coordinate for a given tile index and corner index (03),
// assuming tiles are arranged in a grid in the texture atlas. // assuming tiles are arranged in a grid in the texture atlas.
Vector2 GetTileUV(int tile, int corner) { Vector2 GetTileUV(int tile, int corner) {
@ -39,6 +52,7 @@ Mesh GenerateChunkMesh(Chunk *chunk) {
Vector3 *vertices = malloc(sizeof(Vector3) * maxVerts); Vector3 *vertices = malloc(sizeof(Vector3) * maxVerts);
Vector3 *normals = malloc(sizeof(Vector3) * maxVerts); Vector3 *normals = malloc(sizeof(Vector3) * maxVerts);
Vector2 *texcoords = malloc(sizeof(Vector2) * maxVerts); Vector2 *texcoords = malloc(sizeof(Vector2) * maxVerts);
Color *colors = malloc(sizeof(Color) * maxVerts); // Added in the color array
unsigned short *indices = malloc(sizeof(unsigned short) * maxIndices); unsigned short *indices = malloc(sizeof(unsigned short) * maxIndices);
int vertCount = 0; int vertCount = 0;
@ -115,6 +129,7 @@ Mesh GenerateChunkMesh(Chunk *chunk) {
vertices[vertCount] = face[i]; vertices[vertCount] = face[i];
texcoords[vertCount] = GetTileUV(tileIndex, i); texcoords[vertCount] = GetTileUV(tileIndex, i);
normals[vertCount] = normal; normals[vertCount] = normal;
colors[vertCount] = getFaceBrightness(dir);
vertCount++; vertCount++;
} }
@ -136,6 +151,7 @@ Mesh GenerateChunkMesh(Chunk *chunk) {
mesh.vertices = (float *)vertices; mesh.vertices = (float *)vertices;
mesh.texcoords = (float *)texcoords; mesh.texcoords = (float *)texcoords;
mesh.normals = (float *)normals; mesh.normals = (float *)normals;
mesh.colors = (unsigned char*)colors;
mesh.indices = indices; mesh.indices = indices;
UploadMesh(&mesh, true); // True here tells the function to free the CPU side allocated memory. UploadMesh(&mesh, true); // True here tells the function to free the CPU side allocated memory.