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