Move camera code to own file, allow changing the window size

master
Elias Fleckenstein 2021-07-09 17:49:03 +02:00
parent 95bb0b4f1a
commit d059503b32
4 changed files with 54 additions and 17 deletions

View 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

32
src/camera.c Normal file
View File

@ -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);
}

12
src/camera.h Normal file
View File

@ -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

View 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;
update_view_matrix(prog, view);
mat4x4_perspective(projection, 86.1f / 180.0f * M_PI, (float) width / (float) height, 0.01f, 1000.0f);
glUseProgram(prog->id);
glUniformMatrix4fv(prog->loc_view, 1, GL_FALSE, view[0]);
glUniformMatrix4fv(prog->loc_projection, 1, GL_FALSE, projection[0]);
init_camera(window, prog);
set_window_size(width, height);
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);