Bringing chunk and chunklist together.

This commit is contained in:
2021-04-13 07:44:21 +10:00
parent c40e55f6c5
commit 8198f3d854
6 changed files with 100 additions and 74 deletions

View File

@ -1,19 +0,0 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "chunk.h"
void chunkRender(chunk_t *chunk, shader_t *shader) {
// Coordinates of the chunk.
float x, y, z;
x = (chunk->x * CHUNK_WIDTH);
y = (chunk->y * CHUNK_HEIGHT);
z = (chunk->z * CHUNK_DEPTH);
// Render the primitive
shaderUsePosition(shader, x, y, z, 0, 0, 0);
primitiveDraw(chunk->primitive, 0, chunk->primitive->indiceCount);
}

View File

@ -1,34 +0,0 @@
// Copyright (c) 2021 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include <stdint.h>
#include "../display/shader.h"
#include "../display/primitive.h"
#include "tile.h"
#define CHUNK_WIDTH 3
#define CHUNK_HEIGHT CHUNK_WIDTH
#define CHUNK_DEPTH CHUNK_HEIGHT
typedef struct {
/** Absolute X Y Z chunklist coordinates, a */
int32_t x, y, z;
/** Tiles stored within the chunk. */
tile_t *tiles;
/** Primitive used for rendering the chunk */
primitive_t *primitive;
} chunk_t;
/**
* Render a given chunk.
*
* @param chunk Chunk to render
* @param
*/
void chunkRender(chunk_t *chunk, shader_t *shader);

View File

