twn_camera: fix aspect of ortho projection

This commit is contained in:
veclavtalica 2025-03-07 11:07:05 +03:00
parent 67feb5974a
commit a03e1d885d
3 changed files with 11 additions and 14 deletions

View File

@ -436,26 +436,23 @@ void draw_camera(Vec3 position, Vec3 direction, Vec3 up, float fov, float zoom,
if (!orthographic && fov >= (float)(M_PI)) if (!orthographic && fov >= (float)(M_PI))
log_warn("Invalid fov given (%f)", (double)fov); log_warn("Invalid fov given (%f)", (double)fov);
float const aspect = (float)ctx.base_render_width / (float)ctx.base_render_height;
Camera const camera = { Camera const camera = {
.fov = fov, .fov = fov,
.pos = position, .pos = position,
.target = vec3_norm(direction), .target = vec3_norm(direction),
.up = up, .up = up,
.viewbox = { .viewbox = {
(Vec2){ 1/-zoom, 1/zoom }, aspect/-zoom, aspect/zoom,
(Vec2){ 1/zoom, 1/-zoom } 1/zoom, 1/-zoom
}, },
.far_z = draw_distance .far_z = draw_distance
}; };
if (!orthographic) camera_projection_matrix = orthographic ? camera_orthographic(&camera) : camera_perspective(&camera);
camera_projection_matrix = camera_perspective(&camera);
else
camera_projection_matrix = camera_orthographic(&camera);
camera_far_z = draw_distance;
camera_look_at_matrix = camera_look_at(&camera); camera_look_at_matrix = camera_look_at(&camera);
camera_far_z = draw_distance;
} }

View File

@ -59,15 +59,15 @@ Matrix4 camera_orthographic(const Camera *const camera) {
Matrix4 result = {0}; Matrix4 result = {0};
const float rl = 1.0f / (camera->viewbox[0].y - camera->viewbox[0].x); const float rl = 1.0f / (camera->viewbox.y - camera->viewbox.x);
const float tb = 1.0f / (camera->viewbox[1].x - camera->viewbox[1].y); const float tb = 1.0f / (camera->viewbox.z - camera->viewbox.w);
const float fn = -1.0f / (camera->far_z - -camera->far_z); const float fn = -1.0f / (camera->far_z - -camera->far_z);
result.row[0].x = 2.0f * rl; result.row[0].x = 2.0f * rl;
result.row[1].y = 2.0f * tb; result.row[1].y = 2.0f * tb;
result.row[2].z = 2.0f * fn; result.row[2].z = 2.0f * fn;
result.row[3].x = -(camera->viewbox[0].y + camera->viewbox[0].x) * rl; result.row[3].x = -(camera->viewbox.y + camera->viewbox.x) * rl;
result.row[3].y = -(camera->viewbox[1].x + camera->viewbox[1].y) * tb; 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].z = (camera->far_z + -camera->far_z) * fn;
result.row[3].w = 1.0f; result.row[3].w = 1.0f;

View File

@ -12,7 +12,7 @@ typedef struct Camera {
Vec3 pos; /* eye position */ Vec3 pos; /* eye position */
Vec3 target; /* normalized target vector */ Vec3 target; /* normalized target vector */
Vec3 up; /* normalized up 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 fov; /* field of view, in radians */
float far_z; float far_z;
} Camera; } Camera;