Refactor handling of imported assets a bit

master
outfrost 2019-01-13 20:32:25 +01:00
parent be9c54a94a
commit c9189c7807
5 changed files with 37 additions and 24 deletions

11
assimp_types.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef ASSIMP_TYPES_H_
#define ASSIMP_TYPES_H_
#include <assimp/scene.h>
typedef struct aiScene AiScene;
typedef struct aiNode AiNode;
typedef struct aiMesh AiMesh;
typedef struct aiFace AiFace;
#endif

23
level.c
View File

@ -1,30 +1,29 @@
#include <GL/gl.h>
#include <assimp/cimport.h>
#include <assimp/scene.h>
#include <stdlib.h>
#include <stdio.h> // TODO remove
#include "level.h"
#include "tga.h"
const Block BLOCK_EMPTY = 0;
const Block BLOCK_WALL01 = 1;
const AiScene* levelScene = NULL;
TgaImage* levelImage = NULL;
static const Block BLOCK_EMPTY = 0;
static const Block BLOCK_WALL01 = 0xFF0000FF; // red
Block getBlock(GLushort x, GLushort y) {
if (levelImage == NULL) {
return BLOCK_EMPTY;
}
return ((Block*) (*levelImage).bytes)[x * (*levelImage).header.imageWidth + y];
static const AiScene* blockWall01 = NULL;
static TgaImage* levelImage = NULL;
void initLevel() {
blockWall01 = importScene("out/assets/wall01.3ds");
levelScene = blockWall01;
}
void setImage(TgaImage* image) {
levelImage = image;
}
const struct aiScene* importModel(const char* path) {
const AiScene* importScene(const char* path) {
const struct aiScene* scene = aiImportFile(path, 0u);
if (scene == NULL) {
fprintf(stderr, "Asset import failed at file %s\n", path); // TODO factor logging the heck outta here

13
level.h
View File

@ -1,17 +1,18 @@
#ifndef LEVEL_H_
#define LEVEL_H_
#include <GL/gl.h>
#include <stdint.h>
#include "assimp_types.h"
#include "tga.h"
typedef GLuint Block;
typedef uint32_t Block;
const Block BLOCK_EMPTY;
const Block BLOCK_WALL01;
const AiScene* levelScene;
Block getBlock(GLushort x, GLushort y);
void initLevel();
void setImage(TgaImage* image);
const struct aiScene* importModel(const char* path);
const AiScene* importScene(const char* path);
#endif

2
main.c
View File

@ -24,8 +24,8 @@ int main(int argc, char** argv) {
initRender();
initPerformanceMetering();
initLevel();
model = importModel("out/assets/wall01.3ds");
/*
fprintf(stderr, "*model = ");
print_struct_aiScene(stderr, model);

View File

@ -1,6 +1,8 @@
#include <GL/glut.h>
#include <assimp/scene.h>
#include "assimp_types.h"
#include "level.h"
#include "render.h"
#include "typedefs.h"
#include "performance.h"
@ -19,7 +21,7 @@ void renderScene() {
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
drawModelRecursive(model, (*model).mRootNode);
drawModelRecursive(levelScene, levelScene->mRootNode);
glDisable(GL_LIGHT0);
glFlush();
@ -55,15 +57,15 @@ void drawAxes() {
glEnd();
}
void drawModelRecursive(const struct aiScene* model, const struct aiNode* node) {
void drawModelRecursive(const AiScene* model, const AiNode* node) {
if (((*model).mFlags & AI_SCENE_FLAGS_INCOMPLETE) == AI_SCENE_FLAGS_INCOMPLETE) {
return;
}
for (int i = 0; i < (*node).mNumMeshes; ++i) {
const struct aiMesh* mesh = (*model).mMeshes[(*node).mMeshes[i]];
const AiMesh* mesh = (*model).mMeshes[(*node).mMeshes[i]];
for (int k = 0; k < (*mesh).mNumFaces; ++k) {
const struct aiFace face = (*mesh).mFaces[k];
const AiFace face = (*mesh).mFaces[k];
GLenum faceMode;
switch (face.mNumIndices) {