@ -158,6 +158,42 @@ void chunkListAlign(chunklist_t *list, int32_t x, int32_t y, int32_t z) {
chunkListShift(list, lx, ly, lz);
}
void chunkListRender(chunklist_t *list, shader_t *shader) {
int32_t i, j, tx, ty, tz;
float x, y, z;
chunk_t *chunk;
tile_t *tile;
tiledef_t *tileDef;
for(i = 0; i < list->count; i++) {
chunk = list->chunks + i;
x = (chunk->x * CHUNK_WIDTH);
y = (chunk->y * CHUNK_HEIGHT);
z = (chunk->z * CHUNK_DEPTH);
// Re-render every single tile in the chunk.
j = 0;
for(tx = 0; tx < CHUNK_WIDTH; tx++) {
for(ty = 0; ty < CHUNK_HEIGHT; ty++) {
for(tz = 0; tz < CHUNK_DEPTH; tz++) {
// Get tile for position...
tile = chunk->tiles + (j++);
// Should Tile bother rendering?
if(tile->id == TILE_NULL) continue;
tileDef = list->tilemap->tileDefinitions + tile->id;
if(tileDef->verticeCount == 0 || tileDef->indiceCount == 0) continue;
chunkListTileRender(list, chunk, tile, tileDef, tx, ty, tz);
}
}
}
shaderUsePosition(shader, x, y, z, 0, 0, 0);
primitiveDraw(chunk->primitive, 0, chunk->primitive->indiceCount);
}
}
void chunkListChunkLoad(chunklist_t *list, chunk_t *chunk,
int32_t x, int32_t y, int32_t z
) {
@ -177,6 +213,10 @@ void chunkListChunkLoad(chunklist_t *list, chunk_t *chunk,
tile = chunk->tiles + i;
if(tile->id == TILE_NULL) continue;
tileDef = list->tilemap->tileDefinitions + tile->id;
if(tileDef->verticeCount == 0 || tileDef->indiceCount == 0) continue;
tile->verticeStart = verticeCount;
tile->indiceStart = indiceCount;
verticeCount += tileDef->verticeCount;
indiceCount += tileDef->indiceCount;
@ -184,31 +224,22 @@ void chunkListChunkLoad(chunklist_t *list, chunk_t *chunk,
// Now we know how big the primitive needs to be!
chunk->primitive = primitiveCreate(verticeCount, indiceCount);
indiceCount = 0, verticeCount = 0;//Reset for rendering
// For each tile
i = 0;
for(tx = 0; tx < CHUNK_WIDTH; tx++) {
for(ty = 0; ty < CHUNK_HEIGHT; ty++) {
for(tz = 0; tz < CHUNK_DEPTH; tz++) {
// Get tile for position...
tile = chunk->tiles + (i++);
// Should Chunk bother rendering?
if(tile->id == TILE_NULL) continue;
tileDef = list->tilemap->tileDefinitions + tile->id;
if(tileDef->verticeCount == 0 || tileDef->indiceCount == 0) continue;
tile->indiceStart = indiceCount;
tile->verticeStart = verticeCount;
div = list->tilemap->tileset->divisions + tile->id;
quadBuffer(chunk->primitive, tz,
tx, ty, div->x0, div->y0,
tx+1, ty+1, div->x1, div->y1,
verticeCount, indiceCount
);
indiceCount += tileDef->indiceCount;
verticeCount += tileDef->verticeCount;
// Render the tile's primitive
chunkListTileRender(list, chunk, tile, tileDef, tx, ty, tz);
}
}
}
@ -221,4 +252,15 @@ void chunkListChunkUnload(chunklist_t *list, chunk_t *chunk) {
// Load chunks to zero. TODO: Necessary?
memset(chunk->tiles, TILE_NULL, list->count);
}
void chunkListTileRender(chunklist_t *list, chunk_t *chunk,
tile_t *tile, tiledef_t *tileDef, int32_t x, int32_t y, int32_t z
) {
tilesetdiv_t *div = list->tilemap->tileset->divisions + tile->id;
quadBuffer(chunk->primitive, z,
x, y, div->x0, div->y0,
x+1, y+1, div->x1, div->y1,
tile->verticeStart, tile->indiceStart
);
}

View File

@ -6,11 +6,27 @@
#pragma once
#include <stdint.h>
#include <malloc.h>
#include "./../util/math.h"
#include "chunk.h"
#include "tile.h"
#include "./../util/math.h"
#include "../display/shader.h"
#include "../display/primitive.h"
#include "../display/texture.h"
#define CHUNK_WIDTH 3
#define CHUNK_HEIGHT CHUNK_WIDTH
#define CHUNK_DEPTH CHUNK_HEIGHT
typedef struct {
/** Absolute X Y Z chunklist coordinates */
int32_t x, y, z;
/** Tiles stored within the chunk. */
tile_t *tiles;
/** Primitive used for rendering the chunk */
primitive_t *primitive;
} chunk_t;
typedef struct {
/** Dimensions of the chunk list */
int32_t width, height, depth;
@ -76,6 +92,14 @@ void chunkListShift(chunklist_t *list, int32_t x, int32_t y, int32_t z);
*/
void chunkListAlign(chunklist_t *list, int32_t x, int32_t y, int32_t z);
/**
* Renders a given chunk list.
*
* @param list The list to render.
* @param shader The shader to use.
*/
void chunkListRender(chunklist_t *list, shader_t *shader);
/**
* Loads a given chunk.
*
@ -95,4 +119,19 @@ void chunkListChunkLoad(chunklist_t *list, chunk_t *chunk,
* @param list List that the chunk belongs to.
* @param chunk Chunk to unload.
*/
void chunkListChunkUnload(chunklist_t *list, chunk_t *chunk);
void chunkListChunkUnload(chunklist_t *list, chunk_t *chunk);
/**
* Rendedrs a given chunk tile.
*
* @param list The chunk list that the chunk belongs to.
* @param chunk The chunk to render against.
* @param tile The tile to render.
* @param tileDef The tile's definition information.
* @param x The X Coordinate of the tile (chunk space).
* @param y The Y Coordinate of the tile (chunk space).
* @param z The Z Coordinate of the tile (chunk space).
*/
void chunkListTileRender(chunklist_t *list, chunk_t *chunk,
tile_t *tile, tiledef_t *tileDef, int32_t x, int32_t y, int32_t z
);

View File

@ -20,6 +20,7 @@ tilemap_t * tileMapCreate(tileset_t *tileset) {
return NULL;
}
// tilemap->tileDefinitions->indiceCount = 6;
// tilemap->tileDefinitions->verticeCount = 4;

View File

@ -56,10 +56,7 @@ void worldRender(world_t *world, shader_t *shader) {
int32_t i;
shaderUseTexture(shader, world->texture);
for(i = 0; i < world->chunkList->count; i++) {
chunkRender(world->chunkList->chunks + i, shader);
}
chunkListRender(world->chunkList, shader);
}
void worldDispose(world_t *world) {