StatusNodesT.hh 13.1 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
 *                                                                           *
\*===========================================================================*/

Jan Möbius's avatar
Jan Möbius committed
42

Jan Möbius's avatar
Jan Möbius committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89




//=============================================================================
//
//  Status Nodes
//
//=============================================================================


#ifndef ACG_STATUS_NODES_HH
#define ACG_STATUS_NODES_HH


//== INCLUDES =================================================================


#include <OpenMesh/Core/Mesh/Attributes.hh>

#include "MaterialNode.hh"
#include "DrawModes.hh"

#include <ACG/GL/VertexDeclaration.hh>
#include <ACG/GL/IRenderer.hh>
#include <ACG/GL/DrawMesh.hh>

#include <vector>


//== NAMESPACES ===============================================================


template<class Mod>
class StatusNodes_ModTraits {
    public:
        enum {
            StaticUsage = true
        };
};

namespace ACG {
namespace SceneGraph {


//== CLASS DEFINITION =========================================================

90 91 92 93 94 95
/**
 * @brief The StatusNodesBase class extends StatusNodesT with a halfEdge vbo
 * for coreProfile rendering support.
 *
 * StatusNodesT is supposed to derive from this class.
 */
96
class ACGDLLEXPORT StatusNodesBase
97 98 99 100
{
public:
  StatusNodesBase();
  virtual ~StatusNodesBase();
101

102
protected:
103
  //index buffer objects ,previously direct ram access was used in compat
104
  //now use ibos / vbos on both compat and core profile.
105
  GLuint heVBO_, eIBO_, fIBO_, vIBO_, pIBO_;
106

107
  void updateIBOData(GLuint& bufferName_, size_t numberOfElements_, size_t sizeOfElements_, void* data_);
108
  void updateHEVBOPoints(size_t numberOfElements_, size_t sizeOfElements_, void* data_);
109

110
private:
111
  GLint prevBuffer;
112
  void createHEVBO();
113
  void createIBO(GLuint& _name);
114 115
  void bindHEVBO();
  void unbindHEVBO();
116 117
  void bindIBO(GLuint& _name);
  void unbindIBO();
118 119
};

Jan Möbius's avatar
Jan Möbius committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
template<class Mesh, class Mod, const bool StaticUsage> class StatusNodeBaseT;

template<class Mesh, class Mod>

class StatusNodeBaseT<Mesh, Mod, true> : public MaterialNode {
    public:
        StatusNodeBaseT(BaseNode* _parent, const std::string&  _name) :
            MaterialNode(_parent, _name) {}

        virtual ~StatusNodeBaseT() {}

    protected:
        bool is_vertex_selected(
                const Mesh &mesh, typename Mesh::VertexHandle vh) {
            return Mod::is_vertex_selected(mesh, vh);
        }

        bool is_halfedge_selected(
                const Mesh &mesh, typename Mesh::HalfedgeHandle heh) {
            return Mod::is_halfedge_selected(mesh, heh);
        }

        bool is_edge_selected(const Mesh &mesh, typename Mesh::EdgeHandle eh) {
            return Mod::is_edge_selected(mesh, eh);
        }

        bool is_face_selected(const Mesh &mesh, typename Mesh::FaceHandle fh) {
            return Mod::is_face_selected(mesh, fh);
        }
};

template<class Mesh, class Mod>
class StatusNodeBaseT<Mesh, Mod, false> : public MaterialNode {

    public:
        StatusNodeBaseT(BaseNode* _parent, const std::string&  _name) :
            MaterialNode(_parent, _name), modInstance(0) {}

        virtual ~StatusNodeBaseT() {
            delete modInstance;
        }

        /**
         * Provide the actual instance of the Mod. Transfers ownership.
         *
         * @param mod A Mod instance. Has to be created with new. Ownership
         * will be assumed and it will be deleted with delete.
         */
        void provideModInstance(Mod *mod) {
            delete modInstance;
            modInstance = mod;
        }

