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