]> git.lizzy.rs Git - shadowclad.git/blobdiff - render.c
Render the level's grid of blocks
[shadowclad.git] / render.c
index c10f8f1f03a85b6faf56f69c468bb18c66255e95..176e851ebd4bcfcf11f3b2a5eddbcde60b8c051e 100644 (file)
--- a/render.c
+++ b/render.c
@@ -1,27 +1,51 @@
 #include <GL/glut.h>
-
-#include "assimp_types.h"
+#include <stdbool.h>
 
 #include "level.h"
+#include "performance.h"
 #include "render.h"
 #include "typedefs.h"
-#include "performance.h"
 
 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, 5.0f, 1.0f};
+       
+       glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
+       glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
+       glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
+       glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
+       
+       glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0f);
+       glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05f);
+       glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.005f);
+}
+
 void renderScene() {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
        
        glEnable(GL_NORMALIZE);
        glEnable(GL_CULL_FACE);
+       glEnable(GL_DEPTH_TEST);
        
        glDisable(GL_LIGHTING);
        drawAxes();
        glEnable(GL_LIGHTING);
        
        glEnable(GL_LIGHT0);
-       drawModelRecursive(levelScene, levelScene->mRootNode);
+       glEnable(GL_TEXTURE_2D);
+//     drawSceneRecursive(levelScene, levelScene->mRootNode);
+       renderBlockGrid(levelGrid);
+       glDisable(GL_TEXTURE_2D);
        glDisable(GL_LIGHT0);
        
        glFlush();
@@ -57,13 +81,71 @@ void drawAxes() {
        glEnd();
 }
 
-void drawModelRecursive(const AiScene* model, const AiNode* node) {
-       if (((*model).mFlags & AI_SCENE_FLAGS_INCOMPLETE) == AI_SCENE_FLAGS_INCOMPLETE) {
+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;
        }
        
        for (int i = 0; i < (*node).mNumMeshes; ++i) {
-               const AiMesh* mesh = (*model).mMeshes[(*node).mMeshes[i]];
+               const AiMesh* mesh = (*scene).mMeshes[(*node).mMeshes[i]];
+               
                for (int k = 0; k < (*mesh).mNumFaces; ++k) {
                        const AiFace face = (*mesh).mFaces[k];
                        
@@ -87,6 +169,6 @@ void drawModelRecursive(const AiScene* model, const AiNode* node) {
        }
        
        for (int i = 0; i < (*node).mNumChildren; ++i) {
-               drawModelRecursive(model, (*node).mChildren[i]);
+               drawSceneRecursive(scene, (*node).mChildren[i]);
        }
 }