Developer Documentation
BoxesNode.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 #include <ACG/GL/acg_glew.hh>
44 #include "BoxesNode.hh"
45 #include <ACG/GL/IRenderer.hh>
46 
47 
48 namespace ACG {
49 namespace SceneGraph {
50 
51 BoxesNode::BoxesNode(BaseNode* _parent,
52  std::string _name) :
53  MaterialNode(_parent, _name),
54  updateVBO_(true)
55 {
56  const std::array<Vec4f,6> dir_color = {
57  Vec4f{1.f,0.f,0.f,1.f},
58  Vec4f{0.f,1.f,0.f,1.f},
59  Vec4f{0.f,0.f,1.f,1.f},
60  Vec4f{.4f,0.f,0.f,1.f},
61  Vec4f{0.f,.4f,0.f,1.f},
62  Vec4f{0.f,0.f,.3f,1.f}
63  };
64 
65  const std::array<Vec3f, 8> vertices = {
66  Vec3f( 1.f, -1.f, -1.f),
67  Vec3f( 1.f, 1.f, -1.f),
68  Vec3f( 1.f, 1.f, 1.f),
69  Vec3f( 1.f, -1.f, 1.f),
70 
71  Vec3f(-1.f, -1.f, -1.f),
72  Vec3f(-1.f, 1.f, -1.f),
73  Vec3f(-1.f, 1.f, 1.f),
74  Vec3f(-1.f, -1.f, 1.f)};
75 
76  auto add_triangle = [this, &vertices, &dir_color]
77  (size_t v0, size_t v1, size_t v2, size_t dim)
78  {
79  triangles_.push_back({vertices[v0], vertices[v1], vertices[v2]});
80  triangle_colors_.push_back(dir_color[dim]);
81  };
82 
83  triangles_.reserve(6*2);
84  triangle_colors_.reserve(6*2);
85  add_triangle(0, 1, 2, 0);
86  add_triangle(0, 2, 3, 0);
87  add_triangle(4, 6, 5, 3);
88  add_triangle(4, 7, 6, 3);
89 
90  add_triangle(1, 5, 6, 1);
91  add_triangle(1, 6, 2, 1);
92  add_triangle(0, 3, 7, 4);
93  add_triangle(0, 7, 4, 4);
94 
95  add_triangle(2, 7, 3, 2);
96  add_triangle(2, 6, 7, 2);
97  add_triangle(0, 4, 1, 5);
98  add_triangle(1, 4, 5, 5);
99 }
100 
101 void
102 BoxesNode::
103 boundingBox(Vec3d& _bbMin, Vec3d& _bbMax)
104 {
105  // FIXME: this assumes matrices to only consist of vectors up to unit length
106  for (const auto &be: elem_) {
107  _bbMax.maximize(be.pos + Vec3d(scaleFactor_,scaleFactor_,scaleFactor_));
108  _bbMin.minimize(be.pos + Vec3d(-scaleFactor_, -scaleFactor_, -scaleFactor_));
109  }
110 }
111 
112 void BoxesNode::createVBO()
113 {
114  if (!updateVBO_)
115  return;
116 
117  // generate if needed:
118  geometry_vbo_.bind();
119  instance_vbo_.bind();
120 
121  vertexDecl_.clear();
122  vertexDecl_.addElement(GL_FLOAT, 3, VERTEX_USAGE_POSITION, nullptr, nullptr, 0, geometry_vbo_.id());
123  vertexDecl_.addElement(GL_FLOAT, 4, VERTEX_USAGE_COLOR, nullptr, nullptr, 0, geometry_vbo_.id());
124  std::vector<float> geom_vbo_data; geom_vbo_data.reserve(3 * (3+4) * triangles_.size());
125 
126  vertexDecl_.addElement(GL_FLOAT, 3, VERTEX_USAGE_SHADER_INPUT, nullptr, "inst_v0", 1, instance_vbo_.id());
127  vertexDecl_.addElement(GL_FLOAT, 3, VERTEX_USAGE_SHADER_INPUT, nullptr, "inst_v1", 1, instance_vbo_.id());
128  vertexDecl_.addElement(GL_FLOAT, 3, VERTEX_USAGE_SHADER_INPUT, nullptr, "inst_v2", 1, instance_vbo_.id());
129  vertexDecl_.addElement(GL_FLOAT, 3, VERTEX_USAGE_SHADER_INPUT, nullptr, "inst_pos", 1, instance_vbo_.id());
130  std::vector<float> instance_vbo_data; instance_vbo_data.reserve(3 * 4 * elem_.size());
131 
132  assert(triangles_.size() == triangle_colors_.size());
133  for (size_t i = 0; i < triangles_.size(); ++i)
134  {
135  auto &tri = triangles_[i];
136  auto &color = triangle_colors_[i];
137  for (const auto &point: tri) {
138  std::copy(point.begin(), point.end(), std::back_inserter(geom_vbo_data));
139  std::copy(color.begin(), color.end(), std::back_inserter(geom_vbo_data));
140  }
141  }
142  for (const auto &be: elem_) {
143  for (const auto &point: {
144  be.transform.getRow(0),
145  be.transform.getRow(1),
146  be.transform.getRow(2),
147  be.pos})
148  {
149  std::copy(point.begin(), point.end(), std::back_inserter(instance_vbo_data));
150  }
151  }
152 
153  geometry_vbo_.upload(
154  static_cast<GLsizeiptr>(geom_vbo_data.size()*sizeof(float)),
155  geom_vbo_data.data(),
156  GL_STATIC_DRAW_ARB);
157  instance_vbo_.upload(
158  static_cast<GLsizeiptr>(instance_vbo_data.size()*sizeof(float)),
159  instance_vbo_data.data(),
160  GL_STATIC_DRAW_ARB);
161 
162  // Update done.
163  updateVBO_ = false;
164 
165 }
166 
167 void
168 BoxesNode::
169 getRenderObjects(IRenderer* _renderer, GLState& _state , const DrawModes::DrawMode& /* _drawMode */, const ACG::SceneGraph::Material* _mat)
170 {
171  if (elem_.empty())
172  return;
173 
174  RenderObject ro;
175  ro.initFromState(&_state);
176  ro.setMaterial(_mat);
177 
178  boxesNodeName_ = std::string("BoxesNode: ")+name();
179  ro.debugName = boxesNodeName_;
180 
181  ro.depthTest = true;
182  ro.depthWrite = true;
183 
184  ro.blending = true;
185  ro.blendSrc = GL_SRC_ALPHA;
186  ro.blendDest = GL_ONE_MINUS_SRC_ALPHA;
187 
188  ro.shaderDesc.shadeMode = SG_SHADE_UNLIT;
189  ro.shaderDesc.vertexColors = true;
190  ro.shaderDesc.vertexTemplateFile = "BoxesNode/vert.glsl";
191 
192  ro.setUniform("scale", scaleFactor_);
193 
194  createVBO();
195  ro.vertexBuffer = geometry_vbo_.id();
196  ro.vertexDecl = &vertexDecl_;
197 
198  ro.glDrawInstancedArrays(GL_TRIANGLES, 0,
199  static_cast<GLsizei>(3 * triangles_.size()),
200  static_cast<GLsizei>(elem_.size()));
201  _renderer->addRenderObject(&ro);
202 }
203 
204 } // namespace SceneGraph
205 } // namespace ACG
void initFromState(GLState *_glState)
Initializes a RenderObject instance.
Definition: RenderObject.cc:61
void setUniform(const char *_name, GLint _value)
set values for int uniforms
defined by user via VertexElement::shaderInputName_
Namespace providing different geometric functions concerning angles.
ShaderGenDesc shaderDesc
Drawmode and other shader params.
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
Definition: Vector11T.hh:587
GLuint vertexBuffer
VBO, IBO ids, ignored if VAO is provided.
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
Definition: Vector11T.hh:559
const VertexDeclaration * vertexDecl
Defines the vertex buffer layout, ignored if VAO is provided.
virtual void addRenderObject(RenderObject *_renderObject)
Callback for the scenegraph nodes, which send new render objects via this function.
Definition: IRenderer.cc:104
GLenum blendDest
glBlendFunc: GL_SRC_ALPHA, GL_ZERO, GL_ONE, GL_ONE_MINUS_SRC_ALPHA ...
Interface class between scenegraph and renderer.
Definition: RenderObject.hh:98