Skip to content

Commit

Permalink
Move teme-to-world matrix calculations to phys.
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-graj committed Mar 24, 2023
1 parent 2480505 commit f3ce1c4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
15 changes: 2 additions & 13 deletions src/entity/satellite.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
13 changes: 12 additions & 1 deletion src/phys/phys.c
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,6 +22,8 @@
#include "system.h"
#include "thread.h"

#include <cglm/mat3.h>

enum Timeflow e_timeflow = TIME_REALTIME;
float e_timescale = 1.f;
struct PhysCtx e_phys;
Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 3 additions & 1 deletion src/phys/phys.h
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,6 +17,7 @@

#include "type.h"

#include <cglm/mat4.h>
#include <cglm/vec2.h>
#include <cglm/vec3.h>

Expand All @@ -27,6 +28,7 @@ struct PhysCtx {
double gmst;
vec2 sun_uv;
vec3 sun_dir;
mat4 teme_to_world;
};

enum Timeflow {
Expand Down
9 changes: 1 addition & 8 deletions src/ui/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f3ce1c4

Please sign in to comment.