116
116
117
117
int file_exists (const char * fp );
118
118
char * load_file (const char * fp );
119
+
119
120
GLfloat gl_randf (const GLfloat a , const GLfloat b );
120
121
GLfloat gl_deg2rad (const GLfloat d );
121
122
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]);
244
245
* GL-CAMERA
245
246
*****************************************************************************/
246
247
248
+ typedef enum { ORBIT , FPS } gl_view_mode_t ;
249
+
247
250
typedef struct gl_camera_t {
251
+ gl_view_mode_t view_mode ;
248
252
int * window_width ;
249
253
int * window_height ;
250
254
@@ -429,32 +433,6 @@ void gui_loop(gui_t *gui);
429
433
#include "stb_image_write.h"
430
434
#endif
431
435
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
-
458
436
/******************************************************************************
459
437
* OPENGL UTILS
460
438
*****************************************************************************/
@@ -1197,6 +1175,7 @@ int gl_prog_set_mat4(const GLint id, const char *k, const GLfloat v[4 * 4]) {
1197
1175
void gl_camera_setup (gl_camera_t * camera ,
1198
1176
int * window_width ,
1199
1177
int * window_height ) {
1178
+ camera -> view_mode = FPS ;
1200
1179
camera -> window_width = window_width ;
1201
1180
camera -> window_height = window_height ;
1202
1181
@@ -1207,7 +1186,7 @@ void gl_camera_setup(gl_camera_t *camera,
1207
1186
gl_vec3 (camera -> up , 0.0f , 1.0f , 0.0f );
1208
1187
gl_vec3 (camera -> front , 0.0f , 0.0f , -1.0f );
1209
1188
camera -> yaw = gl_deg2rad (0.0f );
1210
- camera -> pitch = gl_deg2rad (0 .0f );
1189
+ camera -> pitch = gl_deg2rad (30 .0f );
1211
1190
camera -> radius = 1.0f ;
1212
1191
1213
1192
camera -> fov = gl_deg2rad (90.0f );
@@ -1241,24 +1220,32 @@ void gl_camera_update(gl_camera_t *camera) {
1241
1220
gl_perspective (camera -> fov , aspect , camera -> near , camera -> far , camera -> P );
1242
1221
1243
1222
// 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
+ }
1249
1234
1250
1235
// 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 ];
1255
1241
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 ];
1260
1246
1261
- gl_lookat (eye , carrot , camera -> world_up , camera -> V );
1247
+ gl_lookat (eye , carrot , camera -> world_up , camera -> V );
1248
+ }
1262
1249
}
1263
1250
1264
1251
void gl_camera_rotate (gl_camera_t * camera ,
@@ -1272,8 +1259,8 @@ void gl_camera_rotate(gl_camera_t *camera,
1272
1259
pitch += dy * factor ;
1273
1260
1274
1261
// 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;
1277
1264
1278
1265
// pitch = (pitch <= (-M_PI / 2.0) + 1e-5) ? (-M_PI / 2.0) + 1e-5 : pitch;
1279
1266
// pitch = (pitch > 0.0) ? 0.0 : pitch;
@@ -2393,41 +2380,79 @@ void gui_process_input(GLFWwindow *window) {
2393
2380
}
2394
2381
2395
2382
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
+ }
2399
2394
}
2400
2395
2401
2396
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
+ }
2405
2408
}
2406
2409
2407
2410
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
+ }
2414
2423
}
2415
2424
2416
2425
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
+ }
2423
2438
}
2424
2439
2425
2440
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
+ }
2427
2447
}
2428
2448
2429
2449
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
+ }
2431
2456
}
2432
2457
2433
2458
// Handle mouse cursor events
@@ -2456,8 +2481,6 @@ void gui_process_input(GLFWwindow *window) {
2456
2481
} else if (gui -> last_cursor_set ) {
2457
2482
gl_camera_pan (& gui -> camera , gui -> mouse_sensitivity , dx , dy );
2458
2483
}
2459
- // } else if (event.wheel.type == SDL_MOUSEWHEEL && event.wheel.y) {
2460
- // gl_camera_zoom(&gui->camera, gui->mouse_sensitivity, 0, event.wheel.y);
2461
2484
} else {
2462
2485
// Reset cursor
2463
2486
gui -> left_click = 0 ;
@@ -2575,11 +2598,11 @@ void gui_loop(gui_t *gui) {
2575
2598
gui_process_input (gui -> window );
2576
2599
2577
2600
// Draw
2578
- gl_cube_draw (& cube , & gui -> camera );
2601
+ // gl_cube_draw(&cube, &gui->camera);
2579
2602
// gl_camera_frame_draw(&cf, &gui->camera);
2580
2603
gl_axis_frame_draw (& frame , & gui -> camera );
2581
2604
gl_grid_draw (& grid , & gui -> camera );
2582
- gl_points_draw (& points , & gui -> camera , num_points );
2605
+ // gl_points_draw(&points, &gui->camera, num_points);
2583
2606
// gl_triangle_draw(&triangle, &gui->camera);
2584
2607
2585
2608
// Update
0 commit comments