63 #include "MaterialNode.hh"
68 #if QT_VERSION >= 0x050000
69 #include <QJsonDocument>
70 #include <QJsonObject>
71 #elif defined(ENABLE_QJSON)
72 #include <QJson/Serializer>
73 #include <QJson/Parser>
76 #if defined(ENABLE_QJSON) || QT_VERSION >= 0x050000
77 #define JSON_SERIALIZABLE 1
79 #define JSON_SERIALIZABLE 0
86 enum ClassProperties {
88 CP_JSON_SERIALIZABLE = 1
90 CP_JSON_SERIALIZABLE = 0
94 inline QVariantList col2vl(
const ACG::Vec4f &col) {
95 return QVariantList() << col[0] << col[1] << col[2] << col[3];
98 inline ACG::Vec4f vl2col(
const QVariantList &vl) {
100 return ACG::Vec4f(vl[0].toFloat(), vl[1].toFloat(), vl[2].toFloat(), vl[3].toFloat());
107 QVariantMap json_to_variant_map(QString json) {
109 QJson::Parser parser;
111 QVariantMap matMap = parser.parse(json.toUtf8(), &ok).toMap();
112 if (!ok)
return QVariantMap();
114 #elif QT_VERSION >= 0x050000
115 QJsonParseError error;
116 QJsonDocument jsonDoc = QJsonDocument::fromJson(json.toUtf8(), &error);
117 if (error.error != QJsonParseError::NoError || !jsonDoc.isObject())
118 return QVariantMap();
119 return jsonDoc.object().toVariantMap();
121 return QVariantMap();
125 namespace SceneGraph {
130 bool Material::support_json_serialization() {
131 return CP_JSON_SERIALIZABLE;
134 QString Material::serializeToJson()
const {
135 #if JSON_SERIALIZABLE
138 matMap[
"baseColor"] = col2vl(baseColor_);
139 matMap[
"ambientColor"] = col2vl(ambientColor_);
140 matMap[
"diffuseColor"] = col2vl(diffuseColor_);
141 matMap[
"specularColor"] = col2vl(specularColor_);
142 matMap[
"overlayColor"] = col2vl(overlayColor_);
143 matMap[
"shininess"] = shininess_;
144 matMap[
"reflectance"] = reflectance_;
145 matMap[
"pointSize"] = pointSize_;
146 matMap[
"lineWidth"] = lineWidth_;
147 matMap[
"roundPoints"] = roundPoints_;
148 matMap[
"linesSmooth"] = linesSmooth_;
149 matMap[
"alphaTest"] = alphaTest_;
150 matMap[
"alphaClip"] = alphaClip_;
151 matMap[
"blending"] = blending_;
152 matMap[
"blendParam1"] = blendParam1_;
153 matMap[
"blendParam2"] = blendParam2_;
154 matMap[
"colorMaterial"] = colorMaterial_;
155 matMap[
"backfaceCulling"] = backfaceCulling_;
156 matMap[
"multiSampling"] = multiSampling_;
159 QJson::Serializer serializer;
160 QByteArray bytes = serializer.serialize(matMap);
161 return QString::fromUtf8(bytes.constData(), bytes.size());
162 #elif QT_VERSION >= 0x050000
163 const QJsonDocument json_doc(QJsonObject::fromVariantMap(matMap));
164 return QString::fromUtf8(
165 json_doc.toJson(QJsonDocument::Indented));
169 return QString(
"<No suitable serializer at the moment. Sorry.>");
173 void Material::deserializeFromVariantMap(
const QVariantMap &matMap) {
174 if (matMap.contains(
"baseColor")) baseColor_ = vl2col(matMap[
"baseColor"].toList());
175 if (matMap.contains(
"ambientColor")) ambientColor_ = vl2col(matMap[
"ambientColor"].toList());
176 if (matMap.contains(
"diffuseColor")) diffuseColor_ = vl2col(matMap[
"diffuseColor"].toList());
177 if (matMap.contains(
"specularColor")) specularColor_ = vl2col(matMap[
"specularColor"].toList());
178 if (matMap.contains(
"overlayColor")) overlayColor_ = vl2col(matMap[
"overlayColor"].toList());
179 if (matMap.contains(
"shininess")) shininess_ = matMap[
"shininess"].toFloat();
180 if (matMap.contains(
"reflectance")) reflectance_ = matMap[
"reflectance"].toDouble();
181 if (matMap.contains(
"pointSize")) pointSize_ = matMap[
"pointSize"].toFloat();
182 if (matMap.contains(
"lineWidth")) lineWidth_ = matMap[
"lineWidth"].toFloat();
183 if (matMap.contains(
"roundPoints")) roundPoints_ = matMap[
"roundPoints"].toBool();
184 if (matMap.contains(
"linesSmooth")) linesSmooth_ = matMap[
"linesSmooth"].toBool();
185 if (matMap.contains(
"alphaTest")) alphaTest_ = matMap[
"alphaTest"].toBool();
186 if (matMap.contains(
"alphaClip")) alphaClip_ = matMap[
"alphaClip"].toFloat();
187 if (matMap.contains(
"blending")) blending_ = matMap[
"blending"].toBool();
188 if (matMap.contains(
"blendParam1")) blendParam1_ = matMap[
"blendParam1"].toUInt();
189 if (matMap.contains(
"blendParam2")) blendParam2_ = matMap[
"blendParam2"].toUInt();
190 if (matMap.contains(
"colorMaterial")) colorMaterial_ = matMap[
"colorMaterial"].toBool();
191 if (matMap.contains(
"backfaceCulling")) backfaceCulling_ = matMap[
"backfaceCulling"].toBool();
192 if (matMap.contains(
"multiSampling")) multiSampling_ = matMap[
"multiSampling"].toBool();
195 void Material::deserializeFromJson(
const QString &json) {
196 deserializeFromVariantMap(ACG::json_to_variant_map(json));
200 const std::string& _name,
201 unsigned int _applyProperties )
203 applyProperties_(_applyProperties)
248 glIsEnabled(GL_ALPHA_TEST);
251 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
260 glIsEnabled(GL_ALPHA_TEST);
263 glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);
331 glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
484 while (_is && (!_is.eof()) && _is.getline(s,200) ) {
485 std::istringstream buffer(s);
491 std::string specifier =
"";
497 if (specifier ==
"BaseColor") {
498 buffer >> x >> y >> z >> u;
505 else if (specifier ==
"AmbientColor") {
506 buffer >> x >> y >> z >> u;
513 else if (specifier ==
"DiffuseColor") {
514 buffer >> x >> y >> z >> u;
521 else if (specifier ==
"SpecularColor") {
522 buffer >> x >> y >> z >> u;
529 else if (specifier ==
"OverlayColor") {
530 buffer >> x >> y >> z >> u;
537 else if (specifier ==
"Shininess") {
545 else if (specifier ==
"PointSize") {
553 else if (specifier ==
"LineWidth") {
562 std::cerr <<
"MaterialNode parse error while reading string : " << s << std::endl;
void set_line_width(float _f)
set line width
void overlayColor(const Vec4f &_s)
set the overlay color (This can be used to render overlays e.g. additional wireframes in a different ...
void set_overlay_color(const Vec4f &_col)
set overlay color
Namespace providing different geometric functions concerning angles.
static void enable(GLenum _cap)
replaces glEnable, but supports locking
static void disable(GLenum _cap)
replaces glDisable, but supports locking
void set_multisampling(bool _b)
Enable or disable multisampling.
void set_point_size(float _f)
set point size
void enter(GLState &_state, const DrawModes::DrawMode &_drawmode)
set current GL-color and GL-material
float line_width() const
get line width
bool blending()
get whether transparenet or solid objects should be drawn
float point_size() const
get point size
const Vec4f & ambient_color() const
get ambient color
VectorT< float, 4 > Vec4f
const Vec4f & overlay_color() const
Get overlay color.
static void alphaFunc(GLenum _func, GLclampf _ref)
replaces glAlphaFunc, supports locking
void set_specular_color(const Vec4f &_col)
set specular color
const Vec4f & diffuse_color() const
get diffuse color
void leave(GLState &_state, const DrawModes::DrawMode &_drawmode)
restores original GL-color and GL-material
void baseColor(const Vec4f &_c)
set the base color
ACG::SceneGraph::Material materialBackup_
Material Backup.
void set_blending(bool _b)
set whether transparent or solid objects should be drawn
void set_depthFunc(const GLenum &_depth_func)
Call glDepthFunc() to actually change the depth comparison function, and store the new value in this ...
void lineWidth(float _sz)
set line width (default: 1.0)
draw smooth (round) points using glPoint()
ACG::SceneGraph::Material material_
Local material class that actually stores the properties.
Color Material ( Only when a drawmode using shading and lighting is enabled )
void pointSize(float _sz)
set point size (default: 1.0)
void shininess(float _s)
set shininess
void set_diffuse_color(const Vec4f &_col)
set diffuse color
void diffuseColor(const Vec4f &_d)
set the diffuse color.
ACGDLLEXPORT DrawMode SOLID_FACES_COLORED_FLAT_SHADED
draw flat shaded and colored faces (requires face normals and colors)
void read(std::istream &_is)
read MaterialFile
const Vec4f & base_color() const
get base color (used when lighting is off)
void set_shininess(float _shininess)
set specular shininess (must be in [0, 128])
float shininess() const
get specular shininess (must be in [0, 128])
ACGDLLEXPORT DrawMode SOLID_FACES_COLORED_SMOOTH_SHADED
draw smooth shaded and colored faces (requires vertex normals and face colors)
void set_base_color(const Vec4f &_col)
set base color (used when lighting is off)
void specularColor(const Vec4f &_s)
set the specular color
bool multisampling()
Get current multisampling state.
const Vec4f & specular_color() const
get specular color
draw smooth lines using glLine()
static void blendFunc(GLenum _sfactor, GLenum _dfactor)
replaces glBlendFunc, supports locking
MaterialNode(BaseNode *_parent=0, const std::string &_name="<MaterialNode>", unsigned int _applyProperties=(All &~BackFaceCulling))
Default constructor. Applies all properties.
int applyProperties_
OR'ed ApplyProperties.
void enterPick(GLState &_state, PickTarget _target, const DrawModes::DrawMode &_drawMode)
Do nothing in picking.
void set_ambient_color(const Vec4f &_col)
set ambient color
void ambientColor(const Vec4f &_a)
set the ambient color.
void leavePick(GLState &_state, PickTarget _target, const DrawModes::DrawMode &_drawMode)
Do nothing in picking.