]> git.lizzy.rs Git - shadowclad.git/blobdiff - src/engine/render.c
Compute world space transforms for rendering
[shadowclad.git] / src / engine / render.c
index 47ec89ca5da10ed3844d8e1e34b264e8ab340c79..0cfc49f1f57af34cc913d99a37c424f6a1bdebef 100644 (file)
@@ -1,22 +1,21 @@
-#include <GL/glut.h>
-#include <stdbool.h>
+#include "render.h"
 
-#include "game/level.h"
-#include "game/player.h"
+#include <stdbool.h>
+#include <GL/glut.h>
 
 #include "geometry.h"
 #include "performance.h"
 
-const float AXIS_RADIUS = 5.0f;
+float viewportAspectRatio = 1.0f;
+const Scene* cameraAnchor;
+
+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 drawAsset3D(const Asset3D* asset3D);
-
-float viewportAspectRatio = 1.0f;
+static void drawSolid(const Solid* solid);
 
 
 
@@ -38,33 +37,51 @@ void initRender() {
        glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.005f);
 }
 
-void renderScene() {
+void renderFrame() {
        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);
-       
+       moveCameraTo(cameraAnchor);
+
+       renderScene(currentScene, identity());
+
        glFlush();
        glutSwapBuffers();
        frameRendered();
        glutPostRedisplay();
 }
 
+static void renderScene(const Scene* scene, const Transform baseTransform) {
+       if (!scene) {
+               return;
+       }
+
+       Transform transform = multiply(scene->transform, baseTransform);
+
+       glMatrixMode(GL_MODELVIEW);
+       glLoadTransposeMatrixf((const GLfloat*) &transform);
+
+       glDisable(GL_LIGHTING);
+       drawAxes();
+       glEnable(GL_LIGHTING);
+
+       if (scene->solid) {
+               glEnable(GL_LIGHT0);
+               glEnable(GL_TEXTURE_2D);
+               drawSolid(scene->solid);
+               glDisable(GL_TEXTURE_2D);
+               glDisable(GL_LIGHT0);
+       }
+
+       for (size_t i = 0; i < scene->numChildren; ++i) {
+               renderScene(scene->children[i], transform);
+       }
+}
+
 static void setupCamera() {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
@@ -78,8 +95,10 @@ 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);
+       // TODO This needs to account for parent nodes as well
+       Vector3D pos = translationOf(worldTransform(anchor));
        glTranslatef(-pos.x, -pos.y, -pos.z);
 }
 
@@ -105,38 +124,18 @@ 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) {
-                       drawAsset3D(getBlockFromGrid(grid, x, z)->asset3D);
-                       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);
-       drawAsset3D(character->asset3D);
-       glLoadIdentity();
-}
-
-static void drawAsset3D(const Asset3D* asset3D) {
-       if (asset3D == NULL) {
+static void drawSolid(const Solid* solid) {
+       if (solid == NULL) {
                return;
        }
        
        glMatrixMode(GL_MODELVIEW);
        glColor3f(0.5f, 1.0f, 0.0f);
        
-       for (size_t meshIndex = 0; meshIndex < asset3D->numMeshes; ++meshIndex) {
-               const Mesh mesh = asset3D->meshes[meshIndex];
+       for (size_t meshIndex = 0; meshIndex < solid->numMeshes; ++meshIndex) {
+               const Mesh mesh = solid->meshes[meshIndex];
                glBindTexture(GL_TEXTURE_2D,
-                             asset3D->materials[mesh.materialIndex].textureId);
+                             solid->materials[mesh.materialIndex].textureId);
                bool hasNormals = mesh.normals != NULL;
                bool hasTextureCoords = mesh.textureCoords != NULL;