57 #include "MaterialNode.hh" 62 #if QT_VERSION >= 0x050000 63 #include <QJsonDocument> 64 #include <QJsonObject> 65 #elif defined(ENABLE_QJSON) 66 #include <QJson/Serializer> 67 #include <QJson/Parser> 70 #if defined(ENABLE_QJSON) || QT_VERSION >= 0x050000 71 #define JSON_SERIALIZABLE 1 73 #define JSON_SERIALIZABLE 0 80 enum ClassProperties {
82 CP_JSON_SERIALIZABLE = 1
84 CP_JSON_SERIALIZABLE = 0
88 inline QVariantList col2vl(
const ACG::Vec4f &col) {
89 return QVariantList() << col[0] << col[1] << col[2] << col[3];
92 inline ACG::Vec4f vl2col(
const QVariantList &vl) {
94 return ACG::Vec4f(vl[0].toFloat(), vl[1].toFloat(), vl[2].toFloat(), vl[3].toFloat());
101 QVariantMap json_to_variant_map(QString json) {
103 QJson::Parser parser;
105 QVariantMap matMap = parser.parse(json.toUtf8(), &ok).toMap();
106 if (!ok)
return QVariantMap();
108 #elif QT_VERSION >= 0x050000 109 QJsonParseError error;
110 QJsonDocument jsonDoc = QJsonDocument::fromJson(json.toUtf8(), &error);
111 if (error.error != QJsonParseError::NoError || !jsonDoc.isObject())
112 return QVariantMap();
113 return jsonDoc.object().toVariantMap();
115 return QVariantMap();
124 bool Material::support_json_serialization() {
125 return CP_JSON_SERIALIZABLE;
128 QString Material::serializeToJson()
const {
129 #if JSON_SERIALIZABLE 132 matMap[
"baseColor"] = col2vl(baseColor_);
133 matMap[
"ambientColor"] = col2vl(ambientColor_);
134 matMap[
"diffuseColor"] = col2vl(diffuseColor_);
135 matMap[
"specularColor"] = col2vl(specularColor_);
136 matMap[
"overlayColor"] = col2vl(overlayColor_);
137 matMap[
"shininess"] = shininess_;
138 matMap[
"reflectance"] = reflectance_;
139 matMap[
"indexOfRefraction"] = indexOfRefraction_;
140 matMap[
"isRefractive"] = isRefractive_;
141 matMap[
"pointSize"] = pointSize_;
142 matMap[
"lineWidth"] = lineWidth_;
143 matMap[
"roundPoints"] = roundPoints_;
144 matMap[
"linesSmooth"] = linesSmooth_;
145 matMap[
"alphaTest"] = alphaTest_;
146 matMap[
"alphaClip"] = alphaClip_;
147 matMap[
"blending"] = blending_;
148 matMap[
"blendParam1"] = blendParam1_;
149 matMap[
"blendParam2"] = blendParam2_;
150 matMap[
"colorMaterial"] = colorMaterial_;
151 matMap[
"backfaceCulling"] = backfaceCulling_;
152 matMap[
"multiSampling"] = multiSampling_;
155 QJson::Serializer serializer;
156 QByteArray bytes = serializer.serialize(matMap);
157 return QString::fromUtf8(bytes.constData(), bytes.size());
158 #elif QT_VERSION >= 0x050000 159 const QJsonDocument json_doc(QJsonObject::fromVariantMap(matMap));
160 return QString::fromUtf8(
161 json_doc.toJson(QJsonDocument::Indented));
165 return QString(
"<No suitable serializer at the moment. Sorry.>");
169 void Material::deserializeFromVariantMap(
const QVariantMap &matMap) {
170 if (matMap.contains(
"baseColor")) baseColor_ = vl2col(matMap[
"baseColor"].toList());
171 if (matMap.contains(
"ambientColor")) ambientColor_ = vl2col(matMap[
"ambientColor"].toList());
172 if (matMap.contains(
"diffuseColor")) diffuseColor_ = vl2col(matMap[
"diffuseColor"].toList());
173 if (matMap.contains(
"specularColor")) specularColor_ = vl2col(matMap[
"specularColor"].toList());
174 if (matMap.contains(
"overlayColor")) overlayColor_ = vl2col(matMap[
"overlayColor"].toList());
175 if (matMap.contains(
"shininess")) shininess_ = matMap[
"shininess"].toFloat();
176 if (matMap.contains(
"reflectance")) reflectance_ = matMap[
"reflectance"].toDouble();
177 if (matMap.contains(
"indexOfRefraction")) indexOfRefraction_ = matMap[
"indexOfRefraction"].toDouble();
178 if (matMap.contains(
"isRefractive")) isRefractive_ = matMap[
"isRefractive"].toBool();
179 if (matMap.contains(
"pointSize")) pointSize_ = matMap[
"pointSize"].toFloat();
180 if (matMap.contains(
"lineWidth")) lineWidth_ = matMap[
"lineWidth"].toFloat();
181 if (matMap.contains(
"roundPoints")) roundPoints_ = matMap[
"roundPoints"].toBool();
182 if (matMap.contains(
"linesSmooth")) linesSmooth_ = matMap[
"linesSmooth"].toBool();
183 if (matMap.contains(
"alphaTest")) alphaTest_ = matMap[
"alphaTest"].toBool();
184 if (matMap.contains(
"alphaClip")) alphaClip_ = matMap[
"alphaClip"].toFloat();
185 if (matMap.contains(
"blending")) blending_ = matMap[
"blending"].toBool();
186 if (matMap.contains(
"blendParam1")) blendParam1_ = matMap[
"blendParam1"].toUInt();
187 if (matMap.contains(
"blendParam2")) blendParam2_ = matMap[
"blendParam2"].toUInt();
188 if (matMap.contains(
"colorMaterial")) colorMaterial_ = matMap[
"colorMaterial"].toBool();
189 if (matMap.contains(
"backfaceCulling")) backfaceCulling_ = matMap[
"backfaceCulling"].toBool();
190 if (matMap.contains(
"multiSampling")) multiSampling_ = matMap[
"multiSampling"].toBool();
193 void Material::deserializeFromJson(
const QString &json) {
194 deserializeFromVariantMap(ACG::json_to_variant_map(json));
198 const std::string& _name,
199 unsigned int _applyProperties )
201 applyProperties_(_applyProperties)
243 if (_state.compatibilityProfile())
250 glIsEnabled(GL_ALPHA_TEST);
253 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
263 glIsEnabled(GL_ALPHA_TEST);
266 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
335 if(_state.compatibilityProfile())
339 glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
474 if(_state.compatibilityProfile())
497 while (_is && (!_is.eof()) && _is.getline(s,200) ) {
498 std::istringstream buffer(s);
504 std::string specifier =
"";
510 if (specifier ==
"BaseColor") {
511 buffer >> x >> y >> z >> u;
518 else if (specifier ==
"AmbientColor") {
519 buffer >> x >> y >> z >> u;
526 else if (specifier ==
"DiffuseColor") {
527 buffer >> x >> y >> z >> u;
534 else if (specifier ==
"SpecularColor") {
535 buffer >> x >> y >> z >> u;
542 else if (specifier ==
"OverlayColor") {
543 buffer >> x >> y >> z >> u;
550 else if (specifier ==
"Shininess") {
558 else if (specifier ==
"isRefractive") {
566 else if (specifier ==
"indexOfRefraction") {
574 else if (specifier ==
"PointSize") {
582 else if (specifier ==
"LineWidth") {
591 std::cerr <<
"MaterialNode parse error while reading string : " << s << std::endl;
void set_shininess(float _shininess)
set specular shininess (must be in [0, 128])
void pointSize(float _sz)
set point size (default: 1.0)
const Vec4f & specular_color() const
get specular color
void set_diffuse_color(const Vec4f &_col)
set diffuse color
float point_size() const
get point size
void enterPick(GLState &_state, PickTarget _target, const DrawModes::DrawMode &_drawMode) override
Do nothing in picking.
Namespace providing different geometric functions concerning angles.
static void alphaFunc(GLenum _func, GLclampf _ref)
replaces glAlphaFunc, supports locking
VectorT< float, 4 > Vec4f
MaterialNode(BaseNode *_parent=0, const std::string &_name="<MaterialNode>", unsigned int _applyProperties=(All &~BackFaceCulling))
Default constructor. Applies all properties.
const Vec4f & overlay_color() const
Get overlay color.
void baseColor(const Vec4f &_c)
set the base color (Sets the baseColor which is the same as the emission(const Vec4f& _c) ) ...
draw smooth lines using glLine()
static void disable(GLenum _cap, bool _warnRemoved=true)
replaces glDisable, but supports locking
const Vec4f & base_color() const
get base color (used when lighting is off)
void set_overlay_color(const Vec4f &_col)
set overlay color
const Vec4f & diffuse_color() const
get diffuse color
float shininess() const
get specular shininess (must be in [0, 128])
void overlayColor(const Vec4f &_s)
set the overlay color (This can be used to render overlays e.g. additional wireframes in a different ...
void lineWidth(float _sz)
set line width (default: 1.0)
ACG::SceneGraph::Material material_
Local material class that actually stores the properties.
ACG::SceneGraph::Material materialBackup_
Material Backup.
PickTarget
What target to use for picking.
void ambientColor(const Vec4f &_a)
set the ambient color.
void leave(GLState &_state, const DrawModes::DrawMode &_drawmode) override
restores original GL-color and GL-material
void diffuseColor(const Vec4f &_d)
set the diffuse color.
void set_blending(bool _b)
set whether transparent or solid objects should be drawn
void set_ambient_color(const Vec4f &_col)
set ambient color
void set_point_size(float _f)
set point size
DrawMode SOLID_FACES_COLORED_FLAT_SHADED
draw flat shaded and colored faces (requires face normals and colors)
void set_specular_color(const Vec4f &_col)
set specular color
void set_line_width(float _f)
set line width
void leavePick(GLState &_state, PickTarget _target, const DrawModes::DrawMode &_drawMode) override
Do nothing in picking.
static void blendFunc(GLenum _sfactor, GLenum _dfactor)
replaces glBlendFunc, supports locking
void set_base_color(const Vec4f &_col)
set base color (used when lighting is off)
void specularColor(const Vec4f &_s)
set the specular color
draw smooth (round) points using glPoint()
int applyProperties_
OR'ed ApplyProperties.
static void enable(GLenum _cap, bool _warnRemoved=true)
replaces glEnable, but supports locking
void setRefractive(bool _r)
set refractive flag
Color Material ( Only when a drawmode using shading and lighting is enabled )
void read(std::istream &_is)
read MaterialFile
bool blending()
get whether transparenet or solid objects should be drawn
float line_width() const
get line width
void set_multisampling(bool _b)
Enable or disable multisampling.
DrawMode SOLID_FACES_COLORED_SMOOTH_SHADED
draw smooth shaded and colored faces (requires vertex normals and face colors)
void enter(GLState &_state, const DrawModes::DrawMode &_drawmode) override
set current GL-color and GL-material
void set_depthFunc(const GLenum &_depth_func)
Call glDepthFunc() to actually change the depth comparison function, and store the new value in this ...
bool multisampling()
Get current multisampling state.
void shininess(float _s)
set shininess
void indexOfRefraction(double _m)
set index of refraction
const Vec4f & ambient_color() const
get ambient color