57 #include "MaterialNode.hh" 62 #include <QJsonDocument> 63 #include <QJsonObject> 70 enum ClassProperties {
71 CP_JSON_SERIALIZABLE = 1
74 inline QVariantList col2vl(
const ACG::Vec4f &col) {
75 return QVariantList() << col[0] << col[1] << col[2] << col[3];
78 inline ACG::Vec4f vl2col(
const QVariantList &vl) {
80 return ACG::Vec4f(vl[0].toFloat(), vl[1].toFloat(), vl[2].toFloat(), vl[3].toFloat());
87 QVariantMap json_to_variant_map(QString json) {
88 QJsonParseError error;
89 QJsonDocument jsonDoc = QJsonDocument::fromJson(json.toUtf8(), &error);
90 if (error.error != QJsonParseError::NoError || !jsonDoc.isObject())
92 return jsonDoc.object().toVariantMap();
100 bool Material::support_json_serialization() {
101 return CP_JSON_SERIALIZABLE;
104 QString Material::serializeToJson()
const {
107 matMap[
"baseColor"] = col2vl(baseColor_);
108 matMap[
"ambientColor"] = col2vl(ambientColor_);
109 matMap[
"diffuseColor"] = col2vl(diffuseColor_);
110 matMap[
"specularColor"] = col2vl(specularColor_);
111 matMap[
"overlayColor"] = col2vl(overlayColor_);
112 matMap[
"shininess"] = shininess_;
113 matMap[
"reflectance"] = reflectance_;
114 matMap[
"indexOfRefraction"] = indexOfRefraction_;
115 matMap[
"isRefractive"] = isRefractive_;
116 matMap[
"pointSize"] = pointSize_;
117 matMap[
"lineWidth"] = lineWidth_;
118 matMap[
"roundPoints"] = roundPoints_;
119 matMap[
"linesSmooth"] = linesSmooth_;
120 matMap[
"alphaTest"] = alphaTest_;
121 matMap[
"alphaClip"] = alphaClip_;
122 matMap[
"blending"] = blending_;
123 matMap[
"blendParam1"] = blendParam1_;
124 matMap[
"blendParam2"] = blendParam2_;
125 matMap[
"colorMaterial"] = colorMaterial_;
126 matMap[
"backfaceCulling"] = backfaceCulling_;
127 matMap[
"multiSampling"] = multiSampling_;
129 const QJsonDocument json_doc(QJsonObject::fromVariantMap(matMap));
130 return QString::fromUtf8(
131 json_doc.toJson(QJsonDocument::Indented));
134 void Material::deserializeFromVariantMap(
const QVariantMap &matMap) {
135 if (matMap.contains(
"baseColor")) baseColor_ = vl2col(matMap[
"baseColor"].toList());
136 if (matMap.contains(
"ambientColor")) ambientColor_ = vl2col(matMap[
"ambientColor"].toList());
137 if (matMap.contains(
"diffuseColor")) diffuseColor_ = vl2col(matMap[
"diffuseColor"].toList());
138 if (matMap.contains(
"specularColor")) specularColor_ = vl2col(matMap[
"specularColor"].toList());
139 if (matMap.contains(
"overlayColor")) overlayColor_ = vl2col(matMap[
"overlayColor"].toList());
140 if (matMap.contains(
"shininess")) shininess_ = matMap[
"shininess"].toFloat();
141 if (matMap.contains(
"reflectance")) reflectance_ = matMap[
"reflectance"].toDouble();
142 if (matMap.contains(
"indexOfRefraction")) indexOfRefraction_ = matMap[
"indexOfRefraction"].toDouble();
143 if (matMap.contains(
"isRefractive")) isRefractive_ = matMap[
"isRefractive"].toBool();
144 if (matMap.contains(
"pointSize")) pointSize_ = matMap[
"pointSize"].toFloat();
145 if (matMap.contains(
"lineWidth")) lineWidth_ = matMap[
"lineWidth"].toFloat();
146 if (matMap.contains(
"roundPoints")) roundPoints_ = matMap[
"roundPoints"].toBool();
147 if (matMap.contains(
"linesSmooth")) linesSmooth_ = matMap[
"linesSmooth"].toBool();
148 if (matMap.contains(
"alphaTest")) alphaTest_ = matMap[
"alphaTest"].toBool();
149 if (matMap.contains(
"alphaClip")) alphaClip_ = matMap[
"alphaClip"].toFloat();
150 if (matMap.contains(
"blending")) blending_ = matMap[
"blending"].toBool();
151 if (matMap.contains(
"blendParam1")) blendParam1_ = matMap[
"blendParam1"].toUInt();
152 if (matMap.contains(
"blendParam2")) blendParam2_ = matMap[
"blendParam2"].toUInt();
153 if (matMap.contains(
"colorMaterial")) colorMaterial_ = matMap[
"colorMaterial"].toBool();
154 if (matMap.contains(
"backfaceCulling")) backfaceCulling_ = matMap[
"backfaceCulling"].toBool();
155 if (matMap.contains(
"multiSampling")) multiSampling_ = matMap[
"multiSampling"].toBool();
158 void Material::deserializeFromJson(
const QString &json) {
159 deserializeFromVariantMap(ACG::json_to_variant_map(json));
163 const std::string& _name,
164 unsigned int _applyProperties )
166 applyProperties_(_applyProperties)
208 if (_state.compatibilityProfile())
215 glIsEnabled(GL_ALPHA_TEST);
218 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
228 glIsEnabled(GL_ALPHA_TEST);
231 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
300 if(_state.compatibilityProfile())
304 glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
439 if(_state.compatibilityProfile())
462 while (_is && (!_is.eof()) && _is.getline(s,200) ) {
463 std::istringstream buffer(s);
469 std::string specifier =
"";
475 if (specifier ==
"BaseColor") {
476 buffer >> x >> y >> z >> u;
483 else if (specifier ==
"AmbientColor") {
484 buffer >> x >> y >> z >> u;
491 else if (specifier ==
"DiffuseColor") {
492 buffer >> x >> y >> z >> u;
499 else if (specifier ==
"SpecularColor") {
500 buffer >> x >> y >> z >> u;
507 else if (specifier ==
"OverlayColor") {
508 buffer >> x >> y >> z >> u;
515 else if (specifier ==
"Shininess") {
523 else if (specifier ==
"isRefractive") {
531 else if (specifier ==
"indexOfRefraction") {
539 else if (specifier ==
"PointSize") {
547 else if (specifier ==
"LineWidth") {
556 std::cerr <<
"MaterialNode parse error while reading string : " << s << std::endl;
void set_shininess(float _shininess)
set specular shininess (must be in [0, 128])
static void enable(GLenum _cap, bool _warnRemoved=true)
replaces glEnable, but supports locking
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
static void alphaFunc(GLenum _func, GLclampf _ref)
replaces glAlphaFunc, supports locking
void enterPick(GLState &_state, PickTarget _target, const DrawModes::DrawMode &_drawMode) override
Do nothing in picking.
Namespace providing different geometric functions concerning angles.
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()
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
static void disable(GLenum _cap, bool _warnRemoved=true)
replaces glDisable, but supports locking
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)
static void blendFunc(GLenum _sfactor, GLenum _dfactor)
replaces glBlendFunc, supports locking
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.
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.
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