Commit dfc08825 authored by Weizhen Huang's avatar Weizhen Huang

improved material support

parent 7ceefca0
......@@ -171,6 +171,7 @@ bool FileOBJPlugin::readMaterial(QString _filename, OBJImporter& _importer)
f1 = 0;
f2 = 0;
f3 = 0;
static int i;
static bool insideDefintion;
insideDefintion = false;
static int textureId;
......@@ -241,7 +242,7 @@ bool FileOBJPlugin::readMaterial(QString _filename, OBJImporter& _importer)
f2 = getFloat(stream);
f3 = getFloat(stream);
if( stream.status()!=QTextStream::Ok )
if( stream.status()==QTextStream::Ok )
mat.set_Ka(f1,f2,f3);
}
......@@ -251,20 +252,62 @@ bool FileOBJPlugin::readMaterial(QString _filename, OBJImporter& _importer)
f2 = getFloat(stream);
f3 = getFloat(stream);
if( stream.status()!=QTextStream::Ok )
if( stream.status()==QTextStream::Ok )
mat.set_Ks(f1,f2,f3);
}
#if 0
else if (keyWrd == QLatin1String("illum") // diffuse/specular shading model
else if (keyWrd == QLatin1String("Ke")) // emission color
{
; // just skip this
f1 = getFloat(stream);
f2 = getFloat(stream);
f3 = getFloat(stream);
if( stream.status()==QTextStream::Ok )
mat.set_Ke(f1,f2,f3);
}
else if (keyWrd == QLatin1String("Ns") // Shininess [0..200]
else if (keyWrd == QLatin1String("illum")) // diffuse/specular shading model
{
; // just skip this
stream >> i;
if(stream.status() == QTextStream::Ok)
mat.set_illum(i);
}
else if (keyWrd == QLatin1String("Ns")) // Shininess;
{
f1 = getFloat(stream);
if(stream.status() == QTextStream::Ok)
mat.set_Ns(f1);
}
else if (keyWrd == QLatin1String("Ni")) // Refractive index
{
f1 = getFloat(stream);
if(stream.status() == QTextStream::Ok)
mat.set_Ni(f1);
}
else if (keyWrd == QLatin1String("Tr")) // transparency value
{
f1 = getFloat(stream);
if(stream.status() == QTextStream::Ok)
mat.set_Tr(f1);
}
else if (keyWrd == QLatin1String("d")) // material dissolve. The result does not depend upon the thickness of the object, no real transparency
// ignored when Tr exists
{
f1 = getFloat(stream);
if(stream.status() == QTextStream::Ok && !mat.has_Tr())
mat.set_Tr(f1);
}
#if 0
else if (keyWrd == QLatin1String("map_") // map images
{
// map_Ks, specular map
......@@ -282,20 +325,6 @@ bool FileOBJPlugin::readMaterial(QString _filename, OBJImporter& _importer)
if ( ! textureName.isEmpty() )
mat.set_map_Kd( textureName.toStdString(), textureId++ );
}
else if (keyWrd == QLatin1String("Tr")) // transparency value
{
f1 = getFloat(stream);
if( stream.status() == QTextStream::Ok )
mat.set_Tr(f1);
}
else if (keyWrd == QLatin1String("d")) // transparency value
{
f1 = getFloat(stream);
if( stream.status() == QTextStream::Ok )
mat.set_Tr(f1);
}
if ( matStream.status() == QTextStream::Ok && insideDefintion && mat.is_valid() && !matName.isEmpty())
_importer.materials()[matName.toStdString()] = mat;
......
......@@ -70,7 +70,11 @@ class Material
Kd_is_set_(false),
Ka_is_set_(false),
Ks_is_set_(false),
Tr_(0.0),
Ke_is_set_(false),
Ni_is_set_(false),
Ns_is_set_(false),
illum_is_set_(false),
Tr_(1.0), // fully opaque
Tr_is_set_(false),
map_Kd_(""),
index_Kd_(0),
......@@ -85,6 +89,10 @@ class Material
Kd_is_set_ = false;
Ka_is_set_ = false;
Ks_is_set_ = false;
Ke_is_set_ = false;
Ni_is_set_ = false;
Ns_is_set_ = false;
illum_is_set_ = false;
Tr_ = 0.0;
Tr_is_set_ = false;
map_Kd_ = "";
......@@ -103,6 +111,18 @@ class Material
/// Does material have a specular color?
bool has_Ks(void) { return Ks_is_set_; }
/// Is material emissive?
bool has_Ke(void) { return Ke_is_set_; }
/// Does material have an index of refraction?
bool has_Ni(void) { return Ni_is_set_; }
/// Does material have shininess?
bool has_Ns(void) { return Ns_is_set_; }
/// Does material have illumination model?
bool has_illum(void) { return illum_is_set_; }
/// Does material have transparency?
bool has_Tr(void) { return Tr_is_set_; }
......@@ -115,6 +135,9 @@ class Material
if( _mat.Kd_ != Kd_ ||
_mat.Ka_ != Ka_ ||
_mat.Ks_ != Ks_ ||
_mat.Ke_ != Ke_ ||
_mat.Ni_ != Ni_ ||
_mat.Ns_ != Ns_ ||
_mat.Tr_ != Tr_ ||
_mat.map_Kd_ != map_Kd_ ||
_mat.index_Kd_ != index_Kd_ )
......@@ -141,13 +164,29 @@ class Material
void set_Ks( float r, float g, float b )
{ Ks_=OpenMesh::Vec3f(r,g,b); Ks_is_set_=true; }
/// Set emission
void set_Ke( float r, float g, float b )
{ Ke_=OpenMesh::Vec3f(r,g,b); Ke_is_set_=true; }
/// Set index of refraction
void set_Ni( float t )
{ Ni_=t; Ni_is_set_=true; }
/// Set shininess
void set_Ns( float t )
{ Ns_=t; Ns_is_set_=true; }
/// Set illumination model
void set_illum( int t )
{ illum_=t; illum_is_set_=true; }
/// Set transparency value (between 0 and 1)
void set_Tr( float t )
{ Tr_=t; Tr_is_set_=true; }
/// Set texture file and index
void set_map_Kd( std::string _name, int _index_Kd )
{ map_Kd_ = _name, index_Kd_ = _index_Kd; map_Kd_is_set_ = true; };
{ map_Kd_ = _name, index_Kd_ = _index_Kd; map_Kd_is_set_ = true; }
/// Get diffuse color
const OpenMesh::Vec3f& Kd( void ) const { return Kd_; }
......@@ -157,6 +196,21 @@ class Material
/// Get specular color
const OpenMesh::Vec3f& Ks( void ) const { return Ks_; }
/// Get emission
const OpenMesh::Vec3f& Ke( void ) const { return Ke_; }
/// Get index of refraction
float Ni( void ) const { return Ni_; }
/// Get shininess
float Ns( void ) const { return Ns_; }
/// Get illumination model
int illum( void ) const { return illum_; }
/// Is the material refractive (translucent)
bool isRefractive(void) const {return (illum_ == 4 || illum_ == 6 || illum_ == 7 || illum_ == 9);}
/// Get transparency value
float Tr( void ) const { return Tr_; }
......@@ -170,10 +224,14 @@ class Material
private:
unsigned int localNum_;
OpenMesh::Vec3f Kd_; bool Kd_is_set_; // diffuse
OpenMesh::Vec3f Ka_; bool Ka_is_set_; // ambient
OpenMesh::Vec3f Ks_; bool Ks_is_set_; // specular
float Tr_; bool Tr_is_set_; // transperency
OpenMesh::Vec3f Kd_; bool Kd_is_set_; // diffuse
OpenMesh::Vec3f Ka_; bool Ka_is_set_; // ambient
OpenMesh::Vec3f Ks_; bool Ks_is_set_; // specular
OpenMesh::Vec3f Ke_; bool Ke_is_set_; // emission
float Ni_; bool Ni_is_set_; // index of refraction
float Ns_; bool Ns_is_set_; // shininess
int illum_; bool illum_is_set_; // illumination model
float Tr_; bool Tr_is_set_; // transperency
std::string map_Kd_; int index_Kd_; bool map_Kd_is_set_; // Texture
};
......
......@@ -585,6 +585,8 @@ void OBJImporter::addMaterial(std::string _materialName){
//get textureIndex Property
OpenMesh::FPropHandleT< int > indexProperty;
BaseObjectData* triMeshObj = PluginFunctions::baseObjectData(object(currentObject()));
if ( hasTexture( currentObject() ) ){
bool textureAllowed = ! ( objectOptions_[ currentObject() ] & FORCE_NOTEXTURES );
......@@ -612,12 +614,42 @@ void OBJImporter::addMaterial(std::string _materialName){
} else {
color[3] = 1.0;
}
currentTriMesh()->set_color(addedFacesTri_[currentGroup_][i], color );
triMeshObj->materialNode()->set_diffuse_color(color);
objectOptions_[ currentObject() ] |= FACECOLOR;
}
}
// set materialNode color
if (mat.has_Ka()) {
TriMesh::Color color = OpenMesh::color_cast< OpenMesh::Vec4f >(mat.Ka());
triMeshObj->materialNode()->set_ambient_color(color);
}
if (mat.has_Ks()) {
TriMesh::Color color = OpenMesh::color_cast< OpenMesh::Vec4f >(mat.Ks());
triMeshObj->materialNode()->set_specular_color(color);
}
if (mat.has_Ke()) {
TriMesh::Color color = OpenMesh::color_cast< OpenMesh::Vec4f >(mat.Ke());
triMeshObj->materialNode()->set_emission(color);
}
if (mat.has_Ni()) {
float Ni = mat.Ni();
triMeshObj->materialNode()->set_indexOfRefraction(Ni);
}
if (mat.has_Ns()) {
float shininess = mat.Ns();
triMeshObj->materialNode()->set_shininess(shininess);
}
if (mat.has_illum()) {
triMeshObj->materialNode()->set_refractive(mat.isRefractive());
}
bool textureAllowed = ! ( objectOptions_[ currentObject() ] & FORCE_NOTEXTURES );
// Set the texture index in the face index property
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment