diff --git a/source/chunkRenderer.c b/source/chunkRenderer.c index 3585ef9..de91cb1 100644 --- a/source/chunkRenderer.c +++ b/source/chunkRenderer.c @@ -11,6 +11,19 @@ #define ATLAS_SIZE 256 #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 (0–3), // assuming tiles are arranged in a grid in the texture atlas. Vector2 GetTileUV(int tile, int corner) { @@ -39,6 +52,7 @@ Mesh GenerateChunkMesh(Chunk *chunk) { Vector3 *vertices = malloc(sizeof(Vector3) * maxVerts); Vector3 *normals = malloc(sizeof(Vector3) * 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); int vertCount = 0; @@ -115,6 +129,7 @@ Mesh GenerateChunkMesh(Chunk *chunk) { vertices[vertCount] = face[i]; texcoords[vertCount] = GetTileUV(tileIndex, i); normals[vertCount] = normal; + colors[vertCount] = getFaceBrightness(dir); vertCount++; } @@ -136,6 +151,7 @@ Mesh GenerateChunkMesh(Chunk *chunk) { mesh.vertices = (float *)vertices; mesh.texcoords = (float *)texcoords; mesh.normals = (float *)normals; + mesh.colors = (unsigned char*)colors; mesh.indices = indices; UploadMesh(&mesh, true); // True here tells the function to free the CPU side allocated memory.