par_camera_control.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. // CAMERA CONTROL :: https://github.com/prideout/par
  2. // Enables orbit controls (a.k.a. tumble, arcball, trackball) or pan-and-zoom like Google Maps.
  3. //
  4. // This simple library controls a camera that orbits or pans over a 3D object or terrain. No
  5. // assumptions are made about the renderer or platform. In a sense, this is just a math library.
  6. // Clients notify the controller of generic input events (e.g. grab_begin, grab_move, grab_end)
  7. // and retrieve the look-at vectors (position, target, up) or 4x4 matrices for the camera.
  8. //
  9. // In map mode, users can control their viewing position by grabbing and dragging locations in the
  10. // scene. Sometimes this is known as "through-the-lens" camera control. In this mode the controller
  11. // takes an optional raycast callback to support precise grabbing behavior. If this is not required
  12. // for your use case (e.g. a top-down terrain with an orthgraphic projection), provide NULL for the
  13. // callback and the library will simply raycast against the ground plane.
  14. //
  15. // When the controller is in orbit mode, the orientation of the camera is defined by a Y-axis
  16. // rotation followed by an X-axis rotation. Additionally, the camera can fly forward or backward
  17. // along the viewing direction.
  18. //
  19. // For a complex usage example, go to:
  20. // https://github.com/prideout/camera_demo
  21. //
  22. // Distributed under the MIT License, see bottom of file.
  23. #ifndef PAR_CAMERA_CONTROL_H
  24. #define PAR_CAMERA_CONTROL_H
  25. #include <stdbool.h>
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #ifdef PARCC_USE_DOUBLE
  30. typedef double parcc_float;
  31. #else
  32. typedef float parcc_float;
  33. #endif
  34. // Opaque handle to a camera controller.
  35. typedef struct parcc_context_s parcc_context;
  36. // The camera controller can be configured using either a VERTICAL or HORIZONTAL field of view.
  37. // This specifies which of the two FOV angles should be held constant. For example, if you use a
  38. // horizontal FOV, shrinking the viewport width will change the height of the frustum, but will
  39. // leave the frustum width intact.
  40. typedef enum {
  41. PARCC_VERTICAL,
  42. PARCC_HORIZONTAL,
  43. } parcc_fov;
  44. // The controller can be configured in orbit mode or pan-and-zoom mode.
  45. typedef enum {
  46. PARCC_ORBIT, // aka tumble, trackball, or arcball
  47. PARCC_MAP, // pan and zoom like Google Maps
  48. } parcc_mode;
  49. // Pan and zoom constraints for MAP mode.
  50. typedef enum {
  51. // No constraints except that map_min_distance is enforced.
  52. PARCC_CONSTRAIN_NONE,
  53. // Constrains pan and zoom to limit the viewport's extent along the FOV axis so that it always
  54. // lies within the map_extent. With this constraint, it is possible to see the entire map at
  55. // once, but some portion of the map must always be visible.
  56. PARCC_CONSTRAIN_AXIS,
  57. // Constrains pan and zoom to limit the viewport's extent into the map_extent. With this
  58. // constraint, it may be impossible to see the entire map at once, but users can never see any
  59. // of the empty void that lies outside the map extent.
  60. PARCC_CONSTRAIN_FULL,
  61. } parcc_constraint;
  62. // Optional user-provided ray casting function to enable precise panning behavior.
  63. typedef bool (*parcc_raycast_fn)(const parcc_float origin[3], const parcc_float dir[3],
  64. parcc_float* t, void* userdata);
  65. // The parcc_properties structure holds all user-controlled state in the library.
  66. // Many fields are swapped with fallback values values if they are zero-filled.
  67. typedef struct {
  68. // REQUIRED PROPERTIES
  69. parcc_mode mode; // must be PARCC_ORBIT or PARCC_MAP
  70. int viewport_width; // horizontal extent in pixels
  71. int viewport_height; // vertical extent in pixels
  72. parcc_float near_plane; // distance between camera and near clipping plane
  73. parcc_float far_plane; // distance between camera and far clipping plane
  74. // PROPERTIES WITH DEFAULT VALUES
  75. parcc_fov fov_orientation; // defaults to PARCC_VERTICAL
  76. parcc_float fov_degrees; // full field-of-view angle (not half-angle), defaults to 33.
  77. parcc_float zoom_speed; // defaults to 0.01
  78. parcc_float home_target[3]; // world-space coordinate, defaults to (0,0,0)
  79. parcc_float home_upward[3]; // unit-length vector, defaults to (0,1,0)
  80. // MAP-MODE PROPERTIES
  81. parcc_float map_extent[2]; // (required) size of quad centered at home_target
  82. parcc_float map_plane[4]; // plane equation with normalized XYZ, defaults to (0,0,1,0)
  83. parcc_constraint map_constraint; // defaults to PARCC_CONSTRAIN_NONE
  84. parcc_float map_min_distance; // constrains zoom using distance between camera and plane
  85. parcc_raycast_fn raycast_function; // defaults to a simple plane intersector
  86. void* raycast_userdata; // arbitrary data for the raycast callback
  87. // ORBIT-MODE PROPERTIES
  88. parcc_float home_vector[3]; // non-unitized vector from home_target to initial eye position
  89. parcc_float orbit_speed[2]; // rotational speed (defaults to 0.01)
  90. parcc_float orbit_zoom_speed; // zoom speed (defaults to 0.01)
  91. parcc_float orbit_strafe_speed[2]; // strafe speed (defaults to 0.001)
  92. } parcc_properties;
  93. // The parcc_frame structure holds captured camera state for Van Wijk animation and bookmarks.
  94. // From the user's perspective, this should be treated as an opaque structure.
  95. // clang-format off
  96. typedef struct {
  97. parcc_mode mode;
  98. union {
  99. struct { parcc_float extent, center[2]; };
  100. struct { parcc_float phi, theta, pivot_distance, pivot[3]; };
  101. };
  102. } parcc_frame;
  103. // clang-format on
  104. // CONTROLLER CONSTRUCTOR AND DESTRUCTOR
  105. // The constructor is the only function in the library that performs heap allocation. It
  106. // does not retain the given properties pointer, it simply copies values out of it.
  107. parcc_context* parcc_create_context(const parcc_properties* props);
  108. void parcc_destroy_context(parcc_context* ctx);
  109. // PROPERTY SETTERS AND GETTERS
  110. // The client owns its own instance of the property struct and these functions simply copy values in
  111. // or out of the given struct. Changing some properties might cause a small amount of work to be
  112. // performed.
  113. void parcc_set_properties(parcc_context* context, const parcc_properties* props);
  114. void parcc_get_properties(const parcc_context* context, parcc_properties* out);
  115. // CAMERA RETRIEVAL FUNCTIONS
  116. void parcc_get_look_at(const parcc_context* ctx, parcc_float eyepos[3], parcc_float target[3],
  117. parcc_float upward[3]);
  118. void parcc_get_matrices(const parcc_context* ctx, parcc_float projection[16], parcc_float view[16]);
  119. // SCREEN-SPACE FUNCTIONS FOR USER INTERACTION
  120. // Each of these functions take winx / winy coords.
  121. // - The winx coord should be in [0, viewport_width) where 0 is the left-most column.
  122. // - The winy coord should be in [0, viewport_height) where 0 is the top-most row.
  123. //
  124. // The scrolldelta argument is used for zooming. Positive values indicate "zoom in" in MAP mode or
  125. // "move forward" in ORBIT mode. This gets scaled by zoom_speed. In MAP mode, the zoom speed is also
  126. // scaled by distance-to-ground. To prevent zooming in too far, use a non-zero value for
  127. // map_min_distance.
  128. //
  129. // The strafe argument exists only for ORBIT mode and is typically associated with the right mouse
  130. // button or two-finger dragging. This is used to pan the view. Note that orbit mode maintains a
  131. // "pivot point" which is initially set to home_target. When flying forward or backward, the pivot
  132. // does not move. However strafing will cause it to move around. This matches sketchfab behavior.
  133. // When flying past the orbit point, the controller enters a "flipped" state to prevent the flight
  134. // direction from suddenly changing.
  135. void parcc_grab_begin(parcc_context* context, int winx, int winy, bool strafe);
  136. void parcc_grab_update(parcc_context* context, int winx, int winy);
  137. void parcc_grab_end(parcc_context* context);
  138. void parcc_zoom(parcc_context* context, int winx, int winy, parcc_float scrolldelta);
  139. bool parcc_raycast(parcc_context* context, int winx, int winy, parcc_float result[3]);
  140. // BOOKMARKING AND VAN WIJK INTERPOLATION FUNCTIONS
  141. parcc_frame parcc_get_current_frame(const parcc_context* context);
  142. parcc_frame parcc_get_home_frame(const parcc_context* context);
  143. void parcc_goto_frame(parcc_context* context, parcc_frame state);
  144. parcc_frame parcc_interpolate_frames(parcc_frame a, parcc_frame b, double t);
  145. double parcc_get_interpolation_duration(parcc_frame a, parcc_frame b);
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. // -----------------------------------------------------------------------------
  150. // END PUBLIC API
  151. // -----------------------------------------------------------------------------
  152. #ifdef PAR_CAMERA_CONTROL_IMPLEMENTATION
  153. #include <assert.h>
  154. #include <math.h>
  155. #include <memory.h>
  156. #include <stdlib.h>
  157. #define PARCC_PI (3.14159265359)
  158. #define PARCC_MIN(a, b) (a > b ? b : a)
  159. #define PARCC_MAX(a, b) (a > b ? a : b)
  160. #define PARCC_CLAMP(v, lo, hi) PARCC_MAX(lo, PARCC_MIN(hi, v))
  161. #define PARCC_CALLOC(T, N) ((T*)calloc(N * sizeof(T), 1))
  162. #define PARCC_FREE(BUF) free(BUF)
  163. #define PARCC_SWAP(T, A, B) \
  164. { \
  165. T tmp = B; \
  166. B = A; \
  167. A = tmp; \
  168. }
  169. static void parcc_float4_set(parcc_float dst[4], parcc_float x, parcc_float y, parcc_float z,
  170. parcc_float w) {
  171. dst[0] = x;
  172. dst[1] = y;
  173. dst[2] = z;
  174. dst[3] = w;
  175. }
  176. static parcc_float parcc_float4_dot(const parcc_float a[4], const parcc_float b[4]) {
  177. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
  178. }
  179. static void parcc_float3_set(parcc_float dst[3], parcc_float x, parcc_float y, parcc_float z) {
  180. dst[0] = x;
  181. dst[1] = y;
  182. dst[2] = z;
  183. }
  184. static void parcc_float3_add(parcc_float dst[3], const parcc_float a[3], const parcc_float b[3]) {
  185. dst[0] = a[0] + b[0];
  186. dst[1] = a[1] + b[1];
  187. dst[2] = a[2] + b[2];
  188. }
  189. static void parcc_float3_subtract(parcc_float dst[3], const parcc_float a[3],
  190. const parcc_float b[3]) {
  191. dst[0] = a[0] - b[0];
  192. dst[1] = a[1] - b[1];
  193. dst[2] = a[2] - b[2];
  194. }
  195. static parcc_float parcc_float3_dot(const parcc_float a[3], const parcc_float b[3]) {
  196. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
  197. }
  198. static void parcc_float3_cross(parcc_float dst[3], const parcc_float a[3], const parcc_float b[3]) {
  199. dst[0] = a[1] * b[2] - a[2] * b[1];
  200. dst[1] = a[2] * b[0] - a[0] * b[2];
  201. dst[2] = a[0] * b[1] - a[1] * b[0];
  202. }
  203. static void parcc_float3_scale(parcc_float dst[3], parcc_float v) {
  204. dst[0] *= v;
  205. dst[1] *= v;
  206. dst[2] *= v;
  207. }
  208. static void parcc_float3_lerp(parcc_float dst[3], const parcc_float a[3], const parcc_float b[3],
  209. parcc_float t) {
  210. dst[0] = a[0] * (1 - t) + b[0] * t;
  211. dst[1] = a[1] * (1 - t) + b[1] * t;
  212. dst[2] = a[2] * (1 - t) + b[2] * t;
  213. }
  214. static parcc_float parcc_float_lerp(const parcc_float a, const parcc_float b, parcc_float t) {
  215. return a * (1 - t) + b * t;
  216. }
  217. static parcc_float parcc_float3_length(const parcc_float dst[3]) {
  218. return sqrtf(parcc_float3_dot(dst, dst));
  219. }
  220. static void parcc_float3_normalize(parcc_float dst[3]) {
  221. parcc_float3_scale(dst, 1.0f / parcc_float3_length(dst));
  222. }
  223. static void parcc_float3_copy(parcc_float dst[3], const parcc_float src[3]) {
  224. dst[0] = src[0];
  225. dst[1] = src[1];
  226. dst[2] = src[2];
  227. }
  228. static void parcc_float16_look_at(float dst[16], const float eye[3], const float target[3],
  229. const float up[3]) {
  230. parcc_float v3X[3];
  231. parcc_float v3Y[3];
  232. parcc_float v3Z[3];
  233. parcc_float3_copy(v3Y, up);
  234. parcc_float3_normalize(v3Y);
  235. parcc_float3_subtract(v3Z, eye, target);
  236. parcc_float3_normalize(v3Z);
  237. parcc_float3_cross(v3X, v3Y, v3Z);
  238. parcc_float3_normalize(v3X);
  239. parcc_float3_cross(v3Y, v3Z, v3X);
  240. parcc_float4_set(dst + 0, v3X[0], v3Y[0], v3Z[0], 0);
  241. parcc_float4_set(dst + 4, v3X[1], v3Y[1], v3Z[1], 0);
  242. parcc_float4_set(dst + 8, v3X[2], v3Y[2], v3Z[2], 0);
  243. parcc_float4_set(dst + 12, //
  244. -parcc_float3_dot(v3X, eye), //
  245. -parcc_float3_dot(v3Y, eye), //
  246. -parcc_float3_dot(v3Z, eye), 1.0);
  247. }
  248. static void parcc_float16_perspective_y(float dst[16], float fovy_degrees, float aspect_ratio,
  249. float near, float far) {
  250. const parcc_float fovy_radians = fovy_degrees * PARCC_PI / 180;
  251. const parcc_float f = tan(PARCC_PI / 2.0 - 0.5 * fovy_radians);
  252. const parcc_float rangeinv = 1.0f / (near - far);
  253. dst[0] = f / aspect_ratio;
  254. dst[1] = 0;
  255. dst[2] = 0;
  256. dst[3] = 0;
  257. dst[4] = 0;
  258. dst[5] = f;
  259. dst[6] = 0;
  260. dst[7] = 0;
  261. dst[8] = 0;
  262. dst[9] = 0;
  263. dst[10] = (near + far) * rangeinv;
  264. dst[11] = -1;
  265. dst[12] = 0;
  266. dst[13] = 0;
  267. dst[14] = ((near * far) * rangeinv) * 2.0f;
  268. dst[15] = 0;
  269. }
  270. static void parcc_float16_perspective_x(float dst[16], float fovy_degrees, float aspect_ratio,
  271. float near, float far) {
  272. const parcc_float fovy_radians = fovy_degrees * PARCC_PI / 180;
  273. const parcc_float f = tan(PARCC_PI / 2.0 - 0.5 * fovy_radians);
  274. const parcc_float rangeinv = 1.0 / (near - far);
  275. dst[0] = f;
  276. dst[1] = 0;
  277. dst[2] = 0;
  278. dst[3] = 0;
  279. dst[4] = 0;
  280. dst[5] = f * aspect_ratio;
  281. dst[6] = 0;
  282. dst[7] = 0;
  283. dst[8] = 0;
  284. dst[9] = 0;
  285. dst[10] = (near + far) * rangeinv;
  286. dst[11] = -1;
  287. dst[12] = 0;
  288. dst[13] = 0;
  289. dst[14] = ((near * far) * rangeinv) * 2.0;
  290. dst[15] = 0;
  291. }
  292. // Implementation note about the "parcc_frame" POD. This is an abbreviated camera state
  293. // used for animation and bookmarking.
  294. //
  295. // MAP mode:
  296. // - zoom level is represented with the extent of the rectangle formed by the intersection of
  297. // the frustum with the viewing plane at home_target. It is either a width or a height, depending
  298. // on fov_orientation.
  299. // - the pan offset is stored as a 2D vector from home_target that gets projected to map_plane.
  300. //
  301. // ORBIT mode:
  302. // - phi = X-axis rotation in [-pi/2, +pi/2] (applies first)
  303. // - theta = Y-axis rotation in [-pi, +pi] (applies second)
  304. // - pivot is initialized to home_center but might be changed via strafe
  305. // - pivot_distance is the distance between eye and pivot (negative distance = orbit_flipped)
  306. typedef enum { PARCC_GRAB_NONE, PARCC_GRAB, PARCC_GRAB_STRAFE } parcc_grab_state;
  307. static const parcc_float PARCC_MAX_PHI = PARCC_PI / 2.0 - 0.001;
  308. struct parcc_context_s {
  309. parcc_properties props;
  310. parcc_float eyepos[3];
  311. parcc_float target[3];
  312. parcc_grab_state grabbing;
  313. parcc_float grab_point_pivot[3];
  314. parcc_float grab_point_far[3];
  315. parcc_float grab_point_world[3];
  316. parcc_float grab_point_eyepos[3];
  317. parcc_float grab_point_target[3];
  318. parcc_frame grab_frame;
  319. int grab_winx;
  320. int grab_winy;
  321. parcc_float orbit_pivot[3];
  322. bool orbit_flipped;
  323. };
  324. static bool parcc_raycast_plane(const parcc_float origin[3], const parcc_float dir[3],
  325. parcc_float* t, void* userdata);
  326. static void parcc_get_ray_far(parcc_context* context, int winx, int winy, parcc_float result[3]);
  327. static void parcc_move_with_constraints(parcc_context* context, const parcc_float eyepos[3],
  328. const parcc_float target[3]);
  329. parcc_context* parcc_create_context(const parcc_properties* props) {
  330. parcc_context* context = PARCC_CALLOC(parcc_context, 1);
  331. parcc_set_properties(context, props);
  332. parcc_goto_frame(context, parcc_get_home_frame(context));
  333. return context;
  334. }
  335. void parcc_get_properties(const parcc_context* context, parcc_properties* props) {
  336. *props = context->props;
  337. }
  338. void parcc_set_properties(parcc_context* context, const parcc_properties* pprops) {
  339. parcc_properties props = *pprops;
  340. if (props.fov_degrees == 0) {
  341. props.fov_degrees = 33;
  342. }
  343. if (props.zoom_speed == 0) {
  344. props.zoom_speed = 0.01;
  345. }
  346. if (parcc_float3_dot(props.home_upward, props.home_upward) == 0) {
  347. props.home_upward[1] = 1;
  348. }
  349. if (parcc_float4_dot(props.map_plane, props.map_plane) == 0) {
  350. props.map_plane[2] = 1;
  351. }
  352. if (props.orbit_speed[0] == 0) {
  353. props.orbit_speed[0] = 0.01;
  354. }
  355. if (props.orbit_speed[1] == 0) {
  356. props.orbit_speed[1] = 0.01;
  357. }
  358. if (props.orbit_zoom_speed == 0) {
  359. props.orbit_zoom_speed = 0.01;
  360. }
  361. if (props.orbit_strafe_speed[0] == 0) {
  362. props.orbit_strafe_speed[0] = 0.001;
  363. }
  364. if (props.orbit_strafe_speed[1] == 0) {
  365. props.orbit_strafe_speed[1] = 0.001;
  366. }
  367. if (parcc_float3_dot(props.home_vector, props.home_vector) == 0) {
  368. const parcc_float extent = props.fov_orientation == PARCC_VERTICAL ? props.map_extent[1] :
  369. props.map_extent[0];
  370. const parcc_float fov = props.fov_degrees * PARCC_PI / 180.0;
  371. props.home_vector[0] = 0;
  372. props.home_vector[1] = 0;
  373. props.home_vector[2] = 0.5 * extent / tan(fov / 2.0);
  374. }
  375. const bool more_constrained = (int)props.map_constraint > (int)context->props.map_constraint;
  376. const bool orientation_changed = props.fov_orientation != context->props.fov_orientation;
  377. const bool viewport_resized = props.viewport_height != context->props.viewport_height ||
  378. props.viewport_width != context->props.viewport_width;
  379. context->props = props;
  380. if (props.mode == PARCC_MAP && (more_constrained || orientation_changed ||
  381. (viewport_resized && context->props.map_constraint == PARCC_CONSTRAIN_FULL))) {
  382. parcc_move_with_constraints(context, context->eyepos, context->target);
  383. }
  384. }
  385. void parcc_destroy_context(parcc_context* context) { PARCC_FREE(context); }
  386. void parcc_get_matrices(const parcc_context* context, parcc_float projection[16],
  387. parcc_float view[16]) {
  388. parcc_float gaze[3];
  389. parcc_float3_subtract(gaze, context->target, context->eyepos);
  390. parcc_float3_normalize(gaze);
  391. parcc_float right[3];
  392. parcc_float3_cross(right, gaze, context->props.home_upward);
  393. parcc_float3_normalize(right);
  394. parcc_float upward[3];
  395. parcc_float3_cross(upward, right, gaze);
  396. parcc_float3_normalize(upward);
  397. parcc_float16_look_at(view, context->eyepos, context->target, upward);
  398. const parcc_properties props = context->props;
  399. const parcc_float aspect = (parcc_float)props.viewport_width / props.viewport_height;
  400. const parcc_float fov = props.fov_degrees;
  401. if (context->props.fov_orientation == PARCC_HORIZONTAL) {
  402. parcc_float16_perspective_x(projection, fov, aspect, props.near_plane, props.far_plane);
  403. } else {
  404. parcc_float16_perspective_y(projection, fov, aspect, props.near_plane, props.far_plane);
  405. }
  406. }
  407. void parcc_get_look_at(const parcc_context* ctx, parcc_float eyepos[3], parcc_float target[3],
  408. parcc_float upward[3]) {
  409. parcc_float3_copy(eyepos, ctx->eyepos);
  410. parcc_float3_copy(target, ctx->target);
  411. if (upward) {
  412. parcc_float gaze[3];
  413. parcc_float3_subtract(gaze, ctx->target, ctx->eyepos);
  414. parcc_float3_normalize(gaze);
  415. parcc_float right[3];
  416. parcc_float3_cross(right, gaze, ctx->props.home_upward);
  417. parcc_float3_normalize(right);
  418. parcc_float3_cross(upward, right, gaze);
  419. parcc_float3_normalize(upward);
  420. }
  421. }
  422. void parcc_grab_begin(parcc_context* context, int winx, int winy, bool strafe) {
  423. context->grabbing = strafe ? PARCC_GRAB_STRAFE : PARCC_GRAB;
  424. if (context->props.mode == PARCC_MAP) {
  425. if (!parcc_raycast(context, winx, winy, context->grab_point_world)) {
  426. return;
  427. }
  428. parcc_get_ray_far(context, winx, winy, context->grab_point_far);
  429. }
  430. if (context->props.mode == PARCC_ORBIT) {
  431. context->grab_frame = parcc_get_current_frame(context);
  432. context->grab_winx = winx;
  433. context->grab_winy = winy;
  434. parcc_float3_copy(context->grab_point_pivot, context->orbit_pivot);
  435. }
  436. parcc_float3_copy(context->grab_point_eyepos, context->eyepos);
  437. parcc_float3_copy(context->grab_point_target, context->target);
  438. }
  439. void parcc_grab_update(parcc_context* context, int winx, int winy) {
  440. if (context->props.mode == PARCC_MAP && context->grabbing == PARCC_GRAB) {
  441. parcc_float u_vec[3];
  442. parcc_float3_subtract(u_vec, context->grab_point_world, context->grab_point_eyepos);
  443. const parcc_float u_len = parcc_float3_length(u_vec);
  444. parcc_float v_vec[3];
  445. parcc_float3_subtract(v_vec, context->grab_point_far, context->grab_point_world);
  446. const parcc_float v_len = parcc_float3_length(v_vec);
  447. parcc_float far_point[3];
  448. parcc_get_ray_far(context, winx, winy, far_point);
  449. parcc_float translation[3];
  450. parcc_float3_subtract(translation, far_point, context->grab_point_far);
  451. parcc_float3_scale(translation, -u_len / v_len);
  452. parcc_float eyepos[3];
  453. parcc_float3_add(eyepos, context->grab_point_eyepos, translation);
  454. parcc_float target[3];
  455. parcc_float3_add(target, context->grab_point_target, translation);
  456. parcc_move_with_constraints(context, eyepos, target);
  457. }
  458. if (context->props.mode == PARCC_ORBIT && context->grabbing == PARCC_GRAB) {
  459. parcc_frame frame = parcc_get_current_frame(context);
  460. const int delx = context->grab_winx - winx;
  461. const int dely = context->grab_winy - winy;
  462. const parcc_float phi = dely * context->props.orbit_speed[1];
  463. const parcc_float theta = delx * context->props.orbit_speed[0];
  464. frame.phi = context->grab_frame.phi + phi;
  465. frame.theta = context->grab_frame.theta + theta;
  466. frame.phi = PARCC_CLAMP(frame.phi, -PARCC_MAX_PHI, PARCC_MAX_PHI);
  467. parcc_goto_frame(context, frame);
  468. }
  469. if (context->props.mode == PARCC_ORBIT && context->grabbing == PARCC_GRAB_STRAFE) {
  470. parcc_float upward[3];
  471. parcc_float gaze[3];
  472. parcc_float3_subtract(gaze, context->target, context->eyepos);
  473. parcc_float3_normalize(gaze);
  474. parcc_float right[3];
  475. parcc_float3_cross(right, gaze, context->props.home_upward);
  476. parcc_float3_normalize(right);
  477. parcc_float3_cross(upward, right, gaze);
  478. parcc_float3_normalize(upward);
  479. const int delx = context->grab_winx - winx;
  480. const int dely = context->grab_winy - winy;
  481. const parcc_float dx = delx * context->props.orbit_strafe_speed[0];
  482. const parcc_float dy = dely * context->props.orbit_strafe_speed[1];
  483. parcc_float3_scale(right, dx);
  484. parcc_float3_scale(upward, dy);
  485. parcc_float movement[3];
  486. parcc_float3_add(movement, upward, right);
  487. parcc_float3_add(context->orbit_pivot, context->grab_point_pivot, movement);
  488. parcc_float3_add(context->eyepos, context->grab_point_eyepos, movement);
  489. parcc_float3_add(context->target, context->grab_point_target, movement);
  490. }
  491. }
  492. void parcc_zoom(parcc_context* context, int winx, int winy, parcc_float scrolldelta) {
  493. if (context->props.mode == PARCC_MAP) {
  494. parcc_float grab_point_world[3];
  495. if (!parcc_raycast(context, winx, winy, grab_point_world)) {
  496. return;
  497. }
  498. // We intentionally avoid normalizing this vector since you usually
  499. // want to slow down when approaching the surface.
  500. parcc_float u_vec[3];
  501. parcc_float3_subtract(u_vec, grab_point_world, context->eyepos);
  502. // Prevent getting stuck; this needs to be done regardless
  503. // of the user's min_distance setting, which is enforced in
  504. // parcc_move_with_constraints.
  505. const parcc_float zoom_speed = context->props.zoom_speed;
  506. if (scrolldelta > 0.0) {
  507. const parcc_float distance_to_surface = parcc_float3_length(u_vec);
  508. if (distance_to_surface < zoom_speed) {
  509. return;
  510. }
  511. }
  512. parcc_float3_scale(u_vec, scrolldelta * zoom_speed);
  513. parcc_float eyepos[3];
  514. parcc_float3_add(eyepos, context->eyepos, u_vec);
  515. parcc_float target[3];
  516. parcc_float3_add(target, context->target, u_vec);
  517. parcc_move_with_constraints(context, eyepos, target);
  518. }
  519. if (context->props.mode == PARCC_ORBIT) {
  520. parcc_float gaze[3];
  521. parcc_float3_subtract(gaze, context->target, context->eyepos);
  522. parcc_float3_normalize(gaze);
  523. parcc_float3_scale(gaze, context->props.orbit_zoom_speed * scrolldelta);
  524. parcc_float v0[3];
  525. parcc_float3_subtract(v0, context->orbit_pivot, context->eyepos);
  526. parcc_float3_add(context->eyepos, context->eyepos, gaze);
  527. parcc_float3_add(context->target, context->target, gaze);
  528. parcc_float v1[3];
  529. parcc_float3_subtract(v1, context->orbit_pivot, context->eyepos);
  530. if (parcc_float3_dot(v0, v1) < 0) {
  531. context->orbit_flipped = !context->orbit_flipped;
  532. }
  533. }
  534. }
  535. void parcc_grab_end(parcc_context* context) { context->grabbing = PARCC_GRAB_NONE; }
  536. bool parcc_raycast(parcc_context* context, int winx, int winy, parcc_float result[3]) {
  537. const parcc_float width = context->props.viewport_width;
  538. const parcc_float height = context->props.viewport_height;
  539. const parcc_float fov = context->props.fov_degrees * PARCC_PI / 180.0;
  540. const bool vertical_fov = context->props.fov_orientation == PARCC_VERTICAL;
  541. const parcc_float* origin = context->eyepos;
  542. parcc_float gaze[3];
  543. parcc_float3_subtract(gaze, context->target, origin);
  544. parcc_float3_normalize(gaze);
  545. parcc_float right[3];
  546. parcc_float3_cross(right, gaze, context->props.home_upward);
  547. parcc_float3_normalize(right);
  548. parcc_float upward[3];
  549. parcc_float3_cross(upward, right, gaze);
  550. parcc_float3_normalize(upward);
  551. // Remap the grid coordinate into [-1, +1] and shift it to the pixel center.
  552. const parcc_float u = 2.0 * (winx + 0.5) / width - 1.0;
  553. const parcc_float v = 2.0 * (winy + 0.5) / height - 1.0;
  554. // Compute the tangent of the field-of-view angle as well as the aspect ratio.
  555. const parcc_float tangent = tan(fov / 2.0);
  556. const parcc_float aspect = width / height;
  557. // Adjust the gaze so it goes through the pixel of interest rather than the grid center.
  558. if (vertical_fov) {
  559. parcc_float3_scale(right, tangent * u * aspect);
  560. parcc_float3_scale(upward, tangent * v);
  561. } else {
  562. parcc_float3_scale(right, tangent * u);
  563. parcc_float3_scale(upward, tangent * v / aspect);
  564. }
  565. parcc_float3_add(gaze, gaze, right);
  566. parcc_float3_add(gaze, gaze, upward);
  567. parcc_float3_normalize(gaze);
  568. // Invoke the user's callback or fallback function.
  569. parcc_raycast_fn callback = context->props.raycast_function;
  570. parcc_raycast_fn fallback = parcc_raycast_plane;
  571. void* userdata = context->props.raycast_userdata;
  572. if (!callback) {
  573. callback = fallback;
  574. userdata = context;
  575. }
  576. // If the ray misses, then try the fallback function.
  577. parcc_float t;
  578. if (!callback(origin, gaze, &t, userdata)) {
  579. if (callback == fallback) {
  580. return false;
  581. }
  582. if (!fallback(origin, gaze, &t, context)) {
  583. return false;
  584. }
  585. }
  586. parcc_float3_scale(gaze, t);
  587. parcc_float3_add(result, origin, gaze);
  588. return true;
  589. }
  590. parcc_frame parcc_get_current_frame(const parcc_context* context) {
  591. parcc_frame frame;
  592. frame.mode = context->props.mode;
  593. if (context->props.mode == PARCC_MAP) {
  594. const parcc_float* origin = context->eyepos;
  595. const parcc_float* upward = context->props.home_upward;
  596. parcc_float direction[3];
  597. parcc_float3_subtract(direction, context->target, origin);
  598. parcc_float3_normalize(direction);
  599. parcc_float distance;
  600. parcc_raycast_plane(origin, direction, &distance, (void*)context);
  601. const parcc_float fov = context->props.fov_degrees * PARCC_PI / 180.0;
  602. const parcc_float half_extent = distance * tan(fov / 2);
  603. parcc_float target[3];
  604. parcc_float3_scale(direction, distance);
  605. parcc_float3_add(target, origin, direction);
  606. // Compute the tangent frame defined by the map_plane normal and the home_upward vector.
  607. parcc_float uvec[3];
  608. parcc_float vvec[3];
  609. parcc_float target_to_eye[3];
  610. parcc_float3_copy(target_to_eye, context->props.map_plane);
  611. parcc_float3_cross(uvec, upward, target_to_eye);
  612. parcc_float3_cross(vvec, target_to_eye, uvec);
  613. parcc_float3_subtract(target, target, context->props.home_target);
  614. frame.extent = half_extent * 2;
  615. frame.center[0] = parcc_float3_dot(uvec, target);
  616. frame.center[1] = parcc_float3_dot(vvec, target);
  617. }
  618. if (context->props.mode == PARCC_ORBIT) {
  619. parcc_float pivot_to_eye[3];
  620. parcc_float3_subtract(pivot_to_eye, context->eyepos, context->orbit_pivot);
  621. const parcc_float d = parcc_float3_length(pivot_to_eye);
  622. const parcc_float x = pivot_to_eye[0] / d;
  623. const parcc_float y = pivot_to_eye[1] / d;
  624. const parcc_float z = pivot_to_eye[2] / d;
  625. frame.phi = asin(y);
  626. frame.theta = atan2(x, z);
  627. frame.pivot_distance = context->orbit_flipped ? -d : d;
  628. parcc_float3_copy(frame.pivot, context->orbit_pivot);
  629. }
  630. return frame;
  631. }
  632. parcc_frame parcc_get_home_frame(const parcc_context* context) {
  633. const parcc_float width = context->props.viewport_width;
  634. const parcc_float height = context->props.viewport_height;
  635. const parcc_float aspect = width / height;
  636. parcc_frame frame;
  637. frame.mode = context->props.mode;
  638. if (frame.mode == PARCC_MAP) {
  639. const parcc_float map_width = context->props.map_extent[0] / 2;
  640. const parcc_float map_height = context->props.map_extent[1] / 2;
  641. const bool horiz = context->props.fov_orientation == PARCC_HORIZONTAL;
  642. frame.extent = horiz ? context->props.map_extent[0] : context->props.map_extent[1];
  643. frame.center[0] = 0;
  644. frame.center[1] = 0;
  645. if (context->props.map_constraint != PARCC_CONSTRAIN_FULL) {
  646. return frame;
  647. }
  648. if (horiz) {
  649. parcc_float vp_width = frame.extent / 2;
  650. parcc_float vp_height = vp_width / aspect;
  651. if (map_height < vp_height) {
  652. frame.extent = 2 * map_height * aspect;
  653. }
  654. } else {
  655. parcc_float vp_height = frame.extent / 2;
  656. parcc_float vp_width = vp_height * aspect;
  657. if (map_width < vp_width) {
  658. frame.extent = 2 * map_width / aspect;
  659. }
  660. }
  661. }
  662. if (frame.mode == PARCC_ORBIT) {
  663. frame.theta = frame.phi = 0;
  664. parcc_float3_copy(frame.pivot, context->props.home_target);
  665. frame.pivot_distance = parcc_float3_length(context->props.home_vector);
  666. }
  667. return frame;
  668. }
  669. void parcc_goto_frame(parcc_context* context, parcc_frame frame) {
  670. if (context->props.mode == PARCC_MAP) {
  671. const parcc_float* upward = context->props.home_upward;
  672. const parcc_float half_extent = frame.extent / 2.0;
  673. const parcc_float fov = context->props.fov_degrees * PARCC_PI / 180.0;
  674. const parcc_float distance = half_extent / tan(fov / 2);
  675. // Compute the tangent frame defined by the map_plane normal and the home_upward vector.
  676. parcc_float uvec[3];
  677. parcc_float vvec[3];
  678. parcc_float target_to_eye[3];
  679. parcc_float3_copy(target_to_eye, context->props.map_plane);
  680. parcc_float3_cross(uvec, upward, target_to_eye);
  681. parcc_float3_cross(vvec, target_to_eye, uvec);
  682. // Scale the U and V components by the frame coordinate.
  683. parcc_float3_scale(uvec, frame.center[0]);
  684. parcc_float3_scale(vvec, frame.center[1]);
  685. // Obtain the new target position by adding U and V to home_target.
  686. parcc_float3_copy(context->target, context->props.home_target);
  687. parcc_float3_add(context->target, context->target, uvec);
  688. parcc_float3_add(context->target, context->target, vvec);
  689. // Obtain the new eye position by adding the scaled plane normal to the new target
  690. // position.
  691. parcc_float3_scale(target_to_eye, distance);
  692. parcc_float3_add(context->eyepos, context->target, target_to_eye);
  693. }
  694. if (context->props.mode == PARCC_ORBIT) {
  695. parcc_float3_copy(context->orbit_pivot, frame.pivot);
  696. const parcc_float x = sin(frame.theta) * cos(frame.phi);
  697. const parcc_float y = sin(frame.phi);
  698. const parcc_float z = cos(frame.theta) * cos(frame.phi);
  699. parcc_float3_set(context->eyepos, x, y, z);
  700. parcc_float3_scale(context->eyepos, fabs(frame.pivot_distance));
  701. parcc_float3_add(context->eyepos, context->eyepos, context->orbit_pivot);
  702. context->orbit_flipped = frame.pivot_distance < 0;
  703. parcc_float3_set(context->target, x, y, z);
  704. parcc_float3_scale(context->target, context->orbit_flipped ? 1.0 : -1.0);
  705. parcc_float3_add(context->target, context->target, context->eyepos);
  706. }
  707. }
  708. parcc_frame parcc_interpolate_frames(parcc_frame a, parcc_frame b, double t) {
  709. parcc_frame frame;
  710. if (a.mode == PARCC_MAP && b.mode == PARCC_MAP) {
  711. const double rho = sqrt(2.0);
  712. const double rho2 = 2, rho4 = 4;
  713. const double ux0 = a.center[0], uy0 = a.center[1], w0 = a.extent;
  714. const double ux1 = b.center[0], uy1 = b.center[1], w1 = b.extent;
  715. const double dx = ux1 - ux0, dy = uy1 - uy0, d2 = dx * dx + dy * dy, d1 = sqrt(d2);
  716. const double b0 = (w1 * w1 - w0 * w0 + rho4 * d2) / (2.0 * w0 * rho2 * d1);
  717. const double b1 = (w1 * w1 - w0 * w0 - rho4 * d2) / (2.0 * w1 * rho2 * d1);
  718. const double r0 = log(sqrt(b0 * b0 + 1.0) - b0);
  719. const double r1 = log(sqrt(b1 * b1 + 1) - b1);
  720. const double dr = r1 - r0;
  721. const int valid_dr = (dr == dr) && dr != 0;
  722. const double S = (valid_dr ? dr : log(w1 / w0)) / rho;
  723. const double s = t * S;
  724. if (valid_dr) {
  725. const double coshr0 = cosh(r0);
  726. const double u = w0 / (rho2 * d1) * (coshr0 * tanh(rho * s + r0) - sinh(r0));
  727. frame.center[0] = ux0 + u * dx;
  728. frame.center[1] = uy0 + u * dy;
  729. frame.extent = w0 * coshr0 / cosh(rho * s + r0);
  730. return frame;
  731. }
  732. frame.center[0] = ux0 + t * dx;
  733. frame.center[1] = uy0 + t * dy;
  734. frame.extent = w0 * exp(rho * s);
  735. } else if (a.mode == PARCC_ORBIT && b.mode == PARCC_ORBIT) {
  736. frame.phi = parcc_float_lerp(a.phi, b.phi, t);
  737. frame.theta = parcc_float_lerp(a.theta, b.theta, t);
  738. frame.pivot_distance = parcc_float_lerp(a.pivot_distance, b.pivot_distance, t);
  739. parcc_float3_lerp(frame.pivot, a.pivot, b.pivot, t);
  740. } else {
  741. // Cross-mode interpolation is not implemented.
  742. frame = b;
  743. }
  744. return frame;
  745. }
  746. double parcc_get_interpolation_duration(parcc_frame a, parcc_frame b) {
  747. if (a.mode == PARCC_MAP && b.mode == PARCC_MAP) {
  748. const double rho = sqrt(2.0);
  749. const double rho2 = 2, rho4 = 4;
  750. const double ux0 = a.center[0], uy0 = a.center[1], w0 = a.extent;
  751. const double ux1 = b.center[0], uy1 = b.center[1], w1 = b.extent;
  752. const double dx = ux1 - ux0, dy = uy1 - uy0, d2 = dx * dx + dy * dy, d1 = sqrt(d2);
  753. const double b0 = (w1 * w1 - w0 * w0 + rho4 * d2) / (2.0 * w0 * rho2 * d1);
  754. const double b1 = (w1 * w1 - w0 * w0 - rho4 * d2) / (2.0 * w1 * rho2 * d1);
  755. const double r0 = log(sqrt(b0 * b0 + 1.0) - b0);
  756. const double r1 = log(sqrt(b1 * b1 + 1) - b1);
  757. const double dr = r1 - r0;
  758. const int valid_dr = (dr == dr) && dr != 0;
  759. const double S = (valid_dr ? dr : log(w1 / w0)) / rho;
  760. return fabs(S);
  761. } else if (a.mode == PARCC_ORBIT && b.mode == PARCC_ORBIT) {
  762. return 1;
  763. } else {
  764. // Cross-mode interpolation is not implemented.
  765. }
  766. return 0;
  767. }
  768. static bool parcc_raycast_plane(const parcc_float origin[3], const parcc_float dir[3],
  769. parcc_float* t, void* userdata) {
  770. parcc_context* context = (parcc_context*)userdata;
  771. const parcc_float* plane = context->props.map_plane;
  772. parcc_float n[3] = {plane[0], plane[1], plane[2]};
  773. parcc_float p0[3] = {plane[0], plane[1], plane[2]};
  774. parcc_float3_scale(p0, plane[3]);
  775. const parcc_float denom = -parcc_float3_dot(n, dir);
  776. if (denom > 1e-6 || denom < -1e-6) {
  777. parcc_float p0l0[3];
  778. parcc_float3_subtract(p0l0, p0, origin);
  779. *t = parcc_float3_dot(p0l0, n) / -denom;
  780. return *t >= 0;
  781. }
  782. return false;
  783. }
  784. // Finds the point on the frustum's far plane that a pick ray intersects.
  785. static void parcc_get_ray_far(parcc_context* context, int winx, int winy, parcc_float result[3]) {
  786. const parcc_float width = context->props.viewport_width;
  787. const parcc_float height = context->props.viewport_height;
  788. const parcc_float fov = context->props.fov_degrees * PARCC_PI / 180.0;
  789. const bool vertical_fov = context->props.fov_orientation == PARCC_VERTICAL;
  790. const parcc_float* origin = context->eyepos;
  791. parcc_float gaze[3];
  792. parcc_float3_subtract(gaze, context->target, origin);
  793. parcc_float3_normalize(gaze);
  794. parcc_float right[3];
  795. parcc_float3_cross(right, gaze, context->props.home_upward);
  796. parcc_float3_normalize(right);
  797. parcc_float upward[3];
  798. parcc_float3_cross(upward, right, gaze);
  799. parcc_float3_normalize(upward);
  800. // Remap the grid coordinate into [-1, +1] and shift it to the pixel center.
  801. const parcc_float u = 2.0 * (winx + 0.5) / width - 1.0;
  802. const parcc_float v = 2.0 * (winy + 0.5) / height - 1.0;
  803. // Compute the tangent of the field-of-view angle as well as the aspect ratio.
  804. const parcc_float tangent = tan(fov / 2.0);
  805. const parcc_float aspect = width / height;
  806. // Adjust the gaze so it goes through the pixel of interest rather than the grid center.
  807. if (vertical_fov) {
  808. parcc_float3_scale(right, tangent * u * aspect);
  809. parcc_float3_scale(upward, tangent * v);
  810. } else {
  811. parcc_float3_scale(right, tangent * u);
  812. parcc_float3_scale(upward, tangent * v / aspect);
  813. }
  814. parcc_float3_add(gaze, gaze, right);
  815. parcc_float3_add(gaze, gaze, upward);
  816. parcc_float3_scale(gaze, context->props.far_plane);
  817. parcc_float3_add(result, origin, gaze);
  818. }
  819. static void parcc_move_with_constraints(parcc_context* context, const parcc_float eyepos[3],
  820. const parcc_float target[3]) {
  821. const parcc_constraint constraint = context->props.map_constraint;
  822. const parcc_float width = context->props.viewport_width;
  823. const parcc_float height = context->props.viewport_height;
  824. const parcc_float aspect = width / height;
  825. const parcc_float map_width = context->props.map_extent[0] / 2;
  826. const parcc_float map_height = context->props.map_extent[1] / 2;
  827. const parcc_frame home = parcc_get_home_frame(context);
  828. const parcc_frame previous_frame = parcc_get_current_frame(context);
  829. const parcc_float fov = context->props.fov_degrees * PARCC_PI / 180.0;
  830. const parcc_float min_extent = 2.0 * context->props.map_min_distance * tan(fov / 2);
  831. parcc_float3_copy(context->eyepos, eyepos);
  832. parcc_float3_copy(context->target, target);
  833. parcc_frame frame = parcc_get_current_frame(context);
  834. if (frame.extent < min_extent) {
  835. frame.extent = min_extent;
  836. frame.center[0] = previous_frame.center[0];
  837. frame.center[1] = previous_frame.center[1];
  838. }
  839. if (constraint == PARCC_CONSTRAIN_NONE) {
  840. parcc_goto_frame(context, frame);
  841. return;
  842. }
  843. parcc_float x = frame.center[0];
  844. parcc_float y = frame.center[1];
  845. if (context->props.fov_orientation == PARCC_HORIZONTAL) {
  846. parcc_float vp_width = frame.extent / 2;
  847. parcc_float vp_height = vp_width / aspect;
  848. if (map_width < vp_width) {
  849. frame.extent = home.extent;
  850. vp_width = frame.extent / 2;
  851. vp_height = vp_width / aspect;
  852. x = 0;
  853. y = previous_frame.center[1];
  854. }
  855. x = PARCC_CLAMP(x, -map_width + vp_width, map_width - vp_width);
  856. if (map_height < vp_height) {
  857. if (context->props.map_constraint == PARCC_CONSTRAIN_FULL) {
  858. frame.extent = 2 * map_height * aspect;
  859. vp_width = frame.extent / 2;
  860. vp_height = vp_width / aspect;
  861. x = previous_frame.center[0];
  862. x = PARCC_CLAMP(x, -map_width + vp_width, map_width - vp_width);
  863. y = PARCC_CLAMP(y, -map_height + vp_height, map_height - vp_height);
  864. } else {
  865. y = PARCC_CLAMP(y, -vp_height + map_height, vp_height - map_height);
  866. }
  867. } else {
  868. y = PARCC_CLAMP(y, -map_height + vp_height, map_height - vp_height);
  869. }
  870. } else {
  871. parcc_float vp_height = frame.extent / 2;
  872. parcc_float vp_width = vp_height * aspect;
  873. if (map_height < vp_height) {
  874. frame.extent = home.extent;
  875. vp_height = frame.extent / 2;
  876. vp_width = vp_height * aspect;
  877. y = 0;
  878. x = previous_frame.center[0];
  879. }
  880. y = PARCC_CLAMP(y, -map_height + vp_height, map_height - vp_height);
  881. if (map_width < vp_width) {
  882. if (context->props.map_constraint == PARCC_CONSTRAIN_FULL) {
  883. frame.extent = 2 * map_width / aspect;
  884. vp_height = frame.extent / 2;
  885. vp_width = vp_height * aspect;
  886. y = previous_frame.center[1];
  887. y = PARCC_CLAMP(y, -map_height + vp_height, map_height - vp_height);
  888. x = PARCC_CLAMP(x, -map_width + vp_width, map_width - vp_width);
  889. } else {
  890. x = PARCC_CLAMP(x, -vp_width + map_width, vp_width - map_width);
  891. }
  892. } else {
  893. x = PARCC_CLAMP(x, -map_width + vp_width, map_width - vp_width);
  894. }
  895. }
  896. frame.center[0] = x;
  897. frame.center[1] = y;
  898. parcc_goto_frame(context, frame);
  899. }
  900. #endif // PAR_CAMERA_CONTROL_IMPLEMENTATION
  901. #endif // PAR_CAMERA_CONTROL_H
  902. // par_camera_control is distributed under the MIT license:
  903. //
  904. // Copyright (c) 2019 Philip Rideout
  905. //
  906. // Permission is hereby granted, free of charge, to any person obtaining a copy
  907. // of this software and associated documentation files (the "Software"), to deal
  908. // in the Software without restriction, including without limitation the rights
  909. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  910. // copies of the Software, and to permit persons to whom the Software is
  911. // furnished to do so, subject to the following conditions:
  912. //
  913. // The above copyright notice and this permission notice shall be included in
  914. // all copies or substantial portions of the Software.
  915. //
  916. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  917. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  918. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  919. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  920. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  921. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  922. // SOFTWARE.