]> git.lizzy.rs Git - shadowclad.git/commitdiff
Render the level's grid of blocks
authoroutfrost <kotlet.bahn@gmail.com>
Tue, 29 Jan 2019 18:53:59 +0000 (19:53 +0100)
committeroutfrost <kotlet.bahn@gmail.com>
Tue, 29 Jan 2019 18:53:59 +0000 (19:53 +0100)
assimp_types.h
level.c
level.h
render.c

index 52302729a876721c821ee69c01c5ff5c437b4c1e..5869c323cf1367f1bb847ccedd6f38d63bca0bf9 100644 (file)
@@ -7,6 +7,7 @@ typedef struct aiScene AiScene;
 typedef struct aiNode AiNode;
 typedef struct aiMesh AiMesh;
 typedef struct aiFace AiFace;
+typedef struct aiVector3D AiVector3D;
 typedef struct aiString AiString;
 
 #endif
diff --git a/level.c b/level.c
index 8c3b59dff4a67f9ac76aea1b862b3b9142c36806..7e7f02971cace81624029e786861e42c96c1505a 100644 (file)
--- a/level.c
+++ b/level.c
@@ -5,8 +5,6 @@
 #include "level.h"
 #include "logger.h"
 
-BlockGrid levelGrid;
-
 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
                             .sceneData = NULL,
                             .textureIds = NULL };
@@ -14,6 +12,14 @@ static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
                              .sceneData = NULL,
                              .textureIds = NULL };
 
+static Block* testBlocks[9] = { &blockWall01, &blockWall01, &blockWall01,
+                                &blockEmpty, &blockEmpty, &blockEmpty,
+                                &blockWall01, &blockEmpty, &blockWall01 };
+
+BlockGrid levelGrid = { .width = 3,
+                        .depth = 3,
+                        .blocks = testBlocks };
+
 //static TgaImage* levelImage = NULL;
 
 static const char* replaceFileExtension(const AiString path, const char* ext);
@@ -78,6 +84,8 @@ const AiScene* importScene(const char* 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/level.h b/level.h
index 539ab37fbf1a8aeaa539f6b13c5fad8f81cc4135..c2e5c1395b0ce227d2a78603eb43b6fe310381c7 100644 (file)
--- a/level.h
+++ b/level.h
@@ -22,14 +22,20 @@ typedef struct {
 
 typedef struct {
        int width;
-       int height;
-       Block* blocks;
+       int depth;
+       Block** blocks;
 } BlockGrid;
 
+#define BLOCKGRID_CELL_SIZE 2.5f
+
 BlockGrid levelGrid;
 
 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];
+}
+
 #endif
index 2ef266898d61df404fdb97a4d6b44dade6c49bcd..176e851ebd4bcfcf11f3b2a5eddbcde60b8c051e 100644 (file)
--- a/render.c
+++ b/render.c
@@ -1,4 +1,5 @@
 #include <GL/glut.h>
+#include <stdbool.h>
 
 #include "level.h"
 #include "performance.h"
@@ -7,13 +8,16 @@
 
 const float AXIS_RADIUS = 5.0f;
 
+static void renderBlockGrid(const BlockGrid grid);
+static void drawBlock(const Block* block);
+
 void initRender() {
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        
        GLfloat light0_ambient[] = {0.1f, 0.1f, 0.1f, 1.0f};
        GLfloat light0_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
        GLfloat light0_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
-       GLfloat light0_position[] = {5.0f, 10.0f, 10.0f, 1.0f};
+       GLfloat light0_position[] = {5.0f, 10.0f, 5.0f, 1.0f};
        
        glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
        glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
@@ -22,7 +26,7 @@ void initRender() {
        
        glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0f);
        glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05f);
-       glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.001f);
+       glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.005f);
 }
 
 void renderScene() {
@@ -31,13 +35,17 @@ void renderScene() {
        
        glEnable(GL_NORMALIZE);
        glEnable(GL_CULL_FACE);
+       glEnable(GL_DEPTH_TEST);
        
        glDisable(GL_LIGHTING);
        drawAxes();
        glEnable(GL_LIGHTING);
        
        glEnable(GL_LIGHT0);
+       glEnable(GL_TEXTURE_2D);
 //     drawSceneRecursive(levelScene, levelScene->mRootNode);
+       renderBlockGrid(levelGrid);
+       glDisable(GL_TEXTURE_2D);
        glDisable(GL_LIGHT0);
        
        glFlush();
@@ -73,6 +81,63 @@ void drawAxes() {
        glEnd();
 }
 
+static void renderBlockGrid(const BlockGrid grid) {
+       glMatrixMode(GL_MODELVIEW);
+       for (int z = 0; z < grid.depth; ++z) {
+               glLoadIdentity();
+               glTranslatef(0.0f, 0.0f, z * BLOCKGRID_CELL_SIZE);
+               for (int x = 0; x < grid.width; ++x) {
+                       drawBlock(getBlockFromGrid(grid, x, z));
+                       glTranslatef(BLOCKGRID_CELL_SIZE, 0.0f, 0.0f);
+               }
+       }
+       glLoadIdentity();
+}
+
+static void drawBlock(const Block* block) {
+       if (block->sceneData == NULL) {
+               return;
+       }
+       
+       glColor3f(0.5f, 1.0f, 0.0f);
+       
+       for (int i = 0; i < block->sceneData->mNumMeshes; ++i) {
+               glBindTexture(GL_TEXTURE_2D, block->textureIds[i]);
+               const AiMesh* mesh = block->sceneData->mMeshes[i];
+               bool hasNormals = mesh->mNormals != NULL;
+               bool hasTextureCoords = mesh->mTextureCoords[0] != NULL;
+               
+               for (int k = 0; k < mesh->mNumFaces; ++k) {
+                       const AiFace face = mesh->mFaces[k];
+                       
+                       GLenum faceMode;
+                       switch (face.mNumIndices) {
+                               case 1: faceMode = GL_POINTS; break;
+                               case 2: faceMode = GL_LINES; break;
+                               case 3: faceMode = GL_TRIANGLES; break;
+                               default: faceMode = GL_POLYGON; break;
+                       }
+                       
+                       glBegin(faceMode);
+                       
+                       for (int l = 0; l < face.mNumIndices; ++l) {
+                               unsigned int vertexIndex = face.mIndices[l];
+                               if (hasNormals) {
+                                       if (hasTextureCoords) {
+                                               AiVector3D coords = mesh->mTextureCoords[0][vertexIndex];
+                                               glTexCoord2f(coords.x, coords.y);
+                                       }
+                                       glNormal3fv(&mesh->mNormals[vertexIndex].x);
+                               }
+                               glVertex3fv((const GLfloat*) &mesh->mVertices[vertexIndex]);
+                       }
+                       
+                       glEnd();
+               }
+       }
+       glBindTexture(GL_TEXTURE_2D, 0);
+}
+
 void drawSceneRecursive(const AiScene* scene, const AiNode* node) {
        if (((*scene).mFlags & AI_SCENE_FLAGS_INCOMPLETE) == AI_SCENE_FLAGS_INCOMPLETE) {
                return;
@@ -80,6 +145,7 @@ void drawSceneRecursive(const AiScene* scene, const AiNode* node) {
        
        for (int i = 0; i < (*node).mNumMeshes; ++i) {
                const AiMesh* mesh = (*scene).mMeshes[(*node).mMeshes[i]];
+               
                for (int k = 0; k < (*mesh).mNumFaces; ++k) {
                        const AiFace face = (*mesh).mFaces[k];