diff --git a/src/rendering/twn_draw.c b/src/rendering/twn_draw.c index 27650d0..958b588 100644 --- a/src/rendering/twn_draw.c +++ b/src/rendering/twn_draw.c @@ -436,26 +436,23 @@ void draw_camera(Vec3 position, Vec3 direction, Vec3 up, float fov, float zoom, if (!orthographic && fov >= (float)(M_PI)) log_warn("Invalid fov given (%f)", (double)fov); + float const aspect = (float)ctx.base_render_width / (float)ctx.base_render_height; + Camera const camera = { .fov = fov, .pos = position, .target = vec3_norm(direction), .up = up, .viewbox = { - (Vec2){ 1/-zoom, 1/zoom }, - (Vec2){ 1/zoom, 1/-zoom } + aspect/-zoom, aspect/zoom, + 1/zoom, 1/-zoom }, .far_z = draw_distance }; - if (!orthographic) - camera_projection_matrix = camera_perspective(&camera); - else - camera_projection_matrix = camera_orthographic(&camera); - - camera_far_z = draw_distance; - + camera_projection_matrix = orthographic ? camera_orthographic(&camera) : camera_perspective(&camera); camera_look_at_matrix = camera_look_at(&camera); + camera_far_z = draw_distance; } diff --git a/src/twn_camera.c b/src/twn_camera.c index d32e7db..0d39e20 100644 --- a/src/twn_camera.c +++ b/src/twn_camera.c @@ -59,15 +59,15 @@ Matrix4 camera_orthographic(const Camera *const camera) { Matrix4 result = {0}; - const float rl = 1.0f / (camera->viewbox[0].y - camera->viewbox[0].x); - const float tb = 1.0f / (camera->viewbox[1].x - camera->viewbox[1].y); + const float rl = 1.0f / (camera->viewbox.y - camera->viewbox.x); + const float tb = 1.0f / (camera->viewbox.z - camera->viewbox.w); const float fn = -1.0f / (camera->far_z - -camera->far_z); result.row[0].x = 2.0f * rl; result.row[1].y = 2.0f * tb; result.row[2].z = 2.0f * fn; - result.row[3].x = -(camera->viewbox[0].y + camera->viewbox[0].x) * rl; - result.row[3].y = -(camera->viewbox[1].x + camera->viewbox[1].y) * tb; + result.row[3].x = -(camera->viewbox.y + camera->viewbox.x) * rl; + result.row[3].y = -(camera->viewbox.z + camera->viewbox.w) * tb; result.row[3].z = (camera->far_z + -camera->far_z) * fn; result.row[3].w = 1.0f; diff --git a/src/twn_camera_c.h b/src/twn_camera_c.h index df3078b..b686345 100644 --- a/src/twn_camera_c.h +++ b/src/twn_camera_c.h @@ -12,7 +12,7 @@ typedef struct Camera { Vec3 pos; /* eye position */ Vec3 target; /* normalized target vector */ Vec3 up; /* normalized up vector */ - Vec2 viewbox[2]; /* othrographic aabb, ((left, right), (top, bottom)) */ + Vec4 viewbox; /* othrographic aabb, ((left, right), (top, bottom)) */ float fov; /* field of view, in radians */ float far_z; } Camera;