]> git.lizzy.rs Git - shadowclad.git/blobdiff - src/engine/render.c
Create keybinds for scene and render debug functionalities
[shadowclad.git] / src / engine / render.c
index 3b88d44274ab05ec001010a36b0fc1906bfacdc0..c3ddb3a148cda9af5dc98c12fc82b9dfa535d520 100644 (file)
@@ -1,23 +1,22 @@
-#include <GL/glut.h>
-#include <stdbool.h>
-
-#include "game/level.h"
-#include "game/player.h"
+#include "render.h"
 
 #include "geometry.h"
 #include "performance.h"
-#include "scene.h"
 
-const float AXIS_RADIUS = 5.0f;
+float viewportAspectRatio = 1.0f;
+const Scene* cameraAnchor;
+bool debugScene = false;
+bool debugRender = false;
+
+static const float AXIS_RADIUS = 5.0f;
 
+static void renderScene(const Scene*, const Transform baseTransform);
 static void setupCamera();
-static void moveCameraTo(const Vector3D pos);
+static void moveCameraTo(const Scene* scene);
 static void drawAxes();
-static void renderBlockGrid(const BlockGrid grid);
-static void renderCharacter(const Character* character, const Vector3D pos);
 static void drawSolid(const Solid* solid);
 
-float viewportAspectRatio = 1.0f;
+
 
 void initRender() {
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
@@ -35,40 +34,43 @@ 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.005f);
+       
+       //glShadeModel(GL_FLAT);
 }
 
-
-
-void renderSceneNew(const Scene*);
-
-void renderFrame() {
+void renderFrame(GLFWwindow* window) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-       
+
        glEnable(GL_NORMALIZE);
        glEnable(GL_CULL_FACE);
        glEnable(GL_DEPTH_TEST);
-       
+
        setupCamera();
-       moveCameraTo(playerPos);
-       
-       renderSceneNew(currentScene);
-       
+       moveCameraTo(cameraAnchor);
+
+       renderScene(currentScene, identity());
+
        glFlush();
-       glutSwapBuffers();
+       glfwSwapBuffers(window);
        frameRendered();
-       glutPostRedisplay();
 }
 
