20CamTrackball::CamTrackball (
void)
23 this->tb_radius = 1.0f;
34 bool is_handled =
false;
40 this->rot_mouse_x =
event.x;
41 this->rot_mouse_y =
event.y;
42 this->rot_tb_tocam = this->tb_tocam;
43 this->rot_tb_upvec = this->tb_upvec;
47 this->zoom_mouse_y =
event.y;
48 this->zoom_tb_radius = this->tb_radius;
54 this->tb_center = center;
62 if (event.
x == this->rot_mouse_x && event.
y == this->rot_mouse_y)
64 this->tb_tocam = this->rot_tb_tocam;
65 this->tb_upvec = this->rot_tb_upvec;
69 this->handle_tb_rotation(event.
x, event.
y);
76 int mouse_diff = this->zoom_mouse_y -
event.y;
77 float zoom_speed = this->zoom_tb_radius / 100.0f;
78 float cam_diff = (float)mouse_diff * zoom_speed;
79 float new_radius = this->zoom_tb_radius + cam_diff;
80 this->tb_radius =
math::clamp(new_radius, this->cam->z_near, this->cam->z_far);
86 this->tb_radius = this->tb_radius + this->tb_radius / 10.0f;
87 this->tb_radius = std::min(this->cam->z_far, this->tb_radius);
92 this->tb_radius = this->tb_radius - this->tb_radius / 10.0f;
93 this->tb_radius = std::max(this->cam->z_near, this->tb_radius);
111CamTrackball::handle_tb_rotation (
int x,
int y)
115 (this->rot_mouse_x, this->rot_mouse_y);
120 float angle = std::acos(bn_now.
dot(bn_start));
126 math::Vec3f campos = this->tb_center + this->rot_tb_tocam * this->tb_radius;
129 campos, viewdir, this->rot_tb_upvec);
130 axis = cam_to_world.
mult(axis, 0.0f);
134 math::Matrix3f rot = matrix_rotation_from_axis_angle(axis, angle);
135 this->tb_tocam = rot * this->rot_tb_tocam;
136 this->tb_upvec = rot * this->rot_tb_upvec;
142CamTrackball::get_center (
int x,
int y)
148 int const patch_size = 9;
149 int const patch_halfsize = patch_size / 2;
150 int const screen_width = this->cam->width;
151 int const screen_height = this->cam->height;
152 int const center_x = x;
153 int const center_y = y;
157 while (radius <= patch_halfsize)
159 if (x >= 0 && x < screen_width && y > 0 && y < screen_height)
160 glReadPixels(x, screen_height - y - 1, 1, 1,
161 GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
167 if (x > center_x + radius)
173 if (y <= center_y - radius)
178 if (x <= center_x - radius)
183 if (y >= center_y + radius)
195 float const fx =
static_cast<float>(x);
196 float const fy =
static_cast<float>(y);
197 float const fw =
static_cast<float>(this->cam->width);
198 float const fh =
static_cast<float>(this->cam->height);
201 float dist = (this->cam->z_far * this->cam->z_near)
202 / ((this->cam->z_near - this->cam->z_far) * depth + this->cam->z_far);
207 math::Vec3f pnp((2.0f * fx / (fw - 1.0f) - 1.0f) * this->cam->right,
208 (1.0f - 2.0f * fy / (fh - 1.0f)) * this->cam->top,
210 float cosangle = pnp.normalized()[2];
216 1.0f - 2.0f * fy / (fh - 1.0f), 0.0f);
218 ray = this->cam->inv_proj.mult(ray, 1.0f);
220 ray = ray.normalized() * dist;
222 ray = this->cam->inv_view.mult(ray, 0.0f);
224 return this->cam->pos + ray;
230CamTrackball::get_ball_normal (
int x,
int y)
234 sn[0] = 2.0f * (float)x / (
float)(this->cam->width - 1) - 1.0f;
235 sn[1] = 1.0f - 2.0f * (float)y / (
float)(this->cam->height - 1);
236 float z2 = 1.0f - sn[0] * sn[0] - sn[1] * sn[1];
237 sn[2] = z2 > 0.0f ? std::sqrt(z2) : 0.0f;
239 return sn.normalize();
248 this->tb_radius = (center - lookat).norm();
249 this->tb_center = lookat;
250 this->tb_tocam = (center - lookat).normalized();
251 this->tb_upvec = upvec;
Matrix class for arbitrary dimensions and types.
Matrix< T, N, U > mult(Matrix< T, M, U > const &rhs) const
Matrix with matrix multiplication.
Vector class for arbitrary dimensions and types.
Vector< T, N > cross(Vector< T, N > const &other) const
Cross product between this and another vector.
T dot(Vector< T, N > const &other) const
Dot (or scalar) product between self and another vector.
Vector< T, N > & normalize(void)
Normalizes self and returns reference to self.
T const & clamp(T const &v, T const &min=T(0), T const &max=T(1))
Returns value 'v' clamped to the interval specified by 'min' and 'max'.
Matrix< T, 4, 4 > matrix_inverse_viewtrans(Vector< T, 3 > const &campos, Vector< T, 3 > const &viewdir, Vector< T, 3 > const &upvec)
Creates an inverse view transformation matrix.
#define OGL_NAMESPACE_END
#define OGL_NAMESPACE_BEGIN
MouseEventType type
Type of event.
MouseButton button
Button that caused the event.
int button_mask
Button state when event was generated.