|
|
|
|
@ -37,146 +37,6 @@ void setDefaults(render_state* rs, glm::vec2 viewport_dims);
|
|
|
|
|
|
|
|
|
|
// interface
|
|
|
|
|
|
|
|
|
|
// WIP
|
|
|
|
|
size_t |
|
|
|
|
getBlockFreeSpace(asset_memory_block* block) |
|
|
|
|
{ |
|
|
|
|
return (uint8_t*) block |
|
|
|
|
+ block->max_size |
|
|
|
|
- (uint8_t*) block->tail |
|
|
|
|
+ block->tail->total_size; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
uint |
|
|
|
|
getAssetSize(const mesh_group& mg, uint name_len) |
|
|
|
|
{ |
|
|
|
|
uint total = sizeof(render_asset); |
|
|
|
|
total += name_len; |
|
|
|
|
|
|
|
|
|
for (uint i = 0; i < mg.num_meshes; i++) { |
|
|
|
|
mesh_info* mi = mg.meshes[i]; |
|
|
|
|
total += sizeof(mesh_asset); |
|
|
|
|
// NOTE: vertices, normals, and tex coords
|
|
|
|
|
total += 3 * mi->num_vertices * sizeof(glm::vec3); |
|
|
|
|
total += mi->num_indices * sizeof(uint); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return total; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void* |
|
|
|
|
getAddressOffset(void* address, uint offset) |
|
|
|
|
{ |
|
|
|
|
return (void*) ((uint8_t*) address + offset); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// TODO: replace mesh_info structure
|
|
|
|
|
mesh_asset* |
|
|
|
|
initMeshAsset(mesh_asset* mesh, mesh_info* mi) |
|
|
|
|
{ |
|
|
|
|
mesh->num_vertices = mi->num_vertices; |
|
|
|
|
mesh->num_indices = mi->num_indices; |
|
|
|
|
mesh->asset_size = sizeof(mesh_asset); |
|
|
|
|
mesh->vertices = (glm::vec3*) ((uint8_t*) mesh + mesh->asset_size); |
|
|
|
|
// NOTE: vertices, normals, and tex coords
|
|
|
|
|
mesh->asset_size += 3 * mesh->num_vertices * sizeof(glm::vec3); |
|
|
|
|
mesh->asset_size += mesh->num_indices * sizeof(uint); |
|
|
|
|
mesh->asset_size += sizeof(glm::mat4); |
|
|
|
|
// FIXME: this should be set in the outer loop
|
|
|
|
|
//mesh->next = (mesh_asset*) ((uint8_t*) mesh + mesh->asset_size);
|
|
|
|
|
// FIXME: copy vertices, normals, tex coords, and xform
|
|
|
|
|
|
|
|
|
|
return mesh; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
render_asset* |
|
|
|
|
getNextFreeRenderAsset(asset_memory_block* block) |
|
|
|
|
{ |
|
|
|
|
return (render_asset*) |
|
|
|
|
getAddressOffset(block->tail, block->tail->total_size); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#include <cstring> |
|
|
|
|
render_asset* |
|
|
|
|
initRenderAsset( |
|
|
|
|
render_asset* ra, |
|
|
|
|
uint total_size, |
|
|
|
|
uint num_meshes, |
|
|
|
|
const char* filepath) |
|
|
|
|
{ |
|
|
|
|
ra->total_size = total_size; |
|
|
|
|
ra->num_meshes = num_meshes; |
|
|
|
|
ra->name_len = (uint) std::strlen(filepath) + 1; |
|
|
|
|
ra->next = nullptr; |
|
|
|
|
ra->filepath = (char*) ra + sizeof(render_asset); |
|
|
|
|
std::memcpy(ra->filepath, filepath, ra->name_len); |
|
|
|
|
ra->head_mesh = (mesh_asset*) ((uint8_t*) ra->filepath + ra->name_len); |
|
|
|
|
// TODO: texture
|
|
|
|
|
//ra->diffuse_texture = (util_image*) ra->filepath;
|
|
|
|
|
|
|
|
|
|
return ra; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
asset_memory_block* |
|
|
|
|
memInitBlock(size_t initial_size) |
|
|
|
|
{ |
|
|
|
|
// NOTE: need to alloc as uint8_t and recast here so we can get the correct
|
|
|
|
|
// size in bytes
|
|
|
|
|
asset_memory_block* block = (asset_memory_block*) UTIL_ALLOC( |
|
|
|
|
sizeof(asset_memory_block) + initial_size, uint8_t); |
|
|
|
|
block->max_size = initial_size - sizeof(asset_memory_block); |
|
|
|
|
block->head = block->tail = |
|
|
|
|
(render_asset*) ((uint8_t*) block + sizeof(asset_memory_block)); |
|
|
|
|
return block; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
memFreeBlock(asset_memory_block* block) |
|
|
|
|
{ |
|
|
|
|
utilSafeFree(block); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
render_asset* |
|
|
|
|
memPushAsset(asset_memory_block* block, const char* filepath) |
|
|
|
|
{ |
|
|
|
|
// TODO: rewrite mesh.cpp to load assimp structure into render_asset
|
|
|
|
|
// directly
|
|
|
|
|
mesh_group mg; |
|
|
|
|
meLoadFromFile(mg, filepath); |
|
|
|
|
uint name_len = (uint) std::strlen(filepath) + 1; |
|
|
|
|
assert(name_len <= MAX_PATH_SIZE); |
|
|
|
|
uint asize = getAssetSize(mg, name_len); |
|
|
|
|
|
|
|
|
|
if (getBlockFreeSpace(block) < asize) { |
|
|
|
|
LOG(Error) << "not enough free space in block\n"; |
|
|
|
|
return nullptr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
render_asset* ra = getNextFreeRenderAsset(block); |
|
|
|
|
initRenderAsset(ra, asize, mg.num_meshes, filepath); |
|
|
|
|
block->max_size -= asize; |
|
|
|
|
block->asset_count++; |
|
|
|
|
|
|
|
|
|
// NOTE: special case when adding the first asset
|
|
|
|
|
if (block->head == block->tail) |
|
|
|
|
block->head->next = ra; |
|
|
|
|
else |
|
|
|
|
block->tail->next = ra; |
|
|
|
|
|
|
|
|
|
block->tail = ra; |
|
|
|
|
mesh_asset* next = ra->head_mesh; |
|
|
|
|
|
|
|
|
|
for (uint i = 0; i < mg.num_meshes; i++) { |
|
|
|
|
mesh_asset* mesh = initMeshAsset(next, mg.meshes[i]); |
|
|
|
|
next = (mesh_asset*) getAddressOffset(mesh, mesh->asset_size); |
|
|
|
|
// NOTE: leave the last link as nullptr
|
|
|
|
|
(i < mg.num_meshes - 1) ? mesh->next = next : mesh->next = nullptr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return ra; |
|
|
|
|
} |
|
|
|
|
// end WIP
|
|
|
|
|
|
|
|
|
|
render_state* |
|
|
|
|
renInit(const char* title, glm::vec2 viewport_dims, Uint32 SDL_init_flags) |
|
|
|
|
{ |
|
|
|
|
@ -217,8 +77,8 @@ renShutdown(render_state* rs)
|
|
|
|
|
|
|
|
|
|
lightsOut(rs->lights); |
|
|
|
|
utilSafeFree(rs->render_groups); |
|
|
|
|
memFreeBlock(rs->assets); |
|
|
|
|
rs->assets = nullptr; |
|
|
|
|
rs->render_groups = nullptr; |
|
|
|
|
arenaFree(rs->arena); |
|
|
|
|
meShutdownAssimp(); |
|
|
|
|
SDL_GL_DeleteContext(rs->handles->glContext); |
|
|
|
|
SDL_DestroyWindow(rs->handles->window); |
|
|
|
|
|