Skip to content

Commit 98cd42e

Browse files
committed
Add orbit controls
1 parent 7b2dffa commit 98cd42e

File tree

1 file changed

+90
-67
lines changed

1 file changed

+90
-67
lines changed

src/gui.h

+90-67
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116

117117
int file_exists(const char *fp);
118118
char *load_file(const char *fp);
119+
119120
GLfloat gl_randf(const GLfloat a, const GLfloat b);
120121
GLfloat gl_deg2rad(const GLfloat d);
121122
GLfloat gl_rad2deg(const GLfloat r);
@@ -244,7 +245,10 @@ int gl_prog_set_mat4(const GLint id, const char *k, const GLfloat v[4 * 4]);
244245
* GL-CAMERA
245246
*****************************************************************************/
246247

248+
typedef enum { ORBIT, FPS } gl_view_mode_t;
249+
247250
typedef struct gl_camera_t {
251+
gl_view_mode_t view_mode;
248252
int *window_width;
249253
int *window_height;
250254

@@ -429,32 +433,6 @@ void gui_loop(gui_t *gui);
429433
#include "stb_image_write.h"
430434
#endif
431435

432-
// /**
433-
// * Tic, start timer.
434-
// * @returns A timespec encapsulating the time instance when tic() is called
435-
// */
436-
// struct timespec tic(void) {
437-
// struct timespec time_start;
438-
// clock_gettime(CLOCK_MONOTONIC, &time_start);
439-
// return time_start;
440-
// }
441-
442-
// /**
443-
// * Toc, stop timer.
444-
// * @returns Time elapsed in seconds
445-
// */
446-
// float toc(struct timespec *tic) {
447-
// assert(tic != NULL);
448-
// struct timespec toc;
449-
// float time_elasped;
450-
451-
// clock_gettime(CLOCK_MONOTONIC, &toc);
452-
// time_elasped = (toc.tv_sec - tic->tv_sec);
453-
// time_elasped += (toc.tv_nsec - tic->tv_nsec) / 1000000000.0;
454-
455-
// return time_elasped;
456-
// }
457-
458436
/******************************************************************************
459437
* OPENGL UTILS
460438
*****************************************************************************/
@@ -1197,6 +1175,7 @@ int gl_prog_set_mat4(const GLint id, const char *k, const GLfloat v[4 * 4]) {
11971175
void gl_camera_setup(gl_camera_t *camera,
11981176
int *window_width,
11991177
int *window_height) {
1178+
camera->view_mode = FPS;
12001179
camera->window_width = window_width;
12011180
camera->window_height = window_height;
12021181

@@ -1207,7 +1186,7 @@ void gl_camera_setup(gl_camera_t *camera,
12071186
gl_vec3(camera->up, 0.0f, 1.0f, 0.0f);
12081187
gl_vec3(camera->front, 0.0f, 0.0f, -1.0f);
12091188
camera->yaw = gl_deg2rad(0.0f);
1210-
camera->pitch = gl_deg2rad(0.0f);
1189+
camera->pitch = gl_deg2rad(30.0f);
12111190
camera->radius = 1.0f;
12121191

12131192
camera->fov = gl_deg2rad(90.0f);
@@ -1241,24 +1220,32 @@ void gl_camera_update(gl_camera_t *camera) {
12411220
gl_perspective(camera->fov, aspect, camera->near, camera->far, camera->P);
12421221

12431222
// View matrix (Orbit mode)
1244-
// GLfloat eye[3] = {0};
1245-
// eye[0] = camera->position[0];
1246-
// eye[1] = camera->position[1];
1247-
// eye[2] = camera->position[2];
1248-
// gl_lookat(eye, camera->focal, camera->world_up, camera->V);
1223+
if (camera->view_mode == ORBIT) {
1224+
camera->position[0] = camera->radius * sin(camera->pitch) * sin(camera->yaw);
1225+
camera->position[1] = camera->radius * cos(camera->pitch);
1226+
camera->position[2] = camera->radius * sin(camera->pitch) * cos(camera->yaw);
1227+
1228+
GLfloat eye[3] = {0};
1229+
eye[0] = camera->position[0];
1230+
eye[1] = camera->position[1];
1231+
eye[2] = camera->position[2];
1232+
gl_lookat(eye, camera->focal, camera->world_up, camera->V);
1233+
}
12491234

12501235
// View matrix (FPS mode)
1251-
GLfloat eye[3] = {0};
1252-
eye[0] = camera->position[0];
1253-
eye[1] = camera->position[1];
1254-
eye[2] = camera->position[2];
1236+
if (camera->view_mode == FPS) {
1237+
GLfloat eye[3] = {0};
1238+
eye[0] = camera->position[0];
1239+
eye[1] = camera->position[1];
1240+
eye[2] = camera->position[2];
12551241

1256-
GLfloat carrot[3] = {0};
1257-
carrot[0] = camera->position[0] + camera->front[0];
1258-
carrot[1] = camera->position[1] + camera->front[1];
1259-
carrot[2] = camera->position[2] + camera->front[2];
1242+
GLfloat carrot[3] = {0};
1243+
carrot[0] = camera->position[0] + camera->front[0];
1244+
carrot[1] = camera->position[1] + camera->front[1];
1245+
carrot[2] = camera->position[2] + camera->front[2];
12601246

1261-
gl_lookat(eye, carrot, camera->world_up, camera->V);
1247+
gl_lookat(eye, carrot, camera->world_up, camera->V);
1248+
}
12621249
}
12631250

12641251
void gl_camera_rotate(gl_camera_t *camera,
@@ -1272,8 +1259,8 @@ void gl_camera_rotate(gl_camera_t *camera,
12721259
pitch += dy * factor;
12731260

12741261
// Constrain pitch and yaw
1275-
pitch = (pitch > gl_deg2rad(89.0f)) ? gl_deg2rad(89.0f) : pitch;
1276-
pitch = (pitch < gl_deg2rad(-80.0f)) ? gl_deg2rad(-80.0f) : pitch;
1262+
// pitch = (pitch > gl_deg2rad(89.0f)) ? gl_deg2rad(89.0f) : pitch;
1263+
// pitch = (pitch < gl_deg2rad(-80.0f)) ? gl_deg2rad(-80.0f) : pitch;
12771264

12781265
// pitch = (pitch <= (-M_PI / 2.0) + 1e-5) ? (-M_PI / 2.0) + 1e-5 : pitch;
12791266
// pitch = (pitch > 0.0) ? 0.0 : pitch;
@@ -2393,41 +2380,79 @@ void gui_process_input(GLFWwindow *window) {
23932380
}
23942381

23952382
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
2396-
gui->camera.position[0] += camera_speed * gui->camera.front[0];
2397-
gui->camera.position[1] += camera_speed * gui->camera.front[1];
2398-
gui->camera.position[2] += camera_speed * gui->camera.front[2];
2383+
if (gui->camera.view_mode == FPS) {
2384+
gui->camera.position[0] += camera_speed * gui->camera.front[0];
2385+
gui->camera.position[1] += camera_speed * gui->camera.front[1];
2386+
gui->camera.position[2] += camera_speed * gui->camera.front[2];
2387+
} else if (gui->camera.view_mode == ORBIT) {
2388+
gui->camera.pitch += 0.01;
2389+
gui->camera.pitch =
2390+
(gui->camera.pitch >= M_PI) ? M_PI : gui->camera.pitch;
2391+
gui->camera.pitch =
2392+
(gui->camera.pitch <= 0.0f) ? 0.0f : gui->camera.pitch;
2393+
}
23992394
}
24002395

24012396
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
2402-
gui->camera.position[0] -= camera_speed * gui->camera.front[0];
2403-
gui->camera.position[1] -= camera_speed * gui->camera.front[1];
2404-
gui->camera.position[2] -= camera_speed * gui->camera.front[2];
2397+
if (gui->camera.view_mode == FPS) {
2398+
gui->camera.position[0] -= camera_speed * gui->camera.front[0];
2399+
gui->camera.position[1] -= camera_speed * gui->camera.front[1];
2400+
gui->camera.position[2] -= camera_speed * gui->camera.front[2];
2401+
} else if (gui->camera.view_mode == ORBIT) {
2402+
gui->camera.pitch -= 0.01;
2403+
gui->camera.pitch =
2404+
(gui->camera.pitch >= M_PI) ? M_PI : gui->camera.pitch;
2405+
gui->camera.pitch =
2406+
(gui->camera.pitch <= 0.0f) ? 0.0f : gui->camera.pitch;
2407+
}
24052408
}
24062409

24072410
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
2408-
GLfloat camera_left[3] = {0};
2409-
gl_vec3_cross(gui->camera.front, gui->camera.up, camera_left);
2410-
gl_normalize(camera_left, 3);
2411-
gui->camera.position[0] -= camera_left[0] * camera_speed;
2412-
gui->camera.position[1] -= camera_left[1] * camera_speed;
2413-
gui->camera.position[2] -= camera_left[2] * camera_speed;
2411+
if (gui->camera.view_mode == FPS) {
2412+
GLfloat camera_left[3] = {0};
2413+
gl_vec3_cross(gui->camera.front, gui->camera.up, camera_left);
2414+
gl_normalize(camera_left, 3);
2415+
gui->camera.position[0] -= camera_left[0] * camera_speed;
2416+
gui->camera.position[1] -= camera_left[1] * camera_speed;
2417+
gui->camera.position[2] -= camera_left[2] * camera_speed;
2418+
} else if (gui->camera.view_mode == ORBIT) {
2419+
gui->camera.yaw -= 0.01;
2420+
gui->camera.yaw = (gui->camera.yaw >= M_PI) ? M_PI : gui->camera.yaw;
2421+
gui->camera.yaw = (gui->camera.yaw <= -M_PI) ? -M_PI : gui->camera.yaw;
2422+
}
24142423
}
24152424

24162425
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
2417-
GLfloat camera_left[3] = {0};
2418-
gl_vec3_cross(gui->camera.front, gui->camera.up, camera_left);
2419-
gl_normalize(camera_left, 3);
2420-
gui->camera.position[0] += camera_left[0] * camera_speed;
2421-
gui->camera.position[1] += camera_left[1] * camera_speed;
2422-
gui->camera.position[2] += camera_left[2] * camera_speed;
2426+
if (gui->camera.view_mode == FPS) {
2427+
GLfloat camera_left[3] = {0};
2428+
gl_vec3_cross(gui->camera.front, gui->camera.up, camera_left);
2429+
gl_normalize(camera_left, 3);
2430+
gui->camera.position[0] += camera_left[0] * camera_speed;
2431+
gui->camera.position[1] += camera_left[1] * camera_speed;
2432+
gui->camera.position[2] += camera_left[2] * camera_speed;
2433+
} else if (gui->camera.view_mode == ORBIT) {
2434+
gui->camera.yaw += 0.01;
2435+
gui->camera.yaw = (gui->camera.yaw >= M_PI) ? M_PI : gui->camera.yaw;
2436+
gui->camera.yaw = (gui->camera.yaw <= -M_PI) ? -M_PI : gui->camera.yaw;
2437+
}
24232438
}
24242439

24252440
if (glfwGetKey(window, GLFW_KEY_EQUAL) == GLFW_PRESS) {
2426-
gl_camera_zoom(&gui->camera, 1.0, 0, 0.1);
2441+
if (gui->camera.view_mode == FPS) {
2442+
gl_camera_zoom(&gui->camera, 1.0, 0, 0.1);
2443+
} else if (gui->camera.view_mode == ORBIT) {
2444+
gui->camera.radius += 0.1;
2445+
gui->camera.radius = (gui->camera.radius <= 0.01) ? 0.01 : gui->camera.radius;
2446+
}
24272447
}
24282448

24292449
if (glfwGetKey(window, GLFW_KEY_MINUS) == GLFW_PRESS) {
2430-
gl_camera_zoom(&gui->camera, 1.0, 0, -0.1);
2450+
if (gui->camera.view_mode == FPS) {
2451+
gl_camera_zoom(&gui->camera, 1.0, 0, -0.1);
2452+
} else if (gui->camera.view_mode == ORBIT) {
2453+
gui->camera.radius -= 0.1;
2454+
gui->camera.radius = (gui->camera.radius <= 0.01) ? 0.01 : gui->camera.radius;
2455+
}
24312456
}
24322457

24332458
// Handle mouse cursor events
@@ -2456,8 +2481,6 @@ void gui_process_input(GLFWwindow *window) {
24562481
} else if (gui->last_cursor_set) {
24572482
gl_camera_pan(&gui->camera, gui->mouse_sensitivity, dx, dy);
24582483
}
2459-
// } else if (event.wheel.type == SDL_MOUSEWHEEL && event.wheel.y) {
2460-
// gl_camera_zoom(&gui->camera, gui->mouse_sensitivity, 0, event.wheel.y);
24612484
} else {
24622485
// Reset cursor
24632486
gui->left_click = 0;
@@ -2575,11 +2598,11 @@ void gui_loop(gui_t *gui) {
25752598
gui_process_input(gui->window);
25762599

25772600
// Draw
2578-
gl_cube_draw(&cube, &gui->camera);
2601+
// gl_cube_draw(&cube, &gui->camera);
25792602
// gl_camera_frame_draw(&cf, &gui->camera);
25802603
gl_axis_frame_draw(&frame, &gui->camera);
25812604
gl_grid_draw(&grid, &gui->camera);
2582-
gl_points_draw(&points, &gui->camera, num_points);
2605+
// gl_points_draw(&points, &gui->camera, num_points);
25832606
// gl_triangle_draw(&triangle, &gui->camera);
25842607

25852608
// Update

0 commit comments

Comments
 (0)