GlutPrimitiveNode.hh 8.07 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115



//=============================================================================
//
//  CLASS GlutPrimitiveNode
//
//=============================================================================


#ifndef ACG_GLUT_PRIMITIVE_NODE_HH
#define ACG_GLUT_PRIMITIVE_NODE_HH


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


#include "BaseNode.hh"
#include "DrawModes.hh"
#include <string>

#include <ACG/GL/GLPrimitives.hh>


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

namespace ACG {
namespace SceneGraph {


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


/** \class GlutPrimitiveNode GlutPrimitiveNode.hh <ACG/Scenegraph/GlutPrimitiveNode.hh>

    This class is able to render all glut primitives (listed in
    GlutPrimitiveType).
**/

class ACGDLLEXPORT GlutPrimitiveNode : public BaseNode
{

public:

  /// Lists all available primivites
  enum GlutPrimitiveType
  {
    CONE=0, 
    CUBE, 
    DODECAHEDRON, 
    ICOSAHEDRON, 
    OCTAHEDRON, 
    SPHERE, 
    TETRAHEDRON, 
    TORUS
  }; 

  
  struct Primitive
  {
    Vec3d position; // position
    Vec3d axis;    // direction / axis vector

    GlutPrimitiveType type;    // glut primitive type
    
    ACG::Vec4f color; // color
    
    // glut primitive resolution
    double       size;
    double       innersize; // size of inner loop for torus, height for cone
    unsigned int slices, stacks;
    
    // Constructor
Jan Möbius's avatar
Jan Möbius committed
116
    explicit Primitive(GlutPrimitiveType _t) :
Jan Möbius's avatar
Jan Möbius committed
117

Jan Möbius's avatar
Jan Möbius committed
118 119 120 121 122 123 124 125 126 127 128 129
        // default axis is negative z
        axis(Vec3d(0,0,1)),

        // Set the type
        type(_t),

        // set default resolution
        size(1.0),
        innersize(1.0),
        slices(20),
        stacks(20)
    {
Jan Möbius's avatar
Jan Möbius committed
130 131
    }

Jan Möbius's avatar
Jan Möbius committed
132 133 134 135 136 137 138 139 140 141 142
    Primitive(GlutPrimitiveType _t, Vec3d _p, Vec3d _a, ACG::Vec4f _c) :
      position(_p),
      axis(_a),
      type(_t),
      color(_c),

      // set default resolution
      size(1.0),
      innersize(1.0),
      slices(20),
      stacks(20)
Jan Möbius's avatar
Jan Möbius committed
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
    {
      
    }

    // Copy Constructor
    Primitive( const Primitive& _p)
    {
      // use defined = operator
      *this = _p;
    }

    // = operator
    Primitive& operator=(const Primitive& _p)
    {
      type      = _p.type;
      position  = _p.position;
      axis      = _p.axis;
      color     = _p.color;
      size      = _p.size;  
      innersize = _p.innersize;
      slices    = _p.slices;
      stacks    = _p.stacks;

      return *this;
    }
  };
  
  
171 172
  GlutPrimitiveNode( BaseNode*                _parent=0,
                     const std::string &      _name="<GlutPrimitive>" );
Jan Möbius's avatar
Jan Möbius committed
173 174


175 176 177
  GlutPrimitiveNode( GlutPrimitiveType        _type,
                     BaseNode*                _parent=0,
                     const std::string &      _name="<GlutPrimitive>" );
Jan Möbius's avatar
Jan Möbius committed
178 179 180


  /// destructor
Heng Liu's avatar
Heng Liu committed
181 182 183 184 185 186
  virtual ~GlutPrimitiveNode() {
    if(sphere_)
      delete sphere_;
    if(cone_)
      delete cone_;
  }
Jan Möbius's avatar
Jan Möbius committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

  /**
   * Adds a primitive and returns its index.
   *
   * @return the index of the new primitive.
   */
  size_t add_primitive(GlutPrimitiveType _type, Vec3d _pos, Vec3d _axis, ACG::Vec4f _color);

  void clear(){primitives_.clear();};
  
  /// set position
  void set_position(const Vec3d& _p, int _idx = 0);
  /// get position
  const Vec3d get_position(int _idx = 0) const;

  /// get a primitive
  Primitive& get_primitive(int _idx){return primitives_[_idx];};
  
  /// set size
  void set_size(double _s, int _idx = 0);
  /// get size
  double get_size(int _idx = 0) const;
  
  ACG_CLASSNAME(GlutPrimitiveNode);

  /// return available draw modes
  DrawModes::DrawMode availableDrawModes() const;

  /// update bounding box
  void boundingBox(Vec3d& _bbMin, Vec3d& _bbMax);
  
  /// drawing the primitive
  void draw(GLState& _state, const DrawModes::DrawMode& _drawMode);
220
  void draw_obj(GLState& _state, size_t _idx) const;
Jan Möbius's avatar
Jan Möbius committed
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
  
  /// picking
  void pick(GLState& _state, PickTarget _target);
  
  /** \brief Disable internal color processing
  *
  * Disables the internal color processing of the primitives. If disabled,
  * a Materialnodes settings will apply here.
  */
  void setColorInternal(bool _set) { setColor_ = _set; };

  /** \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(IRenderer* _renderer, GLState&  _state , const DrawModes::DrawMode&  _drawMode , const Material* _mat);

private:
  
  std::vector<Primitive> primitives_;
  
  bool setColor_;
  
  // Sphere rendering
  ACG::GLSphere*   sphere_;
Heng Liu's avatar
Heng Liu committed
249
  ACG::GLCone*   cone_;
Jan Möbius's avatar
Jan Möbius committed
250 251 252 253 254 255 256 257 258 259

};


//=============================================================================
} // namespace SceneGraph
} // namespace ACG
//=============================================================================
#endif // ACG_GLUT_PRIMITIVE_NODE_HH
//=============================================================================