Skip to content

Commit

Permalink
Improve camera controls. Change global variable prefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-graj committed Aug 10, 2022
1 parent f7b763e commit 619f050
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 76 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/entity/earth.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/entity/satellite.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -306,15 +306,15 @@ 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]);
}
}

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) {
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/entity/sky.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/gfx/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "gfx.h"

struct Camera g_camera;
struct Camera e_camera;

void camera_view_update(struct Camera *camera)
{
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/gfx/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__ */
8 changes: 4 additions & 4 deletions src/gfx/gfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion src/gfx/gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__ */
18 changes: 9 additions & 9 deletions src/gfx/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 5 additions & 5 deletions src/phys/phys.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/phys/phys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 14 additions & 16 deletions src/ui/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Loading

0 comments on commit 619f050

Please sign in to comment.