2
0

par_camera_control.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. parcc_float grab_point_far[3];
  499. parcc_get_ray_far(context, winx, winy, grab_point_far);
  500. // We intentionally avoid normalizing this vector since you usually
  501. // want to slow down when approaching the surface.
  502. parcc_float u_vec[3];
  503. parcc_float3_subtract(u_vec, grab_point_world, context->eyepos);
  504. // Prevent getting stuck; this needs to be done regardless
  505. // of the user's min_distance setting, which is enforced in
  506. // parcc_move_with_constraints.
  507. const parcc_float zoom_speed = context->props.zoom_speed;
  508. if (scrolldelta > 0.0) {
  509. const parcc_float distance_to_surface = parcc_float3_length(u_vec);
  510. if (distance_to_surface < zoom_speed) {
  511. return;
  512. }
  513. }
  514. parcc_float3_scale(u_vec, scrolldelta * zoom_speed);
  515. parcc_float eyepos[3];
  516. parcc_float3_add(eyepos, context->eyepos, u_vec);
  517. parcc_float target[3];
  518. parcc_float3_add(target, context->target, u_vec);
  519. parcc_move_with_constraints(context, eyepos, target);
  520. }
  521. if (context->props.mode == PARCC_ORBIT) {
  522. parcc_float gaze[3];
  523. parcc_float3_subtract(gaze, context->target, context->eyepos);
  524. parcc_float3_normalize(gaze);
  525. parcc_float3_scale(gaze, context->props.orbit_zoom_speed * scrolldelta);
  526. parcc_float v0[3];
  527. parcc_float3_subtract(v0, context->orbit_pivot, context->eyepos);
  528. parcc_float3_add(context->eyepos, context->eyepos, gaze);
  529. parcc_float3_add(context->target, context->target, gaze);
  530. parcc_float v1[3];
  531. parcc_float3_subtract(v1, context->orbit_pivot, context->eyepos);
  532. if (parcc_float3_dot(v0, v1) < 0) {
  533. context->orbit_flipped = !context->orbit_flipped;
  534. }
  535. }
  536. }
  537. void parcc_grab_end(parcc_context* context) { context->grabbing = PARCC_GRAB_NONE; }
  538. bool parcc_raycast(parcc_context* context, int winx, int winy, parcc_float result[3]) {
  539. const parcc_float width = context->props.viewport_width;
  540. const parcc_float height = context->props.viewport_height;
  541. const parcc_float fov = context->props.fov_degrees * PARCC_PI / 180.0;
  542. const bool vertical_fov = context->props.fov_orientation == PARCC_VERTICAL;
  543. const parcc_float* origin = context->eyepos;
  544. parcc_float gaze[3];
  545. parcc_float3_subtract(gaze, context->target, origin);
  546. parcc_float3_normalize(gaze);
  547. parcc_float right[3];
  548. parcc_float3_cross(right, gaze, context->props.home_upward);
  549. parcc_float3_normalize(right);
  550. parcc_float upward[3];
  551. parcc_float3_cross(upward, right, gaze);
  552. parcc_float3_normalize(upward);
  553. // Remap the grid coordinate into [-1, +1] and shift it to the pixel center.
  554. const parcc_float u = 2.0 * (winx + 0.5) / width - 1.0;
  555. const parcc_float v = 2.0 * (winy + 0.5) / height - 1.0;
  556. // Compute the tangent of the field-of-view angle as well as the aspect ratio.
  557. const parcc_float tangent = tan(fov / 2.0);
  558. const parcc_float aspect = width / height;
  559. // Adjust the gaze so it goes through the pixel of interest rather than the grid center.
  560. if (vertical_fov) {
  561. parcc_float3_scale(right, tangent * u * aspect);
  562. parcc_float3_scale(upward, tangent * v);
  563. } else {
  564. parcc_float3_scale(right, tangent * u);
  565. parcc_float3_scale(upward, tangent * v / aspect);
  566. }
  567. parcc_float3_add(gaze, gaze, right);
  568. parcc_float3_add(gaze, gaze, upward);
  569. parcc_float3_normalize(gaze);
  570. // Invoke the user's callback or fallback function.
  571. parcc_raycast_fn callback = context->props.raycast_function;
  572. parcc_raycast_fn fallback = parcc_raycast_plane;
  573. void* userdata = context->props.raycast_userdata;
  574. if (!callback) {
  575. callback = fallback;
  576. userdata = context;
  577. }
  578. // If the ray misses, then try the fallback function.
  579. parcc_float t;
  580. if (!callback(origin, gaze, &t, userdata)) {
  581. if (callback == fallback) {
  582. return false;
  583. }
  584. if (!fallback(origin, gaze, &t, context)) {
  585. return false;
  586. }
  587. }
  588. parcc_float3_scale(gaze, t);
  589. parcc_float3_add(result, origin, gaze);
  590. return true;
  591. }
  592. parcc_frame parcc_get_current_frame(const parcc_context* context) {
  593. parcc_frame frame;
  594. frame.mode = context->props.mode;
  595. if (context->props.mode == PARCC_MAP) {
  596. const parcc_float* origin = context->eyepos;
  597. const parcc_float* upward = context->props.home_upward;
  598. parcc_float direction[3];
  599. parcc_float3_subtract(direction, context->target, origin);
  600. parcc_float3_normalize(direction);
  601. parcc_float distance;
  602. parcc_raycast_plane(origin, direction, &distance, (void*)context);
  603. const parcc_float fov = context->props.fov_degrees * PARCC_PI / 180.0;
  604. const parcc_float half_extent = distance * tan(fov / 2);
  605. parcc_float target[3];
  606. parcc_float3_scale(direction, distance);
  607. parcc_float3_add(target, origin, direction);
  608. // Compute the tangent frame defined by the map_plane normal and the home_upward vector.
  609. parcc_float uvec[3];
  610. parcc_float vvec[3];
  611. parcc_float target_to_eye[3];
  612. parcc_float3_copy(target_to_eye, context->props.map_plane);
  613. parcc_float3_cross(uvec, upward, target_to_eye);
  614. parcc_float3_cross(vvec, target_to_eye, uvec);
  615. parcc_float3_subtract(target, target, context->props.home_target);
  616. frame.extent = half_extent * 2;
  617. frame.center[0] = parcc_float3_dot(uvec, target);
  618. frame.center[1] = parcc_float3_dot(vvec, target);
  619. }
  620. if (context->props.mode == PARCC_ORBIT) {
  621. parcc_float pivot_to_eye[3];
  622. parcc_float3_subtract(pivot_to_eye, context->eyepos, context->orbit_pivot);
  623. const parcc_float d = parcc_float3_length(pivot_to_eye);
  624. const parcc_float x = pivot_to_eye[0] / d;
  625. const parcc_float y = pivot_to_eye[1] / d;
  626. const parcc_float z = pivot_to_eye[2] / d;
  627. frame.phi = asin(y);
  628. frame.theta = atan2(x, z);
  629. frame.pivot_distance = context->orbit_flipped ? -d : d;
  630. parcc_float3_copy(frame.pivot, context->orbit_pivot);
  631. }
  632. return frame;
  633. }
  634. parcc_frame parcc_get_home_frame(const parcc_context* context) {
  635. const parcc_float width = context->props.viewport_width;
  636. const parcc_float height = context->props.viewport_height;
  637. const parcc_float aspect = width / height;
  638. parcc_frame frame;
  639. frame.mode = context->props.mode;
  640. if (frame.mode == PARCC_MAP) {
  641. const parcc_float map_width = context->props.map_extent[0] / 2;
  642. const parcc_float map_height = context->props.map_extent[1] / 2;
  643. const bool horiz = context->props.fov_orientation == PARCC_HORIZONTAL;
  644. frame.extent = horiz ? context->props.map_extent[0] : context->props.map_extent[1];
  645. frame.center[0] = 0;
  646. frame.center[1] = 0;
  647. if (context->props.map_constraint != PARCC_CONSTRAIN_FULL) {
  648. return frame;
  649. }
  650. if (horiz) {
  651. parcc_float vp_width = frame.extent / 2;
  652. parcc_float vp_height = vp_width / aspect;
  653. if (map_height < vp_height) {
  654. frame.extent = 2 * map_height * aspect;
  655. }
  656. } else {
  657. parcc_float vp_height = frame.extent / 2;
  658. parcc_float vp_width = vp_height * aspect;
  659. if (map_width < vp_width) {
  660. frame.extent = 2 * map_width / aspect;
  661. }
  662. }
  663. }
  664. if (frame.mode == PARCC_ORBIT) {
  665. frame.theta = frame.phi = 0;
  666. parcc_float3_copy(frame.pivot, context->props.home_target);
  667. frame.pivot_distance = parcc_float3_length(context->props.home_vector);
  668. }
  669. return frame;
  670. }
  671. void parcc_goto_frame(parcc_context* context, parcc_frame frame) {
  672. if (context->props.mode == PARCC_MAP) {
  673. const parcc_float* upward = context->props.home_upward;
  674. const parcc_float half_extent = frame.extent / 2.0;
  675. const parcc_float fov = context->props.fov_degrees * PARCC_PI / 180.0;
  676. const parcc_float distance = half_extent / tan(fov / 2);
  677. // Compute the tangent frame defined by the map_plane normal and the home_upward vector.
  678. parcc_float uvec[3];
  679. parcc_float vvec[3];
  680. parcc_float target_to_eye[3];
  681. parcc_float3_copy(target_to_eye, context->props.map_plane);
  682. parcc_float3_cross(uvec, upward, target_to_eye);
  683. parcc_float3_cross(vvec, target_to_eye, uvec);
  684. // Scale the U and V components by the frame coordinate.
  685. parcc_float3_scale(uvec, frame.center[0]);
  686. parcc_float3_scale(vvec, frame.center[1]);
  687. // Obtain the new target position by adding U and V to home_target.
  688. parcc_float3_copy(context->target, context->props.home_target);
  689. parcc_float3_add(context->target, context->target, uvec);
  690. parcc_float3_add(context->target, context->target, vvec);
  691. // Obtain the new eye position by adding the scaled plane normal to the new target
  692. // position.
  693. parcc_float3_scale(target_to_eye, distance);
  694. parcc_float3_add(context->eyepos, context->target, target_to_eye);
  695. }
  696. if (context->props.mode == PARCC_ORBIT) {
  697. parcc_float3_copy(context->orbit_pivot, frame.pivot);
  698. const parcc_float x = sin(frame.theta) * cos(frame.phi);
  699. const parcc_float y = sin(frame.phi);
  700. const parcc_float z = cos(frame.theta) * cos(frame.phi);
  701. parcc_float3_set(context->eyepos, x, y, z);
  702. parcc_float3_scale(context->eyepos, fabs(frame.pivot_distance));
  703. parcc_float3_add(context->eyepos, context->eyepos, context->orbit_pivot);
  704. context->orbit_flipped = frame.pivot_distance < 0;
  705. parcc_float3_set(context->target, x, y, z);
  706. parcc_float3_scale(context->target, context->orbit_flipped ? 1.0 : -1.0);
  707. parcc_float3_add(context->target, context->target, context->eyepos);
  708. }
  709. }
  710. parcc_frame parcc_interpolate_frames(parcc_frame a, parcc_frame b, double t) {
  711. parcc_frame frame;
  712. if (a.mode == PARCC_MAP && b.mode == PARCC_MAP) {
  713. const double rho = sqrt(2.0);
  714. const double rho2 = 2, rho4 = 4;
  715. const double ux0 = a.center[0], uy0 = a.center[1], w0 = a.extent;
  716. const double ux1 = b.center[0], uy1 = b.center[1], w1 = b.extent;
  717. const double dx = ux1 - ux0, dy = uy1 - uy0, d2 = dx * dx + dy * dy, d1 = sqrt(d2);
  718. const double b0 = (w1 * w1 - w0 * w0 + rho4 * d2) / (2.0 * w0 * rho2 * d1);
  719. const double b1 = (w1 * w1 - w0 * w0 - rho4 * d2) / (2.0 * w1 * rho2 * d1);
  720. const double r0 = log(sqrt(b0 * b0 + 1.0) - b0);
  721. const double r1 = log(sqrt(b1 * b1 + 1) - b1);
  722. const double dr = r1 - r0;
  723. const int valid_dr = (dr == dr) && dr != 0;
  724. const double S = (valid_dr ? dr : log(w1 / w0)) / rho;
  725. const double s = t * S;
  726. if (valid_dr) {
  727. const double coshr0 = cosh(r0);
  728. const double u = w0 / (rho2 * d1) * (coshr0 * tanh(rho * s + r0) - sinh(r0));
  729. frame.center[0] = ux0 + u * dx;
  730. frame.center[1] = uy0 + u * dy;
  731. frame.extent = w0 * coshr0 / cosh(rho * s + r0);
  732. return frame;
  733. }
  734. frame.center[0] = ux0 + t * dx;
  735. frame.center[1] = uy0 + t * dy;
  736. frame.extent = w0 * exp(rho * s);
  737. } else if (a.mode == PARCC_ORBIT && b.mode == PARCC_ORBIT) {
  738. frame.phi = parcc_float_lerp(a.phi, b.phi, t);
  739. frame.theta = parcc_float_lerp(a.theta, b.theta, t);
  740. frame.pivot_distance = parcc_float_lerp(a.pivot_distance, b.pivot_distance, t);
  741. parcc_float3_lerp(frame.pivot, a.pivot, b.pivot, t);
  742. } else {
  743. // Cross-mode interpolation is not implemented.
  744. frame = b;
  745. }
  746. return frame;
  747. }
  748. double parcc_get_interpolation_duration(parcc_frame a, parcc_frame b) {
  749. if (a.mode == PARCC_MAP && b.mode == PARCC_MAP) {
  750. const double rho = sqrt(2.0);
  751. const double rho2 = 2, rho4 = 4;
  752. const double ux0 = a.center[0], uy0 = a.center[1], w0 = a.extent;
  753. const double ux1 = b.center[0], uy1 = b.center[1], w1 = b.extent;
  754. const double dx = ux1 - ux0, dy = uy1 - uy0, d2 = dx * dx + dy * dy, d1 = sqrt(d2);
  755. const double b0 = (w1 * w1 - w0 * w0 + rho4 * d2) / (2.0 * w0 * rho2 * d1);
  756. const double b1 = (w1 * w1 - w0 * w0 - rho4 * d2) / (2.0 * w1 * rho2 * d1);
  757. const double r0 = log(sqrt(b0 * b0 + 1.0) - b0);
  758. const double r1 = log(sqrt(b1 * b1 + 1) - b1);
  759. const double dr = r1 - r0;
  760. const int valid_dr = (dr == dr) && dr != 0;
  761. const double S = (valid_dr ? dr : log(w1 / w0)) / rho;
  762. return fabs(S);
  763. } else if (a.mode == PARCC_ORBIT && b.mode == PARCC_ORBIT) {
  764. return 1;
  765. } else {
  766. // Cross-mode interpolation is not implemented.
  767. }
  768. return 0;
  769. }
  770. static bool parcc_raycast_plane(const parcc_float origin[3], const parcc_float dir[3],
  771. parcc_float* t, void* userdata) {
  772. parcc_context* context = (parcc_context*)userdata;
  773. const parcc_float* plane = context->props.map_plane;
  774. parcc_float n[3] = {plane[0], plane[1], plane[2]};
  775. parcc_float p0[3] = {plane[0], plane[1], plane[2]};
  776. parcc_float3_scale(p0, plane[3]);
  777. const parcc_float denom = -parcc_float3_dot(n, dir);
  778. if (denom > 1e-6 || denom < -1e-6) {
  779. parcc_float p0l0[3];
  780. parcc_float3_subtract(p0l0, p0, origin);
  781. *t = parcc_float3_dot(p0l0, n) / -denom;
  782. return *t >= 0;
  783. }
  784. return false;
  785. }
  786. // Finds the point on the frustum's far plane that a pick ray intersects.
  787. static void parcc_get_ray_far(parcc_context* context, int winx, int winy, parcc_float result[3]) {
  788. const parcc_float width = context->props.viewport_width;
  789. const parcc_float height = context->props.viewport_height;
  790. const parcc_float fov = context->props.fov_degrees * PARCC_PI / 180.0;
  791. const bool vertical_fov = context->props.fov_orientation == PARCC_VERTICAL;
  792. const parcc_float* origin = context->eyepos;
  793. parcc_float gaze[3];
  794. parcc_float3_subtract(gaze, context->target, origin);
  795. parcc_float3_normalize(gaze);
  796. parcc_float right[3];
  797. parcc_float3_cross(right, gaze, context->props.home_upward);
  798. parcc_float3_normalize(right);
  799. parcc_float upward[3];
  800. parcc_float3_cross(upward, right, gaze);
  801. parcc_float3_normalize(upward);
  802. // Remap the grid coordinate into [-1, +1] and shift it to the pixel center.
  803. const parcc_float u = 2.0 * (winx + 0.5) / width - 1.0;
  804. const parcc_float v = 2.0 * (winy + 0.5) / height - 1.0;
  805. // Compute the tangent of the field-of-view angle as well as the aspect ratio.
  806. const parcc_float tangent = tan(fov / 2.0);
  807. const parcc_float aspect = width / height;
  808. // Adjust the gaze so it goes through the pixel of interest rather than the grid center.
  809. if (vertical_fov) {
  810. parcc_float3_scale(right, tangent * u * aspect);
  811. parcc_float3_scale(upward, tangent * v);
  812. } else {
  813. parcc_float3_scale(right, tangent * u);
  814. parcc_float3_scale(upward, tangent * v / aspect);
  815. }
  816. parcc_float3_add(gaze, gaze, right);
  817. parcc_float3_add(gaze, gaze, upward);
  818. parcc_float3_scale(gaze, context->props.far_plane);
  819. parcc_float3_add(result, origin, gaze);
  820. }
  821. static void parcc_move_with_constraints(parcc_context* context, const parcc_float eyepos[3],
  822. const parcc_float target[3]) {
  823. const parcc_constraint constraint = context->props.map_constraint;
  824. const parcc_float width = context->props.viewport_width;
  825. const parcc_float height = context->props.viewport_height;
  826. const parcc_float aspect = width / height;
  827. const parcc_float map_width = context->props.map_extent[0] / 2;
  828. const parcc_float map_height = context->props.map_extent[1] / 2;
  829. const parcc_frame home = parcc_get_home_frame(context);
  830. const parcc_frame previous_frame = parcc_get_current_frame(context);
  831. const parcc_float fov = context->props.fov_degrees * PARCC_PI / 180.0;
  832. const parcc_float min_extent = 2.0 * context->props.map_min_distance * tan(fov / 2);
  833. parcc_float3_copy(context->eyepos, eyepos);
  834. parcc_float3_copy(context->target, target);
  835. parcc_frame frame = parcc_get_current_frame(context);
  836. if (frame.extent < min_extent) {
  837. frame.extent = min_extent;
  838. frame.center[0] = previous_frame.center[0];
  839. frame.center[1] = previous_frame.center[1];
  840. }
  841. if (constraint == PARCC_CONSTRAIN_NONE) {
  842. parcc_goto_frame(context, frame);
  843. return;
  844. }
  845. parcc_float x = frame.center[0];
  846. parcc_float y = frame.center[1];
  847. if (context->props.fov_orientation == PARCC_HORIZONTAL) {
  848. parcc_float vp_width = frame.extent / 2;
  849. parcc_float vp_height = vp_width / aspect;
  850. if (map_width < vp_width) {
  851. frame.extent = home.extent;
  852. vp_width = frame.extent / 2;
  853. vp_height = vp_width / aspect;
  854. x = 0;
  855. y = previous_frame.center[1];
  856. }
  857. x = PARCC_CLAMP(x, -map_width + vp_width, map_width - vp_width);
  858. if (map_height < vp_height) {
  859. if (context->props.map_constraint == PARCC_CONSTRAIN_FULL) {
  860. frame.extent = 2 * map_height * aspect;
  861. vp_width = frame.extent / 2;
  862. vp_height = vp_width / aspect;
  863. x = previous_frame.center[0];
  864. x = PARCC_CLAMP(x, -map_width + vp_width, map_width - vp_width);
  865. y = PARCC_CLAMP(y, -map_height + vp_height, map_height - vp_height);
  866. } else {
  867. y = PARCC_CLAMP(y, -vp_height + map_height, vp_height - map_height);
  868. }
  869. } else {
  870. y = PARCC_CLAMP(y, -map_height + vp_height, map_height - vp_height);
  871. }
  872. } else {
  873. parcc_float vp_height = frame.extent / 2;
  874. parcc_float vp_width = vp_height * aspect;
  875. if (map_height < vp_height) {
  876. frame.extent = home.extent;
  877. vp_height = frame.extent / 2;
  878. vp_width = vp_height * aspect;
  879. y = 0;
  880. x = previous_frame.center[0];
  881. }
  882. y = PARCC_CLAMP(y, -map_height + vp_height, map_height - vp_height);
  883. if (map_width < vp_width) {
  884. if (context->props.map_constraint == PARCC_CONSTRAIN_FULL) {
  885. frame.extent = 2 * map_width / aspect;
  886. vp_height = frame.extent / 2;
  887. vp_width = vp_height * aspect;
  888. y = previous_frame.center[1];
  889. y = PARCC_CLAMP(y, -map_height + vp_height, map_height - vp_height);
  890. x = PARCC_CLAMP(x, -map_width + vp_width, map_width - vp_width);
  891. } else {
  892. x = PARCC_CLAMP(x, -vp_width + map_width, vp_width - map_width);
  893. }
  894. } else {
  895. x = PARCC_CLAMP(x, -map_width + vp_width, map_width - vp_width);
  896. }
  897. }
  898. frame.center[0] = x;
  899. frame.center[1] = y;
  900. parcc_goto_frame(context, frame);
  901. }
  902. #endif // PAR_CAMERA_CONTROL_IMPLEMENTATION
  903. #endif // PAR_CAMERA_CONTROL_H
  904. // par_camera_control is distributed under the MIT license:
  905. //
  906. // Copyright (c) 2019 Philip Rideout
  907. //
  908. // Permission is hereby granted, free of charge, to any person obtaining a copy
  909. // of this software and associated documentation files (the "Software"), to deal
  910. // in the Software without restriction, including without limitation the rights
  911. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  912. // copies of the Software, and to permit persons to whom the Software is
  913. // furnished to do so, subject to the following conditions:
  914. //
  915. // The above copyright notice and this permission notice shall be included in
  916. // all copies or substantial portions of the Software.
  917. //
  918. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  919. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  920. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  921. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  922. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  923. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  924. // SOFTWARE.