    protected:
        bool is_vertex_selected(
                const Mesh &mesh, typename Mesh::VertexHandle vh) {
            assert(modInstance);
            return modInstance->is_vertex_selected(mesh, vh);
        }

        bool is_halfedge_selected(
                const Mesh &mesh, typename Mesh::HalfedgeHandle heh) {
            assert(modInstance);
            return modInstance->is_halfedge_selected(mesh, heh);
        }

        bool is_edge_selected(const Mesh &mesh, typename Mesh::EdgeHandle eh) {
            assert(modInstance);
            return modInstance->is_edge_selected(mesh, eh);
        }

        bool is_face_selected(const Mesh &mesh, typename Mesh::FaceHandle fh) {
            assert(modInstance);
            return modInstance->is_face_selected(mesh, fh);
        }

    protected:
        Mod *modInstance;
};


/** \class StatusNodeT StatusNodesT.hh <ACG/Scenegraph/StatusNodesT.hh>
 *
 *     Renders Status flags of Mesh Vertices/Faces/Edges
 *
 *             **/
template <class Mesh, class Mod>
class StatusNodeT :
208 209
        public StatusNodeBaseT<Mesh, Mod, ::StatusNodes_ModTraits<Mod>::StaticUsage>,
        StatusNodesBase
Jan Möbius's avatar
Jan Möbius committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
{
public:
  typedef StatusNodeBaseT<Mesh, Mod, ::StatusNodes_ModTraits<Mod>::StaticUsage> BaseClass;
  typedef Mod ModType;

  /// constructor
  StatusNodeT( const Mesh&         _mesh,
               BaseNode*           _parent = 0,
               const std::string&  _name   = "<StatusNode>" );

  /// destructor
  virtual ~StatusNodeT() {}

  ACG_CLASSNAME(StatusNode);


  /** \brief set geometry invalid, topology and selection is kept
   */
  void updateGeometry();

  /** \brief set topology invalid (updates everything)
    */
  void updateTopology();

  /** \brief set selection invalid (Only selection changed, rest is kept)
    */
  void updateSelection();

  /** \brief Set drawmesh
   *
   * Selections are then rendered with gpu buffers gathered fro the meshnode for improved performance
   *
   * @param _drawmesh Pointer to the drawmesh for which this status node will work
  */
  void setDrawMesh(DrawMeshT<Mesh>* _drawmesh);


  /** \brief support for shader-pipeline
  @param _renderer Render-Interface, collector for Renderobjects
  @param _state current OpenGL state
  @param _drawMode active Drawmode
  @param _mat active Material
  */
  void getRenderObjects(IRenderer* _renderer, GLState&  _state, const DrawModes::DrawMode&  _drawMode, const class Material* _mat);


  DrawModes::DrawMode  availableDrawModes() const;
  void boundingBox(Vec3d& _bbMin, Vec3d& _bbMax);
  void draw(GLState& _state, const DrawModes::DrawMode& _drawMode);
  void pick(GLState& /* _state */ , PickTarget /* _target */ ) {}


private:

  /** build/update cache of active vertices/edges/faces. This function
      automatically enables caching.
  */
  void update_cache();

  typedef typename Mesh::Face           Face;
  typedef typename Mesh::Vertex         Vertex;
  typedef typename Mesh::Halfedge       Halfedge;
  typedef typename Mesh::Edge           Edge;
  typedef typename Mesh::FaceHandle     FaceHandle;
  typedef typename Mesh::HalfedgeHandle HalfedgeHandle;

  typedef typename Mesh::Point       Point;
  typedef typename Mesh::Normal      Normal;

  void draw_points();
  void draw_edges();
  void draw_halfedges();
  void draw_faces(bool _per_vertex);

  Point halfedge_point(const HalfedgeHandle _heh);


private:

  const Mesh&                mesh_;
  DrawMeshT<Mesh>*           drawMesh_;

292
  //indices used for indexed rendering
Jan Möbius's avatar
Jan Möbius committed
293 294 295
  std::vector<unsigned int>  v_cache_, e_cache_, f_cache_, poly_cache_;
  std::vector<FaceHandle>    fh_cache_;

296
  //halfedges are rendered directly from ram in compat profile
Jan Möbius's avatar
Jan Möbius committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
  std::vector<Point>  he_points_;
  std::vector<Normal> he_normals_;

  // bounding box
  Vec3d bbMin_;
  Vec3d bbMax_;

  /// State variables
  bool invalidGeometry_;

  bool vertexIndexInvalid_;
  bool halfedgeCacheInvalid_;
  bool edgeIndexInvalid_;
  bool faceIndexInvalid_;


  // vertex-formats for new renderer
  VertexDeclaration pointVertexDecl_;
  VertexDeclaration halfedgeVertexDecl_;
};



//== CLASS DEFINITION =========================================================


template <class Mesh, unsigned int Bit>
struct StatusModT
{
  static bool is_vertex_selected(const Mesh& _mesh,
                                 typename Mesh::VertexHandle _vh)
  {
    return _mesh.status(_vh).is_bit_set(Bit);
  }

