libc3: remerged
[simavr] / examples / shared / libc3 / src / c3arcball.h
1 /*
2         c3arcball.h
3
4         Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
5         Copyright (c) 1998 Paul Rademacher
6     Feb 1998, Paul Rademacher (rademach@cs.unc.edu)
7     Oct 2003, Nigel Stewart - GLUI Code Cleaning
8
9         This file is part of libc3.
10
11         libc3 is free software: you can redistribute it and/or modify
12         it under the terms of the GNU General Public License as published by
13         the Free Software Foundation, either version 3 of the License, or
14         (at your option) any later version.
15
16         libc3 is distributed in the hope that it will be useful,
17         but WITHOUT ANY WARRANTY; without even the implied warranty of
18         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19         GNU General Public License for more details.
20
21         You should have received a copy of the GNU General Public License
22         along with libc3.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25 /*
26         Arcball, as described by Ken
27         Shoemake in Graphics Gems IV.
28         This class takes as input mouse events (mouse down, mouse drag,
29         mouse up), and creates the appropriate quaternions and 4x4 matrices
30         to represent the rotation given by the mouse.
31
32         This class is used as follows:
33         - initialize [either in the constructor or with set_params()], the
34         center position (x,y) of the arcball on the screen, and the radius
35         - on mouse down, call mouse_down(x,y) with the mouse position
36         - as the mouse is dragged, repeatedly call mouse_motion() with the
37         current x and y positions.  One can optionally pass in the current
38         state of the SHIFT, ALT, and CONTROL keys (passing zero if keys
39         are not pressed, non-zero otherwise), which constrains
40         the rotation to certain axes (X for CONTROL, Y for ALT).
41         - when the mouse button is released, call mouse_up()
42
43         Axis constraints can also be explicitly set with the
44         set_constraints() function.
45
46         The current rotation is stored in the 4x4 float matrix 'rot'.
47         It is also stored in the quaternion 'q_now'.
48  */
49
50 #ifndef __C3ARCBALL_H___
51 #define __C3ARCBALL_H___
52
53 #include "c3quaternion.h"
54
55 typedef struct c3arcball {
56     int         is_mouse_down : 1,  /* true for down, false for up */
57                 is_spinning : 1,
58                 constraint_x : 1,
59                 constraint_y : 1,
60                 zero_increment : 1;
61     c3quat  q_now, q_down, q_drag, q_increment;
62     c3vec2  down_pt;
63     c3mat4  rot, rot_increment;
64     c3mat4  *rot_ptr;
65
66     c3vec2  center;
67     c3f         radius, damp_factor;
68 } c3arcball, *c3arcballp;
69
70 void
71 c3arcball_init(
72                 c3arcballp a );
73 void
74 c3arcball_init_mat4(
75                 c3arcballp a,
76                 c3mat4p mtx );
77 void
78 c3arcball_init_center(
79                 c3arcballp a,
80                 const c3vec2 center,
81                 c3f radius );
82 void
83 c3arcball_set_params(
84                 c3arcballp a,
85                 const c3vec2 center,
86                 c3f radius);
87 c3vec3
88 c3arcball_mouse_to_sphere(
89                 c3arcballp a,
90                 const c3vec2 p);
91 c3vec3
92 c3arcball_constrain_vector(
93                 const c3vec3 vector,
94                 const c3vec3 axis);
95 void
96 c3arcball_mouse_down(
97                 c3arcballp a,
98                 int x,
99                 int y);
100 void
101 c3arcball_mouse_up(
102                 c3arcballp a);
103
104 void
105 c3arcball_mouse_motion(
106                 c3arcballp a,
107                 int x,
108                 int y,
109                 int shift,
110                 int ctrl,
111                 int alt);
112 void
113 c3arcball_set_constraints(
114                 c3arcballp a,
115                 bool _constraint_x,
116                 bool _constraint_y);
117 void
118 c3arcball_idle(
119                 c3arcballp a);
120 void
121 c3arcball_set_damping(
122                 c3arcballp a,
123                 c3f d);
124
125 #endif /* __C3ARCBALL_H___ */