commit a1f6a78399ecd2d2d59590475a6ac3cd3a4e15de
parent 3c142b5842da7e856ae0ce20b2cb72802b79b5bc
Author: Christian Ermann <christianermann@gmail.com>
Date: Wed, 17 Nov 2021 22:17:05 -0800
Moved camera update function into camera source.
Diffstat:
3 files changed, 48 insertions(+), 36 deletions(-)
diff --git a/include/camera.h b/include/camera.h
@@ -23,7 +23,8 @@ typedef struct {
mat4 matrix;
} Camera;
-Camera Camera_create(float x, float y, float z, float yaw, float pitch);
+Camera *Camera_make(float x, float y, float z, float yaw, float pitch);
+void Camera_free(Camera *camera);
void Camera_defaultSettings(Camera* camera);
@@ -36,4 +37,6 @@ void Camera_updateVectors(Camera* camera);
// Update the (projection * view) matrix of the camera.
void Camera_updateMatrix(Camera* camera);
+void Camera_move(Camera *camera);
+
#endif
diff --git a/src/camera.c b/src/camera.c
@@ -1,19 +1,29 @@
#include "camera.h"
-Camera Camera_create(float x, float y, float z, float yaw, float pitch)
+#include <SDL2/SDL.h>
+
+Camera *Camera_make(float x, float y, float z, float yaw, float pitch)
{
- Camera camera;
- camera.position[0] = x;
- camera.position[1] = y;
- camera.position[2] = z;
- camera.yaw = yaw;
- camera.pitch = pitch;
- Camera_defaultSettings(&camera);
- Camera_updateVectors(&camera);
- Camera_updateMatrix(&camera);
+ Camera *camera = malloc(sizeof *camera);
+ camera->position[0] = x;
+ camera->position[1] = y;
+ camera->position[2] = z;
+ camera->yaw = yaw;
+ camera->pitch = pitch;
+ Camera_defaultSettings(camera);
+ Camera_updateVectors(camera);
+ Camera_updateMatrix(camera);
return camera;
}
+void Camera_free(Camera *camera)
+{
+ if (camera)
+ {
+ free(camera);
+ }
+}
+
void Camera_defaultSettings(Camera* camera)
{
camera->world_up[0] = 0.0f;
@@ -85,3 +95,19 @@ void Camera_updateMatrix(Camera* camera)
glm_mat4_mul(projection, view, camera->matrix);
}
+
+void Camera_move(Camera *camera)
+{
+ int dx, dy;
+ SDL_GetRelativeMouseState(&dx, &dy);
+ Camera_updateRotation(camera, dx, -dy);
+
+ const Uint8* kb = SDL_GetKeyboardState(NULL);
+ float x = kb[SDL_SCANCODE_D] - kb[SDL_SCANCODE_A];
+ float y = kb[SDL_SCANCODE_SPACE] - kb[SDL_SCANCODE_LSHIFT];
+ float z = kb[SDL_SCANCODE_W] - kb[SDL_SCANCODE_S];
+ Camera_updatePosition(camera, x, y, z);
+
+ Camera_updateVectors(camera);
+ Camera_updateMatrix(camera);
+}
+\ No newline at end of file
diff --git a/src/main.c b/src/main.c
@@ -11,24 +11,6 @@
#include <SDL2/SDL_opengl.h>
#include <stdio.h>
-const int WIDTH = 512, HEIGHT = 512;
-
-void Camera_update(Camera* camera)
-{
- int dx, dy;
- SDL_GetRelativeMouseState(&dx, &dy);
- Camera_updateRotation(camera, dx, -dy);
-
- const Uint8* kb = SDL_GetKeyboardState(NULL);
- float x = kb[SDL_SCANCODE_D] - kb[SDL_SCANCODE_A];
- float y = kb[SDL_SCANCODE_SPACE] - kb[SDL_SCANCODE_LSHIFT];
- float z = kb[SDL_SCANCODE_W] - kb[SDL_SCANCODE_S];
- Camera_updatePosition(camera, x, y, z);
-
- Camera_updateVectors(camera);
- Camera_updateMatrix(camera);
-}
-
float perlinSDF(const Vec3 p)
{
return perlin(p[0] * 0.01f, p[1] * 0.01f, p[2] * 0.01f)
@@ -56,9 +38,9 @@ int main(int argc, char** argv)
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
- Camera camera = Camera_create(0, 0, -3, 0, 0);
+ Camera *camera = Camera_make(0, 0, -3, 0, 0);
- ChunkManager chunk_manager = ChunkManager_create(camera.position, 3,
+ ChunkManager chunk_manager = ChunkManager_create(camera->position, 3,
perlinSDF, 0.0f);
ChunkManager_drawChunks(&chunk_manager);
@@ -99,13 +81,13 @@ int main(int argc, char** argv)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- Camera_update(&camera);
- glUniformMatrix4fv(cam_loc, 1, GL_FALSE, camera.matrix[0]);
+ Camera_move(camera);
+ glUniformMatrix4fv(cam_loc, 1, GL_FALSE, camera->matrix[0]);
- glUniform3fv(view_pos_loc, 1, camera.position);
- glUniform3fv(pointlight_pos_loc, 1, camera.position);
+ glUniform3fv(view_pos_loc, 1, camera->position);
+ glUniform3fv(pointlight_pos_loc, 1, camera->position);
- ChunkManager_recenter(&chunk_manager, camera.position);
+ ChunkManager_recenter(&chunk_manager, camera->position);
ChunkManager_drawChunks(&chunk_manager);
SDL_GL_SwapWindow(app->window);