Developer Documentation
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  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 
53 //=============================================================================
54 //
55 // CLASS PointNode - IMPLEMENTATION
56 //
57 //=============================================================================
58 
59 //== INCLUDES =================================================================
60 
61 #include "PointNode.hh"
62 #include <ACG/GL/IRenderer.hh>
63 
64 //== NAMESPACES ===============================================================
65 
66 namespace ACG {
67 namespace SceneGraph {
68 
69 
70 //== IMPLEMENTATION ==========================================================
71 
72 
73 void
75 boundingBox(Vec3d& _bbMin, Vec3d& _bbMax)
76 {
77  ConstPointIter p_it=points_.begin(), p_end=points_.end();
78  for (; p_it!=p_end; ++p_it) {
79  _bbMin.minimize(*p_it);
80  _bbMax.maximize(*p_it);
81  }
82 }
83 
84 
85 //----------------------------------------------------------------------------
86 
87 
91 {
92  return ( DrawModes::POINTS |
95 }
96 
97 
98 //----------------------------------------------------------------------------
99 
100 
101 void
103 draw(GLState& /* _state */ , const DrawModes::DrawMode& _drawMode)
104 {
105  if (points_.empty())
106  return;
107 
108  // points
109  if (_drawMode & DrawModes::POINTS)
110  {
111  ACG::GLState::disable(GL_LIGHTING);
112  ACG::GLState::enableClientState(GL_VERTEX_ARRAY);
113  ACG::GLState::vertexPointer(&points_[0]);
114  glDrawArrays(GL_POINTS, 0, int(points_.size()));
115  }
116 
117 
118  // points and normals
119  if (_drawMode & DrawModes::POINTS_SHADED)
120  {
121  if (points_.size() == normals_.size())
122  {
123  ACG::GLState::enable(GL_LIGHTING);
124  ACG::GLState::enableClientState(GL_VERTEX_ARRAY);
125  ACG::GLState::vertexPointer(&points_[0]);
126  ACG::GLState::enableClientState(GL_NORMAL_ARRAY);
127  ACG::GLState::normalPointer(&normals_[0]);
128  glDrawArrays(GL_POINTS, 0, int(points_.size()));
129  }
130  }
131 
132 
133  // points and colors
134  if (_drawMode & DrawModes::POINTS_COLORED)
135  {
136  if (points_.size() == colors_.size())
137  {
138  ACG::GLState::disable(GL_LIGHTING);
139  ACG::GLState::enableClientState(GL_VERTEX_ARRAY);
140  ACG::GLState::vertexPointer(&points_[0]);
141  ACG::GLState::enableClientState(GL_COLOR_ARRAY);
142  ACG::GLState::colorPointer(&colors_[0]);
143  glDrawArrays(GL_POINTS, 0, int(points_.size()));
144  } else
145  std::cerr << "Mismatch size!" << std::endl;
146  }
147 
148 
149  // disable arrays
150  ACG::GLState::disableClientState(GL_VERTEX_ARRAY);
151  ACG::GLState::disableClientState(GL_NORMAL_ARRAY);
152  ACG::GLState::disableClientState(GL_COLOR_ARRAY);
153 }
154 
155 void
157 getRenderObjects( IRenderer* _renderer, GLState& _state , const DrawModes::DrawMode& _drawMode , const Material* _mat )
158 {
159  if (points_.empty())
160  return;
161 
162  // setup RenderObject
163  RenderObject ro;
164  ro.debugName = "PointNode";
165 
166 
167  // define vertex format
168  // buffer address may change so do this every frame
169 
170  vertexDecl_.clear();
171  vertexDecl_.addElement(GL_DOUBLE, 3, VERTEX_USAGE_POSITION, &points_[0]);
172  if (!normals_.empty())
173  vertexDecl_.addElement(GL_DOUBLE, 3, VERTEX_USAGE_NORMAL, &normals_[0]);
174  if (!colors_.empty())
175  vertexDecl_.addElement(GL_FLOAT, 4, VERTEX_USAGE_COLOR, &colors_[0]);
176 
177  vertexDecl_.setVertexStride(0);
178 
179  ro.vertexDecl = &vertexDecl_;
180 
181  for (unsigned int i = 0; i < _drawMode.getNumLayers(); ++i)
182  {
183  const DrawModes::DrawModeProperties* props = _drawMode.getLayer(i);
184 
185  if (props->primitive() == DrawModes::PRIMITIVE_POINT)
186  {
187  // reset renderobject
188  ro.initFromState(&_state);
189  ro.setMaterial(_mat);
190  ro.setupShaderGenFromDrawmode(props);
191 
192  ro.priority = 0;
193  ro.depthTest = true;
194  ro.depthWrite = true;
195  ro.depthFunc = GL_LESS;
196 
197  // use pointsize shader
198  QString geomTemplate = ShaderProgGenerator::getShaderDir();
199  geomTemplate += "PointSize/geometry.tpl";
200 
201  QString fragTemplate = ShaderProgGenerator::getShaderDir();
202  fragTemplate += "PointSize/fragment.tpl";
203 
204  ro.shaderDesc.geometryTemplateFile = geomTemplate;
205  ro.shaderDesc.fragmentTemplateFile = fragTemplate;
206 
207  // shader uniforms
208  ro.setUniform("screenSize", Vec2f((float)_state.viewport_width(), (float)_state.viewport_height()));
209  ro.setUniform("pointSize", _mat->pointSize());
210 
211  ro.glDrawArrays(GL_POINTS, 0, (GLsizei)points_.size());
212  _renderer->addRenderObject(&ro);
213  }
214  }
215 }
216 
217 
218 //=============================================================================
219 } // namespace SceneGraph
220 } // namespace ACG
221 //=============================================================================
VectorT< float, 2 > Vec2f
Definition: VectorT.hh:108
DrawMode POINTS_COLORED
draw colored, but not lighted points (requires point colors)
Definition: DrawModes.cc:80
static void colorPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glColorPointer, supports locking
static void enableClientState(GLenum _cap)
replaces glEnableClientState, supports locking
void getRenderObjects(IRenderer *_renderer, GLState &_state, const DrawModes::DrawMode &_drawMode, const Material *_mat)
draw points and normals via renderer plugin
Definition: PointNode.cc:157
DrawModeProperties stores a set of properties that defines, how to render an object.
Definition: DrawModes.hh:183
void setupShaderGenFromDrawmode(const SceneGraph::DrawModes::DrawModeProperties *_props)
Fills out ShaderGenDesc parameters based on Drawmode properties.
void setVertexStride(unsigned int _stride)
void draw(GLState &_state, const DrawModes::DrawMode &_drawMode)
draw points and normals
Definition: PointNode.cc:103
int priority
Priority to allow sorting of objects.
static void vertexPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glVertexPointer, supports locking
const VertexDeclaration * vertexDecl
Defines the vertex buffer layout, ignored if VAO is provided.
int viewport_width() const
get viewport width
Definition: GLState.hh:825
DrawMode POINTS
draw unlighted points using the default base color
Definition: DrawModes.cc:79
static QString getShaderDir()
size_t getNumLayers() const
returns the layer count
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
Definition: Vector11T.hh:562
static void enable(GLenum _cap)
replaces glEnable, but supports locking
DrawModes::DrawMode availableDrawModes() const
return available draw modes
Definition: PointNode.cc:90
virtual void addRenderObject(RenderObject *_renderObject)
Callback for the scenegraph nodes, which send new render objects via this function.
Definition: IRenderer.cc:108
void boundingBox(Vec3d &_bbMin, Vec3d &_bbMax)
update bounding box
Definition: PointNode.cc:75
void setUniform(const char *_name, GLint _value)
set values for int uniforms
const DrawModeProperties * getLayer(unsigned int _i) const
returns the property set at layer i
static void disable(GLenum _cap)
replaces glDisable, but supports locking
DrawMode POINTS_SHADED
draw shaded points (requires point normals)
Definition: DrawModes.cc:81
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
static void disableClientState(GLenum _cap)
replaces glDisableClientState, supports locking
void addElement(const VertexElement *_pElement)
GLenum depthFunc
GL_LESS, GL_LEQUAL, GL_GREATER ..
static void normalPointer(GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glNormalPointer, supports locking
void initFromState(GLState *_glState)
Initializes a RenderObject instance.
Definition: RenderObject.cc:69
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
Definition: Vector11T.hh:534
int viewport_height() const
get viewport height
Definition: GLState.hh:827
Interface class between scenegraph and renderer.
void pointSize(float _sz)
set point size (default: 1.0)
ShaderGenDesc shaderDesc
Drawmode and other shader params.