diff --git a/wrap/gui/GUI/camera.h b/wrap/gui/GUI/camera.h new file mode 100644 index 00000000..2952bd11 --- /dev/null +++ b/wrap/gui/GUI/camera.h @@ -0,0 +1,72 @@ +#ifndef CAMERA_H +#define CAMERA_H + +#include +#include + +namespace vcg { + +template class ViewGL { +public: + void GetView(); + void SetView(); + Point3 Project(const Point3 &p) const; + Point3 UnProject(const Point3 &p) const; + Point3 ViewPoint(); + + Matrix44 proj; + Matrix44 model; + Matrix44 matrix; + Matrix44 inverse; + int viewport[4]; +}; + +template void Camera::GetView() { + glGetDoublev(GL_PROJECTION_MATRIX, (double *)&proj); + glGetDoublev(GL_MODELVIEW_MATRIX, (double *)&model); + glGetIntegerv(GL_VIEWPORT, viewport); + + proj.Transpose(); + model.Transpose(); + + matrix.Import(proj * model); + inverse = matrix; + Invert(inverse); +} + +template Point3 Camera::ViewPoint() { + return inverse * Point3(0, 0, 0); + /*Matrix44d model(model_matrix); + model.Invert(); + Point3d view = model * Point3d(0, 0, 0); + return Point3(view[0], view[1], view[2]); */ +} + +template Point3 Camera::Project(const Point3 &p) const { + Point3 r; + r = matrix * p; + r[0] = (r[0]+1)*(viewport[2]/2.0)+viewport[0]; + r[1] =(r[1]+1)*(viewport[3]/2.0)+viewport[1]; + return r; + /*double r[3]; + gluProject(p[0], p[1], p[2], model_matrix, proj_matrix, viewport, &r[0], &r[1], &r[2]); + return Point3((T)r[0], (T)r[1], (T)r[2]);*/ +} + +template Point3 Camera::UnProject(const Point3 &p) const { + Point3 s; + s[0] = (p[0]- viewport[0])/ (viewport[2]/2.0) - 1; + s[1] = (p[1]- viewport[1])/ (viewport[3]/2.0) - 1; + s[2] = p[2]; + s[1] = -s[1]; // pezza aggiunta per il tan2.... ????????????? + s = inverse * s; + return s; + /*double r[3]; + gluUnProject(p[0], p[1], p[2], model_matrix, proj_matrix, viewport, &r[0], &r[1], &r[2]); + return Point3((T)r[0], (T)r[1], (T)r[2]);*/ +} + + +}//namespace + +#endif \ No newline at end of file diff --git a/wrap/gui/GUI/frustum.h b/wrap/gui/GUI/frustum.h new file mode 100644 index 00000000..4741f9db --- /dev/null +++ b/wrap/gui/GUI/frustum.h @@ -0,0 +1,94 @@ +#ifndef FRUSTUM_H +#define FRUSTUM_H + +#include +#include +#include + + +namespace vcg { + + template class Frustum: public Camera { +public: + void GetView(); + bool IsOutside(Point3 &point); + bool IsOutside(Point3 &point, T radius); + T Distance(Point3 &point, int plane); + Point3 ViewPoint(); + +protected: + T resolution; + Plane3 planes[6]; + Point3 view_point; +}; + + +//Implementation +template Point3 Frustum::ViewPoint() { + return view_point; +} + +template bool Frustum::IsOutside(Point3 &point) { + Point3 r = Project(point); + if(r[0] < viewport[0] || r[0] > viewport[0]+viewport[2] || + r[1] < viewport[1] || r[1] > viewport[1]+viewport[3]) + return true; + return false; +} + +template bool Frustum::IsOutside(Point3 &point, T radius) { + for(int i = 0; i < 4; i++) { + T dist = Distance(point, i); + if(dist < -radius) + return true; + } + return false; +} + +template T Frustum::Distance(Point3 &point, int plane) { + return Distance(point, planes[plane]); +} + +template void Frustum::GetView() { + Camera::GetView(); + + Point3d NE, SE, SW, NW, ne, se, sw, nw; + int t = viewport[1] + viewport[3]; + int b = viewport[1]; + int r = viewport[0] + viewport[2]; + int l = viewport[0]; + + Point3d NE, SE, SW, NW, ne, se, sw, nw; + gluUnProject(l, b, 0, model_matrix, proj_matrix, viewport, &nw[0], &nw[1], &nw[2]); + gluUnProject(l, t, 0, model_matrix, proj_matrix, viewport, &sw[0], &sw[1], &sw[2]); + gluUnProject(r, b, 0, model_matrix, proj_matrix, viewport, &ne[0], &ne[1], &ne[2]); + gluUnProject(r, t, 0, model_matrix, proj_matrix, viewport, &se[0], &se[1], &se[2]); + gluUnProject(l, b, 1, model_matrix, proj_matrix, viewport, &NW[0], &NW[1], &NW[2]); + gluUnProject(l, t, 1, model_matrix, proj_matrix, viewport, &SW[0], &SW[1], &SW[2]); + gluUnProject(r, b, 1, model_matrix, proj_matrix, viewport, &NE[0], &NE[1], &NE[2]); + gluUnProject(r, t, 1, model_matrix, proj_matrix, viewport, &SE[0], &SE[1], &SE[2]); + + view_point = Camera::ViewPoint(); + + planes[0].init(view_point, Point3().Import(nw), Point3().Import(ne)); + planes[1].init(view_point, Point3().Import(ne), Point3().Import(se)); + planes[2].init(view_point, Point3().Import(se), Point3().Import(sw)); + planes[3].init(view_point, Point3().Import(sw), Point3().Import(nw)); + planes[4].init(Point3().Import(se), Point3().Import(sw), Point3().Import(nw)); + planes[5].init(Point3().Import(SW), Point3().Import(SE), Point3().Import(NE)); + + for(int i = 0; i < 6; i++) + planes[i].Normalize(); + + //calcoliamo la risoluzione: dimenzione di un pixel a distanza 1 dal view_point + resolution = (T)((ne + NE) - (nw + NW)).Norm() /( viewport[2] * ((ne + NE) - (nw + NW)).Norm()); +} + +}//namespace + +#endif + + + + + diff --git a/wrap/gui/GUI/trackball.h b/wrap/gui/GUI/trackball.h new file mode 100644 index 00000000..79c9bdb0 --- /dev/null +++ b/wrap/gui/GUI/trackball.h @@ -0,0 +1,75 @@ +#ifndef TRACKBALL_H +#define TRACKBALL_H + +#include + +namespace vcg { + +class Trackball { +public: + Similarf track; + Similarf local; + + Trackball(); + void SetIdentity(); + void SetPosition(const Similarf &local, int millisec = 0); + + //operating + void GetView(); + void Apply(); + void Draw(); + void Reset(); + + //interface + void MouseDown(int x, int y, Button button); + void MouseMove(int x, int y); + void MouseUp(int x, int y, Button button); + void MouseWheel(Button notch); + void ButtonUp(Button button); + void ButtonDown(Button button); + + //spinning interface + void SetSpinnable(bool on); + bool IsSpinnable(); + bool SetSpinning(Quaternionf &spin); + void StopSpinning(); + bool IsSpinning(); + + //interfaccia navigation: + void Back(); + void Forward(); + void Home(); + void HistorySize(int lenght); + + enum { LOCAL, VIEW, SCREEN }; + + enum { + ROTATE = 0, ROTATE_G = 1, //really makes sense only in VIEW system + ROTATE_X = 2, ROTATE_Y = 3, ROTATE_Z = 4, // Axis Constrained Rotation + DRAG_X = 5, DRAG_Y = 6, DRAG_Z = 7, // Drag constrained to an axis (trackball axis) + DRAG_XY = 8, DRAG_YZ = 9, DRAG_XZ = 10, // Drag constrained to a plane + SCALE = 11, //scale respect to center of trackball + NONE = 12 //disable trackball + }; + enum Button { BUTTON_LEFT = 1, BUTTON_MIDDLE = 2, BUTTON_RIGHT = 4, WHEEL = 8, + KEY_SHIFT = 16, KEY_CTRL = 32, KEY_ALT = 64, HANDLE = 128 }; +protected: + Camera camera; + + TrackMode *current; + + Similarf &last(); + int last_x, last_y; + bool dragging; + int button_mask; + + Quaternionf spin; + bool spinnable; + bool spinning; + + std::list history; +}; + +}//namespace + +#endif \ No newline at end of file diff --git a/wrap/gui/GUI/trackmode.h b/wrap/gui/GUI/trackmode.h new file mode 100644 index 00000000..4572cd12 --- /dev/null +++ b/wrap/gui/GUI/trackmode.h @@ -0,0 +1,36 @@ +#ifndef TRACKMODE_H +#define TRACKMODE_H + +namespace vcg { + +class TrackMode { +public: +}; + +class SphereMode: public TrackMode { +public: +}; + +class GravityMode: public TrackMode { +public: +}; + +class CylinderMode: public TrackMode { +public: +}; + +class PlaneMode: public TrackMode { +public: +}; + +class LineMode: public TrackMode { +public: +}; + +class ScaleMode: public TrackMode { +public: +}; + +}//namespace + +#endif \ No newline at end of file