Developer Documentation
MaterialNode.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  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 
53 //=============================================================================
54 //
55 // CLASS MaterialNode
56 //
57 //=============================================================================
58 
59 #ifndef ACG_MATERIAL_NODE_HH
60 #define ACG_MATERIAL_NODE_HH
61 
62 
63 //== INCLUDES =================================================================
64 
65 #include "BaseNode.hh"
66 #include <string>
67 #include <QVariantMap>
68 
69 //== NAMESPACES ===============================================================
70 
71 namespace ACG {
72 
73 QVariantMap ACGDLLEXPORT json_to_variant_map(QString json);
74 
75 namespace SceneGraph {
76 
77 
78 //== CLASS DEFINITION =========================================================
79 
85 class ACGDLLEXPORT Material {
86 
87  friend class MaterialNode;
88 
89 public:
92  baseColor_(GLState::default_base_color),
93  ambientColor_(GLState::default_ambient_color),
94  diffuseColor_(GLState::default_diffuse_color),
95  specularColor_(GLState::default_specular_color),
96  overlayColor_(GLState::default_overlay_color),
97  shininess_(GLState::default_shininess),
98  reflectance_(0.0),
99  pointSize_(1.0),
100  lineWidth_(1.0),
101  roundPoints_(false),
102  linesSmooth_(false),
103  alphaTest_(false),
104  alphaClip_(0),
105  blending_(false),
106  blendParam1_(GL_SRC_ALPHA),
107  blendParam2_(GL_ONE_MINUS_SRC_ALPHA),
108  colorMaterial_(true),
109  backfaceCulling_(false),
110  multiSampling_(true) {};
111 
113  virtual ~Material() {};
114 
116  Material(const Material& _m) :
117  baseColor_(_m.baseColor_),
118  ambientColor_(_m.ambientColor_),
119  diffuseColor_(_m.diffuseColor_),
120  specularColor_(_m.specularColor_),
121  overlayColor_(_m.overlayColor_),
122  shininess_(_m.shininess_),
123  reflectance_(_m.reflectance_),
124  pointSize_(_m.pointSize_),
125  lineWidth_(_m.lineWidth_),
126  roundPoints_(_m.roundPoints_),
127  linesSmooth_(_m.linesSmooth_),
128  alphaTest_(_m.alphaTest_),
129  alphaClip_(_m.alphaClip_),
130  blending_(_m.blending_),
131  blendParam1_(_m.blendParam1_),
132  blendParam2_(_m.blendParam2_),
133  colorMaterial_(_m.colorMaterial_),
134  backfaceCulling_(_m.backfaceCulling_),
135  multiSampling_(_m.multiSampling_) {};
136 
137  static bool support_json_serialization();
138  QString serializeToJson() const;
139  void deserializeFromJson(const QString &json);
140  void deserializeFromVariantMap(const QVariantMap &matMap);
141 
149  void color(const Vec4f& _c) {
150  Vec4f c(0.0,0.0,0.0,1.0);
151  baseColor(c);
152  overlayColor(c);
153  c = _c * 0.2f; c[3]=_c[3]; ambientColor(c);
154  c = _c * 0.6f; c[3]=_c[3]; diffuseColor(c);
155  c = _c * 0.8f; c[3]=_c[3]; specularColor(c);
156  }
157 
160  color( Vec4f(0.2 + double(rand())/double(RAND_MAX)*0.8,
161  0.2 + double(rand())/double(RAND_MAX)*0.8,
162  0.2 + double(rand())/double(RAND_MAX)*0.8,
163  1.0));
164  }
165 
167  void baseColor(const Vec4f& _c) { baseColor_ = _c;}
169  const Vec4f& baseColor() const { return baseColor_; }
170 
172  void ambientColor(const Vec4f& _a) { ambientColor_ = _a; }
174  const Vec4f& ambientColor() const { return ambientColor_; }
175 
177  void diffuseColor(const Vec4f& _d) { diffuseColor_ = _d; }
179  const Vec4f& diffuseColor() const { return diffuseColor_; }
180 
182  void specularColor(const Vec4f& _s) { specularColor_ = _s; }
184  const Vec4f& specularColor() const { return specularColor_; }
185 
187  void overlayColor(const Vec4f& _s) { overlayColor_ = _s; }
189  const Vec4f& overlayColor() const { return overlayColor_; }
190 
192  void colorMaterial( const bool _cm) { colorMaterial_ = _cm; }
194  void enableColorMaterial() { colorMaterial_ = true; }
196  void disableColorMaterial() { colorMaterial_ = false; }
198  bool colorMaterial() { return colorMaterial_; }
199 
201  void shininess(float _s) { shininess_ = _s; }
203  float shininess() const { return shininess_; }
204 
206  void reflectance(double _m) { reflectance_ = _m; }
208  double reflectance() const { return reflectance_; }
209 
211  void pointSize(float _sz) { pointSize_ = _sz; }
213  float pointSize() const { return pointSize_; }
214 
216  void lineWidth(float _sz) { lineWidth_ = _sz; }
218  float lineWidth() const { return lineWidth_; }
219 
221  void roundPoints(bool _b) { roundPoints_ = _b; }
223  bool roundPoints() const { return roundPoints_; }
224 
226  void lineSmooth(bool _b) { linesSmooth_ = _b; }
228  bool lineSmooth() const { return linesSmooth_; }
229 
231  void enableAlphaTest(float _clip) {
232  alphaTest_ = true; alphaClip_ = _clip;
233  }
234 
236  void disableAlphaTest() { alphaTest_ = false; }
237 
239  bool alphaTest() const { return alphaTest_; };
240 
243  multiSampling_ = true;
244  }
245 
248  multiSampling_ = false;
249  }
250 
252  bool multiSampling() const {
253  return multiSampling_;
254  }
255 
257  void multisampling( bool _state ) {
258  multiSampling_ = _state;
259  }
260 
262  float alphaValue() const { return alphaClip_; };
263 
264  bool blending() const { return blending_; };
265 
266  GLenum blendingParam1() const { return blendParam1_; };
267  GLenum blendingParam2() const { return blendParam2_; };
268 
270  void enableBlending(GLenum _p1 = GL_SRC_ALPHA,
271  GLenum _p2 = GL_ONE_MINUS_SRC_ALPHA)
272  { blending_ = true; blendParam1_ = _p1; blendParam2_ = _p2; }
274  void disableBlending() { blending_ = false; }
275 
276  bool backfaceCulling() const { return backfaceCulling_; };
277 
279  void enableBackfaceCulling() { backfaceCulling_ = true; }
281  void disableBackfaceCulling() { backfaceCulling_ = false; }
282 
283 protected:
284 
285  Vec4f baseColor_;
286  Vec4f ambientColor_;
287  Vec4f diffuseColor_;
288  Vec4f specularColor_;
289  Vec4f overlayColor_;
290 
291  float shininess_;
292  double reflectance_;
293  float pointSize_;
294  float lineWidth_;
295 
296  bool roundPoints_;
297  bool linesSmooth_;
298 
299  bool alphaTest_;
300  float alphaClip_;
301 
302  bool blending_;
303  GLenum blendParam1_;
304  GLenum blendParam2_;
305 
306  bool colorMaterial_;
307  bool backfaceCulling_;
308 
309  bool multiSampling_;
310 };
311 
312 
321 class ACGDLLEXPORT MaterialNode : public BaseNode
322 {
323 public:
324 
327  {
329  None=0,
331  All=0xffff,
333  BaseColor=1,
337  PointSize=4,
339  LineWidth=8,
341  RoundPoints=16,
343  LineSmooth=32,
345  AlphaTest=64,
347  Blending=128,
349  BackFaceCulling=256,
351  ColorMaterial=512,
353  MultiSampling=1024
354  };
355 
357  MaterialNode( BaseNode* _parent = 0,
358  const std::string& _name = "<MaterialNode>",
359  unsigned int _applyProperties = (All & ~BackFaceCulling));
360 
362  virtual ~MaterialNode() {};
363 
365  void read( std::istream & _is);
366 
367  //===========================================================================
370  //===========================================================================
371 
372  ACG_CLASSNAME(MaterialNode);
373 
375  void enter(GLState& _state, const DrawModes::DrawMode& _drawmode);
377  void leave(GLState& _state, const DrawModes::DrawMode& _drawmode);
378 
379 
381  void enterPick(GLState& _state, PickTarget _target, const DrawModes::DrawMode& _drawMode );
382 
384  void leavePick(GLState& _state, PickTarget _target, const DrawModes::DrawMode& _drawMode );
385 
388  //===========================================================================
391  //===========================================================================
392 
394  void set_color(const Vec4f& _c) {
395  material_.color(_c);
396  }
397 
400  material_.generateRandomColor();
401  }
402 
404  void set_base_color(const Vec4f& _c) { material_.baseColor(_c); }
406  const Vec4f& base_color() const { return material_.baseColor(); }
407 
409  void set_ambient_color(const Vec4f& _a) { material_.ambientColor(_a); }
411  const Vec4f& ambient_color() const { return material_.ambientColor(); }
412 
414  void set_diffuse_color(const Vec4f& _d) { material_.diffuseColor(_d); }
416  const Vec4f& diffuse_color() const { return material_.diffuseColor(); }
417 
419  void set_specular_color(const Vec4f& _s) { material_.specularColor(_s); }
421  const Vec4f& specular_color() const { return material_.specularColor(); }
422 
424  void set_overlay_color(const Vec4f& _s) { material_.overlayColor(_s); }
426  const Vec4f& overlay_color() const { return material_.overlayColor(); }
427 
429  void colorMaterial( const bool _cm) { material_.colorMaterial(_cm); }
431  void enable_color_material() { material_.enableColorMaterial(); }
433  void disable_color_material() { material_.disableColorMaterial(); }
435  bool colorMaterial() { return material_.colorMaterial(); }
436 
438  void set_shininess(float _s) { material_.shininess(_s); }
440  float shininess() const { return material_.shininess(); }
441 
443  void set_reflectance(double _m) { material_.reflectance(_m); }
445  double reflectance() const { return material_.reflectance(); }
446 
449  //===========================================================================
452  //===========================================================================
454  void set_point_size(float _sz) { material_.pointSize(_sz); }
456  float point_size() const { return material_.pointSize(); }
457 
459  void set_line_width(float _sz) { material_.lineWidth(_sz); }
461  float line_width() const { return material_.lineWidth(); }
462 
464  void set_round_points(bool _b) { material_.roundPoints(_b); }
466  bool round_points() const { return material_.roundPoints(); }
467 
469  void set_line_smooth(bool _b) { material_.lineSmooth(_b); }
471  bool line_smooth() const { return material_.lineSmooth(); }
472 
475  //===========================================================================
478  //===========================================================================
479 
481  void enable_alpha_test(float _clip) { material_.enableAlphaTest(_clip); }
482 
484  void disable_alpha_test() { material_.disableAlphaTest(); }
485 
487  bool alpha_test() { return material_.alphaTest(); };
488 
491  //===========================================================================
494  //===========================================================================
495 
497  void enable_multisampling() { material_.enableMultisampling(); }
498 
500  void disable_multisampling() { material_.disableMultisampling(); }
501 
503  bool multiSampling() { return material_.multiSampling(); }
504 
506  void set_multisampling( bool _state ) { material_.multisampling(_state); }
507 
510  float alpha_value(){ return material_.alphaValue(); };
512 
513  bool blending() { return material_.blending(); };
514 
515  GLenum blending_param1() { return material_.blendingParam1(); };
516  GLenum blending_param2() { return material_.blendingParam2(); };
517 
519  void enable_blending(GLenum _p1 = GL_SRC_ALPHA,
520  GLenum _p2 = GL_ONE_MINUS_SRC_ALPHA) {
521  material_.enableBlending(_p1, _p2);
522  }
524  void disable_blending() { material_.disableBlending(); }
525 
526  bool backface_culling() { return material_.backfaceCulling(); };
527 
529  void enable_backface_culling() { material_.enableBackfaceCulling(); }
530 
532  void disable_backface_culling() { material_.disableBackfaceCulling(); }
533 
535  unsigned int applyProperties() const { return applyProperties_; }
536 
538  void applyProperties(unsigned int _applyProperties) {
539  applyProperties_ = _applyProperties;
540  }
541 
543  ACG::SceneGraph::Material& material() { return material_; }
544 
546  void set_material(const ACG::SceneGraph::Material& _m) { material_ = _m; }
547 
548 private:
549 
552 
555 
558 };
559 
560 
561 //=============================================================================
562 } // namespace SceneGraph
563 } // namespace ACG
564 //=============================================================================
565 #endif // ACG_MATERIAL_NODE_HH defined
566 //=============================================================================
567 
float lineWidth() const
get line width
const Vec4f & baseColor() const
get the base color
void disable_alpha_test()
disable alpha test
void specularColor(const Vec4f &_s)
set the specular color
const Vec4f & overlayColor() const
get the overlay color (This can be used to render overlays e.g. additional wireframes in a different ...
virtual ~MaterialNode()
Destructor.
void lineSmooth(bool _b)
set: smooth lines enabled
void disable_backface_culling()
disable backface culling (not active by default, see applyProperties)
const Vec4f & ambient_color() const
get the ambient color.
void set_reflectance(double _m)
set reflectance
void set_random_color()
Generates a random color and sets it.
void roundPoints(bool _b)
set: round points enabled
bool line_smooth() const
get: rsmooth lines enabled
void disableBackfaceCulling()
disable backface culling (not active by default, see applyProperties)
VectorT< float, 4 > Vec4f
Definition: VectorT.hh:144
void enableBackfaceCulling()
enable backface culling (not active by default, see applyProperties)
bool alphaTest() const
Return state of Alpha test.
void enable_color_material()
Enable Color Material.
bool round_points() const
get round points enabled
bool colorMaterial()
get colorMaterial state
void set_specular_color(const Vec4f &_s)
set the specular color
bool alpha_test()
Return state of Alpha test.
void colorMaterial(const bool _cm)
Set colorMaterial.
ACG::SceneGraph::Material & material()
Get material object reference.
float shininess() const
get shininess
void multisampling(bool _state)
Set state of multisampling.
void disable_blending()
disable blending
void set_multisampling(bool _state)
Set state of multisampling.
void disableColorMaterial()
Disable Color Material.
void set_shininess(float _s)
set shininess
ACG::SceneGraph::Material materialBackup_
Material Backup.
void set_ambient_color(const Vec4f &_a)
set the ambient color.
void enableAlphaTest(float _clip)
enable alpha test (draw pixels if alpha >= _clip)
ACG::SceneGraph::MaterialNode MaterialNode
Materialnode.
int applyProperties_
OR&#39;ed ApplyProperties.
void baseColor(const Vec4f &_c)
set the base color
float point_size() const
get point size
void enableBlending(GLenum _p1=GL_SRC_ALPHA, GLenum _p2=GL_ONE_MINUS_SRC_ALPHA)
enable blending with Parameters (_p1, _p2)
float alphaValue() const
get current alpha value for alpha_test
void overlayColor(const Vec4f &_s)
set the overlay color (This can be used to render overlays e.g. additional wireframes in a different ...
float line_width() const
get line width
PickTarget
What target to use for picking.
Definition: BaseNode.hh:99
bool colorMaterial()
get colorMaterial state
void enable_backface_culling()
enable backface culling (not active by default, see applyProperties)
bool multiSampling()
Get state of multisampling.
void ambientColor(const Vec4f &_a)
set the ambient color.
void colorMaterial(const bool _cm)
Set colorMaterial.
bool lineSmooth() const
get: rsmooth lines enabled
void lineWidth(float _sz)
set line width (default: 1.0)
ApplyProperties
Apply which properties? Others will be ignored. Values may be OR&#39;ed.
void enable_blending(GLenum _p1=GL_SRC_ALPHA, GLenum _p2=GL_ONE_MINUS_SRC_ALPHA)
enable blending with Parameters (_p1, _p2)
void disable_color_material()
Disable Color Material.
void set_diffuse_color(const Vec4f &_d)
set the diffuse color.
void enableMultisampling()
Enable Multisampling.
void disable_multisampling()
enable alpha test (draw pixels if alpha >= _clip)
Clear all attribute bits.
Definition: Attributes.hh:86
void diffuseColor(const Vec4f &_d)
set the diffuse color.
void reflectance(double _m)
set reflectance ( not used in OpenGL Rendering)
void set_round_points(bool _b)
set round points enabled
float shininess() const
get shininess
const Vec4f & overlay_color() const
get the overlay color
const Vec4f & diffuse_color() const
get the diffuse color.
ACG::SceneGraph::Material material_
Local material class that actually stores the properties.
void set_overlay_color(const Vec4f &_s)
set the overlay color
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
unsigned int applyProperties() const
get properties that will be applied (OR&#39;ed ApplyProperties)
Material(const Material &_m)
Copy constructor.
void disableBlending()
disable blending
void applyProperties(unsigned int _applyProperties)
set properties that will be applied (OR&#39;ed ApplyProperties)
const Vec4f & specularColor() const
get the specular color
void set_line_smooth(bool _b)
set: smooth lines enabled
void set_line_width(float _sz)
set line width (default: 1.0)
void enableColorMaterial()
Enable Color Material.
const Vec4f & base_color() const
get the base color
void disableMultisampling()
enable alpha test (draw pixels if alpha >= _clip)
void generateRandomColor()
Creates a randomized color and sets it.
void enable_multisampling()
Enable Multisampling.
void shininess(float _s)
set shininess
void set_color(const Vec4f &_c)
set color (base, ambient, diffuse, specular) based on _c
const Vec4f & diffuseColor() const
get the diffuse color.
double reflectance() const
get reflectance ( not used in OpenGL Rendering)
bool roundPoints() const
get: round points enabled
void enable_alpha_test(float _clip)
enable alpha test (draw pixels if alpha >= _clip)
void color(const Vec4f &_c)
Set color based on _c.
float pointSize() const
get point size
void pointSize(float _sz)
set point size (default: 1.0)
void set_base_color(const Vec4f &_c)
set the base color
bool multiSampling() const
Get state of multisampling.
const Vec4f & specular_color() const
get the specular color
void disableAlphaTest()
disable alpha test
Material()
Default constructor.
Definition: MaterialNode.hh:91
virtual ~Material()
Deconstructor.
void set_material(const ACG::SceneGraph::Material &_m)
Set material object.
double reflectance() const
get reflectance
void set_point_size(float _sz)
set point size (default: 1.0)
const Vec4f & ambientColor() const
get the ambient color.