From: outfrost Date: Tue, 29 Jan 2019 18:53:59 +0000 (+0100) Subject: Render the level's grid of blocks X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=fb66089ab508cb3e6b292d5d08763c4f3d424785;p=shadowclad.git Render the level's grid of blocks --- diff --git a/assimp_types.h b/assimp_types.h index 5230272..5869c32 100644 --- a/assimp_types.h +++ b/assimp_types.h @@ -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 8c3b59d..7e7f029 100644 --- 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 539ab37..c2e5c13 100644 --- 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 diff --git a/render.c b/render.c index 2ef2668..176e851 100644 --- a/render.c +++ b/render.c @@ -1,4 +1,5 @@ #include +#include #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];