From f3ce1c4a6e2d1b44b631ed4a52a4bf278bcc070f Mon Sep 17 00:00:00 2001 From: Wojciech Graj Date: Fri, 24 Mar 2023 02:19:05 +0100 Subject: [PATCH] Move teme-to-world matrix calculations to phys. --- src/entity/satellite.c | 15 ++------------- src/phys/phys.c | 13 ++++++++++++- src/phys/phys.h | 4 +++- src/ui/info.c | 9 +-------- 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/entity/satellite.c b/src/entity/satellite.c index 467697a..5ee14de 100644 --- a/src/entity/satellite.c +++ b/src/entity/satellite.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Wojciech Graj + * Copyright (c) 2022-2023 Wojciech Graj * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -86,7 +86,6 @@ static struct BO vbo_orbit_colors; static struct Shader shader; -static mat4 teme_to_world; static mat4 transform; static size_t n_satellites_sync = 0; @@ -134,8 +133,6 @@ void satellite_init(void) const char *urls[] = { "https://celestrak.org/pub/satcat.txt", "https://celestrak.org/pub/TLE/catalog.txt" }; dl_multi_init(&dl_multi, 2, urls); - glm_mat4_identity(teme_to_world); - vao_init(&vao_satellites); vao_bind(&vao_satellites); bo_init(&vbo_verts, GL_ARRAY_BUFFER, TRUE); @@ -442,14 +439,6 @@ void satellites_phys(void) if (!n_satellites) return; - mat3 t; - glm_vec3_copy((vec3){ 0.f, 0.f, 1.f }, t[2]); - glm_vec3_copy((vec3){ (float)cos(e_phys.gmst), (float)-sin(e_phys.gmst), 0.f }, t[0]); - glm_vec3_cross(t[2], t[0], t[1]); - glm_vec3_norm(t[1]); - glm_mat3_scale(t, 1.f / 6371.f); - glm_mat4_ins3(t, teme_to_world); - size_t i; for (i = 0; i < n_satellite_orbits; i++) { struct OrbitData *data = &g_array_index(satellite_orbit_data, struct OrbitData, i); @@ -476,7 +465,7 @@ void satellites_phys_sync(void) void satellites_render(void) { shader_bind(&shader); - camera_mvp_generate(&e_camera, &teme_to_world, transform); + camera_mvp_generate(&e_camera, &e_phys.teme_to_world, transform); glUniformMatrix4fv(LOCU_TRANSFORM, 1, GL_FALSE, (const GLfloat *)&transform); if (satellites_renderable) { diff --git a/src/phys/phys.c b/src/phys/phys.c index f5fe6c3..703735d 100644 --- a/src/phys/phys.c +++ b/src/phys/phys.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Wojciech Graj + * Copyright (c) 2022-2023 Wojciech Graj * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -22,6 +22,8 @@ #include "system.h" #include "thread.h" +#include + enum Timeflow e_timeflow = TIME_REALTIME; float e_timescale = 1.f; struct PhysCtx e_phys; @@ -66,10 +68,19 @@ void phys_phys(void) cosf(phys_ctx_sync.sun_uv[1] * (float)G_PI), }, phys_ctx_sync.sun_dir); + + mat3 t; + glm_vec3_copy((vec3){ 0.f, 0.f, 1.f }, t[2]); + glm_vec3_copy((vec3){ (float)cos(phys_ctx_sync.gmst), (float)-sin(phys_ctx_sync.gmst), 0.f }, t[0]); + glm_vec3_cross(t[2], t[0], t[1]); + glm_vec3_norm(t[1]); + glm_mat3_scale(t, 1.f / 6371.f); + glm_mat4_ins3(t, phys_ctx_sync.teme_to_world); } void phys_init(void) { + glm_mat4_identity(phys_ctx_sync.teme_to_world); phys_phys(); phys_phys_sync(); } diff --git a/src/phys/phys.h b/src/phys/phys.h index 9afe117..dbbfe2a 100644 --- a/src/phys/phys.h +++ b/src/phys/phys.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Wojciech Graj + * Copyright (c) 2022-2023 Wojciech Graj * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -17,6 +17,7 @@ #include "type.h" +#include #include #include @@ -27,6 +28,7 @@ struct PhysCtx { double gmst; vec2 sun_uv; vec3 sun_dir; + mat4 teme_to_world; }; enum Timeflow { diff --git a/src/ui/info.c b/src/ui/info.c index 6ec23d5..a7fdea4 100644 --- a/src/ui/info.c +++ b/src/ui/info.c @@ -101,15 +101,8 @@ void on_info_jump_to_clicked(GtkButton *button, gpointer user_data) double r[3], v[3]; getRVForDate(&satellite->tle, e_phys.epoch_ms, r, v); - mat3 t; - glm_vec3_copy((vec3){ 0.f, 0.f, 1.f }, t[2]); - glm_vec3_copy((vec3){ (float)cos(e_phys.gmst), (float)-sin(e_phys.gmst), 0.f }, t[0]); - glm_vec3_cross(t[2], t[0], t[1]); - glm_vec3_norm(t[1]); - glm_mat3_scale(t, 1.05f / 6371.f); - vec3 pos; - glm_mat3_mulv(t, (vec3){ r[0], r[1], r[2] }, pos); + glm_mat4_mulv3(e_phys.teme_to_world, (vec3){ r[0], r[1], r[2] }, 1.f, pos); float rad = glm_vec3_norm(pos); e_camera.rad = glm_clamp(rad, 1.1f, 10.f); glm_vec3_scale(pos, e_camera.rad / rad, e_camera.pos);