Commit 939636c2 authored by Mike Kremer's avatar Mike Kremer
Browse files

Added Material class to material node which stores material properties.

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@9040 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 0271619d
......@@ -71,24 +71,7 @@ MaterialNode::MaterialNode( BaseNode* _parent,
const std::string& _name,
unsigned int _applyProperties )
: BaseNode(_parent, _name),
applyProperties_(_applyProperties),
base_color_(GLState::default_base_color),
ambient_color_(GLState::default_ambient_color),
diffuse_color_(GLState::default_diffuse_color),
specular_color_(GLState::default_specular_color),
shininess_(GLState::default_shininess),
point_size_(1.0),
line_width_(1.0),
round_points_(false),
lines_smooth_(false),
alpha_test_(false),
alpha_clip_(0),
blending_(false),
blend_param1_(GL_ONE),
blend_param2_(GL_ZERO),
color_material_(true),
backface_culling_(false),
multiSampling_(true)
applyProperties_(_applyProperties)
{}
......@@ -100,7 +83,7 @@ void MaterialNode::enter(GLState& _state, DrawModes::DrawMode _drawmode )
if (applyProperties_ & BaseColor)
{
base_color_backup_ = _state.base_color();
_state.set_base_color(base_color_);
_state.set_base_color(material_.base_color());
}
if (applyProperties_ & Material)
......@@ -110,22 +93,22 @@ void MaterialNode::enter(GLState& _state, DrawModes::DrawMode _drawmode )
specular_color_backup_ = _state.specular_color();
shininess_backup_ = _state.shininess();
_state.set_ambient_color(ambient_color_);
_state.set_diffuse_color(diffuse_color_);
_state.set_specular_color(specular_color_);
_state.set_shininess(shininess_);
_state.set_ambient_color(material_.ambient_color());
_state.set_diffuse_color(material_.diffuse_color());
_state.set_specular_color(material_.specular_color());
_state.set_shininess(material_.shininess());
}
if (applyProperties_ & PointSize)
{
point_size_backup_ = _state.point_size();
_state.set_point_size(point_size_);
_state.set_point_size(material_.point_size());
}
if (applyProperties_ & LineWidth)
{
line_width_backup_ = _state.line_width();
_state.set_line_width(line_width_);
_state.set_line_width(material_.line_width());
}
if (applyProperties_ & RoundPoints)
......@@ -133,8 +116,8 @@ void MaterialNode::enter(GLState& _state, DrawModes::DrawMode _drawmode )
round_points_backup_ = glIsEnabled(GL_POINT_SMOOTH) &&
glIsEnabled(GL_ALPHA_TEST);
if( round_points_) {
glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
if( material_.round_points() ) {
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glEnable(GL_POINT_SMOOTH);
} else
glDisable(GL_POINT_SMOOTH);
......@@ -145,7 +128,7 @@ void MaterialNode::enter(GLState& _state, DrawModes::DrawMode _drawmode )
lines_smooth_backup_ = glIsEnabled(GL_LINE_SMOOTH) &&
glIsEnabled(GL_ALPHA_TEST);
if( lines_smooth_) {
if( material_.line_smooth() ) {
glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);
glEnable(GL_LINE_SMOOTH);
} else
......@@ -155,7 +138,7 @@ void MaterialNode::enter(GLState& _state, DrawModes::DrawMode _drawmode )
if (applyProperties_ & MultiSampling)
{
multiSampling_backup_ = _state.multisampling();
_state.set_multisampling( multiSampling_ );
_state.set_multisampling( material_.multi_sampling() );
}
if (applyProperties_ & AlphaTest)
......@@ -163,9 +146,9 @@ void MaterialNode::enter(GLState& _state, DrawModes::DrawMode _drawmode )
alpha_test_backup_ = glIsEnabled(GL_ALPHA_TEST);
glGetFloatv(GL_ALPHA_TEST_REF, &alpha_clip_backup_);
if(alpha_test_)
if(material_.alpha_test())
{
glAlphaFunc(GL_GREATER, alpha_clip_);
glAlphaFunc(GL_GREATER, material_.alpha_value() );
glEnable(GL_ALPHA_TEST);
}
else
......@@ -181,12 +164,12 @@ void MaterialNode::enter(GLState& _state, DrawModes::DrawMode _drawmode )
glGetIntegerv( GL_BLEND_SRC, (GLint*) &blend_param1_backup_);
glGetIntegerv( GL_BLEND_DST, (GLint*) &blend_param2_backup_);
_state.set_blending(blending_);
_state.set_blending(material_.blending());
if (blending_)
if (material_.blending())
{
glDepthFunc(GL_LEQUAL);
glBlendFunc(blend_param1_, blend_param2_);
glBlendFunc(material_.blending_param1(), material_.blending_param2());
glEnable(GL_BLEND);
}
else
......@@ -201,7 +184,7 @@ void MaterialNode::enter(GLState& _state, DrawModes::DrawMode _drawmode )
{
backface_culling_backup_ = glIsEnabled(GL_CULL_FACE);
if ( backface_culling_ )
if ( material_.backface_culling() )
glEnable( GL_CULL_FACE );
else
glDisable( GL_CULL_FACE );
......@@ -213,7 +196,7 @@ void MaterialNode::enter(GLState& _state, DrawModes::DrawMode _drawmode )
{
color_material_backup_ = glIsEnabled(GL_COLOR_MATERIAL);
if (color_material_ ) {
if (material_.color_material() ) {
glDisable( GL_COLOR_MATERIAL );
glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
glEnable( GL_COLOR_MATERIAL );
......@@ -362,7 +345,7 @@ MaterialNode::read(std::istream& _is)
{
if (sscanf(s, "BaseColor %f %f %f %f", &x, &y, &z, &u))
{
base_color_ = Vec4f(x,y,z,u);
material_.set_base_color(Vec4f(x,y,z,u));
}
}
// AmbientColor
......@@ -370,7 +353,7 @@ MaterialNode::read(std::istream& _is)
{
if (sscanf(s, "AmbientColor %f %f %f %f", &x, &y, &z, &u))
{
ambient_color_ = Vec4f(x,y,z,u);
material_.set_ambient_color(Vec4f(x,y,z,u));
}
}
// DiffuseColor
......@@ -378,7 +361,7 @@ MaterialNode::read(std::istream& _is)
{
if (sscanf(s, "DiffuseColor %f %f %f %f", &x, &y, &z, &u))
{
diffuse_color_ = Vec4f(x,y,z,u);
material_.set_diffuse_color(Vec4f(x,y,z,u));
}
}
// SpecularColor
......@@ -386,7 +369,7 @@ MaterialNode::read(std::istream& _is)
{
if (sscanf(s, "SpecularColor %f %f %f %f", &x, &y, &z, &u))
{
specular_color_ = Vec4f(x,y,z,u);
material_.set_specular_color(Vec4f(x,y,z,u));
}
}
// Shininess
......@@ -394,7 +377,7 @@ MaterialNode::read(std::istream& _is)
{
if (sscanf(s, "Shininess %f", &x))
{
shininess_ = x;
material_.set_shininess(x);
}
}
// PointSize
......@@ -402,7 +385,7 @@ MaterialNode::read(std::istream& _is)
{
if (sscanf(s, "PointSize %f", &x))
{
point_size_ = x;
material_.set_point_size(x);
}
}
// LineWidth
......@@ -410,7 +393,7 @@ MaterialNode::read(std::istream& _is)
{
if (sscanf(s, "LineWidth %f", &x))
{
line_width_ = x;
material_.set_line_width(x);
}
}
......
......@@ -67,6 +67,208 @@ namespace SceneGraph {
//== CLASS DEFINITION =========================================================
/** \class Material
Class to store the properties of a material.
**/
class ACGDLLEXPORT Material {
public:
/// Default constructor
Material() :
base_color_(GLState::default_base_color),
ambient_color_(GLState::default_ambient_color),
diffuse_color_(GLState::default_diffuse_color),
specular_color_(GLState::default_specular_color),
shininess_(GLState::default_shininess),
point_size_(1.0),
line_width_(1.0),
round_points_(false),
lines_smooth_(false),
alpha_test_(false),
alpha_clip_(0),
blending_(false),
blend_param1_(GL_ONE),
blend_param2_(GL_ZERO),
color_material_(true),
backface_culling_(false),
multiSampling_(true) {};
/// Deconstructor
virtual ~Material() {};
/// Copy constructor
Material(const Material& _m) :
base_color_(_m.base_color_),
ambient_color_(_m.ambient_color_),
diffuse_color_(_m.diffuse_color_),
specular_color_(_m.specular_color_),
shininess_(_m.shininess_),
mirror_(_m.mirror_),
point_size_(_m.point_size_),
line_width_(_m.line_width_),
round_points_(_m.round_points_),
lines_smooth_(_m.lines_smooth_),
alpha_test_(_m.alpha_test_),
alpha_clip_(_m.alpha_clip_),
blending_(_m.blending_),
blend_param1_(_m.blend_param1_),
blend_param2_(_m.blend_param2_),
color_material_(_m.color_material_),
backface_culling_(_m.backface_culling_),
multiSampling_(_m.multiSampling_) {};
/// set color (base, ambient, diffuse, specular) based on _c
void set_color(const Vec4f& _c) {
Vec4f c;
set_base_color(_c);
c = _c * 0.2f; c[3]=_c[3]; set_ambient_color(c);
c = _c * 0.6f; c[3]=_c[3]; set_diffuse_color(c);
c = _c * 0.8f; c[3]=_c[3]; set_specular_color(c);
}
/// set the base color
void set_base_color(const Vec4f& _c) { base_color_ = _c;}
/// get the base color
const Vec4f& base_color() const { return base_color_; }
/// set the ambient color.
void set_ambient_color(const Vec4f& _a) { ambient_color_ = _a; }
/// get the ambient color.
const Vec4f& ambient_color() const { return ambient_color_; }
/// set the diffuse color.
void set_diffuse_color(const Vec4f& _d) { diffuse_color_ = _d; }
/// get the diffuse color.
const Vec4f& diffuse_color() const { return diffuse_color_; }
/// set the specular color
void set_specular_color(const Vec4f& _s) { specular_color_ = _s; }
/// get the specular color
const Vec4f& specular_color() const { return specular_color_; }
/// Set colorMaterial
void color_material( const bool _cm) { color_material_ = _cm; }
/// Enable Color Material
void enable_color_material() { color_material_ = true; }
/// Disable Color Material
void disable_color_material() { color_material_ = false; }
/// get colorMaterial state
bool color_material() { return color_material_; }
/// set shininess
void set_shininess(float _s) { shininess_ = _s; }
/// get shininess
float shininess() const { return shininess_; }
/// set mirror
void set_mirror(double _m) { mirror_ = _m; }
/// get mirror
double mirror() const { return mirror_; }
/// set point size (default: 1.0)
void set_point_size(float _sz) { point_size_ = _sz; }
/// get point size
float point_size() const { return point_size_; }
/// set line width (default: 1.0)
void set_line_width(float _sz) { line_width_ = _sz; }
/// get line width
float line_width() const { return line_width_; }
/// set: round points enabled
void set_round_points(bool _b) { round_points_ = _b; }
/// get: round points enabled
bool round_points() const { return round_points_; }
/// set: smooth lines enabled
void set_line_smooth(bool _b) { lines_smooth_ = _b; }
/// get: rsmooth lines enabled
bool line_smooth() const { return lines_smooth_; }
/// enable alpha test (draw pixels if alpha >= _clip)
void enable_alpha_test(float _clip) {
alpha_test_ = true; alpha_clip_ = _clip;
}
/// disable alpha test
void disable_alpha_test() { alpha_test_ = false; }
/// Return state of Alpha test
bool alpha_test() { return alpha_test_; };
/// Enable Multisampling
void enable_multisampling() {
multiSampling_ = true;
}
/// enable alpha test (draw pixels if alpha >= _clip)
void disable_multisampling() {
multiSampling_ = false;
}
/// Get state of multisampling
bool multi_sampling() {
return multiSampling_;
}
/// Set state of multisampling
void set_multisampling( bool _state ) {
multiSampling_ = _state;
}
///get current alpha value for alpha_test
float alpha_value(){ return alpha_clip_; };
bool blending() { return blending_; };
GLenum blending_param1() { return blend_param1_; };
GLenum blending_param2() { return blend_param2_; };
/// enable blending with Parameters (_p1, _p2)
void enable_blending(GLenum _p1 = GL_SRC_ALPHA,
GLenum _p2 = GL_ONE_MINUS_SRC_ALPHA)
{ blending_ = true; blend_param1_ = _p1; blend_param2_ = _p2; }
/// disable blending
void disable_blending() { blending_ = false; }
bool backface_culling() { return backface_culling_; };
/// enable backface culling (not active by default, see applyProperties)
void enable_backface_culling() { backface_culling_ = true; }
/// disable backface culling (not active by default, see applyProperties)
void disable_backface_culling() { backface_culling_ = false; }
private:
Vec4f base_color_;
Vec4f ambient_color_;
Vec4f diffuse_color_;
Vec4f specular_color_;
float shininess_;
double mirror_;
float point_size_;
float line_width_;
bool round_points_;
bool lines_smooth_;
bool alpha_test_;
float alpha_clip_;
bool blending_;
GLenum blend_param1_;
GLenum blend_param2_;
bool color_material_;
bool backface_culling_;
bool multiSampling_;
};
/** \class MaterialNode MaterialNode.hh <ACG/Scenegraph/MaterialNode.hh>
Set material and some other stuff like alphatest and blending
......@@ -110,12 +312,10 @@ public:
MultiSampling=1024
};
/// Default constructor. Applies all properties.
MaterialNode( BaseNode* _parent = 0,
const std::string& _name = "<MaterialNode>",
unsigned int _applyProperties = (All & ~BackFaceCulling));
const std::string& _name = "<MaterialNode>",
unsigned int _applyProperties = (All & ~BackFaceCulling));
/// Destructor.
virtual ~MaterialNode() {};
......@@ -142,55 +342,56 @@ public:
/** \brief Do nothing in picking */
void leavePick(GLState& _state, PickTarget _target, DrawModes::DrawMode _drawMode );
/** @} */
/** @} */
//===========================================================================
/** @name Color and material settings ( Applied to all objects below this node )
* @{ */
//===========================================================================
//===========================================================================
/** @name Color and material settings ( Applied to all objects below this node )
* @{ */
//===========================================================================
/// set color (base, ambient, diffuse, specular) based on _c
void set_color(const Vec4f& _c) {
Vec4f c;
set_base_color(_c);
c = _c * 0.2f; c[3]=_c[3]; set_ambient_color(c);
c = _c * 0.6f; c[3]=_c[3]; set_diffuse_color(c);
c = _c * 0.8f; c[3]=_c[3]; set_specular_color(c);
material_.set_color(_c);
}
/// set the base color
void set_base_color(const Vec4f& _c) { base_color_ = _c;}
void set_base_color(const Vec4f& _c) { material_.set_base_color(_c); }
/// get the base color
const Vec4f& base_color() const { return base_color_; }
const Vec4f& base_color() const { return material_.base_color(); }
/// set the ambient color.
void set_ambient_color(const Vec4f& _a) { ambient_color_ = _a; }
void set_ambient_color(const Vec4f& _a) { material_.set_ambient_color(_a); }
/// get the ambient color.
const Vec4f& ambient_color() const { return ambient_color_; }
const Vec4f& ambient_color() const { return material_.ambient_color(); }
/// set the diffuse color.
void set_diffuse_color(const Vec4f& _d) { diffuse_color_ = _d; }
void set_diffuse_color(const Vec4f& _d) { material_.set_diffuse_color(_d); }
/// get the diffuse color.
const Vec4f& diffuse_color() const { return diffuse_color_; }
const Vec4f& diffuse_color() const { return material_.diffuse_color(); }
/// set the specular color
void set_specular_color(const Vec4f& _s) { specular_color_ = _s; }
void set_specular_color(const Vec4f& _s) { material_.set_specular_color(_s); }
/// get the specular color
const Vec4f& specular_color() const { return specular_color_; }
const Vec4f& specular_color() const { return material_.specular_color(); }
/// Set colorMaterial
void colorMaterial( const bool _colorMaterial) { color_material_ = _colorMaterial; }
void colorMaterial( const bool _cm) { material_.color_material(_cm); }
/// Enable Color Material
void enable_color_material() { color_material_ = true; }
void enable_color_material() { material_.enable_color_material(); }
/// Disable Color Material
void disable_color_material() { color_material_ = false; }
void disable_color_material() { material_.disable_color_material(); }
/// get colorMaterial state
bool colorMaterial() { return color_material_; }
bool colorMaterial() { return material_.color_material(); }
/// set shininess
void set_shininess(float _s) { shininess_ = _s; }
void set_shininess(float _s) { material_.set_shininess(_s); }
/// get shininess
float shininess() const { return shininess_; }
float shininess() const { return material_.shininess(); }
/// set mirror
void set_mirror(double _m) { material_.set_mirror(_m); }
/// get mirror
double mirror() const { return material_.mirror(); }
/** @} */
......@@ -199,24 +400,24 @@ public:
* @{ */
//===========================================================================
/// set point size (default: 1.0)
void set_point_size(float _sz) { point_size_ = _sz; }
void set_point_size(float _sz) { material_.set_point_size(_sz); }
/// get point size
float point_size() const { return point_size_; }
float point_size() const { return material_.point_size(); }
/// set line width (default: 1.0)
void set_line_width(float _sz) { line_width_ = _sz; }
void set_line_width(float _sz) { material_.set_line_width(_sz); }
/// get line width
float line_width() const { return line_width_; }
float line_width() const { return material_.line_width(); }
/// set: round points enabled
void set_round_points(bool _b) { round_points_ = _b; }
/// get: round points enabled
bool round_points() const { return round_points_; }
/// set round points enabled
void set_round_points(bool _b) { material_.set_round_points(_b); }
/// get round points enabled
bool round_points() const { return material_.round_points(); }
/// set: smooth lines enabled
void set_line_smooth(bool _b) { lines_smooth_ = _b; }
void set_line_smooth(bool _b) { material_.set_line_smooth(_b); }
/// get: rsmooth lines enabled
bool line_smooth() const { return lines_smooth_; }
bool line_smooth() const { return material_.line_smooth(); }
/** @} */
......@@ -226,15 +427,13 @@ public:
//===========================================================================
/// enable alpha test (draw pixels if alpha >= _clip)
void enable_alpha_test(float _clip) {
alpha_test_ = true; alpha_clip_ = _clip;
}
void enable_alpha_test(float _clip) { material_.enable_alpha_test(_clip); }
/// disable alpha test
void disable_alpha_test() { alpha_test_ = false; }
void disable_alpha_test() { material_.disable_alpha_test(); }
/// Return state of Alpha test
bool alpha_test() { return alpha_test_; };
bool alpha_test() { return material_.alpha_test(); };
/** @} */
......@@ -244,92 +443,89 @@ public:
//===========================================================================
/// Enable Multisampling
void enable_multisampling() {
multiSampling_ = true;
}
void enable_multisampling() { material_.enable_multisampling(); }
/// enable alpha test (draw pixels if alpha >= _clip)
void disable_multisampling() {
multiSampling_ = false;
}
void disable_multisampling() { material_.disable_multisampling(); }
/// Get state of multisampling
bool multiSampling() {
return multiSampling_;
}
bool multiSampling() { return material_.multi_sampling(); }
/// Set state of multisampling
void set_multisampling( bool _state ) {
multiSampling_ = _state;
}
void set_multisampling( bool _state ) { material_.set_multisampling(_state); }
/** @} */
///get current alpha value for alpha_test
float alpha_value(){ return alpha_clip_; };
float alpha_value(){ return material_.alpha_value(); };
bool blending() { return blending_; };
bool blending() { return material_.blending(); };
GLenum blending_param1() { return blend_param1_; };
GLenum blending_param2() { return blend_param2_; };
GLenum blending_param1() { return material_.blending_param1(); };
GLenum blending_param2() { return material_.blending_param2(); };
/// enable blending with Parameters (_p1, _p2)
void enable_blending(GLenum _p1 = GL_SRC_ALPHA,
GLenum _p2 = GL_ONE_MINUS_SRC_ALPHA)
{ blending_ = true; blend_param1_ = _p1; blend_param2_ = _p2; }
GLenum _p2 = GL_ONE_MINUS_SRC_ALPHA) {
material_.enable_blending(_p1, _p2);
}
/// disable blending
void disable_blending() { blending_ = false; }
void disable_blending() { material_.disable_blending(); }
bool backface_culling() { return backface_culling_; };