]> git.lizzy.rs Git - shadowclad.git/blobdiff - src/engine/render.c
Finish moving to scene tree implementation; clean up unused code
[shadowclad.git] / src / engine / render.c
index 68385b07919af5a40cf14e00ece7d3c5b3b57034..a26cfab5206219f04cc1a3b29f83314467d9319d 100644 (file)
@@ -1,23 +1,21 @@
 #include <GL/glut.h>
 #include <stdbool.h>
 
-#include "game/level.h"
-#include "game/player.h"
-
 #include "geometry.h"
 #include "performance.h"
+#include "scene.h"
+
+float viewportAspectRatio = 1.0f;
+const Scene* cameraAnchor;
 
-const float AXIS_RADIUS = 5.0f;
+static const float AXIS_RADIUS = 5.0f;
 
+static void renderScene(const Scene*);
 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() {
@@ -38,33 +36,49 @@ 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);
+
        glFlush();
        glutSwapBuffers();
        frameRendered();
        glutPostRedisplay();
 }
 
+void renderScene(const Scene* scene) {
+       if (!scene) {
+               return;
+       }
+
+       glMatrixMode(GL_MODELVIEW);
+       glLoadTransposeMatrixf((const GLfloat*) &scene->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]);
+       }
+}
+
 static void setupCamera() {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
@@ -78,8 +92,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(anchor->transform);
        glTranslatef(-pos.x, -pos.y, -pos.z);
 }
 
@@ -105,26 +121,6 @@ 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 void drawSolid(const Solid* solid) {
        if (solid == NULL) {
                return;