]> git.lizzy.rs Git - shadowclad.git/commitdiff
Add the concept of a player character and load its 3D model
authoroutfrost <kotlet.bahn@gmail.com>
Thu, 31 Jan 2019 02:54:34 +0000 (03:54 +0100)
committeroutfrost <kotlet.bahn@gmail.com>
Thu, 31 Jan 2019 02:57:12 +0000 (03:57 +0100)
Also, a bit of code cleanup

12 files changed:
Makefile
asset.c [new file with mode: 0644]
asset.h [new file with mode: 0644]
assimp_types.h
level.c
level.h
logger.c
main.c
performance.c
player.c [new file with mode: 0644]
player.h [new file with mode: 0644]
render.c

index 28a753a23d6bc51b2918846defb856842fddb2ef..6dcf1047887dcc357bfdca8493b480315eaa1b14 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,8 +4,8 @@ LDLIBS ::= -L/usr/local/lib -lGL -lGLEW -lglut -lassimp $(LDLIBS)
 
 # Prefix all object file names with the compilation directory
 objects ::= $(addprefix out/, \
-              main.o level.o logger.o \
-              performance.o render.o tga.o ui.o)
+              main.o asset.o level.o logger.o \
+              performance.o player.o render.o tga.o ui.o)
 
 # Set executable extension for the platform
 ifeq ($(OS),Windows_NT)
diff --git a/asset.c b/asset.c
new file mode 100644 (file)
index 0000000..ff7c3db
--- /dev/null
+++ b/asset.c
@@ -0,0 +1,20 @@
+#include <assimp/cimport.h>
+#include <assimp/postprocess.h>
+
+#include "assimp_types.h"
+
+#include "logger.h"
+
+const AiScene* importScene(const char* path) {
+       const AiScene* scene = aiImportFile(path, aiProcess_PreTransformVertices);
+       if (scene == NULL) {
+               logError("Failed to import asset from %s", path);
+       }
+       else if ((scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) == AI_SCENE_FLAGS_INCOMPLETE) {
+               logError("Incomplete scene imported from %s", path);
+               aiReleaseImport(scene);
+               scene = NULL;
+       }
+       return scene;
+       // TODO aiReleaseImport(scene);
+}
diff --git a/asset.h b/asset.h
new file mode 100644 (file)
index 0000000..03e268f
--- /dev/null
+++ b/asset.h
@@ -0,0 +1,8 @@
+#ifndef ASSET_H_
+#define ASSET_H_
+
+#include "assimp_types.h"
+
+const AiScene* importScene(const char* path);
+
+#endif
index 5869c323cf1367f1bb847ccedd6f38d63bca0bf9..5e58279290017d23ca1df232d581f0509ef01360 100644 (file)
@@ -3,6 +3,11 @@
 
 #include <assimp/scene.h>
 
+#ifdef ASSIMP_DOUBLE_PRECISION
+       #error "ASSIMP_DOUBLE_PRECISION is defined"
+       #error "shadowclad relies on ai_real defined as a single precision float"
+#endif
+
 typedef struct aiScene AiScene;
 typedef struct aiNode AiNode;
 typedef struct aiMesh AiMesh;
diff --git a/level.c b/level.c
index 1048f86c7150fad1015e129c2065f38e95c2487c..1a1778b29c0c170b5fc8b5b39357e0c759153b92 100644 (file)
--- a/level.c
+++ b/level.c
@@ -1,10 +1,10 @@
 #include <GL/gl.h>
-#include <assimp/cimport.h>
-#include <assimp/postprocess.h>
 #include <stdlib.h>
 
+#include "asset.h"
 #include "level.h"
 #include "logger.h"
+#include "player.h"
 
 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
                             .sceneData = NULL,
@@ -21,10 +21,13 @@ BlockGrid levelGrid = { .width = 3,
                         .depth = 3,
                         .blocks = testBlocks };
 
-//static TgaImage* levelImage = NULL;
+#define DEFAULT_PLAYER_SPAWN_POS { -BLOCKGRID_CELL_SIZE, 0.0f, -BLOCKGRID_CELL_SIZE }
+AiVector3D playerSpawnPos = DEFAULT_PLAYER_SPAWN_POS;
 
 static const char* replaceFileExtension(const AiString path, const char* ext);
 
