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
65namespace ACG {
66
67QVariantMap ACGDLLEXPORT json_to_variant_map(QString json);
68
69namespace SceneGraph {
70
71
72//== CLASS DEFINITION =========================================================
73
79class ACGDLLEXPORT Material {
80
81 friend class MaterialNode;
82
83public:
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&) = default;
113
114 static bool support_json_serialization();
115 QString serializeToJson() const;
116 void deserializeFromJson(const QString &json);
117 void deserializeFromVariantMap(const QVariantMap &matMap);
118
126 void color(const Vec4f& _c) {
127 Vec4f c(0.0,0.0,0.0,1.0);
128 baseColor(c);
129 overlayColor(c);
130 c = _c * 0.2f; c[3]=_c[3]; ambientColor(c);
131 c = _c * 0.6f; c[3]=_c[3]; diffuseColor(c);
132 c = _c * 0.8f; c[3]=_c[3]; specularColor(c);
133 }
134
137 color( Vec4f(0.2 + double(rand())/double(RAND_MAX)*0.8,
138 0.2 + double(rand())/double(RAND_MAX)*0.8,
139 0.2 + double(rand())/double(RAND_MAX)*0.8,
140 1.0));
141 }
142
144 void baseColor(const Vec4f& _c) { baseColor_ = _c;}
146 const Vec4f& baseColor() const { return baseColor_; }
147
149 void emissionColor(const Vec4f& _c) { baseColor_ = _c;}
151 const Vec4f& emissionColor() const { return baseColor_; }
152
154 void ambientColor(const Vec4f& _a) { ambientColor_ = _a; }
156 const Vec4f& ambientColor() const { return ambientColor_; }
157
159 void diffuseColor(const Vec4f& _d) { diffuseColor_ = _d; }
161 const Vec4f& diffuseColor() const { return diffuseColor_; }
162
164 void specularColor(const Vec4f& _s) { specularColor_ = _s; }
166 const Vec4f& specularColor() const { return specularColor_; }
167
169 void overlayColor(const Vec4f& _s) { overlayColor_ = _s; }
171 const Vec4f& overlayColor() const { return overlayColor_; }
172
174 void colorMaterial( const bool _cm) { colorMaterial_ = _cm; }
176 void enableColorMaterial() { colorMaterial_ = true; }
178 void disableColorMaterial() { colorMaterial_ = false; }
180 bool colorMaterial() { return colorMaterial_; }
181
183 void shininess(float _s) { shininess_ = _s; }
185 float shininess() const { return shininess_; }
186
188 void reflectance(double _m) { reflectance_ = _m; }
190 double reflectance() const { return reflectance_; }
191
193 void indexOfRefraction(double _m) { indexOfRefraction_ = _m; }
195 double indexOfRefraction() const { return indexOfRefraction_; }
196
198 void setRefractive(bool _r) { isRefractive_ = _r; }
200 bool isRefractive() const {return isRefractive_;}
201
203 void pointSize(float _sz) { pointSize_ = _sz; }
205 float pointSize() const { return pointSize_; }
206
208 void lineWidth(float _sz) { lineWidth_ = _sz; }
210 float lineWidth() const { return lineWidth_; }
211
213 void roundPoints(bool _b) { roundPoints_ = _b; }
215 bool roundPoints() const { return roundPoints_; }
216
218 void lineSmooth(bool _b) { linesSmooth_ = _b; }
220 bool lineSmooth() const { return linesSmooth_; }
221
223 void enableAlphaTest(float _clip) {
224 alphaTest_ = true; alphaClip_ = _clip;
225 }
226
228 void disableAlphaTest() { alphaTest_ = false; }
229
231 bool alphaTest() const { return alphaTest_; };
232
235 multiSampling_ = true;
236 }
237
240 multiSampling_ = false;
241 }
242
244 bool multiSampling() const {
245 return multiSampling_;
246 }
247
249 void multisampling( bool _state ) {
250 multiSampling_ = _state;
251 }
252
254 float alphaValue() const { return alphaClip_; };
255
256 bool blending() const { return blending_; };
257
258 GLenum blendingParam1() const { return blendParam1_; };
259 GLenum blendingParam2() const { return blendParam2_; };
260
262 void enableBlending(GLenum _p1 = GL_SRC_ALPHA,
263 GLenum _p2 = GL_ONE_MINUS_SRC_ALPHA)
264 { blending_ = true; blendParam1_ = _p1; blendParam2_ = _p2; }
266 void disableBlending() { blending_ = false; }
267
268 bool backfaceCulling() const { return backfaceCulling_; };
269
271 void enableBackfaceCulling() { backfaceCulling_ = true; }
273 void disableBackfaceCulling() { backfaceCulling_ = false; }
274
275 bool isEmissive() const { return (baseColor_[0] > 0.f || baseColor_[1] > 0.f || baseColor_[2] > 0.f); }
276
277protected:
278
279 Vec4f baseColor_;
280 Vec4f ambientColor_;
281 Vec4f diffuseColor_;
282 Vec4f specularColor_;
283 Vec4f overlayColor_;
284
285 float shininess_;
286 double reflectance_;
287 double indexOfRefraction_;
288 bool isRefractive_;
289 float pointSize_;
290 float lineWidth_;
291
292 bool roundPoints_;
293 bool linesSmooth_;
294
295 bool alphaTest_;
296 float alphaClip_;
297
298 bool blending_;
299 GLenum blendParam1_;
300 GLenum blendParam2_;
301
302 bool colorMaterial_;
303 bool backfaceCulling_;
304
305 bool multiSampling_;
306};
307
308
317class ACGDLLEXPORT MaterialNode : public BaseNode
318{
319public:
320
323 {
325 None=0,
327 All=0xffff,
329 BaseColor=1,
333 PointSize=4,
335 LineWidth=8,
337 RoundPoints=16,
339 LineSmooth=32,
341 AlphaTest=64,
343 Blending=128,
345 BackFaceCulling=256,
347 ColorMaterial=512,
349 MultiSampling=1024
350 };
351
353 MaterialNode( BaseNode* _parent = 0,
354 const std::string& _name = "<MaterialNode>",
355 unsigned int _applyProperties = (All & ~BackFaceCulling));
356
358 virtual ~MaterialNode() {};
359
361 void read( std::istream & _is);
362
363 //===========================================================================
366 //===========================================================================
367
369
371 void enter(GLState& _state, const DrawModes::DrawMode& _drawmode) override;
373 void leave(GLState& _state, const DrawModes::DrawMode& _drawmode) override;
374
375
377 void enterPick(GLState& _state, PickTarget _target, const DrawModes::DrawMode& _drawMode ) override;
378
380 void leavePick(GLState& _state, PickTarget _target, const DrawModes::DrawMode& _drawMode ) override;
381
384 //===========================================================================
387 //===========================================================================
388
390 void set_color(const Vec4f& _c) {
391 material_.color(_c);
392 }
393
396 material_.generateRandomColor();
397 }
398
400 void set_base_color(const Vec4f& _c) { material_.baseColor(_c); }
402 const Vec4f& base_color() const { return material_.baseColor(); }
403
405 void set_emission(const Vec4f& _c) { material_.baseColor(_c); }
407 const Vec4f& emission() const { return material_.baseColor(); }
408
410 void set_ambient_color(const Vec4f& _a) { material_.ambientColor(_a); }
412 const Vec4f& ambient_color() const { return material_.ambientColor(); }
413
415 void set_diffuse_color(const Vec4f& _d) { material_.diffuseColor(_d); }
417 const Vec4f& diffuse_color() const { return material_.diffuseColor(); }
418
420 void set_specular_color(const Vec4f& _s) { material_.specularColor(_s); }
422 const Vec4f& specular_color() const { return material_.specularColor(); }
423
425 void set_overlay_color(const Vec4f& _s) { material_.overlayColor(_s); }
427 const Vec4f& overlay_color() const { return material_.overlayColor(); }
428
430 void colorMaterial( const bool _cm) { material_.colorMaterial(_cm); }
432 void enable_color_material() { material_.enableColorMaterial(); }
434 void disable_color_material() { material_.disableColorMaterial(); }
436 bool colorMaterial() { return material_.colorMaterial(); }
437
439 void set_shininess(float _s) { material_.shininess(_s); }
441 float shininess() const { return material_.shininess(); }
442
444 void set_reflectance(double _m) { material_.reflectance(_m); }
446 double reflectance() const { return material_.reflectance(); }
447
449 void set_indexOfRefraction(double _m) { material_.indexOfRefraction(_m); }
451 double indexOfRefraction() const { return material_.indexOfRefraction(); }
452
454 void set_refractive(bool _r) { material_.isRefractive_ = _r; }
456 bool isRefractive() const {return material_.isRefractive_;}
457
460 //===========================================================================
463 //===========================================================================
465 void set_point_size(float _sz) { material_.pointSize(_sz); }
467 float point_size() const { return material_.pointSize(); }
468
470 void set_line_width(float _sz) { material_.lineWidth(_sz); }
472 float line_width() const { return material_.lineWidth(); }
473
475 void set_round_points(bool _b) { material_.roundPoints(_b); }
477 bool round_points() const { return material_.roundPoints(); }
478
480 void set_line_smooth(bool _b) { material_.lineSmooth(_b); }
482 bool line_smooth() const { return material_.lineSmooth(); }
483
486 //===========================================================================
489 //===========================================================================
490
492 void enable_alpha_test(float _clip) { material_.enableAlphaTest(_clip); }
493
495 void disable_alpha_test() { material_.disableAlphaTest(); }
496
498 bool alpha_test() { return material_.alphaTest(); };
499
502 //===========================================================================
505 //===========================================================================
506
508 void enable_multisampling() { material_.enableMultisampling(); }
509
511 void disable_multisampling() { material_.disableMultisampling(); }
512
514 bool multiSampling() { return material_.multiSampling(); }
515
517 void set_multisampling( bool _state ) { material_.multisampling(_state); }
518
522 float alpha_value(){ return material_.alphaValue(); };
523
524 bool blending() { return material_.blending(); };
525
526 GLenum blending_param1() { return material_.blendingParam1(); };
527 GLenum blending_param2() { return material_.blendingParam2(); };
528
530 void enable_blending(GLenum _p1 = GL_SRC_ALPHA,
531 GLenum _p2 = GL_ONE_MINUS_SRC_ALPHA) {
532 material_.enableBlending(_p1, _p2);
533 }
535 void disable_blending() { material_.disableBlending(); }
536
537 bool backface_culling() { return material_.backfaceCulling(); };
538
540 void enable_backface_culling() { material_.enableBackfaceCulling(); }
541
543 void disable_backface_culling() { material_.disableBackfaceCulling(); }
544
546 unsigned int applyProperties() const { return applyProperties_; }
547
549 void applyProperties(unsigned int _applyProperties) {
550 applyProperties_ = _applyProperties;
551 }
552
554 ACG::SceneGraph::Material& material() { return material_; }
555
557 void set_material(const ACG::SceneGraph::Material& _m) { material_ = _m; }
558
559private:
560
563
566
569};
570
571
572//=============================================================================
573} // namespace SceneGraph
574} // namespace ACG
575//=============================================================================
576#endif // ACG_MATERIAL_NODE_HH defined
577//=============================================================================
578
ACG::SceneGraph::MaterialNode MaterialNode
Materialnode.
void set_multisampling(bool _state)
Set state of multisampling.
void disable_blending()
disable blending
bool multiSampling()
Get state of multisampling.
void set_overlay_color(const Vec4f &_s)
set the overlay color
void enable_multisampling()
Enable Multisampling.
void set_diffuse_color(const Vec4f &_d)
set the diffuse color.
void set_specular_color(const Vec4f &_s)
set the specular color
double indexOfRefraction() const
get index of refraction
void enable_backface_culling()
enable backface culling (not active by default, see applyProperties)
bool colorMaterial()
get colorMaterial state
void set_color(const Vec4f &_c)
set color (base, ambient, diffuse, specular) based on _c
bool isRefractive() const
get refractive flag
int applyProperties_
OR'ed ApplyProperties.
void set_material(const ACG::SceneGraph::Material &_m)
Set material object.
ACG::SceneGraph::Material materialBackup_
Material Backup.
void set_refractive(bool _r)
set refractive flag
void applyProperties(unsigned int _applyProperties)
set properties that will be applied (OR'ed ApplyProperties)
const Vec4f & base_color() const
get the base color ( same as emission() )
void enable_alpha_test(float _clip)
enable alpha test (draw pixels if alpha >= _clip)
void set_random_color()
Generates a random color and sets it.
ACG::SceneGraph::Material material_
Local material class that actually stores the properties.
void disable_backface_culling()
disable backface culling (not active by default, see applyProperties)
void set_indexOfRefraction(double _m)
set index of refraction
void set_line_smooth(bool _b)
set: smooth lines enabled
ApplyProperties
Apply which properties? Others will be ignored. Values may be OR'ed.
void enable_color_material()
Enable Color Material.
void set_base_color(const Vec4f &_c)
set the base color ( Same as set_emission(const Vec4f& _c) )
unsigned int applyProperties() const
get properties that will be applied (OR'ed ApplyProperties)
float shininess() const
get shininess
void set_reflectance(double _m)
set reflectance
float alpha_value()
get current alpha value for alpha_test
void disable_color_material()
Disable Color Material.
double reflectance() const
get reflectance
bool round_points() const
get round points enabled
void set_round_points(bool _b)
set round points enabled
const Vec4f & diffuse_color() const
get the diffuse color.
const Vec4f & overlay_color() const
get the overlay color
void set_line_width(float _sz)
set line width (default: 1.0)
float line_width() const
get line width
void set_ambient_color(const Vec4f &_a)
set the ambient color.
ACG_CLASSNAME(MaterialNode)
set current GL-color and GL-material
bool alpha_test()
Return state of Alpha test.
const Vec4f & ambient_color() const
get the ambient color.
ACG::SceneGraph::Material & material()
Get material object reference.
const Vec4f & emission() const
get emission ( same as base_color() )
void set_emission(const Vec4f &_c)
set emission ( same as set_base_color(const Vec4f& _c) )
void disable_alpha_test()
disable alpha test
bool line_smooth() const
get: rsmooth lines enabled
void disable_multisampling()
enable alpha test (draw pixels if alpha >= _clip)
void enable_blending(GLenum _p1=GL_SRC_ALPHA, GLenum _p2=GL_ONE_MINUS_SRC_ALPHA)
enable blending with Parameters (_p1, _p2)
const Vec4f & specular_color() const
get the specular color
void colorMaterial(const bool _cm)
Set colorMaterial.
void set_shininess(float _s)
set shininess
void set_point_size(float _sz)
set point size (default: 1.0)
float point_size() const
get point size
virtual ~MaterialNode()
Destructor.
virtual ~Material()
Deconstructor.
bool isRefractive() const
get refractive flag
void disableMultisampling()
enable alpha test (draw pixels if alpha >= _clip)
void setRefractive(bool _r)
set refractive flag
Material(const Material &)=default
Copy constructor.
void lineWidth(float _sz)
set line width (default: 1.0)
bool roundPoints() const
get: round points enabled
void enableColorMaterial()
Enable Color Material.
bool colorMaterial()
get colorMaterial state
const Vec4f & specularColor() const
get the specular color
const Vec4f & ambientColor() const
get the ambient color.
void indexOfRefraction(double _m)
set index of refraction
void disableBlending()
disable blending
void roundPoints(bool _b)
set: round points enabled
void disableColorMaterial()
Disable Color Material.
const Vec4f & diffuseColor() const
get the diffuse color.
void color(const Vec4f &_c)
Set color based on _c.
void emissionColor(const Vec4f &_c)
set emission ( Same as baseColor( const Vec4f& _c )) )
void shininess(float _s)
set shininess
void generateRandomColor()
Creates a randomized color and sets it.
void enableBackfaceCulling()
enable backface culling (not active by default, see applyProperties)
bool alphaTest() const
Return state of Alpha test.
void baseColor(const Vec4f &_c)
set the base color (Sets the baseColor which is the same as the emission(const Vec4f& _c) )
double indexOfRefraction() const
get index of refraction ( not used in OpenGL Rendering)
float shininess() const
get shininess
void specularColor(const Vec4f &_s)
set the specular color
void pointSize(float _sz)
set point size (default: 1.0)
const Vec4f & emissionColor() const
get emission ( Same as baseColor() )
Material()
Default constructor.
Definition: MaterialNode.hh:85
float pointSize() const
get point size
const Vec4f & overlayColor() const
get the overlay color (This can be used to render overlays e.g. additional wireframes in a different ...
void multisampling(bool _state)
Set state of multisampling.
bool multiSampling() const
Get state of multisampling.
void disableAlphaTest()
disable alpha test
void lineSmooth(bool _b)
set: smooth lines enabled
void disableBackfaceCulling()
disable backface culling (not active by default, see applyProperties)
void ambientColor(const Vec4f &_a)
set the ambient color.
float alphaValue() const
get current alpha value for alpha_test
float lineWidth() const
get line width
bool lineSmooth() const
get: rsmooth lines enabled
void enableBlending(GLenum _p1=GL_SRC_ALPHA, GLenum _p2=GL_ONE_MINUS_SRC_ALPHA)
enable blending with Parameters (_p1, _p2)
void reflectance(double _m)
set reflectance ( not used in OpenGL Rendering)
void enableMultisampling()
Enable Multisampling.
double reflectance() const
get reflectance ( not used in OpenGL Rendering)
void colorMaterial(const bool _cm)
Set colorMaterial.
void enableAlphaTest(float _clip)
enable alpha test (draw pixels if alpha >= _clip)
const Vec4f & baseColor() const
get the base color ( Same as emission() )
void overlayColor(const Vec4f &_s)
set the overlay color (This can be used to render overlays e.g. additional wireframes in a different ...
void diffuseColor(const Vec4f &_d)
set the diffuse color.
PickTarget
What target to use for picking.
Definition: PickTarget.hh:74
Namespace providing different geometric functions concerning angles.
VectorT< float, 4 > Vec4f
Definition: VectorT.hh:138