]> git.lizzy.rs Git - shadowclad.git/commitdiff
Render player character and make the camera follow it
authoroutfrost <kotlet.bahn@gmail.com>
Thu, 14 Feb 2019 02:43:07 +0000 (03:43 +0100)
committeroutfrost <kotlet.bahn@gmail.com>
Thu, 14 Feb 2019 02:43:07 +0000 (03:43 +0100)
main.c
player.c
player.h
render.c
render.h
ui.c

diff --git a/main.c b/main.c
index f054e3a062fdd9515d2a27fb081b0d13d936125a..262860be339667556fbfdaa126840e29c9f7a25e 100644 (file)
--- a/main.c
+++ b/main.c
@@ -55,6 +55,7 @@ int main(int argc, char** argv) {
        initRender();
        //initPerformanceMetering();
        initLevel();
+       initPlayer();
        
        glutMainLoop();
        return 0;
index d81d78b23820486edca92395166ff406567dc7fb..4e12613f1948a9c41e08398bd2dceaff33ce3250 100644 (file)
--- a/player.c
+++ b/player.c
@@ -2,15 +2,12 @@
 #include "logger.h"
 #include "player.h"
 
-PlayerCharacter playerCharacter = { .asset3D = NULL };
-
-static Vector3D playerPos;
+Character playerCharacter = { .asset3D = NULL };
 
 
 
 void initPlayer() {
        playerCharacter.asset3D = importAsset("assets/playercharacter.3ds");
-       // TODO import textures
 }
 
 void spawnPlayer() {
index 9a0eb929b1940d131725961f8398dd2c5fa96d37..19898d58b57381985598944d894e331f44d914ab 100644 (file)
--- a/player.h
+++ b/player.h
@@ -7,7 +7,10 @@
 
 typedef struct {
        const Asset3D* asset3D;
-} PlayerCharacter;
+} Character;
+
+Character playerCharacter;
+Vector3D playerPos;
 
 void spawnPlayer();
 
index 05ab0c1baf19284dc8d05fc15d49538b5b6b9418..27190c697b078ec4a3029ac653613a94b2c590b3 100644 (file)
--- a/render.c
+++ b/render.c
@@ -3,13 +3,19 @@
 
 #include "level.h"
 #include "performance.h"
+#include "player.h"
 #include "typedefs.h"
 
 const float AXIS_RADIUS = 5.0f;
 
+static void setupCamera();
+static void moveCameraTo(const Vector3D pos);
 static void drawAxes();
 static void renderBlockGrid(const BlockGrid grid);
-static void drawBlock(const Block* block);
+static void renderCharacter(const Character* character, const Vector3D pos);
+static void drawAsset3D(const Asset3D* asset3D);
+
+float viewportAspectRatio = 1.0f;
 
 
 
@@ -33,12 +39,14 @@ void initRender() {
 
 void renderScene() {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-       glLoadIdentity();
        
        glEnable(GL_NORMALIZE);
        glEnable(GL_CULL_FACE);
        glEnable(GL_DEPTH_TEST);
        
+       setupCamera();
+       moveCameraTo(playerPos);
+       
        glDisable(GL_LIGHTING);
        drawAxes();
        glEnable(GL_LIGHTING);
@@ -46,6 +54,7 @@ void renderScene() {
        glEnable(GL_LIGHT0);
        glEnable(GL_TEXTURE_2D);
        renderBlockGrid(levelGrid);
+       renderCharacter(&playerCharacter, playerPos);
        glDisable(GL_TEXTURE_2D);
        glDisable(GL_LIGHT0);
        
@@ -55,7 +64,26 @@ void renderScene() {
        glutPostRedisplay();
 }
 
+static void setupCamera() {
+       glMatrixMode(GL_PROJECTION);
+       glLoadIdentity();
+       glOrtho(-16.0,
+               16.0,
+               -16.0/viewportAspectRatio,
+               16.0/viewportAspectRatio,
+               -128.0,
+               128.0);
+       glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
+       glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
+}
+
+static void moveCameraTo(const Vector3D pos) {
+       glMatrixMode(GL_PROJECTION);
+       glTranslatef(-pos.x, -pos.y, -pos.z);
+}
+
 static void drawAxes() {
+       glMatrixMode(GL_MODELVIEW);
        // X axis
        glColor3f(1.0f, 0.0f, 0.0f);
        glBegin(GL_LINES);
@@ -82,21 +110,28 @@ static void renderBlockGrid(const BlockGrid grid) {
                glLoadIdentity();
                glTranslatef(0.0f, 0.0f, z * BLOCKGRID_CELL_SIZE);
                for (int x = 0; x < grid.width; ++x) {
-                       drawBlock(getBlockFromGrid(grid, x, z));
+                       drawAsset3D(getBlockFromGrid(grid, x, z)->asset3D);
                        glTranslatef(BLOCKGRID_CELL_SIZE, 0.0f, 0.0f);
                }
        }
        glLoadIdentity();
 }
 
-static void drawBlock(const Block* block) {
-       if (block->asset3D == NULL) {
+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) {
                return;
        }
        
+       glMatrixMode(GL_MODELVIEW);
        glColor3f(0.5f, 1.0f, 0.0f);
        
-       const Asset3D* asset3D = block->asset3D;
        for (int meshIndex = 0; meshIndex < asset3D->numMeshes; ++meshIndex) {
                const Mesh mesh = asset3D->meshes[meshIndex];
                glBindTexture(GL_TEXTURE_2D,
index e4c46d4cb7a87131108ade05c877f2d0e9d52bb1..9251b16e2f28453de47e2a77c8f2b396b4873c59 100644 (file)
--- a/render.h
+++ b/render.h
@@ -1,6 +1,8 @@
 #ifndef RENDER_H_
 #define RENDER_H_
 
+float viewportAspectRatio;
+
 void initRender();
 void renderScene();
 
diff --git a/ui.c b/ui.c
index 71effdbcb53e5c4f6f1fbacf7bd6d07f561ff438..a5b8b1ec3b936fc511cf3d8827135ce722871c08 100644 (file)
--- a/ui.c
+++ b/ui.c
@@ -1,22 +1,12 @@
 #include <GL/gl.h>
 
+#include "render.h"
+
 void resizeStage(GLsizei width, GLsizei height) {
        if (height == 0)
                height = 1;
        
-       // Set device viewport dimensions
        glViewport(0, 0, width, height);
-       // Switch to projection matrix
-       glMatrixMode(GL_PROJECTION);
-       // Reset the projection matrix
-       glLoadIdentity();
-       
-       GLfloat aspectRatio = (GLfloat) width / (GLfloat) height;
-       
-       glOrtho(-8.0, 24.0, -16.0/aspectRatio, 16.0/aspectRatio, -128.0, 128.0);
-       
-       glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
-       glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
        
-       glMatrixMode(GL_MODELVIEW);
+       viewportAspectRatio = (float) width / (float) height;
 }