Developer Documentation
Loading...
Searching...
No Matches
PointNode.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
46
47//=============================================================================
48//
49// CLASS PointNode - IMPLEMENTATION
50//
51//=============================================================================
52
53//== INCLUDES =================================================================
54
55#include <ACG/GL/acg_glew.hh>
56#include "PointNode.hh"
57#include <ACG/GL/IRenderer.hh>
58
59//== NAMESPACES ===============================================================
60
61namespace ACG {
62namespace SceneGraph {
63
64
65//== IMPLEMENTATION ==========================================================
66
67
68void
70boundingBox(Vec3d& _bbMin, Vec3d& _bbMax)
71{
72 ConstPointIter p_it=points_.begin(), p_end=points_.end();
73 for (; p_it!=p_end; ++p_it) {
74 _bbMin.minimize(*p_it);
75 _bbMax.maximize(*p_it);
76 }
77}
78
79
80//----------------------------------------------------------------------------
81
82
91
92
93//----------------------------------------------------------------------------
94
95
96void
98draw(GLState& /* _state */ , const DrawModes::DrawMode& _drawMode)
99{
100 if (points_.empty())
101 return;
102
103 // points
104 if (_drawMode & DrawModes::POINTS)
105 {
106 ACG::GLState::disable(GL_LIGHTING);
107 ACG::GLState::enableClientState(GL_VERTEX_ARRAY);
108 ACG::GLState::vertexPointer(&points_[0]);
109 glDrawArrays(GL_POINTS, 0, int(points_.size()));
110 }
111
112
113 // points and normals
114 if (_drawMode & DrawModes::POINTS_SHADED)
115 {
116 if (points_.size() == normals_.size())
117 {
118 ACG::GLState::enable(GL_LIGHTING);
119 ACG::GLState::enableClientState(GL_VERTEX_ARRAY);
120 ACG::GLState::vertexPointer(&points_[0]);
121 ACG::GLState::enableClientState(GL_NORMAL_ARRAY);
122 ACG::GLState::normalPointer(&normals_[0]);
123 glDrawArrays(GL_POINTS, 0, int(points_.size()));
124 }
125 }
126
127
128 // points and colors
129 if (_drawMode & DrawModes::POINTS_COLORED)
130 {
131 if (points_.size() == colors_.size())
132 {
133 ACG::GLState::disable(GL_LIGHTING);
134 ACG::GLState::enableClientState(GL_VERTEX_ARRAY);
135 ACG::GLState::vertexPointer(&points_[0]);
136 ACG::GLState::enableClientState(GL_COLOR_ARRAY);
137 ACG::GLState::colorPointer(&colors_[0]);
138 glDrawArrays(GL_POINTS, 0, int(points_.size()));
139 } else
140 std::cerr << "Mismatch size!" << std::endl;
141 }
142
143
144 // disable arrays
145 ACG::GLState::disableClientState(GL_VERTEX_ARRAY);
146 ACG::GLState::disableClientState(GL_NORMAL_ARRAY);
147 ACG::GLState::disableClientState(GL_COLOR_ARRAY);
148}
149
150void
151PointNode::
152update_vbo()
153{
154 if (!vbo_needs_update_)
155 return;
156
157 if (vbo_ == 0) {
158 glGenBuffers(1, &vbo_);
159 }
160
161 vertexDecl_.clear();
162
163 vertexDecl_.addElement(GL_FLOAT, 3, VERTEX_USAGE_POSITION);
164 // number of floats per point
165 size_t elem_size = 3;
166 if (!normals_.empty())
167 {
168 assert(normals_.size() == points_.size());
169 vertexDecl_.addElement(GL_FLOAT, 3, VERTEX_USAGE_NORMAL, elem_size*sizeof(float));
170 elem_size += 3;
171 }
172 if (!colors_.empty())
173 {
174 assert(colors_.size() == points_.size());
175 vertexDecl_.addElement(GL_FLOAT, 4, VERTEX_USAGE_COLOR, elem_size*sizeof(float));
176 elem_size += 4;
177 }
178 vertexDecl_.setVertexStride(elem_size * sizeof(float));
179
180 vbo_data_.clear();
181 vbo_data_.reserve(elem_size * points_.size());
182
183 for (size_t i=0; i < points_.size(); ++i) {
184 vbo_data_.push_back(static_cast<float>(points_[i][0]));
185 vbo_data_.push_back(static_cast<float>(points_[i][1]));
186 vbo_data_.push_back(static_cast<float>(points_[i][2]));
187 if (!normals_.empty()) {
188 vbo_data_.push_back(static_cast<float>(normals_[i][0]));
189 vbo_data_.push_back(static_cast<float>(normals_[i][1]));
190 vbo_data_.push_back(static_cast<float>(normals_[i][2]));
191 }
192 if (!colors_.empty()) {
193 vbo_data_.push_back(colors_[i][0]);
194 vbo_data_.push_back(colors_[i][1]);
195 vbo_data_.push_back(colors_[i][2]);
196 vbo_data_.push_back(colors_[i][3]);
197 }
198 }
199 assert(vbo_data_.size() == points_.size() * elem_size);
200
201 glBindBuffer(GL_ARRAY_BUFFER, vbo_);
202 glBufferData(GL_ARRAY_BUFFER, vbo_data_.size()*sizeof(float) , vbo_data_.data() , GL_STATIC_DRAW);
203 glBindBuffer(GL_ARRAY_BUFFER, 0);
204 vbo_needs_update_ = false;
205}
206
207void
209getRenderObjects( IRenderer* _renderer, GLState& _state , const DrawModes::DrawMode& _drawMode , const Material* _mat )
210{
211 if (points_.empty())
212 return;
213
214 update_vbo();
215
216 RenderObject ro;
217 ro.debugName = "PointNode";
218
219 ro.vertexDecl = &vertexDecl_;
220 ro.vertexBuffer = vbo_;
221
222 for (unsigned int i = 0; i < _drawMode.getNumLayers(); ++i)
223 {
224 const DrawModes::DrawModeProperties* props = _drawMode.getLayer(i);
225
226 if (props->primitive() == DrawModes::PRIMITIVE_POINT)
227 {
228 // reset renderobject
229 ro.initFromState(&_state);
230 ro.setMaterial(_mat);
232
233 ro.priority = 0;
234 ro.depthTest = true;
235 ro.depthWrite = true;
236 ro.depthFunc = GL_LESS;
237
238 // use pointsize shader
239 QString geomTemplate = ShaderProgGenerator::getShaderDir();
240 geomTemplate += "PointSize/geometry.tpl";
241
242 QString fragTemplate = ShaderProgGenerator::getShaderDir();
243 fragTemplate += "PointSize/fragment.tpl";
244
245 ro.shaderDesc.geometryTemplateFile = geomTemplate;
246 ro.shaderDesc.fragmentTemplateFile = fragTemplate;
247
248 // shader uniforms
249 ro.setUniform("screenSize", Vec2f((float)_state.viewport_width(), (float)_state.viewport_height()));
250 ro.setUniform("pointSize", _mat->pointSize());
251
252 ro.glDrawArrays(GL_POINTS, 0, (GLsizei)points_.size());
253 _renderer->addRenderObject(&ro);
254 }
255 }
256}
257
258
259//=============================================================================
260} // namespace SceneGraph
261} // namespace ACG
262//=============================================================================
static void disableClientState(GLenum _cap)
replaces glDisableClientState, supports locking
Definition GLState.cc:1584
static void colorPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glColorPointer, supports locking
Definition GLState.cc:2005
static void vertexPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glVertexPointer, supports locking
Definition GLState.cc:1961
static void enable(GLenum _cap, bool _warnRemoved=true)
replaces glEnable, but supports locking
Definition GLState.cc:1507
static void normalPointer(GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glNormalPointer, supports locking
Definition GLState.cc:1983
static void enableClientState(GLenum _cap)
replaces glEnableClientState, supports locking
Definition GLState.cc:1570
int viewport_width() const
get viewport width
Definition GLState.hh:847
static void disable(GLenum _cap, bool _warnRemoved=true)
replaces glDisable, but supports locking
Definition GLState.cc:1527
int viewport_height() const
get viewport height
Definition GLState.hh:849
virtual void addRenderObject(RenderObject *_renderObject)
Callback for the scenegraph nodes, which send new render objects via this function.
Definition IRenderer.cc:104
DrawModeProperties stores a set of properties that defines, how to render an object.
Definition DrawModes.hh:177
const DrawModeProperties * getLayer(unsigned int _i) const
returns the property set at layer i
Definition DrawModes.cc:525
size_t getNumLayers() const
returns the layer count
Definition DrawModes.cc:521
void pointSize(float _sz)
set point size (default: 1.0)
DrawModes::DrawMode availableDrawModes() const override
return available draw modes
Definition PointNode.cc:85
void boundingBox(Vec3d &_bbMin, Vec3d &_bbMax) override
update bounding box
Definition PointNode.cc:70
void draw(GLState &_state, const DrawModes::DrawMode &_drawMode) override
draw points and normals
Definition PointNode.cc:98
void getRenderObjects(IRenderer *_renderer, GLState &_state, const DrawModes::DrawMode &_drawMode, const Material *_mat) override
draw points and normals via renderer plugin
Definition PointNode.cc:209
void addElement(const VertexElement *_pElement)
void setVertexStride(unsigned int _stride)
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
Definition Vector11T.hh:588
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
Definition Vector11T.hh:560
DrawMode POINTS_COLORED
draw colored, but not lighted points (requires point colors)
Definition DrawModes.cc:74
DrawMode POINTS
draw unlighted points using the default base color
Definition DrawModes.cc:73
DrawMode POINTS_SHADED
draw shaded points (requires point normals)
Definition DrawModes.cc:75
Namespace providing different geometric functions concerning angles.
VectorT< float, 2 > Vec2f
Definition VectorT.hh:102
@ VERTEX_USAGE_NORMAL
"inNormal"
@ VERTEX_USAGE_COLOR
"inColor"
@ VERTEX_USAGE_POSITION
"inPosition"
Interface class between scenegraph and renderer.
ShaderGenDesc shaderDesc
Drawmode and other shader params.
const VertexDeclaration * vertexDecl
Defines the vertex buffer layout, ignored if VAO is provided.
void setupShaderGenFromDrawmode(const SceneGraph::DrawModes::DrawModeProperties *_props)
Fills out ShaderGenDesc parameters based on Drawmode properties.
int priority
Priority to allow sorting of objects.
GLuint vertexBuffer
VBO, IBO ids, ignored if VAO is provided.
void setUniform(const char *_name, GLint _value)
set values for int uniforms
void initFromState(GLState *_glState)
Initializes a RenderObject instance.
GLenum depthFunc
GL_LESS, GL_LEQUAL, GL_GREATER ..