]> git.lizzy.rs Git - shadowclad.git/blobdiff - render.c
Render the level's grid of blocks
[shadowclad.git] / render.c
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];