From 619f050cf1b4ddc9bcc0e9a41a36db030cfa23b3 Mon Sep 17 00:00:00 2001 From: Wojciech Graj Date: Wed, 10 Aug 2022 16:43:01 +0200 Subject: [PATCH] Improve camera controls. Change global variable prefix. --- Makefile | 10 +++++++--- src/entity/earth.c | 6 +++--- src/entity/satellite.c | 12 ++++++------ src/entity/sky.c | 2 +- src/gfx/camera.c | 4 ++-- src/gfx/camera.h | 2 +- src/gfx/gfx.c | 8 ++++---- src/gfx/gfx.h | 2 +- src/gfx/render.c | 18 +++++++++--------- src/phys/phys.c | 10 +++++----- src/phys/phys.h | 2 +- src/ui/input.c | 30 ++++++++++++++---------------- src/ui/timemgr.c | 36 ++++++++++++++++++------------------ src/ui/timemgr.h | 4 ++-- src/ui/ui.c | 4 ++-- src/ui/ui.h | 2 +- util/winbuild.sh | 2 +- 17 files changed, 78 insertions(+), 76 deletions(-) diff --git a/Makefile b/Makefile index 2e39867..9ed573a 100755 --- a/Makefile +++ b/Makefile @@ -1,15 +1,19 @@ TARGET := orbvis +HAS_PACKAGES := $(shell pkg-config --exists gtk+-3.0 cglm libcurl epoxy ; echo $$?) +ifneq ($(HAS_PACKAGES), 0) + $(error Missing packages) +endif + CC := gcc WARNINGS := -Wall -Wextra -Wpedantic -Wdouble-promotion -Wstrict-prototypes -Wshadow -Wduplicated-cond -Wduplicated-branches -Wjump-misses-init -Wnull-dereference -Wrestrict -Wlogical-op -Walloc-zero -Wformat-security -Wformat-signedness -Winit-self -Wlogical-op -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wswitch-enum -Wundef -Wwrite-strings -Wno-discarded-qualifiers -CFLAGS := -march=native -O3 +CFLAGS := -march=native -O3 `pkg-config --cflags gtk+-3.0 cglm libcurl epoxy` LDFLAGS := -lm ifeq ($(MAKECMDGOALS),windows) - CFLAGS += `pkg-config --cflags gtk+-3.0 cglm libcurl epoxy` -flto + CFLAGS += -flto LDFLAGS += -lopengl32 `pkg-config --libs gtk+-3.0 cglm libcurl epoxy` -mwindows -flto else - CFLAGS += `pkg-config --cflags gl gtk+-3.0 cglm libcurl epoxy` LDFLAGS += -ldl `pkg-config --libs gl gtk+-3.0 cglm libcurl epoxy` ifeq ($(MAKECMDGOALS),debug) CFLAGS += -fsanitize=address -fsanitize=undefined -g -ftrapv -fno-omit-frame-pointer -lrt diff --git a/src/entity/earth.c b/src/entity/earth.c index b3d5d01..587c63c 100644 --- a/src/entity/earth.c +++ b/src/entity/earth.c @@ -90,16 +90,16 @@ void earth_render(void) texture_bind(&texture_gradient); mat4 transform; - camera_mvp_generate(&g_camera, &model.model_mat, transform); + camera_mvp_generate(&e_camera, &model.model_mat, transform); shader_bind(&shader); - glUniform3fv(LOCU_SUN_DIR, 1, g_phys.sun_dir); + glUniform3fv(LOCU_SUN_DIR, 1, e_phys.sun_dir); glUniform1i(LOCU_TEXTURE_DAY, 0); glUniform1i(LOCU_TEXTURE_NIGHT, 1); glUniform1i(LOCU_TEXTURE_SPECULAR, 2); glUniform1i(LOCU_TEXTURE_CLOUDS, 3); glUniform1i(LOCU_TEXTURE_GRADIENT, 4); - glUniform3fv(LOCU_LOOK_POS, 1, g_camera.pos); + glUniform3fv(LOCU_LOOK_POS, 1, e_camera.pos); glUniformMatrix4fv(LOCU_TRANSFORM, 1, GL_FALSE, (const GLfloat *)&transform); glUniform1i(LOCU_CLOUDS, gs_clouds); glUniform1i(LOCU_LIGHTING, gs_lighting); diff --git a/src/entity/satellite.c b/src/entity/satellite.c index eaddb18..2e963cc 100644 --- a/src/entity/satellite.c +++ b/src/entity/satellite.c @@ -297,7 +297,7 @@ void satellites_phys(void) mat3 t; glm_vec3_copy((vec3){ 0.f, 0.f, 1.f }, t[2]); - glm_vec3_copy((vec3){ (float)cos(g_phys.gmst), (float)-sin(g_phys.gmst), 0.f }, t[0]); + 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); @@ -306,7 +306,7 @@ void satellites_phys(void) size_t i; for (i = 0; i < n_satellites; i++) { double r[3], v[3]; - getRVForDate(&satellites[i].tle, g_phys.epoch_ms, r, v); + getRVForDate(&satellites[i].tle, e_phys.epoch_ms, r, v); glm_vec3_copy((vec3){ r[0], r[1], r[2] }, satellite_verts[i]); } } @@ -314,7 +314,7 @@ void satellites_phys(void) void satellites_render(void) { shader_bind(&shader); - camera_mvp_generate(&g_camera, &teme_to_world, transform); + camera_mvp_generate(&e_camera, &teme_to_world, transform); glUniformMatrix4fv(LOCU_TRANSFORM, 1, GL_FALSE, (const GLfloat *)&transform); if (satellites_renderable) { @@ -348,7 +348,7 @@ static void satellite_toggle_orbit(guint32 idx) gint64 period = MS_IN_DAY / (float)satellites[idx].tle.n; int i; for (i = 0; i < count; i++) { - gint64 epoch_ms = g_phys.epoch_ms + period * i / count; + gint64 epoch_ms = e_phys.epoch_ms + period * i / count; double r[3], v[3]; getRVForDate(&satellites[idx].tle, epoch_ms, r, v); glm_vec3_copy((vec3){ r[0], r[1], r[2] }, satellite_orbits[first + i]); @@ -392,8 +392,8 @@ static void satellite_toggle_orbit(guint32 idx) void satellite_select(double xpos, double ypos) { vec3 p1, p2; - glm_unproject((vec3){ xpos, g_gl_ctx.res_y - ypos, 0 }, transform, (vec4){ 0, 0, g_gl_ctx.res_x, g_gl_ctx.res_y }, p1); - glm_unproject((vec3){ xpos, g_gl_ctx.res_y - ypos, 1 }, transform, (vec4){ 0, 0, g_gl_ctx.res_x, g_gl_ctx.res_y }, p2); + glm_unproject((vec3){ xpos, e_gl_ctx.res_y - ypos, 0 }, transform, (vec4){ 0, 0, e_gl_ctx.res_x, e_gl_ctx.res_y }, p1); + glm_unproject((vec3){ xpos, e_gl_ctx.res_y - ypos, 1 }, transform, (vec4){ 0, 0, e_gl_ctx.res_x, e_gl_ctx.res_y }, p2); vec3 dir; glm_vec3_sub(p2, p1, dir); diff --git a/src/entity/sky.c b/src/entity/sky.c index 1ca88b5..a0613a0 100644 --- a/src/entity/sky.c +++ b/src/entity/sky.c @@ -65,7 +65,7 @@ void sky_render(void) texture_bind(&texture); mat4 transform; - camera_mvp_generate(&g_camera, &model.model_mat, transform); + camera_mvp_generate(&e_camera, &model.model_mat, transform); shader_bind(&shader); glUniform1i(LOCU_TEXTURE, 0); diff --git a/src/gfx/camera.c b/src/gfx/camera.c index 4c09826..bb5af1c 100644 --- a/src/gfx/camera.c +++ b/src/gfx/camera.c @@ -4,7 +4,7 @@ #include "gfx.h" -struct Camera g_camera; +struct Camera e_camera; void camera_view_update(struct Camera *camera) { @@ -13,7 +13,7 @@ void camera_view_update(struct Camera *camera) void camera_proj_update(struct Camera *camera) { - glm_perspective(camera->fov, g_gl_ctx.res_x / (float)g_gl_ctx.res_y, camera->near, camera->far, camera->proj_mat); + glm_perspective(camera->fov, e_gl_ctx.res_x / (float)e_gl_ctx.res_y, camera->near, camera->far, camera->proj_mat); } void camera_mvp_generate(struct Camera *camera, mat4 *model, mat4 mvp) diff --git a/src/gfx/camera.h b/src/gfx/camera.h index 1cee5e1..d391057 100644 --- a/src/gfx/camera.h +++ b/src/gfx/camera.h @@ -28,6 +28,6 @@ void camera_mvp_generate(struct Camera *camera, mat4 *model, mat4 mvp); void camera_zoom(struct Camera *camera, float offset); -extern struct Camera g_camera; +extern struct Camera e_camera; #endif /* __CAMERA_H__ */ diff --git a/src/gfx/gfx.c b/src/gfx/gfx.c index ce7fa00..4e7906d 100644 --- a/src/gfx/gfx.c +++ b/src/gfx/gfx.c @@ -6,7 +6,7 @@ #include "thread.h" #include "timemgr.h" -struct GLCtx g_gl_ctx; +struct GLCtx e_gl_ctx; static GtkGLArea *glarea; static void on_glarea_realize(GtkWidget *widget, gpointer user_data); @@ -35,10 +35,10 @@ void on_glarea_resize(GtkGLArea *area, int width, int height, gpointer user_data { (void)area; (void)user_data; - g_gl_ctx.res_x = width; - g_gl_ctx.res_y = height; + e_gl_ctx.res_x = width; + e_gl_ctx.res_y = height; glViewport(0, 0, width, height); - camera_proj_update(&g_camera); + camera_proj_update(&e_camera); } /* Main Loop */ diff --git a/src/gfx/gfx.h b/src/gfx/gfx.h index 2f49625..8130c37 100644 --- a/src/gfx/gfx.h +++ b/src/gfx/gfx.h @@ -12,6 +12,6 @@ struct GLCtx { void gfx_init(GtkBuilder *builder); -extern struct GLCtx g_gl_ctx; +extern struct GLCtx e_gl_ctx; #endif /* __GFX_H__ */ diff --git a/src/gfx/render.c b/src/gfx/render.c index ca5d58f..48b533b 100644 --- a/src/gfx/render.c +++ b/src/gfx/render.c @@ -18,17 +18,17 @@ void render_init(void) { - glm_vec3_copy((vec3){ 0, 5.0, 0 }, g_camera.pos); - glm_vec3_copy((vec3){ 0, 0, 0 }, g_camera.target); - glm_vec3_copy((vec3){ 0, 0, 1 }, g_camera.up); + glm_vec3_copy((vec3){ 0, 5.0, 0 }, e_camera.pos); + glm_vec3_copy((vec3){ 0, 0, 0 }, e_camera.target); + glm_vec3_copy((vec3){ 0, 0, 1 }, e_camera.up); - g_camera.rad = 5.f; - g_camera.fov = PI / 2.f; - g_camera.near = 0.05f; - g_camera.far = 100.f; + e_camera.rad = 5.f; + e_camera.fov = PI / 2.f; + e_camera.near = 0.05f; + e_camera.far = 100.f; - camera_view_update(&g_camera); - camera_proj_update(&g_camera); + camera_view_update(&e_camera); + camera_proj_update(&e_camera); glDepthFunc(GL_LESS); diff --git a/src/phys/phys.c b/src/phys/phys.c index d97935b..37325d5 100644 --- a/src/phys/phys.c +++ b/src/phys/phys.c @@ -7,7 +7,7 @@ #include "thread.h" #include "timemgr.h" -struct PhysCtx g_phys; +struct PhysCtx e_phys; static gint64 epoch_ms; static struct PhysCtx phys_ctx_sync; @@ -18,15 +18,15 @@ static void phys_phys_sync(void); void phys_phys(void) { gint64 cur_epoch_ms = system_epoch_ms(); - switch (g_timeflow) { + switch (e_timeflow) { case TIME_REALTIME: phys_ctx_sync.epoch_ms = cur_epoch_ms; break; case TIME_PAUSE: - phys_ctx_sync.epoch_ms = g_phys.epoch_ms; + phys_ctx_sync.epoch_ms = e_phys.epoch_ms; break; case TIME_ARBITRARY: - phys_ctx_sync.epoch_ms = g_phys.epoch_ms + (gint64)((cur_epoch_ms - epoch_ms) * g_timescale); + phys_ctx_sync.epoch_ms = e_phys.epoch_ms + (gint64)((cur_epoch_ms - epoch_ms) * e_timescale); break; } epoch_ms = cur_epoch_ms; @@ -75,7 +75,7 @@ void *phys_thrd(void *arguments) void phys_phys_sync(void) { - g_phys = phys_ctx_sync; + e_phys = phys_ctx_sync; } void phys_sync(void) diff --git a/src/phys/phys.h b/src/phys/phys.h index 38c01bf..687a27d 100644 --- a/src/phys/phys.h +++ b/src/phys/phys.h @@ -15,7 +15,7 @@ struct PhysCtx { vec3 sun_dir; }; -extern struct PhysCtx g_phys; +extern struct PhysCtx e_phys; void phys_init(void); void *phys_thrd(void *arguments); diff --git a/src/ui/input.c b/src/ui/input.c index 10d5747..023781b 100644 --- a/src/ui/input.c +++ b/src/ui/input.c @@ -44,11 +44,11 @@ gboolean on_glarea_key_press_event(GtkWidget *widget, GdkEvent *event, gpointer if (key->state == GDK_CONTROL_MASK) { switch (key->keyval) { case GDK_KEY_minus: - camera_zoom(&g_camera, -0.2f); + camera_zoom(&e_camera, -0.2f); break; case GDK_KEY_plus: case GDK_KEY_equal: - camera_zoom(&g_camera, 0.2f); + camera_zoom(&e_camera, 0.2f); break; } } @@ -87,29 +87,27 @@ gboolean on_glarea_motion_notify_event(GtkWidget *widget, GdkEvent *event, gpoin (void)user_data; GdkEventMotion *motion = &event->motion; if (cursor_pressed) { - float d_ang_x = (2.f * PI / g_gl_ctx.res_x); - float d_ang_y = (PI / g_gl_ctx.res_y); + float d_ang_x = (2.f * PI / e_gl_ctx.res_x); + float d_ang_y = (PI / e_gl_ctx.res_y); float ang_x = (float)(cursor_xpos - motion->x) * d_ang_x; float ang_y = (float)(cursor_ypos - motion->y) * d_ang_y; vec3 view_dir; - glm_vec3_sub(g_camera.target, g_camera.pos, view_dir); - glm_vec3_norm(view_dir); - float cos_ang = glm_vec3_dot(view_dir, g_camera.up); - if (cos_ang * sgn(ang_y) < -0.9999f * g_camera.rad) { - ang_y = 0; - } + glm_vec3_sub(e_camera.target, e_camera.pos, view_dir); + glm_vec3_normalize(view_dir); + float cam_ang = acosf(glm_vec3_dot(view_dir, e_camera.up)); + ang_y = glm_clamp(1e-2f, cam_ang + ang_y, PI - 1e-2f) - cam_ang; vec3 right; - glm_vec3_cross(g_camera.up, view_dir, right); + glm_vec3_cross(e_camera.up, view_dir, right); mat4 rot_mat; glm_mat4_identity(rot_mat); - glm_rotate_at(rot_mat, g_camera.target, ang_x, g_camera.up); - glm_rotate_at(rot_mat, g_camera.target, ang_y, right); - glm_mat4_mulv3(rot_mat, g_camera.pos, 1.f, g_camera.pos); + glm_rotate_at(rot_mat, e_camera.target, ang_x, e_camera.up); + glm_rotate_at(rot_mat, e_camera.target, ang_y, right); + glm_mat4_mulv3(rot_mat, e_camera.pos, 1.f, e_camera.pos); - camera_view_update(&g_camera); + camera_view_update(&e_camera); } cursor_xpos = motion->x; cursor_ypos = motion->y; @@ -122,7 +120,7 @@ gboolean on_glarea_scroll_event(GtkWidget *widget, GdkEvent *event, gpointer use (void)user_data; GdkEventScroll *scroll = &event->scroll; - camera_zoom(&g_camera, scroll->delta_y * 0.2); + camera_zoom(&e_camera, scroll->delta_y * 0.2); return TRUE; } diff --git a/src/ui/timemgr.c b/src/ui/timemgr.c index 424727f..e8b549d 100644 --- a/src/ui/timemgr.c +++ b/src/ui/timemgr.c @@ -6,8 +6,8 @@ #include -enum Timeflow g_timeflow = TIME_REALTIME; -float g_timescale = 1.f; +enum Timeflow e_timeflow = TIME_REALTIME; +float e_timescale = 1.f; static GtkToggleToolButton *play_button; static GtkEntry *time_entry; @@ -25,9 +25,9 @@ void on_play_toggled(GtkToggleToolButton *toggle_tool_button, gpointer user_data { (void)user_data; if (!gtk_toggle_tool_button_get_active(toggle_tool_button)) - g_timeflow = TIME_PAUSE; - else if (g_timeflow == TIME_PAUSE) - g_timeflow = TIME_ARBITRARY; + e_timeflow = TIME_PAUSE; + else if (e_timeflow == TIME_PAUSE) + e_timeflow = TIME_ARBITRARY; } void on_realtime_clicked(GtkToolButton *toolbutton, gpointer user_data) @@ -35,25 +35,25 @@ void on_realtime_clicked(GtkToolButton *toolbutton, gpointer user_data) (void)toolbutton; (void)user_data; timeflow_set(TIME_REALTIME); - g_timescale = 1.f; + e_timescale = 1.f; } void on_decelerate_clicked(GtkToolButton *toolbutton, gpointer user_data) { (void)toolbutton; (void)user_data; - if (g_timeflow == TIME_REALTIME) + if (e_timeflow == TIME_REALTIME) timeflow_set(TIME_ARBITRARY); - g_timescale *= 0.95f; + e_timescale *= 0.95f; } void on_accelerate_clicked(GtkToolButton *toolbutton, gpointer user_data) { (void)toolbutton; (void)user_data; - if (g_timeflow == TIME_REALTIME) + if (e_timeflow == TIME_REALTIME) timeflow_set(TIME_ARBITRARY); - g_timescale *= 1.05f; + e_timescale *= 1.05f; } void on_time_activate(GtkEntry *entry, gpointer user_data) @@ -61,13 +61,13 @@ void on_time_activate(GtkEntry *entry, gpointer user_data) (void)entry; (void)user_data; - gtk_window_set_focus(g_window_main, NULL); + gtk_window_set_focus(e_window_main, NULL); GDateTime *dt = g_date_time_new_from_iso8601(gtk_entry_get_text(time_entry), NULL); if (!dt) return; - g_phys.epoch_ms = 1000LL * g_date_time_to_unix(dt) + g_date_time_get_microsecond(dt) / 1000LL; + e_phys.epoch_ms = 1000LL * g_date_time_to_unix(dt) + g_date_time_get_microsecond(dt) / 1000LL; g_date_time_unref(dt); timeflow_set(TIME_PAUSE); } @@ -77,7 +77,7 @@ void on_speed_activate(GtkEntry *entry, gpointer user_data) (void)entry; (void)user_data; - gtk_window_set_focus(g_window_main, NULL); + gtk_window_set_focus(e_window_main, NULL); const gchar *string = gtk_entry_get_text(speed_entry); char *end; @@ -85,15 +85,15 @@ void on_speed_activate(GtkEntry *entry, gpointer user_data) if (end == string) return; - if (g_timeflow == TIME_REALTIME) + if (e_timeflow == TIME_REALTIME) timeflow_set(TIME_ARBITRARY); - g_timescale = speed; + e_timescale = speed; } void timeflow_set(enum Timeflow timeflow) { - g_timeflow = timeflow; + e_timeflow = timeflow; gtk_toggle_tool_button_set_active(play_button, (timeflow == TIME_PAUSE) ? FALSE : TRUE); } @@ -115,10 +115,10 @@ void timemgr_init(GtkBuilder *builder) void timemgr_tic(void) { if (!gtk_widget_has_focus(GTK_WIDGET(time_entry))) - gtk_entry_set_text(time_entry, epoch_to_iso8601(g_phys.epoch_ms, gs_gmt, TRUE)); + gtk_entry_set_text(time_entry, epoch_to_iso8601(e_phys.epoch_ms, gs_gmt, TRUE)); if (!gtk_widget_has_focus(GTK_WIDGET(speed_entry))) { char buf[16]; - snprintf(buf, 16, "%.3fx", (double)g_timescale); + snprintf(buf, 16, "%.3fx", (double)e_timescale); gtk_entry_set_text(speed_entry, buf); } } diff --git a/src/ui/timemgr.h b/src/ui/timemgr.h index 6c26515..906ab2b 100644 --- a/src/ui/timemgr.h +++ b/src/ui/timemgr.h @@ -9,8 +9,8 @@ enum Timeflow { TIME_ARBITRARY, }; -extern enum Timeflow g_timeflow; -extern float g_timescale; +extern enum Timeflow e_timeflow; +extern float e_timescale; void timemgr_init(GtkBuilder *builder); void timemgr_tic(void); diff --git a/src/ui/ui.c b/src/ui/ui.c index 56e31e8..c845f9d 100644 --- a/src/ui/ui.c +++ b/src/ui/ui.c @@ -14,7 +14,7 @@ const gchar *FILENAME_GUI = "res/ui/ui.glade"; -GtkWindow *g_window_main; +GtkWindow *e_window_main; static void on_window_main_destroy(GtkWidget *widget, gpointer user_data); static gboolean on_window_main_delete_event(GtkWidget *widget, GdkEvent *event, gpointer user_data); @@ -58,7 +58,7 @@ void ui_init(int argc, char ***argv) NULL); GtkWidget *window_main = GTK_WIDGET(gtk_builder_get_object(builder, "window_main")); - g_window_main = GTK_WINDOW(window_main); + e_window_main = GTK_WINDOW(window_main); gfx_init(builder); input_init(builder); diff --git a/src/ui/ui.h b/src/ui/ui.h index 2889f43..bc64c47 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -6,6 +6,6 @@ void ui_init(int argc, char ***argv); void ui_deinit(void); -extern GtkWindow *g_window_main; +extern GtkWindow *e_window_main; #endif /* __UI_H__ */ diff --git a/util/winbuild.sh b/util/winbuild.sh index e95d600..7f6aecd 100755 --- a/util/winbuild.sh +++ b/util/winbuild.sh @@ -20,7 +20,7 @@ cd share mkdir -p glib-2.0/schemas cp $MINGW_PATH/share/glib-2.0/schemas/gschemas.compiled glib-2.0/schemas/gschemas.compiled mkdir -p icons -cp -r $MINGW_PATH/share/icons/Adwaita icons/Adwaita +cp -r $MINGW_PATH/share/icons icons cd .. cp -r $MINGW_PATH/ssl ssl cp ../util/README_WIN.txt README.txt