diff --git a/src/entity.cpp b/src/entity.cpp new file mode 100644 index 0000000..1bece4d --- /dev/null +++ b/src/entity.cpp @@ -0,0 +1,46 @@ + +#include + +#include "entity.h" + + +bool +initEntity(Entity* e, + MemoryArena* arena, + model* mdl, + u32 num_attrib_mappings, + GLBufferToAttribMapping* attrib_mappings, + const char* name) +{ + e->num_meshes = mdl->num_meshes; + e->meshes = ARENA_ALLOC(arena, GLmesh, e->num_meshes); + e->xform = ARENA_ALLOC(arena, glm::mat4, 1); + *e->xform = glm::mat4(1.f); + e->name = arenaCopyCStr(arena, name); + + for (u32 i = 0; i< e->num_meshes; i++) { + GLmesh* glm = &e->meshes[i]; + *glm = loadGLMesh(mdl->meshes[i], GL_TRIANGLES, glm::vec3(0, 0, 0), + num_attrib_mappings, attrib_mappings); + + if (glm->vao_id == 0) { + printf("%s(), error initializing entity\n", __FUNCTION__); + return false; + } + } + + return true; +} + +void +setEntityPosition(Entity* e, glm::vec3 pos) +{ + *e->xform = glm::translate(*e->xform, pos); +} + +void +rotateEntity(Entity* e, glm::vec3 axis, float radians) +{ + *e->xform = glm::rotate(*e->xform, radians, axis); +} + diff --git a/src/entity.h b/src/entity.h new file mode 100644 index 0000000..e7a1bf0 --- /dev/null +++ b/src/entity.h @@ -0,0 +1,30 @@ + +#pragma once + +#include "asset.h" +#include "shader.h" +#include "types.h" +#include "util.h" + + +struct Entity +{ + u32 num_meshes; + GLmesh* meshes; + GLTexture* diffuse_texture; // NOTE: pointer into gl_ctx->textures array + glm::mat4* xform; + char* name; +}; + + +bool initEntity(Entity* e, + MemoryArena* arena, + model* mdl, + u32 num_attrib_mappings, + GLBufferToAttribMapping* attrib_mappings, + const char* name = ""); + +void setEntityPosition(Entity* e, glm::vec3 pos); + +void rotateEntity(Entity* e, glm::vec3 axis, float radians); + diff --git a/src/main.cpp b/src/main.cpp index d19fdae..b162909 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,9 +4,9 @@ #include #include -#include #include "asset.h" +#include "entity.h" #include "dumbLog.h" #include "GLDebug.h" #include "shader.h" @@ -25,15 +25,6 @@ struct SDLHandles // TODO: node/tree structure for entities struct Node; -struct Entity -{ - u32 num_meshes; - GLmesh* meshes; - GLTexture* diffuse_texture; // NOTE: pointer into gl_ctx->textures array - glm::mat4* xform; - char* name; -}; - struct RenderGroup { shader_program* shader; @@ -93,58 +84,6 @@ getFreeEntity(RenderGroup* rg) return nullptr; } -bool -initEntity(Entity* e, - MemoryArena* arena, - model* mdl, - u32 num_attrib_mappings, - GLBufferToAttribMapping* attrib_mappings, - const char* name = "") -{ - e->num_meshes = mdl->num_meshes; - e->meshes = ARENA_ALLOC(arena, GLmesh, e->num_meshes); - e->xform = ARENA_ALLOC(arena, glm::mat4, 1); - *e->xform = glm::mat4(1.f); - e->name = arenaCopyCStr(arena, name); - - for (u32 i = 0; i< e->num_meshes; i++) { - GLmesh* glm = &e->meshes[i]; - *glm = loadGLMesh(mdl->meshes[i], GL_TRIANGLES, glm::vec3(0, 0, 0), - num_attrib_mappings, attrib_mappings); - - if (glm->vao_id == 0) { - printf("%s(), error initializing entity\n", __FUNCTION__); - return false; - } - } - - return true; -} - -void -setEntityPosition(Entity* e, glm::vec3 pos) -{ - *e->xform = glm::translate(*e->xform, pos); -} - -void -rotateEntity(Entity* e, glm::vec3 axis, float radians) -{ - *e->xform = glm::rotate(*e->xform, radians, axis); -} - -// NOTE: equivalent to rgAppend() in libTangerine -model* -getModelByPath(RenderState* rs, const char* filepath) -{ - model* mdl = assetGetCachedModel(&rs->assets, utilFNV64a_str(filepath)); - - if (!mdl) - mdl = assetLoadFromFile(&rs->assets, rs->assets.arena, filepath); - - return mdl; -} - RenderGroup* getFreeRenderGroup(RenderState* rs) { diff --git a/src/shader.h b/src/shader.h index cdb30dc..18e2dd1 100644 --- a/src/shader.h +++ b/src/shader.h @@ -6,6 +6,7 @@ #include #include "types.h" +#include "util.h" struct gl_uniform diff --git a/src/types.h b/src/types.h index 4b893a7..7068408 100644 --- a/src/types.h +++ b/src/types.h @@ -1,6 +1,9 @@ #pragma once +#include + + typedef uint8_t u8; typedef int32_t i32; typedef uint16_t u16;