summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author0scar <qgt268@alumni.ku.dk>2024-02-13 09:22:47 +0000
committer0scar <qgt268@alumni.ku.dk>2024-02-13 09:22:47 +0000
commitd7c92c74dc40fede3b2dae24abac55852f20ca9c (patch)
treeae356b8f1529f475327383170d7f53913cdb64ea
parent137b122f68b76854af309dd3b7c60d31968108d0 (diff)
Automatically fix ratio
-rw-r--r--src/rendering/include/engine/rendering/rendering.h19
-rw-r--r--src/rendering/src/rendering.c16
-rw-r--r--src/rendering/src/window.c11
3 files changed, 41 insertions, 5 deletions
diff --git a/src/rendering/include/engine/rendering/rendering.h b/src/rendering/include/engine/rendering/rendering.h
index bc9e07a..2940490 100644
--- a/src/rendering/include/engine/rendering/rendering.h
+++ b/src/rendering/include/engine/rendering/rendering.h
@@ -60,12 +60,27 @@ typedef struct {
u32 mvp;
} RenderObject;
+typedef enum {
+ Camera_Perspective,
+ Camera_Orthogonal,
+} CameraType;
+
typedef struct {
/* Position of the camera in world-space. */
vec3 pos;
/* Perspective matrix. Initialize with r_perspective_ortho or r_perspective. */
/* Alternatively, use `glm_perspective` or `glm_ortho`. */
mat4 per;
+
+ /* Used to re-calculate the perspective matrix when resizing the window */
+ CameraType type;
+ /* Yes, could use a singular "f32 arg", but this is more extendable in the
+ * future. */
+ union {
+ struct {f32 fov;} perspective;
+ struct {f32 sz;} orthogonal;
+ } parameters;
+
} Camera;
/* Rendering functions */
@@ -75,8 +90,8 @@ void drawcall_reset(void);
void render(Window* w);
/* Misc */
-void r_perspective(f32 ratio, f32 fov, Camera *c);
-void r_perspective_ortho(f32 ratio, f32 sz, Camera *c);
+void r_perspective(f32 fov, Camera *c);
+void r_perspective_ortho(f32 sz, Camera *c);
void r_set_camera(Camera* c);
diff --git a/src/rendering/src/rendering.c b/src/rendering/src/rendering.c
index bf57578..3db29fb 100644
--- a/src/rendering/src/rendering.c
+++ b/src/rendering/src/rendering.c
@@ -166,11 +166,23 @@ void render_present(Window* w) {
void drawcall_reset(void) { drawcall_len = 0; }
-void r_perspective(f32 ratio, f32 fov, Camera *c) {
+void r_perspective(f32 fov, Camera *c) {
+ const f64 ratio = (f64)GLOBAL_PLATFORM->window->windowsize[0]
+ / (f64)GLOBAL_PLATFORM->window->windowsize[1];
+
+ c->type = Camera_Perspective;
+ c->parameters.perspective.fov = fov;
+
glm_perspective(fov , ratio, 0.1, 100.0f, c->per);
}
-void r_perspective_ortho(f32 ratio, f32 sz, Camera *c) {
+void r_perspective_ortho(f32 sz, Camera *c) {
+ const f64 ratio = (f64)GLOBAL_PLATFORM->window->windowsize[0]
+ / (f64)GLOBAL_PLATFORM->window->windowsize[1];
+
+ c->type = Camera_Orthogonal;
+ c->parameters.orthogonal.sz = sz;
+
glm_ortho(-sz * ratio, sz * ratio, -sz, sz, -sz * 10.f, sz * 10.f, c->per);
}
diff --git a/src/rendering/src/window.c b/src/rendering/src/window.c
index a1a5833..f684272 100644
--- a/src/rendering/src/window.c
+++ b/src/rendering/src/window.c
@@ -31,10 +31,19 @@ void glfw_err_callback(int code, const char* description) {
}
void window_size_callback(GLFWwindow* window, int width, int height) {
- GladGLContext* gl = GLOBAL_PLATFORM->window->context;
+ const GladGLContext* gl = GLOBAL_PLATFORM->window->context;
+ Camera* c = GLOBAL_PLATFORM->cam;
gl->Viewport(0,0, width, height);
GLOBAL_PLATFORM->window->windowsize[0] = width;
GLOBAL_PLATFORM->window->windowsize[1] = height;
+
+ if (c->type == Camera_Perspective) {
+ r_perspective(c->parameters.perspective.fov, c);
+ }
+ else if (c->type == Camera_Orthogonal) {
+ r_perspective_ortho(c->parameters.orthogonal.sz, c);
+ }
+
}
GladGLContext* create_context(GLFWwindow *window) {