-void renderSceneNew(const Scene* scene) {
+static void renderScene(const Scene* scene, const Transform baseTransform) {
        if (!scene) {
                return;
        }
 
+       Transform transform = multiply(scene->transform, baseTransform);
+
        glMatrixMode(GL_MODELVIEW);
-       glLoadTransposeMatrixf((const GLfloat*) &scene->transform);
+       glLoadTransposeMatrixf((const GLfloat*) &transform);
 
        glDisable(GL_LIGHTING);
-       drawAxes();
+
+       if (debugScene) {
+               drawAxes();
+       }
+
        glEnable(GL_LIGHTING);
 
        if (scene->solid) {
@@ -80,39 +82,10 @@ void renderSceneNew(const Scene* scene) {
        }
 
        for (size_t i = 0; i < scene->numChildren; ++i) {
-               renderSceneNew(scene->children[i]);
+               renderScene(scene->children[i], transform);
        }
 }
 
-
-
-void renderScene() {
-       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-       
-       glEnable(GL_NORMALIZE);
-       glEnable(GL_CULL_FACE);
-       glEnable(GL_DEPTH_TEST);
-       
-       setupCamera();
-       moveCameraTo(playerPos);
-       
-       glDisable(GL_LIGHTING);
-       drawAxes();
-       glEnable(GL_LIGHTING);
-       
-       glEnable(GL_LIGHT0);
-       glEnable(GL_TEXTURE_2D);
-       renderBlockGrid(levelGrid);
-       renderCharacter(&playerCharacter, playerPos);
-       glDisable(GL_TEXTURE_2D);
-       glDisable(GL_LIGHT0);
-       
-       glFlush();
-       glutSwapBuffers();
-       frameRendered();
-       glutPostRedisplay();
-}
-
 static void setupCamera() {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
@@ -126,8 +99,9 @@ static void setupCamera() {
        glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
 }
 
-static void moveCameraTo(const Vector3D pos) {
+static void moveCameraTo(const Scene* anchor) {
        glMatrixMode(GL_PROJECTION);
+       Vector3D pos = translationOf(worldTransform(anchor));
        glTranslatef(-pos.x, -pos.y, -pos.z);
 }
 
@@ -153,24 +127,8 @@ static void drawAxes() {
        glEnd();
 }
 
-static void renderBlockGrid(const BlockGrid grid) {
-       glMatrixMode(GL_MODELVIEW);
-       for (size_t z = 0; z < grid.depth; ++z) {
-               glLoadIdentity();
-               glTranslatef(0.0f, 0.0f, z * BLOCKGRID_CELL_SIZE);
-               for (size_t x = 0; x < grid.width; ++x) {
-                       drawSolid(getBlockFromGrid(grid, x, z)->solid);
-                       glTranslatef(BLOCKGRID_CELL_SIZE, 0.0f, 0.0f);
-               }
-       }
-       glLoadIdentity();
-}
-
-static void renderCharacter(const Character* character, const Vector3D pos) {
-       glMatrixMode(GL_MODELVIEW);
-       glTranslatef(pos.x, pos.y, pos.z);
-       drawSolid(character->solid);
-       glLoadIdentity();
+static GLfloat absolute(GLfloat a) {
+       return a < 0 ? -a : a;
 }
 
 static void drawSolid(const Solid* solid) {
@@ -185,12 +143,27 @@ static void drawSolid(const Solid* solid) {
                const Mesh mesh = solid->meshes[meshIndex];
                glBindTexture(GL_TEXTURE_2D,
                              solid->materials[mesh.materialIndex].textureId);
-               bool hasNormals = mesh.normals != NULL;
-               bool hasTextureCoords = mesh.textureCoords != NULL;
                
                for (size_t faceIndex = 0; faceIndex < mesh.numFaces; ++faceIndex) {
                        const Face face = mesh.faces[faceIndex];
                        
+                       if (debugRender && face.normals) {
+                               glDisable(GL_LIGHTING);
+                               glDisable(GL_TEXTURE_2D);
+                               glBegin(GL_LINES);
+                               for (size_t i = 0; i < face.numIndices; ++i) {
+                                       size_t vertIndex = face.indices[i];
+                                       Vector3D vertex = mesh.vertices[vertIndex];
+                                       Vector3D normal = face.normals[i];
+                                       glColor3f(absolute(normal.x), absolute(normal.y), absolute(normal.z));
+                                       glVertex3f(vertex.x, vertex.y, vertex.z);
+                                       glVertex3f(vertex.x + normal.x, vertex.y + normal.y, vertex.z + normal.z);
+                               }
+                               glEnd();
+                               glEnable(GL_TEXTURE_2D);
+                               glEnable(GL_LIGHTING);
+                       }
+                       
                        GLenum faceMode;
                        switch (face.numIndices) {
                                case 1: faceMode = GL_POINTS; break;
@@ -203,14 +176,15 @@ static void drawSolid(const Solid* solid) {
                        
                        for (size_t i = 0; i < face.numIndices; ++i) {
                                size_t vertIndex = face.indices[i];
-                               if (hasNormals) {
-                                       if (hasTextureCoords) {
+                               if (face.normals) {
+                                       if (mesh.textureCoords) {
                                                Vector3D coords = mesh.textureCoords[vertIndex];
                                                glTexCoord2f(coords.x, coords.y);
                                        }
-                                       Vector3D normal = mesh.normals[vertIndex];
+                                       Vector3D normal = face.normals[i];
                                        glNormal3f(normal.x, normal.y, normal.z);
                                }
+
                                Vector3D vertex = mesh.vertices[vertIndex];
                                glVertex3f(vertex.x, vertex.y, vertex.z);
                        }