]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Move camera code to own file, allow changing the window size
authorElias Fleckenstein <eliasfleckenstein@web.de>
Fri, 9 Jul 2021 15:49:03 +0000 (17:49 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Fri, 9 Jul 2021 15:49:03 +0000 (17:49 +0200)
src/Makefile
src/camera.c [new file with mode: 0644]
src/camera.h [new file with mode: 0644]
src/client.c

index 34e267c78934275aca5f884bb4da98ec0ec3aad7..36f6394d004dffd2ebca2bf61c72663f358032fa 100644 (file)
@@ -1,6 +1,6 @@
 COMMON = array.o list.o map.o signal.o util.o types.o node.o queue.o
 SERVER = $(COMMON) server.o servercommands.o servermap.o perlin.o facecache.o mapgen.o mapdb.o
-CLIENT = $(COMMON) client.o clientcommands.o clientmap.o clientnode.o mesh.o scene.o shaders.o blockmesh.o texture.o
+CLIENT = $(COMMON) camera.o client.o clientcommands.o clientmap.o clientnode.o mesh.o scene.o shaders.o blockmesh.o texture.o
 LIBRARIES = -lpthread -lm -lz
 FLAGS = -g -fmax-errors=4
 
diff --git a/src/camera.c b/src/camera.c
new file mode 100644 (file)
index 0000000..467cc7f
--- /dev/null
@@ -0,0 +1,32 @@
+#include "client.h"
+#include "camera.h"
+
+static mat4x4 view, projection;
+static GLFWwindow *window;
+static ShaderProgram *prog;
+
+void set_camera_position(v3f pos)
+{
+       mat4x4_translate(view, -pos.x, -pos.y, -pos.z);
+       glUniformMatrix4fv(prog->loc_view, 1, GL_FALSE, view[0]);
+}
+
+void set_window_size(int width, int height)
+{
+       mat4x4_perspective(projection, 86.1f / 180.0f * M_PI, (float) width / (float) height, 0.01f, 1000.0f);
+       glUniformMatrix4fv(prog->loc_projection, 1, GL_FALSE, projection[0]);
+}
+
+static void framebuffer_size_callback(__attribute__((unused)) GLFWwindow* window, int width, int height)
+{
+       glViewport(0, 0, width, height);
+       set_window_size(width, height);
+}
+
+void init_camera(GLFWwindow *w, ShaderProgram *p)
+{
+       window = w;
+       prog = p;
+
+       glfwSetFramebufferSizeCallback(window, &framebuffer_size_callback);
+}
diff --git a/src/camera.h b/src/camera.h
new file mode 100644 (file)
index 0000000..cc9e29f
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef _CAMERA_H_
+#define _CAMERA_H_
+
+#include <GLFW/glfw3.h>
+#include "shaders.h"
+#include "types.h"
+
+void init_camera(GLFWwindow *window, ShaderProgram *prog);
+void set_camera_position(v3f pos);
+void set_window_size(int width, int height);
+
+#endif
index ebac21f75fa98995a41c784d9e0808cea8058e9c..aa3e342b1664a6f0923832f2b2564f8a85cbdb36 100644 (file)
@@ -9,6 +9,7 @@
 #include <GL/glew.h>
 #include <GL/gl.h>
 #include <GLFW/glfw3.h>
+#include "camera.h"
 #include "client.h"
 #include "clientmap.h"
 #include "clientnode.h"
@@ -44,12 +45,6 @@ static void *reciever_thread(__attribute__((unused)) void *unused)
        return NULL;
 }
 
-static void update_view_matrix(ShaderProgram *prog, mat4x4 view)
-{
-       mat4x4_translate(view, -client.pos.x, -client.pos.y, -client.pos.z);
-       glUniformMatrix4fv(prog->loc_view, 1, GL_FALSE, view[0]);
-}
-
 static void client_loop()
 {
        if(! glfwInit()) {
@@ -91,22 +86,18 @@ static void client_loop()
        init_client_node_definitions();
        clientmap_start_meshgen();
 
-       mat4x4 view, projection;
+       glUseProgram(prog->id);
 
-       update_view_matrix(prog, view);
-       mat4x4_perspective(projection, 86.1f / 180.0f * M_PI, (float) width / (float) height, 0.01f, 1000.0f);
+       init_camera(window, prog);
+       set_window_size(width, height);
 
-       glUseProgram(prog->id);
-       glUniformMatrix4fv(prog->loc_view, 1, GL_FALSE, view[0]);
-       glUniformMatrix4fv(prog->loc_projection, 1, GL_FALSE, projection[0]);
+       bool pos_changed = true;
 
        while (! glfwWindowShouldClose(window) && client.state != CS_DISCONNECTED && ! interrupted) {
                glEnable(GL_DEPTH_TEST);
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                glClearColor(0.52941176470588f, 0.8078431372549f, 0.92156862745098f, 1.0f);
 
-               bool pos_changed = false;
-
                if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
                        pos_changed = true;
                        client.pos.z -= 1.0f;
@@ -128,7 +119,9 @@ static void client_loop()
                }
 
                if (pos_changed) {
-                       update_view_matrix(prog, view);
+                       set_camera_position(client.pos);
+                       pos_changed = false;
+
                        pthread_mutex_lock(&client.mtx);
                        (void) (write_u32(client.fd, SC_POS) && write_v3f32(client.fd, client.pos));
                        pthread_mutex_unlock(&client.mtx);