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 
45 
46 
47 //=============================================================================
48 //
49 // CLASS MaterialNode
50 //
51 //=============================================================================
52 
53 #ifndef ACG_MATERIAL_NODE_HH
54 #define ACG_MATERIAL_NODE_HH
55 
56 
57 //== INCLUDES =================================================================
58 
59 #include "BaseNode.hh"
60 #include <string>
61 #include <QVariantMap>
62 
63 //== NAMESPACES ===============================================================
64 
65 namespace ACG {
66 
67 QVariantMap ACGDLLEXPORT json_to_variant_map(QString json);
68 
69 namespace SceneGraph {
70 
71 
72 //== CLASS DEFINITION =========================================================
73 
79 class ACGDLLEXPORT Material {
80 
81  friend class MaterialNode;
82 
83 public:
86  baseColor_(GLState::default_base_color),
87  ambientColor_(GLState::default_ambient_color),
88  diffuseColor_(GLState::default_diffuse_color),
89  specularColor_(GLState::default_specular_color),
90  overlayColor_(GLState::default_overlay_color),
91  shininess_(GLState::default_shininess),
92  reflectance_(0.0),
93  indexOfRefraction_(1.0),
94  isRefractive_(false),
95  pointSize_(1.0),
96  lineWidth_(1.0),
97  roundPoints_(false),
98  linesSmooth_(false),
99  alphaTest_(false),
100  alphaClip_(0),
101  blending_(false),
102  blendParam1_(GL_SRC_ALPHA),
103  blendParam2_(GL_ONE_MINUS_SRC_ALPHA),
104  colorMaterial_(true),
105  backfaceCulling_(false),
106  multiSampling_(true) {};
107 
109  virtual ~Material() {};
110 
112  Material(const Material& _m) :
113  baseColor_(_m.baseColor_),
114  ambientColor_(_m.ambientColor_),
115  diffuseColor_(_m.diffuseColor_),
116  specularColor_(_m.specularColor_),
117  overlayColor_(_m.overlayColor_),
118  shininess_(_m.shininess_),
119  reflectance_(_m.reflectance_),
120  indexOfRefraction_(_m.indexOfRefraction_),
121  isRefractive_(_m.isRefractive_),
122  pointSize_(_m.pointSize_),
123  lineWidth_(_m.lineWidth_),
124  roundPoints_(_m.roundPoints_),
125  linesSmooth_(_m.linesSmooth_),
126  alphaTest_(_m.alphaTest_),
127  alphaClip_(_m.alphaClip_),
128  blending_(_m.blending_),
129  blendParam1_(_m.blendParam1_),
130  blendParam2_(_m.blendParam2_),
131  colorMaterial_(_m.colorMaterial_),
132  backfaceCulling_(_m.backfaceCulling_),
133  multiSampling_(_m.multiSampling_) {};
134 
135  static bool support_json_serialization();
136  QString serializeToJson() const;
137  void deserializeFromJson(const QString &json);
138  void deserializeFromVariantMap(const QVariantMap &matMap);
139 
147  void color(const Vec4f& _c) {
148  Vec4f c(0.0,0.0,0.0,1.0);
149  baseColor(c);
150  overlayColor(c);
151  c = _c * 0.2f; c[3]=_c[3]; ambientColor(c);
152  c = _c * 0.6f; c[3]=_c[3]; diffuseColor(c);
153  c = _c * 0.8f; c[3]=_c[3]; specularColor(c);
154  }
155 
158  color( Vec4f(0.2 + double(rand())/double(RAND_MAX)*0.8,
159  0.2 + double(rand())/double(RAND_MAX)*0.8,
160  0.2 + double(rand())/double(RAND_MAX)*0.8,
161  1.0));
162  }
163 
165  void baseColor(const Vec4f& _c) { baseColor_ = _c;}
167  const Vec4f& baseColor() const { return baseColor_; }
168 
170  void emissionColor(const Vec4f& _c) { baseColor_ = _c;}
172  const Vec4f& emissionColor() const { return baseColor_; }
173 
175  void ambientColor(const Vec4f& _a) { ambientColor_ = _a; }
177  const Vec4f& ambientColor() const { return ambientColor_; }
178 
180  void diffuseColor(const Vec4f& _d) { diffuseColor_ = _d; }
182  const Vec4f& diffuseColor() const { return diffuseColor_; }
183 
185  void specularColor(const Vec4f& _s) { specularColor_ = _s; }
187  const Vec4f& specularColor() const { return specularColor_; }
188 
190  void overlayColor(const Vec4f& _s) { overlayColor_ = _s; }
192  const Vec4f& overlayColor() const { return overlayColor_; }
193 
195  void colorMaterial( const bool _cm) { colorMaterial_ = _cm; }
197  void enableColorMaterial() { colorMaterial_ = true; }
199  void disableColorMaterial() { colorMaterial_ = false; }
201  bool colorMaterial() { return colorMaterial_; }
202 
204  void shininess(float _s) { shininess_ = _s; }
206  float shininess() const { return shininess_; }
207 
209  void reflectance(double _m) { reflectance_ = _m; }
211  double reflectance() const { return reflectance_; }
212 
214  void indexOfRefraction(double _m) { indexOfRefraction_ = _m; }
216  double indexOfRefraction() const { return indexOfRefraction_; }
217 
219  void setRefractive(bool _r) { isRefractive_ = _r; }
221  bool isRefractive() const {return isRefractive_;}
222 
224  void pointSize(float _sz) { pointSize_ = _sz; }
226  float pointSize() const { return pointSize_; }
227 
229  void lineWidth(float _sz) { lineWidth_ = _sz; }
231  float lineWidth() const { return lineWidth_; }
232 
234  void roundPoints(bool _b) { roundPoints_ = _b; }
236  bool roundPoints() const { return roundPoints_; }
237 
239  void lineSmooth(bool _b) { linesSmooth_ = _b; }
241  bool lineSmooth() const { return linesSmooth_; }
242 
244  void enableAlphaTest(float _clip) {
245  alphaTest_ = true; alphaClip_ = _clip;
246  }
247 
249  void disableAlphaTest() { alphaTest_ = false; }
250 
252  bool alphaTest() const { return alphaTest_; };
253 
256  multiSampling_ = true;
257  }
258 
261  multiSampling_ = false;
262  }
263 
265  bool multiSampling() const {
266  return multiSampling_;
267  }
268 
270  void multisampling( bool _state ) {
271  multiSampling_ = _state;
272  }
273 
275  float alphaValue() const { return alphaClip_; };
276 
277  bool blending() const { return blending_; };
278 
279  GLenum blendingParam1() const { return blendParam1_; };
280  GLenum blendingParam2() const { return blendParam2_; };
281 
283  void enableBlending(GLenum _p1 = GL_SRC_ALPHA,
284  GLenum _p2 = GL_ONE_MINUS_SRC_ALPHA)
285  { blending_ = true; blendParam1_ = _p1; blendParam2_ = _p2; }
287  void disableBlending() { blending_ = false; }
288 
289  bool backfaceCulling() const { return backfaceCulling_; };
290 
292  void enableBackfaceCulling() { backfaceCulling_ = true; }
294  void disableBackfaceCulling() { backfaceCulling_ = false; }
295 
296  bool isEmissive() const { return (baseColor_[0] > 0.f || baseColor_[1] > 0.f || baseColor_[2] > 0.f); }
297 
298 protected:
299 
300  Vec4f baseColor_;
301  Vec4f ambientColor_;
302  Vec4f diffuseColor_;
303  Vec4f specularColor_;
304  Vec4f overlayColor_;
305 
306  float shininess_;
307  double reflectance_;
308  double indexOfRefraction_;
309  bool isRefractive_;
310  float pointSize_;
311  float lineWidth_;
312 
313  bool roundPoints_;
314  bool linesSmooth_;
315 
316  bool alphaTest_;
317  float alphaClip_;
318 
319  bool blending_;
320  GLenum blendParam1_;
321  GLenum blendParam2_;
322 
323  bool colorMaterial_;
324  bool backfaceCulling_;
325 
326  bool multiSampling_;
327 };
328 
329 
338 class ACGDLLEXPORT MaterialNode : public BaseNode
339 {
340 public:
341 
344  {
346  None=0,
348  All=0xffff,
350  BaseColor=1,
354  PointSize=4,
356  LineWidth=8,
358  RoundPoints=16,
360  LineSmooth=32,
362  AlphaTest=64,
364  Blending=128,
366  BackFaceCulling=256,
368  ColorMaterial=512,
370  MultiSampling=1024
371  };
372 
374  MaterialNode( BaseNode* _parent = 0,
375  const std::string& _name = "<MaterialNode>",
376  unsigned int _applyProperties = (All & ~BackFaceCulling));
377 
379  virtual ~MaterialNode() {};
380 
382  void read( std::istream & _is);
383 
384  //===========================================================================
387  //===========================================================================
388 
389  ACG_CLASSNAME(MaterialNode);
390 
392  void enter(GLState& _state, const DrawModes::DrawMode& _drawmode);
394  void leave(GLState& _state, const DrawModes::DrawMode& _drawmode);
395 
396 
398  void enterPick(GLState& _state, PickTarget _target, const DrawModes::DrawMode& _drawMode );
399 
401  void leavePick(GLState& _state, PickTarget _target, const DrawModes::DrawMode& _drawMode );
402 
405  //===========================================================================
408  //===========================================================================
409 
411  void set_color(const Vec4f& _c) {
412  material_.color(_c);
413  }
414 
417  material_.generateRandomColor();
418  }
419 
421  void set_base_color(const Vec4f& _c) { material_.baseColor(_c); }
423  const Vec4f& base_color() const { return material_.baseColor(); }
424 
426  void set_emission(const Vec4f& _c) { material_.baseColor(_c); }
428  const Vec4f& emission() const { return material_.baseColor(); }
429 
431  void set_ambient_color(const Vec4f& _a) { material_.ambientColor(_a); }
433  const Vec4f& ambient_color() const { return material_.ambientColor(); }
434 
436  void set_diffuse_color(const Vec4f& _d) { material_.diffuseColor(_d); }
438  const Vec4f& diffuse_color() const { return material_.diffuseColor(); }
439 
441  void set_specular_color(const Vec4f& _s) { material_.specularColor(_s); }
443  const Vec4f& specular_color() const { return material_.specularColor(); }
444 
446  void set_overlay_color(const Vec4f& _s) { material_.overlayColor(_s); }
448  const Vec4f& overlay_color() const { return material_.overlayColor(); }
449 
451  void colorMaterial( const bool _cm) { material_.colorMaterial(_cm); }
453  void enable_color_material() { material_.enableColorMaterial(); }
455  void disable_color_material() { material_.disableColorMaterial(); }
457  bool colorMaterial() { return material_.colorMaterial(); }
458 
460  void set_shininess(float _s) { material_.shininess(_s); }
462  float shininess() const { return material_.shininess(); }
463 
465  void set_reflectance(double _m) { material_.reflectance(_m); }
467  double reflectance() const { return material_.reflectance(); }
468 
470  void set_indexOfRefraction(double _m) { material_.indexOfRefraction(_m); }
472  double indexOfRefraction() const { return material_.indexOfRefraction(); }
473 
475  void set_refractive(bool _r) { material_.isRefractive_ = _r; }
477  bool isRefractive() const {return material_.isRefractive_;}
478 
481  //===========================================================================
484  //===========================================================================
486  void set_point_size(float _sz) { material_.pointSize(_sz); }
488  float point_size() const { return material_.pointSize(); }
489 
491  void set_line_width(float _sz) { material_.lineWidth(_sz); }
493  float line_width() const { return material_.lineWidth(); }
494 
496  void set_round_points(bool _b) { material_.roundPoints(_b); }
498  bool round_points() const { return material_.roundPoints(); }
499 
501  void set_line_smooth(bool _b) { material_.lineSmooth(_b); }
503  bool line_smooth() const { return material_.lineSmooth(); }
504 
507  //===========================================================================
510  //===========================================================================
511 
513  void enable_alpha_test(float _clip) { material_.enableAlphaTest(_clip); }
514 
516  void disable_alpha_test() { material_.disableAlphaTest(); }
517 
519  bool alpha_test() { return material_.alphaTest(); };
520 
523  //===========================================================================
526  //===========================================================================
527 
529  void enable_multisampling() { material_.enableMultisampling(); }
530 
532  void disable_multisampling() { material_.disableMultisampling(); }
533 
535  bool multiSampling() { return material_.multiSampling(); }
536 
538  void set_multisampling( bool _state ) { material_.multisampling(_state); }
539 
542  float alpha_value(){ return material_.alphaValue(); };
544 
545  bool blending() { return material_.blending(); };
546 
547  GLenum blending_param1() { return material_.blendingParam1(); };
548  GLenum blending_param2() { return material_.blendingParam2(); };
549 
551  void enable_blending(GLenum _p1 = GL_SRC_ALPHA,
552  GLenum _p2 = GL_ONE_MINUS_SRC_ALPHA) {
553  material_.enableBlending(_p1, _p2);
554  }
556  void disable_blending() { material_.disableBlending(); }
557 
558  bool backface_culling() { return material_.backfaceCulling(); };
559 
561  void enable_backface_culling() { material_.enableBackfaceCulling(); }
562 
564  void disable_backface_culling() { material_.disableBackfaceCulling(); }
565 
567  unsigned int applyProperties() const { return applyProperties_; }
568 
570  void applyProperties(unsigned int _applyProperties) {
571  applyProperties_ = _applyProperties;
572  }
573 
575  ACG::SceneGraph::Material& material() { return material_; }
576 
578  void set_material(const ACG::SceneGraph::Material& _m) { material_ = _m; }
579 
580 private:
581 
584 
587 
590 };
591 
592 
593 //=============================================================================
594 } // namespace SceneGraph
595 } // namespace ACG
596 //=============================================================================
597 #endif // ACG_MATERIAL_NODE_HH defined
598 //=============================================================================
599 
void enableAlphaTest(float _clip)
enable alpha test (draw pixels if alpha >= _clip)
void disable_multisampling()
enable alpha test (draw pixels if alpha >= _clip)
bool isRefractive() const
get refractive flag
void colorMaterial(const bool _cm)
Set colorMaterial.
const Vec4f & emissionColor() const
get emission ( Same as baseColor() )
Namespace providing different geometric functions concerning angles.
void disableColorMaterial()
Disable Color Material.
void lineSmooth(bool _b)
set: smooth lines enabled
ACG::SceneGraph::MaterialNode MaterialNode
Materialnode.
void set_diffuse_color(const Vec4f &_d)
set the diffuse color.
void lineWidth(float _sz)
set line width (default: 1.0)
void indexOfRefraction(double _m)
set index of refraction
void enable_color_material()
Enable Color Material.
int applyProperties_
OR&#39;ed ApplyProperties.
ApplyProperties
Apply which properties? Others will be ignored. Values may be OR&#39;ed.
void enableColorMaterial()
Enable Color Material.
Clear all attribute bits.
Definition: Attributes.hh:81
void ambientColor(const Vec4f &_a)
set the ambient color.
void set_round_points(bool _b)
set round points enabled
float lineWidth() const
get line width
void set_specular_color(const Vec4f &_s)
set the specular color
void enableMultisampling()
Enable Multisampling.
const Vec4f & diffuse_color() const
get the diffuse color.
float alphaValue() const
get current alpha value for alpha_test
void set_color(const Vec4f &_c)
set color (base, ambient, diffuse, specular) based on _c
float line_width() const
get line width
void set_material(const ACG::SceneGraph::Material &_m)
Set material object.
double indexOfRefraction() const
get index of refraction ( not used in OpenGL Rendering)
void pointSize(float _sz)
set point size (default: 1.0)
void set_emission(const Vec4f &_c)
set emission ( same as set_base_color(const Vec4f& _c) )
bool line_smooth() const
get: rsmooth lines enabled
void emissionColor(const Vec4f &_c)
set emission ( Same as baseColor( const Vec4f& _c )) )
void set_multisampling(bool _state)
Set state of multisampling.
VectorT< float, 4 > Vec4f
Definition: VectorT.hh:138
ACG::SceneGraph::Material materialBackup_
Material Backup.
void roundPoints(bool _b)
set: round points enabled
void specularColor(const Vec4f &_s)
set the specular color
float shininess() const
get shininess
bool colorMaterial()
get colorMaterial state
void diffuseColor(const Vec4f &_d)
set the diffuse color.
double reflectance() const
get reflectance
bool alpha_test()
Return state of Alpha test.
void disable_blending()
disable blending
void disable_backface_culling()
disable backface culling (not active by default, see applyProperties)
void multisampling(bool _state)
Set state of multisampling.
void set_line_smooth(bool _b)
set: smooth lines enabled
void enableBackfaceCulling()
enable backface culling (not active by default, see applyProperties)
const Vec4f & baseColor() const
get the base color ( Same as emission() )
void enableBlending(GLenum _p1=GL_SRC_ALPHA, GLenum _p2=GL_ONE_MINUS_SRC_ALPHA)
enable blending with Parameters (_p1, _p2)
const Vec4f & ambientColor() const
get the ambient color.
void disableBackfaceCulling()
disable backface culling (not active by default, see applyProperties)
void overlayColor(const Vec4f &_s)
set the overlay color (This can be used to render overlays e.g. additional wireframes in a different ...
virtual ~Material()
Deconstructor.
float pointSize() const
get point size
Material()
Default constructor.
Definition: MaterialNode.hh:85
ACG::SceneGraph::Material material_
Local material class that actually stores the properties.
void reflectance(double _m)
set reflectance ( not used in OpenGL Rendering)
void baseColor(const Vec4f &_c)
set the base color (Sets the baseColor which is the same as the emission(const Vec4f& _c) ) ...
bool multiSampling()
Get state of multisampling.
void set_line_width(float _sz)
set line width (default: 1.0)
void set_indexOfRefraction(double _m)
set index of refraction
void enable_blending(GLenum _p1=GL_SRC_ALPHA, GLenum _p2=GL_ONE_MINUS_SRC_ALPHA)
enable blending with Parameters (_p1, _p2)
float shininess() const
get shininess
double indexOfRefraction() const
get index of refraction
void set_overlay_color(const Vec4f &_s)
set the overlay color
bool round_points() const
get round points enabled
bool colorMaterial()
get colorMaterial state
Material(const Material &_m)
Copy constructor.
void color(const Vec4f &_c)
Set color based on _c.
const Vec4f & ambient_color() const
get the ambient color.
void set_random_color()
Generates a random color and sets it.
void set_shininess(float _s)
set shininess
unsigned int applyProperties() const
get properties that will be applied (OR&#39;ed ApplyProperties)
const Vec4f & emission() const
get emission ( same as base_color() )
const Vec4f & specularColor() const
get the specular color
void disable_color_material()
Disable Color Material.
void generateRandomColor()
Creates a randomized color and sets it.
const Vec4f & specular_color() const
get the specular color
bool alphaTest() const
Return state of Alpha test.
void set_refractive(bool _r)
set refractive flag
void set_ambient_color(const Vec4f &_a)
set the ambient color.
void set_point_size(float _sz)
set point size (default: 1.0)
PickTarget
What target to use for picking.
Definition: PickTarget.hh:73
double reflectance() const
get reflectance ( not used in OpenGL Rendering)
void colorMaterial(const bool _cm)
Set colorMaterial.
const Vec4f & base_color() const
get the base color ( same as emission() )
void setRefractive(bool _r)
set refractive flag
ACG::SceneGraph::Material & material()
Get material object reference.
const Vec4f & diffuseColor() const
get the diffuse color.
float point_size() const
get point size
bool isRefractive() const
get refractive flag
void disableAlphaTest()
disable alpha test
void set_reflectance(double _m)
set reflectance
void disableBlending()
disable blending
void enable_backface_culling()
enable backface culling (not active by default, see applyProperties)
void enable_alpha_test(float _clip)
enable alpha test (draw pixels if alpha >= _clip)
const Vec4f & overlayColor() const
get the overlay color (This can be used to render overlays e.g. additional wireframes in a different ...
void set_base_color(const Vec4f &_c)
set the base color ( Same as set_emission(const Vec4f& _c) )
void shininess(float _s)
set shininess
virtual ~MaterialNode()
Destructor.
void disable_alpha_test()
disable alpha test
void enable_multisampling()
Enable Multisampling.
void disableMultisampling()
enable alpha test (draw pixels if alpha >= _clip)
void applyProperties(unsigned int _applyProperties)
set properties that will be applied (OR&#39;ed ApplyProperties)
bool lineSmooth() const
get: rsmooth lines enabled
const Vec4f & overlay_color() const
get the overlay color
bool roundPoints() const
get: round points enabled
bool multiSampling() const
Get state of multisampling.