Developer Documentation
BoundingBoxNode.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 
43 //=============================================================================
44 //
45 // CLASS BoundingBoxNode - IMPLEMENTATION
46 //
47 //=============================================================================
48 
49 //== INCLUDES =================================================================
50 
51 #include "BoundingBoxNode.hh"
52 #include "SceneGraph.hh"
53 #include "../GL/GLPrimitives.hh"
54 
55 
56 //== NAMESPACES ===============================================================
57 
58 namespace ACG {
59 namespace SceneGraph {
60 
61 //== IMPLEMENTATION ==========================================================
62 
63 DrawModes::DrawMode
66 {
67  return DrawModes::WIREFRAME;
68 }
69 
70 //----------------------------------------------------------------------------
71 
72 BoundingBoxNode::BoundingBoxNode( BaseNode* _parent, std::string _name ) :
73 MaterialNode(_parent,
74  _name,
77  box_(0)
78 {
80 
81 
82  box_ = new GLLineBox();
83 }
84 
85 //----------------------------------------------------------------------------
87  delete box_;
88 }
89 
90 //----------------------------------------------------------------------------
91 
92 void BoundingBoxNode::computeAABB( Vec3d* _outMin, Vec3d* _outMax )
93 {
95  ACG::SceneGraph::traverse(this, act);
96 
97  if (_outMin)
98  *_outMin = (ACG::Vec3d) act.bbMin();
99 
100  if (_outMax)
101  *_outMax = (ACG::Vec3d) act.bbMax();
102 }
103 
104 //----------------------------------------------------------------------------
105 
106 void
108 draw(GLState& _state , const DrawModes::DrawMode& _drawMode)
109 {
110  if (_drawMode & DrawModes::WIREFRAME)
111  {
112  ACG::Vec3d bbmin;
113  ACG::Vec3d bbmax;
114  computeAABB(&bbmin, &bbmax);
115 
116  ACG::Vec3d bbcenter = (bbmin + bbmax) * 0.5;
117  ACG::Vec3d bbsize = bbmax - bbmin;
118 
119 
120  glPushAttrib (GL_ENABLE_BIT);
121 
122  ACG::GLState::disable(GL_LIGHTING);
123 
124  _state.push_modelview_matrix();
125 
126  _state.translate(bbcenter);
127  _state.scale(bbsize[0], bbsize[1], bbsize[2]);
128 
129  glColor4f(0.0f,1.0f,0.0f,1.0f);
130 
131  box_->draw_primitive();
132 
133  _state.pop_modelview_matrix();
134  glPopAttrib ();
135  }
136 }
137 
138 //----------------------------------------------------------------------------
139 
140 void BoundingBoxNode::getRenderObjects(IRenderer* _renderer, GLState& _state , const DrawModes::DrawMode& _drawMode , const ACG::SceneGraph::Material* _mat)
141 {
142  int dmlayerId = _drawMode.getLayerIndexByPrimitive(DrawModes::PRIMITIVE_WIREFRAME);
143 
144  if (dmlayerId >= 0)
145  {
146  ACG::Vec3d bbmin;
147  ACG::Vec3d bbmax;
148  computeAABB(&bbmin, &bbmax);
149 
150  ACG::Vec3d bbcenter = (bbmin + bbmax) * 0.5;
151  ACG::Vec3d bbsize = bbmax - bbmin;
152 
153  // create renderobject
154  RenderObject ro;
155  ro.initFromState(&_state);
156  ro.depthTest = true;
157  ro.shaderDesc.shadeMode = SG_SHADE_UNLIT;
158  ro.emissive = Vec3f(0.0f, 1.0f, 0.0f);
159 
160  ro.modelview.translate(bbcenter);
161  ro.modelview.scale(bbsize[0], bbsize[1], bbsize[2]);
162 
163  box_->addToRenderer_primitive(_renderer, &ro);
164  }
165 }
166 
167 //=============================================================================
168 } // namespace SceneGraph
169 } // namespace ACG
170 //=============================================================================
const Vec3d & bbMax() const
Returns maximum point of the bounding box.
Definition: SceneGraph.hh:403
void initFromState(GLState *_glState)
Initializes a RenderObject instance.
Definition: RenderObject.cc:61
BoundingBoxNode(BaseNode *_parent=0, std::string _name="<BoundingBoxNode>")
default constructor
void pop_modelview_matrix()
pop modelview matrix
Definition: GLState.cc:1026
int getLayerIndexByPrimitive(DrawModePrimitive _type) const
search for layer with specified primitive
Definition: DrawModes.cc:618
void computeAABB(Vec3d *_outMin, Vec3d *_outMax)
compute aabb of subtree
void draw(GLState &_state, const DrawModes::DrawMode &_drawMode) override
draw lines and normals
DrawModes::DrawMode drawMode() const
Return the own draw modes of this node.
Definition: BaseNode.hh:430
Namespace providing different geometric functions concerning angles.
void getRenderObjects(IRenderer *_renderer, GLState &_state, const DrawModes::DrawMode &_drawMode, const ACG::SceneGraph::Material *_mat) override
draw with renderobjects
DrawModes::DrawMode availableDrawModes() const override
return available draw modes
ShaderGenDesc shaderDesc
Drawmode and other shader params.
void translate(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with translation matrix (x,y,z)
void scale(double _s)
scale by (_s, _s, _s)
Definition: GLState.hh:775
void push_modelview_matrix()
push modelview matrix
Definition: GLState.cc:1010
static void disable(GLenum _cap, bool _warnRemoved=true)
replaces glDisable, but supports locking
Definition: GLState.cc:1527
VectorT< float, 3 > Vec3f
Definition: VectorT.hh:119
void scale(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with scaling matrix (x,y,z)
DrawMode WIREFRAME
draw wireframe
Definition: DrawModes.cc:78
const Vec3d & bbMin() const
Returns minimum point of the bounding box.
Definition: SceneGraph.hh:401
Interface class between scenegraph and renderer.
Definition: RenderObject.hh:98
void translate(double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
translate by (_x, _y, _z)
Definition: GLState.cc:533
void traverse(BaseNode *_node, Action &_action)
Definition: SceneGraph.hh:137
virtual ~BoundingBoxNode()
destructor
VectorT< double, 3 > Vec3d
Definition: VectorT.hh:121
GLMatrixd modelview
Modelview transform.