PolyLineNodeT.hh 11.5 KB
Newer Older
Jan Möbius's avatar
 
Jan Möbius committed
1 2 3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Martin Schultz's avatar
Martin Schultz committed
4 5 6 7
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
Jan Möbius's avatar
 
Jan Möbius committed
8 9
 *                                                                           *
 *---------------------------------------------------------------------------*
Martin Schultz's avatar
Martin Schultz committed
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
 * 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
39 40 41 42 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
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                       *
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/




//=============================================================================
//
// CLASS PolyLineNodeT
//
// Author:  David Bommes <bommes@cs.rwth-aachen.de>
//
//=============================================================================


#ifndef ACG_POLYLINENODET_HH
#define ACG_POLYLINENODET_HH


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

#include <ACG/Scenegraph/BaseNode.hh>
#include <ACG/Scenegraph/DrawModes.hh>
70 71
#include <ACG/GL/VertexDeclaration.hh>
#include <ACG/GL/IRenderer.hh>
72
#include <ACG/GL/GLPrimitives.hh>
73
#include <ACG/GL/globjects.hh>
Jan Möbius's avatar
 
Jan Möbius committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

//== FORWARDDECLARATIONS ======================================================

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

namespace ACG {
namespace SceneGraph {

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




/** \class PolyLineNodeT PolyLineNodeT.hh <ACG/.../PolyLineNodeT.hh>

    Brief Description.

    A more elaborate description follows.
*/

template <class PolyLine>
class PolyLineNodeT : public BaseNode
{
public:

  // typedefs for easy access
  typedef typename PolyLine::Point Point;

  /// Constructor
103
  PolyLineNodeT(PolyLine& _pl, BaseNode* _parent = 0, std::string _name = "<PolyLineNode>");
Jan Möbius's avatar
 
Jan Möbius committed
104 105

  /// Destructor
106
  ~PolyLineNodeT();
Jan Möbius's avatar
 
Jan Möbius committed
107 108 109 110 111 112 113

  PolyLine& polyline() { return polyline_; }

  /// static name of this class
  ACG_CLASSNAME(PolyLineNodeT);

  /// return available draw modes
114
  DrawModes::DrawMode availableDrawModes() const;
Jan Möbius's avatar
 
Jan Möbius committed
115 116 117 118 119 120 121 122 123 124

  /// update bounding box
  void boundingBox(Vec3d& _bbMin, Vec3d& _bbMax);

  /// draw lines and normals
  void draw(GLState& /*_state*/,const DrawModes::DrawMode& _drawMode);

  /// picking
  void pick(GLState& _state, PickTarget _target);

125 126 127 128 129 130 131 132 133 134 135
  /** \brief Add the objects to the given renderer
    *
    * @param _renderer The renderer which will be used. Add your geometry into this class
    * @param _state    The current GL State when this object is called
    * @param _drawMode The active draw mode
    * @param _mat      Current material
    */
  void getRenderObjects(ACG::IRenderer* _renderer, ACG::GLState&  _state , const ACG::SceneGraph::DrawModes::DrawMode&  _drawMode , const ACG::SceneGraph::Material* _mat);

  /// Trigger an update of the vbo
  void update() { updateVBO_ = true; };
Jan Möbius's avatar
 
Jan Möbius committed
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

  /** Add custom data to the vertex buffer for rendering with shaders
   *
   * The element description declares the type of data and the name in the shader.
   * The provided buffer has to store data for each vertex: num elements in buffer = polyline().n_vertices()
   * If the stride offset in  _desc.pointer_ is 0, it is assumed that the buffer stores the data without memory alignment.
   * This function does not create a local copy of the buffer, so the provided memory address
   * has to be valid whenever an internal vbo update is triggered.
   * Custom attributes are then available in the vertex shader by accessing the input with name tangentElement.shaderInputName_.
   *
   * Example:
   *
   * in plugin:
   *  have allocated buffer: tangentData_ of type Vec3f*  (member of a class)
   *  
   *  ACG::VertexElement tangentDesc;
   *  tangentDesc.type_ = GL_FLOAT;
   *  tangentDesc.numElements_ = 3;
   *  tangentDesc.usage_ = ACG::VERTEX_USAGE_USER_DEFINED;
   *  tangentDesc.shaderInputName_ = "myTangent";
   *  tangentDesc.pointer_ = 0;
   *
   *  polylineNode->addCustomBuffer(tangentDesc, tangentData_);
   *
   * in vertex shader:
   *  in vec3 myTangent;
   *  ..
   *  
   *
   * @param _desc  type description of an element in the buffer
   * @param _buffer pointer to data buffer, has to be a valid address for the remaining time
   * @return id of custom buffer
   */
  int addCustomBuffer(const ACG::VertexElement& _desc, const void* _buffer);


  /** Update pointer to custom data
   *
   * @param _id  id of the custom buffer; return value of addCustomBuffer
   * @param _buffer pointer to data buffer, has to be a valid address for the remaining time
   */
  void setCustomBuffer(int _id, const void* _buffer);

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


