Developer Documentation
LineNodeGLCompat.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 LineNode - IMPLEMENTATION
50 //
51 //=============================================================================
52 
53 //== INCLUDES =================================================================
54 #include <ACG/GL/acg_glew.hh>
55 #include "LineNode.hh"
56 #include <ACG/GL/IRenderer.hh>
57 
58 //== NAMESPACES ===============================================================
59 
60 namespace ACG {
61 namespace SceneGraph {
62 
63 //== IMPLEMENTATION ==========================================================
64 
65 void
66 LineNode::
67 drawCompat(GLState& _state , const DrawModes::DrawMode& _drawMode)
68 {
69  if (_drawMode & DrawModes::WIREFRAME)
70  {
71  ACG::GLState::disable(GL_LIGHTING);
72 
73 
74  // if (line_mode_ == LineSegmentsMode)
75  // glBegin(GL_LINES);
76  // else
77  // glBegin(GL_LINE_STRIP);
78 
79 
80  if (line_mode_ == LineSegmentsMode)
81  {
82  // first check if (new standard) 4-channel colors are specified
83  if( (points_.size()/2 == colors4f_.size()) )
84  {
85  // enable blending of lines
86  GLboolean blendb;
87  glGetBooleanv( GL_BLEND, &blendb);
88  glEnable(GL_BLEND);
89  // blend ontop of prev. drawn mesh
90  GLboolean depthmaskb;
91  glGetBooleanv( GL_DEPTH_WRITEMASK, &depthmaskb);
92  glDepthMask(GL_FALSE);
93 
94  glBegin(GL_LINES);
95 
96  ConstPointIter p_it=points_.begin(), p_end=points_.end();
97  ConstColor4fIter c_it=colors4f_.begin();
98 
99  Color4f c(1.0f,1.0f,1.0f,1.0f);
100  if(c_it != colors4f_.end()) {
101  c = *c_it;
102  }
103 
104  int cnt = 0;
105  for (; p_it!=p_end; ++p_it)
106  {
107  if ((cnt > 0) && (cnt % 2 == 0) && (c_it+1) != colors4f_.end()) {
108  ++c_it;
109  c = *c_it;
110  }
111 
112  glColor(c);
113  glVertex(*p_it);
114 
115  ++cnt;
116  }
117 
118  glEnd();
119 
120  // disable blending of lines
121  if( blendb == GL_FALSE )
122  glDisable(GL_BLEND);
123 
124  // enable depth mask
125  if( depthmaskb == GL_TRUE )
126  glDepthMask(GL_TRUE);
127 
128  }
129  else if ((line_mode_ == LineSegmentsMode) && (points_.size()/2 == colors_.size()) )
130  {
131  glBegin(GL_LINES);
132  ConstPointIter p_it=points_.begin(), p_end=points_.end();
133  ConstColorIter c_it=colors_.begin();
134 
135  Color c((char)255, (char)255, (char)255);
136  if(c_it != colors_.end()) {
137  c = *c_it;
138  }
139 
140  int cnt = 0;
141  for (; p_it!=p_end; ++p_it)
142  {
143  if ((cnt > 0) && (cnt % 2 == 0) && (c_it+1) != colors_.end()) {
144  ++c_it;
145  c = *c_it;
146  }
147 
148  glColor(c);
149  glVertex(*p_it);
150 
151  ++cnt;
152  }
153  glEnd();
154  }
155  else
156  {
157  glBegin(GL_LINES);
158 
159  ConstPointIter p_it=points_.begin(), p_end=points_.end();
160 
161  for (; p_it!=p_end; ++p_it)
162  {
163  glVertex(*p_it);
164  }
165 
166  glEnd();
167  }
168  }
169  else
170  {
171  _state.set_color(_state.base_color());
172  glBegin(GL_LINE_STRIP);
173  ConstPointIter p_it=points_.begin(), p_end=points_.end();
174  for (; p_it!=p_end; ++p_it)
175  glVertex(*p_it);
176  glEnd();
177  }
178 
179  //glEnd();
180  }
181 }
182 
183 //----------------------------------------------------------------------------
184 
185 void LineNode::pickCompat(GLState& _state , PickTarget _target)
186 {
187  if (n_points() == 0)
188  return;
189 
190  // Bind the vertex array
191  ACG::GLState::bindBuffer(GL_ARRAY_BUFFER_ARB, 0);
192  ACG::GLState::vertexPointer( &(points_)[0] );
193  ACG::GLState::enableClientState(GL_VERTEX_ARRAY);
194 
195  const size_t n_edges = n_points() - 1;
196 
197  switch (_target)
198  {
199  case PICK_EDGE:
200  {
201  _state.pick_set_maximum (n_edges);
202  pick_edgesCompat(_state, 0);
203  break;
204  }
205 
206  case PICK_ANYTHING:
207  {
208  _state.pick_set_maximum (n_edges);
209  pick_edgesCompat(_state, 0);
210  break;
211  }
212 
213  default:
214  break;
215  }
216 
217  //Disable the vertex array
218  ACG::GLState::disableClientState(GL_VERTEX_ARRAY);
219 }
220 
221 //----------------------------------------------------------------------------
222 
223 void LineNode::pick_edgesCompat(GLState& _state, unsigned int _offset)
224 {
225  // Check if we have any edges to draw (% 0 causes division by zero on windows)
226  if (n_points() < 2)
227  return;
228 
229  const float line_width_old = _state.line_width();
230  _state.set_line_width(picking_line_width());
231  _state.pick_set_name (0);
232 
233  glDepthRange(0.0, 0.99);
234 
235  if (line_mode_ == PolygonMode)
236  {
237  const size_t n_edges = n_points() - 1;
238  for (size_t i = 0; i < n_edges; ++i)
239  {
240  _state.pick_set_name(i + _offset);
241  glBegin(GL_LINES);
242  glArrayElement(static_cast<GLint>(i));
243  glArrayElement(static_cast<GLint>(i + 1));
244  glEnd();
245  }
246  }
247  else if (line_mode_ == LineSegmentsMode)
248  {
249  const size_t n_edges = n_points() / 2;
250  for (size_t i = 0; i < n_edges; ++i)
251  {
252  _state.pick_set_name(i + _offset);
253  glBegin(GL_LINES);
254  glArrayElement(static_cast<GLint>(2*i));
255  glArrayElement(static_cast<GLint>(2*i + 1));
256  glEnd();
257  }
258  }
259 
260  glDepthRange(0.0, 1.0);
261 
262  _state.set_line_width(line_width_old);
263 }
264 
265 //=============================================================================
266 } // namespace SceneGraph
267 } // namespace ACG
268 //=============================================================================
DrawMode WIREFRAME
draw wireframe
Definition: DrawModes.cc:78
Namespace providing different geometric functions concerning angles.
pick any of the prior targets (should be implemented for all nodes)
Definition: PickTarget.hh:84
static void bindBuffer(GLenum _target, GLuint _buffer)
replaces glBindBuffer, supports locking
static void disable(GLenum _cap, bool _warnRemoved=true)
replaces glDisable, but supports locking
float picking_line_width() const
get line width used by the picking renderer. Defaults to line_width().
Definition: LineNode.hh:175
void glVertex(const Vec2i &_v)
Wrapper: glVertex for Vec2i.
Definition: gl.hh:85
static void vertexPointer(GLint _size, GLenum _type, GLsizei _stride, const GLvoid *_pointer)
replaces glVertexPointer, supports locking
static void disableClientState(GLenum _cap)
replaces glDisableClientState, supports locking
static void enableClientState(GLenum _cap)
replaces glEnableClientState, supports locking
picks edges (may not be implemented for all nodes)
Definition: PickTarget.hh:80
size_t n_points() const
number of points
Definition: LineNode.hh:181
PickTarget
What target to use for picking.
Definition: PickTarget.hh:73
void glColor(const Vec3f &_v)
Wrapper: glColor for Vec3f.
Definition: gl.hh:134