+
+
 void initLevel() {
        const AiScene* sceneData = importScene("out/assets/wall01.3ds");
        blockWall01.sceneData = sceneData;
@@ -90,6 +93,7 @@ void buildLevelFromImage(TgaImage* image) {
                              .blocks = malloc(image->header.imageWidth
                                               * image->header.imageHeight
                                               * sizeof(Block*)) };
+       playerSpawnPos = (AiVector3D) DEFAULT_PLAYER_SPAWN_POS;
        
        for (int z = 0; z < newGrid.depth; ++z) {
                for (int x = 0; x < newGrid.width; ++x) {
@@ -99,6 +103,10 @@ void buildLevelFromImage(TgaImage* image) {
                                case 0xFFFF0000:
                                        block = &blockWall01;
                                        break;
+                               case 0xFF00FFFF:
+                                       block = &blockEmpty;
+                                       playerSpawnPos = (AiVector3D) { x * BLOCKGRID_CELL_SIZE, 0.0f, z * BLOCKGRID_CELL_SIZE };
+                                       break;
                                default:
                                        block = &blockEmpty;
                                        break;
@@ -108,20 +116,7 @@ void buildLevelFromImage(TgaImage* image) {
        }
        
        levelGrid = newGrid;
-}
-
-const AiScene* importScene(const char* path) {
-       const AiScene* scene = aiImportFile(path, aiProcess_PreTransformVertices);
-       if (scene == NULL) {
-               logError("Failed to import asset from %s", path);
-       }
-       else if ((scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) == AI_SCENE_FLAGS_INCOMPLETE) {
-               logError("Incomplete scene imported from %s", path);
-               aiReleaseImport(scene);
-               scene = NULL;
-       }
-       return scene;
-       // TODO aiReleaseImport(scene);
+       spawnPlayer();
 }
 
 /** BUGS
diff --git a/level.h b/level.h
index ea43b400676d5084b580641d75ccfad5be8c3d8a..83fefd908e0248be3f8641bf5d1c05755c84f053 100644 (file)
--- a/level.h
+++ b/level.h
@@ -29,10 +29,10 @@ typedef struct {
 #define BLOCKGRID_CELL_SIZE 2.5f
 
 BlockGrid levelGrid;
+AiVector3D playerSpawnPos;
 
 void initLevel();
 void buildLevelFromImage(TgaImage* image);
-const AiScene* importScene(const char* path);
 
 static inline Block* getBlockFromGrid(BlockGrid grid, int x, int z) {
        return grid.blocks[(z * grid.width) + x];
index 23b0dd81e0b29c23f9fc9b006860c83942e27887..373df634f708dd4ea7eb9a491756e0202c8411bb 100644 (file)
--- a/logger.c
+++ b/logger.c
@@ -5,6 +5,8 @@
 
 LogLevel logLevel = LOGLEVEL_DEBUG;
 
+
+
 void logMessage(LogLevel msgLevel, const char* file, int line, const char* message, ...) {
        if (msgLevel > logLevel) {
                return;
diff --git a/main.c b/main.c
index 527a936ef9af12784783567135f367806958a654..8076c54c61e660cbeb7d585296e2af73da60294c 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,7 +1,6 @@
 #include <GL/glxew.h>
 #include <GL/glut.h>
 
-//#include "debugutil.h"
 #include "level.h"
 #include "logger.h"
 #include "performance.h"
@@ -17,7 +16,6 @@ int main(int argc, char** argv) {
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
        glutCreateWindow(NULL);
        
-       // glutSetWindowTitle(getGlInfoString());
        glutSetWindowTitle("shadowclad");
        
        logInfo("OpenGL %s", (const char*) glGetString(GL_VERSION));
index 916517b98ae8c2b9da064d04c4e0fff1b237910d..443652cedccbaae87b648c6ca6bf0c2f0354067f 100644 (file)
@@ -14,6 +14,8 @@ static Timepoint lastDisplayTime;
 static int frames = 0;
 static bool meteringEnabled = false;
 
+
+
 void initPerformanceMetering() {
        if (clock_gettime(CLOCK_MONOTONIC, &lastDisplayTime) != 0) {
                logWarning("Clock read failed, performance metering unavailable");
diff --git a/player.c b/player.c
new file mode 100644 (file)
index 0000000..dfe41f3
--- /dev/null
+++ b/player.c
@@ -0,0 +1,20 @@
+#include "asset.h"
+#include "level.h"
+#include "logger.h"
+#include "player.h"
+
+PlayerCharacter playerCharacter = { .sceneData = NULL,
+                                    .textureIds = NULL };
+
+static AiVector3D playerPos;
+
+
+
+void initPlayer() {
+       playerCharacter.sceneData = importScene("out/assets/playercharacter.3ds");
+       // TODO import textures
+}
+
+void spawnPlayer() {
+       playerPos = playerSpawnPos;
+}
diff --git a/player.h b/player.h
new file mode 100644 (file)
index 0000000..f480c45
--- /dev/null
+++ b/player.h
@@ -0,0 +1,15 @@
+#ifndef PLAYER_H_
+#define PLAYER_H_
+
+#include <GL/gl.h>
+
+#include "assimp_types.h"
+
+typedef struct {
+       const AiScene* sceneData;
+       GLuint* textureIds;
+} PlayerCharacter;
+
+void spawnPlayer();
+
+#endif
index ce77a0f05079caaf8822c467a48d64d45756b848..87acb699f18ab8920e1d4ac0fe874ecb486eaf1d 100644 (file)
--- a/render.c
+++ b/render.c
@@ -13,6 +13,8 @@ static void drawAxes();
 static void renderBlockGrid(const BlockGrid grid);
 static void drawBlock(const Block* block);
 
+
+
 void initRender() {
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);