  static bool is_edge_selected(const Mesh& _mesh,
                               typename Mesh::EdgeHandle _eh)
  {
    return _mesh.status(_eh).is_bit_set(Bit);
  }

  static bool is_halfedge_selected(const Mesh& _mesh,
				   typename Mesh::HalfedgeHandle _heh)
  {
    return _mesh.status(_heh).is_bit_set(Bit);
  }

  static bool is_face_selected(const Mesh& _mesh,
                               typename Mesh::FaceHandle _fh)
  {
    return _mesh.status(_fh).is_bit_set(Bit);
  }
};



//== CLASS DEFINITION =========================================================


template <class Mesh>
struct SelectionModT
  : public StatusModT<Mesh, OpenMesh::Attributes::SELECTED>
{};



/** \class SelectionNodeT StatusNodesT.hh <ACG/Scenegraph/StatusNodesT.hh>
 *
 *     Renders the Selection status of Mesh Vertices/Faces/Edges
 *
 *             **/
template <class Mesh>
class SelectionNodeT
  : virtual public StatusNodeT<Mesh, SelectionModT<Mesh> >
{
public:

  /** \brief Constructor
   * @param _mesh reference to mesh with status property
   * @param _parent Parent node in the scenegraph
   * @param _name Name of the Node
   */
  SelectionNodeT( const Mesh&         _mesh,
                  BaseNode*           _parent = 0,
                  const std::string&  _name   = "<SelectionNode>" )
    : StatusNodeT<Mesh, SelectionModT<Mesh> > (_mesh, _parent, _name)
  {}
};


//== CLASS DEFINITION =========================================================


template <class Mesh>
struct LockModT
  : public StatusModT<Mesh, OpenMesh::Attributes::LOCKED>
{};


template <class Mesh>
class LockNodeT : public StatusNodeT<Mesh, LockModT<Mesh> >
{
public:

  LockNodeT( const Mesh&         _mesh,
             BaseNode*           _parent = 0,
             const std::string&  _name   = "<LockNode>" )
    : StatusNodeT<Mesh, LockModT<Mesh> > (_mesh, _parent, _name)
  {}
};


//=============================================================================
} // namespace SceneGraph
} // namespace ACG
//=============================================================================
#if defined(INCLUDE_TEMPLATES) && !defined(ACG_STATUS_NODES_C)
#define ACG_STATUS_NODES_TEMPLATES
415
#include "StatusNodesT_impl.hh"
Jan Möbius's avatar
Jan Möbius committed
416 417 418 419 420
#endif
//=============================================================================
#endif // ACG_STATUS_NODES_HH defined
//=============================================================================