  /** \brief Create the vertex declaration
  *
  * The vertex data in the vbo contains both vertex and edge colors,
  * so the vertex declaration decides which bytes to use for colored rendering (if any).
  * @param _dst Vertex declaration to initialize
  * @param _colorSource 0 - no colors, 1 - vertex colors, 2 - edge colors
  *
  */
  void setupVertexDeclaration(VertexDeclaration* _dst, int _colorSource) const;


  /** \brief Fill a buffer with vertex data
  *
  * Fill the specified buffer with vertex buffer data.
  * The buffer size in bytes can be calculated as vertex stride * num vertices.
  * If _addLineStripEndVertex is true, then the buffer size is  vertex stride * (num vertices + 1) instead.
  * Vertex layout and stride can be queried with via setupVertexDeclaration.
  * @param _buf                   address to buffer to which the vertices are written
  * @param _bufSize               size in bytes available in the specified buffer
  * @param _addLineStripEndVertex add a copy of the first vertex to the end of the buffer (useful to close the line in line_strip rendering mode)
  * @return                       number of bytes written
  *
  */
  size_t fillVertexBuffer(void* _buf, size_t _bufSize, bool _addLineStripEndVertex);

Jan Möbius's avatar
 
Jan Möbius committed
207 208
private:

David Bommes's avatar
David Bommes committed
209 210 211 212
  void pick_vertices       ( GLState& _state );
  void pick_spheres        ( GLState& _state );
  void pick_spheres_screen ( GLState& _state );
  void pick_edges          ( GLState& _state, unsigned int _offset);
Jan Möbius's avatar
 
Jan Möbius committed
213 214 215 216 217 218 219

  /// Copy constructor (not used)
  PolyLineNodeT(const PolyLineNodeT& _rhs);

  /// Assignment operator (not used)
  PolyLineNodeT& operator=(const PolyLineNodeT& _rhs);

220
  /// Vertex layout without vertex colors
221 222
  ACG::VertexDeclaration vertexDecl_;

223 224 225 226 227 228
  /// Vertex layout with vertex colors
  ACG::VertexDeclaration vertexDeclVCol_;

  /// Vertex layout with edge colors
  ACG::VertexDeclaration vertexDeclECol_;

229 230 231
  /// Custom vertex data for shader based rendering
  std::vector< std::pair<ACG::VertexElement, const void*> > customBuffers_;

232 233 234
  /// Map from custom properties in PolyLine to id in customBuffers_
  std::map< typename PolyLine::CustomPropertyHandle, int> polylinePropMap_;

Jan Möbius's avatar
Jan Möbius committed
235 236 237 238 239
  /** \brief Trigger an update of the vbo
   *
   * If the polyLine is changed, you have to call this function to update the buffers.
   *
   */
240 241
  void updateVBO();

242 243 244 245 246 247 248
  /** \brief Write vertex data for rendering to a buffer
   *
   * @param _vertex index of polyline vertex
   * @param _dst address of vertex in buffer
   */
  void writeVertex(unsigned int _vertex, void* _dst);

249 250 251 252 253 254 255 256
  /** \brief Write color for rendering to a buffer
  *
  * @param _vertex index of polyline vertex
  * @param _colorSourceVertex vertex or edge color?
  * @param _dst address of vertex in buffer
  */
  void writeVertexColor(unsigned int _vertex, bool _colorSourceVertex, void* _dst) const;

Jan Möbius's avatar
 
Jan Möbius committed
257 258
private:

Jan Möbius's avatar
Jan Möbius committed
259
  /// The associated poly line
Jan Möbius's avatar
 
Jan Möbius committed
260
  PolyLine& polyline_;
261 262

  /// VBO used to render the poly line
263
  GeometryBuffer vbo_;
264 265 266 267 268 269 270 271 272

  /// Index buffer for selected vertices
  std::vector<unsigned int> selectedVertexIndexBuffer_;

  /// Index buffer for selected edges
  std::vector<unsigned int> selectedEdgeIndexBuffer_;

  /// Flag to trigger update of vbo
  bool updateVBO_;
273 274 275

  /// Sphere for VertexSphere DrawMode
  GLSphere* sphere_;
276 277 278 279 280 281

  /// This defines a local point spheres draw mode for all polyLine nodes
  DrawModes::DrawMode POINTS_SPHERES;

  /// This defines a local point spheres draw mode for all polyLine nodes with constant screen size
  DrawModes::DrawMode POINTS_SPHERES_SCREEN;
Jan Möbius's avatar
 
Jan Möbius committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
};


//=============================================================================
} // namespace SceneGraph
} // namespace ACG
//=============================================================================
#if defined(INCLUDE_TEMPLATES) && !defined(ACG_POLYLINENODET_C)
#define ACG_POLYLINENODET_TEMPLATES
#include "PolyLineNodeT.cc"
#endif
//=============================================================================
#endif // ACG_POLYLINENODET_HH defined
//=============================================================================