Manipulators.h

Go to the documentation of this file.
00001 #ifndef MANIPULATORS_H_
00002 #define MANIPULATORS_H_
00003 
00004 /* greebo: This file contains the manipulator classes like
00005  *
00006  * - Translate Maniplator
00007  * - Rotate Manipulator
00008  * - Scale Manipulator
00009  *
00010  * that derive from the abstract base class Manipulator
00011  */
00012 
00013 #include "renderable.h"
00014 #include "pivot.h"
00015 #include "../camera/view.h"
00016 #include "math/matrix.h"
00017 #include "selectionlib.h"
00018 #include "Manipulatables.h"
00019 
00020 struct Pivot2World
00021 {
00022         Matrix4 m_worldSpace;
00023         Matrix4 m_viewpointSpace;
00024         Matrix4 m_viewplaneSpace;
00025         Vector3 m_axis_screen;
00026 
00027         void update (const Matrix4& pivot2world, const Matrix4& modelview, const Matrix4& projection,
00028                 const Matrix4& viewport)
00029         {
00030             Pivot2World_worldSpace(m_worldSpace, pivot2world, modelview, projection, viewport);
00031             Pivot2World_viewpointSpace(m_viewpointSpace, m_axis_screen, pivot2world, modelview, projection, viewport);
00032             Pivot2World_viewplaneSpace(m_viewplaneSpace, pivot2world, modelview, projection, viewport);
00033         }
00034 };
00035 
00036 // The abstract base class for a manipulator
00037 class Manipulator
00038 {
00039     public:
00040         virtual Manipulatable* GetManipulatable () = 0;
00041         virtual void testSelect (const View& view, const Matrix4& pivot2world)
00042         {
00043         }
00044         // This function is responsible for bringing the visual representation
00045         // of this manipulator onto the screen
00046         virtual void render (Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world)
00047         {
00048         }
00049         virtual void setSelected (bool select) = 0;
00050         virtual bool isSelected () const = 0;
00051         virtual ~Manipulator ();
00052 };
00053 
00054 // =======================================================================================
00055 
00056 /*  The Manipulator for Rotations
00057  */
00058 class RotateManipulator: public Manipulator
00059 {
00060 
00061         // helper class for rendering a circle
00062         struct RenderableCircle: public OpenGLRenderable
00063         {
00064                 Array<PointVertex> m_vertices;
00065 
00066                 RenderableCircle (std::size_t size) :
00067                     m_vertices(size)
00068                 {
00069                 }
00070                 void render (RenderStateFlags state) const
00071                 {
00072                     glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &m_vertices.data()->colour);
00073                     glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &m_vertices.data()->vertex);
00074                     glDrawArrays(GL_LINE_LOOP, 0, GLsizei(m_vertices.size()));
00075                 }
00076                 void setColour (const Colour4b& colour)
00077                 {
00078                     for (Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i) {
00079                         (*i).colour = colour;
00080                     }
00081                 }
00082         };
00083 
00084         // helper class for rendering a semi-circle
00085         struct RenderableSemiCircle: public OpenGLRenderable
00086         {
00087                 Array<PointVertex> m_vertices;
00088 
00089                 RenderableSemiCircle (std::size_t size) :
00090                     m_vertices(size)
00091                 {
00092                 }
00093                 void render (RenderStateFlags state) const
00094                 {
00095                     glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &m_vertices.data()->colour);
00096                     glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &m_vertices.data()->vertex);
00097                     glDrawArrays(GL_LINE_STRIP, 0, GLsizei(m_vertices.size()));
00098                 }
00099                 void setColour (const Colour4b& colour)
00100                 {
00101                     for (Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i) {
00102                         (*i).colour = colour;
00103                     }
00104                 }
00105         };
00106 
00107         RotateFree m_free;
00108         RotateAxis m_axis;
00109         Vector3 m_axis_screen;
00110         RenderableSemiCircle m_circle_x;
00111         RenderableSemiCircle m_circle_y;
00112         RenderableSemiCircle m_circle_z;
00113         RenderableCircle m_circle_screen;
00114         RenderableCircle m_circle_sphere;
00115         SelectableBool m_selectable_x;
00116         SelectableBool m_selectable_y;
00117         SelectableBool m_selectable_z;
00118         SelectableBool m_selectable_screen;
00119         SelectableBool m_selectable_sphere;
00120         Pivot2World m_pivot;
00121         Matrix4 m_local2world_x;
00122         Matrix4 m_local2world_y;
00123         Matrix4 m_local2world_z;
00124         bool m_circle_x_visible;
00125         bool m_circle_y_visible;
00126         bool m_circle_z_visible;
00127 
00128     public:
00129         static Shader* m_state_outer;
00130 
00131         // Constructor
00132         RotateManipulator (Rotatable& rotatable, std::size_t segments, float radius);
00133 
00134         void UpdateColours ();
00135         void updateCircleTransforms ();
00136 
00137         void render (Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world);
00138 
00139         void testSelect (const View& view, const Matrix4& pivot2world);
00140 
00141         Manipulatable* GetManipulatable ();
00142 
00143         void setSelected (bool select);
00144         bool isSelected () const;
00145 };
00146 
00147 // =======================================================================================
00148 
00149 /* The Manipulator for translation operations
00150  */
00151 class TranslateManipulator: public Manipulator
00152 {
00153         // Helper class
00154         struct RenderableArrowLine: public OpenGLRenderable
00155         {
00156                 PointVertex m_line[2];
00157 
00158                 RenderableArrowLine ()
00159                 {
00160                 }
00161                 void render (RenderStateFlags state) const
00162                 {
00163                     glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &m_line[0].colour);
00164                     glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &m_line[0].vertex);
00165                     glDrawArrays(GL_LINES, 0, 2);
00166                 }
00167                 void setColour (const Colour4b& colour)
00168                 {
00169                     m_line[0].colour = colour;
00170                     m_line[1].colour = colour;
00171                 }
00172         };
00173 
00174         // Helper class
00175         struct RenderableArrowHead: public OpenGLRenderable
00176         {
00177                 Array<FlatShadedVertex> m_vertices;
00178 
00179                 RenderableArrowHead (std::size_t size) :
00180                     m_vertices(size)
00181                 {
00182                 }
00183                 void render (RenderStateFlags state) const
00184                 {
00185                     glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(FlatShadedVertex), &m_vertices.data()->colour);
00186                     glVertexPointer(3, GL_FLOAT, sizeof(FlatShadedVertex), &m_vertices.data()->vertex);
00187                     glNormalPointer(GL_FLOAT, sizeof(FlatShadedVertex), &m_vertices.data()->normal);
00188                     glDrawArrays(GL_TRIANGLES, 0, GLsizei(m_vertices.size()));
00189                 }
00190                 void setColour (const Colour4b& colour)
00191                 {
00192                     for (Array<FlatShadedVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i) {
00193                         (*i).colour = colour;
00194                     }
00195                 }
00196         };
00197 
00198         // Helper class
00199         struct RenderableQuad: public OpenGLRenderable
00200         {
00201                 PointVertex m_quad[4];
00202                 void render (RenderStateFlags state) const
00203                 {
00204                     glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &m_quad[0].colour);
00205                     glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &m_quad[0].vertex);
00206                     glDrawArrays(GL_LINE_LOOP, 0, 4);
00207                 }
00208                 void setColour (const Colour4b& colour)
00209                 {
00210                     m_quad[0].colour = colour;
00211                     m_quad[1].colour = colour;
00212                     m_quad[2].colour = colour;
00213                     m_quad[3].colour = colour;
00214                 }
00215         };
00216 
00217         TranslateFree m_free;
00218         TranslateAxis m_axis;
00219         RenderableArrowLine m_arrow_x;
00220         RenderableArrowLine m_arrow_y;
00221         RenderableArrowLine m_arrow_z;
00222         RenderableArrowHead m_arrow_head_x;
00223         RenderableArrowHead m_arrow_head_y;
00224         RenderableArrowHead m_arrow_head_z;
00225         RenderableQuad m_quad_screen;
00226         SelectableBool m_selectable_x;
00227         SelectableBool m_selectable_y;
00228         SelectableBool m_selectable_z;
00229         SelectableBool m_selectable_screen;
00230         Pivot2World m_pivot;
00231     public:
00232         static Shader* m_state_wire;
00233         static Shader* m_state_fill;
00234 
00235         // Constructor
00236         TranslateManipulator (Translatable& translatable, std::size_t segments, float length);
00237 
00238         void UpdateColours ();
00239         bool manipulator_show_axis (const Pivot2World& pivot, const Vector3& axis);
00240 
00241         void render (Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world);
00242         void testSelect (const View& view, const Matrix4& pivot2world);
00243         Manipulatable* GetManipulatable ();
00244 
00245         void setSelected (bool select);
00246         bool isSelected () const;
00247 }; // class TranslateManipulator
00248 
00249 // =======================================================================================
00250 
00251 /* The Manipulator for scale operations
00252  */
00253 class ScaleManipulator: public Manipulator
00254 {
00255         // Helper class
00256         struct RenderableArrow: public OpenGLRenderable
00257         {
00258                 PointVertex m_line[2];
00259 
00260                 void render (RenderStateFlags state) const
00261                 {
00262                     glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &m_line[0].colour);
00263                     glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &m_line[0].vertex);
00264                     glDrawArrays(GL_LINES, 0, 2);
00265                 }
00266                 void setColour (const Colour4b& colour)
00267                 {
00268                     m_line[0].colour = colour;
00269                     m_line[1].colour = colour;
00270                 }
00271         };
00272 
00273         // Helper class
00274         struct RenderableQuad: public OpenGLRenderable
00275         {
00276                 PointVertex m_quad[4];
00277                 void render (RenderStateFlags state) const
00278                 {
00279                     glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &m_quad[0].colour);
00280                     glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &m_quad[0].vertex);
00281                     glDrawArrays(GL_QUADS, 0, 4);
00282                 }
00283                 void setColour (const Colour4b& colour)
00284                 {
00285                     m_quad[0].colour = colour;
00286                     m_quad[1].colour = colour;
00287                     m_quad[2].colour = colour;
00288                     m_quad[3].colour = colour;
00289                 }
00290         };
00291 
00292         ScaleFree m_free;
00293         ScaleAxis m_axis;
00294         RenderableArrow m_arrow_x;
00295         RenderableArrow m_arrow_y;
00296         RenderableArrow m_arrow_z;
00297         RenderableQuad m_quad_screen;
00298         SelectableBool m_selectable_x;
00299         SelectableBool m_selectable_y;
00300         SelectableBool m_selectable_z;
00301         SelectableBool m_selectable_screen;
00302         Pivot2World m_pivot;
00303 
00304     public:
00305 
00306         // Constructor
00307         ScaleManipulator (Scalable& scalable, std::size_t segments, float length);
00308 
00309         Pivot2World& getPivot ()
00310         {
00311             return m_pivot;
00312         }
00313 
00314         void UpdateColours ();
00315 
00316         void render (Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world);
00317         void testSelect (const View& view, const Matrix4& pivot2world);
00318         Manipulatable* GetManipulatable ();
00319 
00320         void setSelected (bool select);
00321         bool isSelected () const;
00322 
00323 }; // class ScaleManipulator
00324 
00325 #endif /*MANIPULATORS_H_*/

Generated by  doxygen 1.6.2