Developer Documentation
BSplineSurfaceNodeT.hh
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 // CLASS BSplineSurfaceNodeT
48 // Author: Ellen Dekkers <dekkers@cs.rwth-aachen.de>
49 //
50 //=============================================================================
51 
52 
53 #ifndef ACG_BSPLINESURFACENODET_HH
54 #define ACG_BSPLINESURFACENODET_HH
55 
56 
57 //== INCLUDES =================================================================
58 
59 #include <ACG/Scenegraph/BaseNode.hh>
60 #include <ACG/Scenegraph/DrawModes.hh>
61 
62 #include <QGLWidget>
63 
64 #include <QImage>
65 
66 
67 #include <ACG/GL/globjects.hh>
68 #include <ACG/GL/VertexDeclaration.hh>
69 #include <ACG/GL/GLPrimitives.hh>
70 
71 //== FORWARDDECLARATIONS ======================================================
72 
73 //== NAMESPACES ===============================================================
74 
75 namespace ACG {
76 namespace SceneGraph {
77 
78 //== CLASS DEFINITION =========================================================
79 
80 
87 template <class BSplineSurfaceType>
89 {
90  public:
91 
92  // typedefs for easy access
93  typedef typename BSplineSurfaceType::Point Point;
94 
96  BSplineSurfaceNodeT(BSplineSurfaceType& _bss,
97  BaseNode* _parent=0,
98  std::string _name="<BSplineSurfaceNode>" ) :
99  BaseNode(_parent, _name),
100  bsplineSurface_(_bss),
101  bspline_draw_mode_(NORMAL),
102  bspline_selection_draw_mode_(NONE),
103  pick_radius_(1.0),
104  resolution_(16),
105  controlnet_color_( Vec4f(34.f/255.f, 139.f/255.f, 34.f/255.f, 1.f) ),
106  controlnet_highlight_color_( Vec4f(1.0f, 1.0f, 1.0f, 1.0f) ),
107  surface_color_( Vec4f(178.0f/255.0f, 34.0f/255.0f, 34.0f/255.0f, 1.0f) ),
108  surface_highlight_color_( Vec4f(1.0f, 1.0f, 1.0f, 1.0f) ),
109  render_control_net_(false),
110  render_bspline_surface_(true),
111  adaptive_sampling_(false),
112  controlPointSelectionTexture_valid_(false),
113  knotVectorSelectionTexture_valid_(false),
114  pick_texture_idx_(0),
115  pick_texture_res_(256),
116  pick_texture_baseidx_(0),
117  cp_selection_texture_idx_(0),
118  cp_selection_texture_res_(256),
119  knot_selection_texture_idx_(0),
120  knot_selection_texture_res_(256),
121  arb_texture_idx_(0),
122  arb_texture_used_(false),
123  arb_texture_repeat_(false),
124  arb_texture_repeat_u_(1.0),
125  arb_texture_repeat_v_(1.0),
126  surfaceIndexCount_(0),
127  invalidateSurfaceMesh_(true),
128  controlNetSelIndices_(0),
129  controlNetLineIndices_(0),
130  invalidateControlNetMesh_(true),
131  invalidateControlNetMeshSel_(true)
132  {
133  cylinder_ = new GLCylinder(16, 1, 1.0f, true, true);
134  sphere_ = new GLSphere(5, 5);
135  fancySphere_ = new GLSphere(16, 16);
136  }
137 
140  {
141  delete cylinder_;
142  delete sphere_;
143  delete fancySphere_;
144  }
145 
146  enum BSplineDrawMode {
147  NORMAL = 0,
148  FANCY = 1
149  };
150 
151  enum BSplineSelectionDrawMode {
152  NONE = 0,
153  CONTROLPOINT = 1,
154  KNOTVECTOR = 2
155  };
156 
157 
158  BSplineSurfaceType& bsplineSurface() { return bsplineSurface_; }
159 
160  void set_pick_radius( double _pr) { pick_radius_ = _pr; }
161 
164 
166  DrawModes::DrawMode availableDrawModes() const override;
167 
169  void boundingBox(Vec3d& _bbMin, Vec3d& _bbMax) override;
170 
172  void draw(GLState& _state, const DrawModes::DrawMode& _drawMode) override;
173 
175  void getRenderObjects(IRenderer* _renderer, GLState& _state , const DrawModes::DrawMode& _drawMode , const Material* _mat) override;
176 
178  void pick(GLState& _state, PickTarget _target) override;
179 
181  void updateGeometry();
182 
183  void set_rendering_resolution(int _res){resolution_ = _res;};
184 
185  void render_control_net(bool _render) {render_control_net_ = _render;};
186 
187  bool render_control_net() {return render_control_net_; };
188 
189  void render_bspline_surface(bool _render) {render_bspline_surface_ = _render;};
190 
191  bool render_bspline_surface() { return render_bspline_surface_; };
192 
193  void set_bspline_draw_mode(BSplineDrawMode _mode) {bspline_draw_mode_ = _mode;};
194 
195  void set_selection_draw_mode(BSplineSelectionDrawMode _mode) {bspline_selection_draw_mode_ = _mode;};
196 
197  BSplineSelectionDrawMode get_selection_draw_mode() const { return bspline_selection_draw_mode_; }
198 
199  void adaptive_sampling(bool _adaptive){adaptive_sampling_ = _adaptive;};
200 
201  void cpSelectionTextureValid (bool _valid){controlPointSelectionTexture_valid_ = _valid;};
202  void knotSelectionTextureValid(bool _valid){knotVectorSelectionTexture_valid_ = _valid;};
203 
204 
206  int& pick_texture_res( ) { return pick_texture_res_; }
207 
209  void set_arb_texture( const QImage& _texture, bool _repeat = false, float _u_repeat = 1.0f, float _v_repeat = 1.0f);
210  void set_arb_texture(const GLuint _texture) { arb_texture_idx_ = _texture; }
211  void set_repeat_arb_texture(bool _repeat = true, float _urep = 5.0f, float _vrep = 5.0f) { arb_texture_repeat_ = _repeat; arb_texture_repeat_u_ = _urep; arb_texture_repeat_v_ = _vrep; }
212  bool get_repeat_arb_texture( ) { return arb_texture_repeat_; }
213 
214 
215 
216 private:
217 
218  void pick_vertices( GLState& _state );
219  void pick_spline( GLState& _state );
220  void pick_surface( GLState& _state, unsigned int _offset );
221 
222  void draw_cylinder( const Point& _p0, const Point& _axis, double _r, GLState& _state);
223  void draw_sphere ( const Point& _p0, double _r, GLState& _state, GLSphere* _sphere);
224 
227 
230 
231  void render(GLState& _state, bool _fill);
232 
233  void drawSurface(GLState& _state, bool _fill = true);
234 
235  void drawTexturedSurface(GLState& _state, GLuint _texture_idx);
236 
237  void drawControlNet(GLState& _state);
238 
239  void drawFancyControlNet(GLState& _state);
240 
241  void updateControlPointSelectionTexture(GLState& _state);
242 
243  void updateKnotVectorSelectionTexture(GLState& _state);
244 
246  void pick_init_texturing ( );
249  void pick_create_texture( GLState& _state);
250 
252  void pick_draw_textured_nurbs( GLState& _state);
253 
255  void selection_init_texturing(GLuint & _texture_idx);
256 
258  void create_cp_selection_texture(GLState& _state);
261 
263  void draw_textured_nurbs( GLState& _state);
264 
265  ACG::Vec4f generateHighlightColor(ACG::Vec4f _color);
266 
268  void updateSurfaceMesh(int _vertexCountU = 50, int _vertexCountV = 50);
269 
271  void updateControlNetMesh();
272 
275 
277  void updateTexBuffers();
278 
279 private:
280 
281  BSplineSurface& bsplineSurface_;
282 
283  BSplineDrawMode bspline_draw_mode_;
284 
285  BSplineSelectionDrawMode bspline_selection_draw_mode_;
286 
287  double pick_radius_;
288 
289  int resolution_;
290 
291  Vec4f controlnet_color_;
292  Vec4f controlnet_highlight_color_;
293 
294  Vec4f surface_color_;
295  Vec4f surface_highlight_color_;
296 
297  bool render_control_net_;
298  bool render_bspline_surface_;
299 
300  bool adaptive_sampling_;
301 
302  bool controlPointSelectionTexture_valid_;
303  bool knotVectorSelectionTexture_valid_;
304 
305  QImage pick_texture_image_;
306  GLuint pick_texture_idx_;
307  int pick_texture_res_;
308  // used to only re-create pick_texture_image_ if picking indices changed...
309  unsigned int pick_texture_baseidx_;
310 
311  // texturing stuff for control point selection highlighting
312  QImage cp_selection_texture_image_;
313  GLuint cp_selection_texture_idx_;
314  int cp_selection_texture_res_;
315 
316  // texturing stuff for knot vector selection highlighting
317  QImage knot_selection_texture_image_;
318  GLuint knot_selection_texture_idx_;
319  int knot_selection_texture_res_;
320 
321  // texturing stuff for using arbitrary textures
322  QImage arb_texture_image_;
323  GLuint arb_texture_idx_;
324  bool arb_texture_used_;
325  bool arb_texture_repeat_;
326  float arb_texture_repeat_u_;
327  float arb_texture_repeat_v_;
328 
329  GLCylinder* cylinder_;
330  GLSphere* sphere_;
331  GLSphere* fancySphere_;
332 
333 
334  // surface mesh
335  GeometryBuffer surfaceVBO_;
336  IndexBuffer surfaceIBO_;
337  VertexDeclaration surfaceDecl_;
338  int surfaceIndexCount_;
339  bool invalidateSurfaceMesh_;
340 
341  // control net mesh
342  GeometryBuffer controlNetVBO_;
343  IndexBuffer controlNetSelIBO_;
344  int controlNetSelIndices_;
345  IndexBuffer controlNetLineIBO_;
346  int controlNetLineIndices_;
347  VertexDeclaration controlNetDecl_;
348  bool invalidateControlNetMesh_;
349  bool invalidateControlNetMeshSel_;
350 
351  // GPU based evaluation
352  TextureBuffer knotTexBufferU_;
353  TextureBuffer knotTexBufferV_;
354  Texture2D controlPointTex_;
355 };
356 
357 //=============================================================================
358 } // namespace SceneGraph
359 } // namespace ACG
360 //=============================================================================
361 #if defined(INCLUDE_TEMPLATES) && !defined(ACG_BSPLINESURFACENODET_C)
362 #define ACG_BSPLINESURFACENODET_TEMPLATES
363 #include "BSplineSurfaceNodeT_impl.hh"
364 #endif
365 //=============================================================================
366 #endif // ACG_BSPLINESURFACENODET_HH defined
367 //=============================================================================
368 
void pick_init_texturing()
generate index and setup texture parameters
void draw_textured_nurbs(GLState &_state)
draw textured nurbs patch
DrawModes::DrawMode availableDrawModes() const override
return available draw modes
Namespace providing different geometric functions concerning angles.
void updateTexBuffers()
update texture resources for gpu-based spline evaluation
void boundingBox(Vec3d &_bbMin, Vec3d &_bbMax) override
update bounding box
int & pick_texture_res()
Should be a power of 2.
void updateControlNetMesh()
update vertex + index buffer of control net mesh
Class to define the vertex input layout.
void create_knot_selection_texture(GLState &_state)
creates texture to put onto nurbs curve for visualization of knotvector selection ...
void pick(GLState &_state, PickTarget _target) override
picking
void updateGeometry()
update vertex buffer for rendering
PickTarget
What target to use for picking.
Definition: PickTarget.hh:73
BSplineSurfaceNodeT & operator=(const BSplineSurfaceNodeT &_rhs)
Assignment operator (not used)
void pick_create_texture(GLState &_state)
create texture image
ACG_CLASSNAME(BSplineSurfaceNodeT)
static name of this class
void selection_init_texturing(GLuint &_texture_idx)
generate index and setup texture parameters for selection visualization
void draw(GLState &_state, const DrawModes::DrawMode &_drawMode) override
draw lines and normals
void create_cp_selection_texture(GLState &_state)
creates texture to put onto nurbs curve for visualization of control point selection ...
void getRenderObjects(IRenderer *_renderer, GLState &_state, const DrawModes::DrawMode &_drawMode, const Material *_mat) override
create render objects
BSplineSurfaceNodeT(BSplineSurfaceType &_bss, BaseNode *_parent=0, std::string _name="<BSplineSurfaceNode>")
Constructor.
void pick_draw_textured_nurbs(GLState &_state)
draw textured nurbs patch
void set_arb_texture(const QImage &_texture, bool _repeat=false, float _u_repeat=1.0f, float _v_repeat=1.0f)
use arbitrary texture (in SOLID_TEXTURED mode)
void updateControlNetMeshSel()
update index buffer of selected control points
void updateSurfaceMesh(int _vertexCountU=50, int _vertexCountV=50)
update vertex + index buffer of surface mesh