Browse Source

move Entity structure and interface to separate files

main
cinnaboot 5 years ago
parent
commit
062205a2f9
  1. 46
      src/entity.cpp
  2. 30
      src/entity.h
  3. 63
      src/main.cpp
  4. 1
      src/shader.h
  5. 3
      src/types.h

46
src/entity.cpp

@ -0,0 +1,46 @@
#include <glm/gtc/matrix_transform.hpp>
#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);
}

30
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);

63
src/main.cpp

@ -4,9 +4,9 @@
#include <iostream>
#include <SDL2/SDL.h>
#include <glm/gtc/matrix_transform.hpp>
#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)
{

1
src/shader.h

@ -6,6 +6,7 @@
#include <glm/glm.hpp>
#include "types.h"
#include "util.h"
struct gl_uniform

3
src/types.h

@ -1,6 +1,9 @@
#pragma once
#include <cstdint>
typedef uint8_t u8;
typedef int32_t i32;
typedef uint16_t u16;

Loading…
Cancel
Save