Commit 4eb42874 authored by Jan Möbius's avatar Jan Möbius

Fixed some texture related issues (They were loaded but not displayed)

parent 48cbdd97
......@@ -411,13 +411,14 @@ void FileOBJPlugin::createAllGroupObjects(OBJImporter& _importer) {
if (OpenFlipper::Options::gui() && loadOptions_ != 0) {
if (!loadFaceColor_->isChecked())
_importer.objectOptions()[ i ] |= OBJImporter::FORCE_NOCOLOR;
_importer.setOption(OBJImporter::FORCE_NOCOLOR, i);
if (!loadNormals_->isChecked())
_importer.objectOptions()[ i ] |= OBJImporter::FORCE_NONORMALS;
_importer.setOption(OBJImporter::FORCE_NONORMALS, i);
if (!loadTexCoords_->isChecked() || !loadTextures_->isChecked())
_importer.objectOptions()[ i ] |= OBJImporter::FORCE_NOTEXTURES;
_importer.setOption(OBJImporter::FORCE_NOTEXTURES, i);
}
}
}
......@@ -532,9 +533,9 @@ void FileOBJPlugin::addTextures(OBJImporter& _importer, int _objectID ){
QString fullName = _importer.path() + QDir::separator() + options.join(" ");
QFileInfo info(fullName);
if ( info.exists() )
if ( info.exists() ) {
emit addMultiTexture("OBJ Data", info.baseName().trimmed(), fullName, object->id(), textureId );
else {
} else {
emit log(LOGWARN, tr("Unable to load texture image %1").arg( QString(material.map_Kd().c_str()) ) );
addMultiTexture("OBJ Data","Unknown Texture image " + QString::number(textureId), "unknown.png", object->id(), textureId );
}
......@@ -731,9 +732,12 @@ void FileOBJPlugin::readOBJFile(QByteArray& _bufferedFile, QString _filename, OB
Material& mat = _importer.materials()[matname.toStdString()];
if ( mat.has_Texture() ){
//add object if not already there
_importer.useMaterial( matname.toStdString() );
}
}
}
else if (mode == NONE && keyWrd == QLatin1String("v"))
......@@ -1096,7 +1100,7 @@ void FileOBJPlugin::readOBJFile(QByteArray& _bufferedFile, QString _filename, OB
// end
else if (mode == CURVE && keyWrd == QLatin1String("end")){
if ( _importer.isCurve( _importer.currentObject() ) ){
if ( _importer.isCurve( _importer.currentGroup() ) ){
// set up the spline curve
_importer.currentCurve()->set_degree( _importer.degreeU() );
_importer.currentCurve()->autocompute_knotvector(false);
......@@ -1224,7 +1228,7 @@ void FileOBJPlugin::readOBJFile(QByteArray& _bufferedFile, QString _filename, OB
// end
else if (mode == SURFACE && keyWrd == QLatin1String("end")){
if ( _importer.isSurface( _importer.currentObject() ) ){
if ( _importer.isSurface( _importer.currentGroup() ) ){
// remove first 4 entries since they are the first and last knot (for both direction)
cpIndices.erase(cpIndices.begin());
......@@ -1268,10 +1272,9 @@ void FileOBJPlugin::readOBJFile(QByteArray& _bufferedFile, QString _filename, OB
#endif
}
//checks, if an object with a specified type was added. if not, point cloud was read
bool isType = faceCount != 0;
#ifdef ENABLE_BSPLINECURVE_SUPPORT
isType = isType || curveCount != 0;
#endif
......@@ -1287,6 +1290,7 @@ void FileOBJPlugin::readOBJFile(QByteArray& _bufferedFile, QString _filename, OB
if (!inGroup)
_importer.setCurrentGroup(0);
}
}
///check file types and read general info like vertices
......@@ -1734,12 +1738,11 @@ void FileOBJPlugin::checkTypes(QByteArray& _bufferedFile, QString _filename, OBJ
for (unsigned int i = 0; i < _importer.n_vertices(); ++i)
_importer.useVertex(i);
} else {
unsigned int currentOptions = _importer.objectOptions()[_importer.currentObject()];
// this is only a triangle mesh if the object is not a curve and not a surface
// also ignore if it is set to NONE
if (!(currentOptions & OBJImporter::CURVE) &&
!(currentOptions & OBJImporter::SURFACE) &&
(currentOptions != OBJImporter::NONE))
if (!(_importer.isCurve(_importer.currentGroup())) &&
!(_importer.isSurface(_importer.currentGroup())) &&
(_importer.isNone(_importer.currentGroup())) )
_importer.setObjectOptions(OBJImporter::TRIMESH);
}
}
......@@ -1854,7 +1857,7 @@ int FileOBJPlugin::loadObject(QString _filename) {
importer.addGroup( QFileInfo(_filename).fileName() );
//check if something was found
if ( importer.objectOptions().empty() && objIDs.empty() ){
if ( importer.noOptions() && objIDs.empty() ){
forceTriangleMesh_ = false;
forcePolyMesh_ = false;
......@@ -1862,7 +1865,7 @@ int FileOBJPlugin::loadObject(QString _filename) {
return -1;
}
//then parse the obj
//then parse the obj
readOBJFile( bufferedFile, _filename, importer );
// finish up
......@@ -1877,8 +1880,6 @@ int FileOBJPlugin::loadObject(QString _filename) {
pluginExists( "datacontrol", dataControlExists );
if ( dataControlExists ){
std::vector<OBJImporter::ObjectOptions> options = importer.objectOptions();
#if defined ENABLE_BSPLINECURVE_SUPPORT || defined ENABLE_BSPLINESURFACE_SUPPORT
std::map<int, QString> groupNames;
#endif
......@@ -1894,14 +1895,14 @@ int FileOBJPlugin::loadObject(QString _filename) {
std::vector<int> surfaceIds;
int lastSurfaceParent = -2;
#endif
for(unsigned int i = 0; i < importer.objectCount(); i++) {
for(unsigned int i = 0; i < importer.groupCount(); i++) {
// skip the object if it has no option
// this can happen if the object only included other objects
if (options[i] != NONE) {
if ( !importer.isNone(i) ) {
BaseObject* obj = importer.object(i);
if(obj) {
#ifdef ENABLE_BSPLINECURVE_SUPPORT
if (options[i] & OBJImporter::CURVE) {
if ( importer.isCurve(i) ) {
// store the parent group name for later grouping
groupNames[obj->id()] = importer.groupName(importer.getCurveParentId(i));
......@@ -1938,7 +1939,7 @@ int FileOBJPlugin::loadObject(QString _filename) {
#endif
#ifdef ENABLE_BSPLINESURFACE_SUPPORT
if (options[i] & OBJImporter::SURFACE) {
if ( importer.isSurface(i)) {
// store the parent group name for later grouping
groupNames[obj->id()] = importer.groupName(importer.getSurfaceParentId(i));
......@@ -1972,7 +1973,7 @@ int FileOBJPlugin::loadObject(QString _filename) {
}
#endif
if ( (options[i] & OBJImporter::TRIMESH) || (options[i] & OBJImporter::POLYMESH) )
if ( (importer.isTriangleMesh(i) || importer.isPolyMesh(i) ) )
objIDs.push_back( obj->id() );
} else {
std::cerr << "Object is NULL!" << std::endl;
......@@ -2016,8 +2017,8 @@ int FileOBJPlugin::loadObject(QString _filename) {
}
}
//check all new objects
for(unsigned int i=0; i < importer.objectCount(); i++){
//check all new objects which are created for each group
for(unsigned int i=0; i < importer.groupCount(); i++){
BaseObject* object = importer.object(i);
if(object == NULL) continue;
......@@ -2059,6 +2060,7 @@ int FileOBJPlugin::loadObject(QString _filename) {
}
#endif
//textures
if ( importer.hasTexture(i) && !importer.hasOption( i, OBJImporter::FORCE_NOTEXTURES ) ){
......
......@@ -156,7 +156,7 @@ void OBJImporter::setObject(BaseObject* _object, int _groupId) {
//-----------------------------------------------------------------------------
/// get id of the active object
int OBJImporter::currentObject(){
int OBJImporter::currentGroup(){
return currentGroup_;
}
......@@ -259,7 +259,7 @@ void OBJImporter::addUsedVertices(int _groupId) {
/// set vertex texture coordinate
void OBJImporter::setVertexTexCoord(VertexHandle _vh, int _texCoordID){
if ( isTriangleMesh( currentObject() ) ){
if ( isTriangleMesh( currentGroup() ) ){
//handle triangle meshes
if ( !currentTriMesh() ) return;
......@@ -278,7 +278,7 @@ void OBJImporter::setVertexTexCoord(VertexHandle _vh, int _texCoordID){
std::cerr << "Error: TexCoord ID too large" << std::endl;
}
} else if ( isPolyMesh( currentObject() ) ){
} else if ( isPolyMesh( currentGroup() ) ){
//handle poly meshes
if ( !currentPolyMesh() ) return;
......@@ -304,7 +304,7 @@ void OBJImporter::setVertexTexCoord(VertexHandle _vh, int _texCoordID){
/// set vertex normal
void OBJImporter::setNormal(int _index, int _normalID){
if ( isTriangleMesh( currentObject() ) ){
if ( isTriangleMesh( currentGroup() ) ){
//handle triangle meshes
if ( !currentTriMesh() ) return;
......@@ -326,7 +326,7 @@ void OBJImporter::setNormal(int _index, int _normalID){
std::cerr << "Error: normal ID too large" << std::endl;
}
} else if ( isPolyMesh( currentObject() ) ){
} else if ( isPolyMesh( currentGroup() ) ){
//handle poly meshes
if ( !currentPolyMesh() ) return;
......@@ -355,7 +355,7 @@ void OBJImporter::setNormal(int _index, int _normalID){
bool OBJImporter::addFace(const VHandles& _indices, OpenMesh::FaceHandle &_outFH, std::vector< TriMesh::VertexHandle > &_outTriVertices, std::vector< PolyMesh::VertexHandle > &_outPolyVertices)
{
if ( isTriangleMesh( currentObject() ) ){
if ( isTriangleMesh( currentGroup() ) ){
//handle triangle meshes
if ( !currentTriMesh() ) return false;
......@@ -409,7 +409,7 @@ bool OBJImporter::addFace(const VHandles& _indices, OpenMesh::FaceHandle &_outFH
_outFH = fh;
return true;
} else if ( isPolyMesh( currentObject() ) ){
} else if ( isPolyMesh( currentGroup() ) ){
//handle poly meshes
if ( !currentPolyMesh() ) return false;
......@@ -498,7 +498,7 @@ void OBJImporter::addFace(const VHandles& _indices, const std::vector<int>& _fac
if(!addFace(_indices, fh, triVertices, polyVertices) || !fh.is_valid())
return;
if ( isTriangleMesh(currentObject()) )
if ( isTriangleMesh(currentGroup()) )
{
//perhaps request texCoords for the mesh
if ( !currentTriMesh()->has_halfedge_texcoords2D() )
......@@ -526,7 +526,7 @@ void OBJImporter::addFace(const VHandles& _indices, const std::vector<int>& _fac
} else
std::cerr << "Error: cannot set texture coordinates. undefined index." << std::endl;
}
}else if (isPolyMesh(currentObject()))
}else if (isPolyMesh(currentGroup()))
{
addedFacePoly_ = fh;
......@@ -566,7 +566,7 @@ void OBJImporter::addFace(const VHandles& _indices, const std::vector<int>& _fac
void OBJImporter::addMaterial(std::string _materialName){
if ( isTriangleMesh( currentObject() ) ){
if ( isTriangleMesh( currentGroup() ) ){
//handle triangle meshes
if ( !currentTriMesh() ) return;
......@@ -579,11 +579,11 @@ void OBJImporter::addMaterial(std::string _materialName){
//get textureIndex Property
OpenMesh::FPropHandleT< int > indexProperty;
BaseObjectData* triMeshObj = PluginFunctions::baseObjectData(object(currentObject()));
BaseObjectData* triMeshObj = PluginFunctions::baseObjectData(object(currentGroup()));
if ( hasTexture( currentObject() ) ){
if ( hasTexture( currentGroup() ) ){
bool textureAllowed = ! ( objectOptions_[ currentObject() ] & FORCE_NOTEXTURES );
bool textureAllowed = ! ( objectOptions_[ currentGroup() ] & FORCE_NOTEXTURES );
if ( textureAllowed ){
......@@ -632,7 +632,7 @@ void OBJImporter::addMaterial(std::string _materialName){
for (unsigned int i=0; i < addedFacesTri_[currentGroup_].size(); i++){
if ( mat.has_Kd() ) {
bool colorAllowed = ! ( objectOptions_[ currentObject() ] & FORCE_NOCOLOR );
bool colorAllowed = ! ( objectOptions_[ currentGroup() ] & FORCE_NOCOLOR );
if ( currentTriMesh()->has_face_colors() && colorAllowed ){
......@@ -645,28 +645,28 @@ void OBJImporter::addMaterial(std::string _materialName){
color[3] = 1.0;
}
currentTriMesh()->set_color(addedFacesTri_[currentGroup_][i], color );
objectOptions_[ currentObject() ] |= FACECOLOR;
objectOptions_[ currentGroup() ] |= FACECOLOR;
}
}
bool textureAllowed = ! ( objectOptions_[ currentObject() ] & FORCE_NOTEXTURES );
bool textureAllowed = ! ( objectOptions_[ currentGroup() ] & FORCE_NOTEXTURES );
// Set the texture index in the face index property
if ( mat.has_Texture() ) {
if ( hasTexture( currentObject() ) && textureAllowed )
if ( hasTexture( currentGroup() ) && textureAllowed )
currentTriMesh()->property(indexProperty, addedFacesTri_[currentGroup_][i]) = mat.map_Kd_index();
} else {
// If we don't have the info, set it to no texture
if ( hasTexture( currentObject() ) && textureAllowed )
if ( hasTexture( currentGroup() ) && textureAllowed )
currentTriMesh()->property(indexProperty, addedFacesTri_[currentGroup_][i]) = 0;
}
}
}
} else if ( isPolyMesh( currentObject() ) ){
} else if ( isPolyMesh( currentGroup() ) ){
//handle poly meshes
if ( !currentPolyMesh() ) return;
......@@ -678,11 +678,11 @@ void OBJImporter::addMaterial(std::string _materialName){
//get textureIndex Property
OpenMesh::FPropHandleT< int > indexProperty;
BaseObjectData* polyMeshObj = PluginFunctions::baseObjectData(object(currentObject()));
BaseObjectData* polyMeshObj = PluginFunctions::baseObjectData(object(currentGroup()));
if ( hasTexture( currentObject() ) ){
if ( hasTexture( currentGroup() ) ){
bool textureAllowed = ! ( objectOptions_[ currentObject() ] & FORCE_NOTEXTURES );
bool textureAllowed = ! ( objectOptions_[ currentGroup() ] & FORCE_NOTEXTURES );
if ( textureAllowed ){
......@@ -692,7 +692,7 @@ void OBJImporter::addMaterial(std::string _materialName){
}
if ( mat.has_Kd() ) {
bool colorAllowed = ! ( objectOptions_[ currentObject() ] & FORCE_NOCOLOR );
bool colorAllowed = ! ( objectOptions_[ currentGroup() ] & FORCE_NOCOLOR );
if ( currentPolyMesh()->has_face_colors() && colorAllowed && addedFacePoly_.is_valid() ){
......@@ -707,7 +707,7 @@ void OBJImporter::addMaterial(std::string _materialName){
currentPolyMesh()->set_color(addedFacePoly_, color );
polyMeshObj->materialNode()->set_diffuse_color(color);
objectOptions_[ currentObject() ] |= FACECOLOR;
objectOptions_[ currentGroup() ] |= FACECOLOR;
}
}
......@@ -741,17 +741,17 @@ void OBJImporter::addMaterial(std::string _materialName){
polyMeshObj->materialNode()->set_refractive(mat.isRefractive());
}
bool textureAllowed = ! ( objectOptions_[ currentObject() ] & FORCE_NOTEXTURES );
bool textureAllowed = ! ( objectOptions_[ currentGroup() ] & FORCE_NOTEXTURES );
// Set the texture index in the face index property
if ( mat.has_Texture() ) {
if ( hasTexture( currentObject() ) && textureAllowed && addedFacePoly_.is_valid())
if ( hasTexture( currentGroup() ) && textureAllowed && addedFacePoly_.is_valid())
currentPolyMesh()->property(indexProperty, addedFacePoly_) = mat.map_Kd_index();
} else {
// If we don't have the info, set it to no texture
if ( hasTexture( currentObject() ) && textureAllowed && addedFacePoly_.is_valid())
if ( hasTexture( currentGroup() ) && textureAllowed && addedFacePoly_.is_valid())
currentPolyMesh()->property(indexProperty, addedFacePoly_) = 0;
}
}
......@@ -763,14 +763,22 @@ void OBJImporter::addMaterial(std::string _materialName){
// force all meshes to be opened with specific type
void OBJImporter::forceMeshType( ObjectOptions _meshType ){
for (unsigned int i=0; i < objectOptions_.size(); i++){
bool isMesh = (objectOptions_[i] & TRIMESH) | (objectOptions_[i] & POLYMESH);
bool correctType = objectOptions_[i] & _meshType;
if ( isMesh && !correctType )
objectOptions_[i] = _meshType;
}
bool isMesh = (objectOptions_[currentGroup_] & TRIMESH) | (objectOptions_[currentGroup_] & POLYMESH);
bool correctType = objectOptions_[currentGroup_] & _meshType;
if ( isMesh && !correctType ) {
ObjectOptions options = objectOptions_[currentGroup_];
options &= ~(1<<1);
options &= ~(1<<2);
options &= ~(1<<3);
options |= _meshType;
objectOptions_[currentGroup_] = options;
}
}
//-----------------------------------------------------------------------------
......@@ -797,6 +805,10 @@ bool OBJImporter::isSurface(int _objectID){
return objectOptions_[ _objectID ] & SURFACE;
}
bool OBJImporter::isNone(int _objectID) {
return objectOptions_[ _objectID ] == NONE;
}
//-----------------------------------------------------------------------------
bool OBJImporter::hasNormals(int _objectID){
......@@ -819,6 +831,17 @@ bool OBJImporter::hasTextureCoords(int _objectID){
//-----------------------------------------------------------------------------
void OBJImporter::setOption( ObjectOptionsE _option) {
objectOptions_[ currentGroup_ ] |= _option;
}
/// Set Object Option
void OBJImporter::setOption( ObjectOptionsE _option, int _groupId ) {
objectOptions_[ _groupId ] |= _option;
}
//-----------------------------------------------------------------------------
unsigned int OBJImporter::n_vertices(){
return vertices_.size();
}
......@@ -837,24 +860,24 @@ unsigned int OBJImporter::n_texCoords(){
//-----------------------------------------------------------------------------
unsigned int OBJImporter::objectCount(){
unsigned int OBJImporter::groupCount(){
return groupNames_.size();
}
//-----------------------------------------------------------------------------
BaseObject* OBJImporter::object(int _objectID) {
BaseObject* OBJImporter::object(int _groupId) {
if (objectCount() == 0 || objectCount() <= (unsigned int)_objectID)
if (groupCount() == 0 || groupCount() <= (unsigned int)_groupId)
return NULL;
if(triMeshes_[_objectID] != NULL) return triMeshes_[_objectID];
else if(polyMeshes_[_objectID] != NULL) return polyMeshes_[_objectID];
if(triMeshes_[_groupId] != NULL) return triMeshes_[_groupId];
else if(polyMeshes_[_groupId] != NULL) return polyMeshes_[_groupId];
#ifdef ENABLE_BSPLINECURVE_SUPPORT
else if(bSplineCurves_[_objectID] != NULL) return bSplineCurves_[_objectID];
else if(bSplineCurves_[_groupId] != NULL) return bSplineCurves_[_groupId];
#endif
#ifdef ENABLE_BSPLINESURFACE_SUPPORT
else if(bSplineSurfaces_[_objectID] != NULL) return bSplineSurfaces_[_objectID];
else if(bSplineSurfaces_[_groupId] != NULL) return bSplineSurfaces_[_groupId];
#endif
return NULL;
......@@ -891,8 +914,8 @@ void OBJImporter::setObjectOptions(ObjectOptions _options) {
//-----------------------------------------------------------------------------
std::vector< OBJImporter::ObjectOptions >& OBJImporter::objectOptions(){
return objectOptions_;
bool OBJImporter::noOptions() {
return objectOptions_.empty();
}
//-----------------------------------------------------------------------------
......@@ -993,7 +1016,7 @@ void OBJImporter::finish() {
// and add them as new isolated face
if(invalidFaces_[currentGroup_].empty()) return;
if (isTriangleMesh(currentObject())) {
if (isTriangleMesh(currentGroup())) {
// Handle triangle meshes
if ( !currentTriMesh() ) return;
......@@ -1157,17 +1180,18 @@ const std::vector<std::string> OBJImporter::usedMaterials(unsigned int _objectID
void OBJImporter::useMaterial( std::string _materialName ){
while( (int)usedMaterials_.size() - 1 < currentObject() )
while( (int)usedMaterials_.size() - 1 < currentGroup() )
usedMaterials_.push_back( std::vector<std::string>() );
//check that it is not added already
for (unsigned int i=0; i < usedMaterials_[ currentObject() ].size(); i++ )
if ( usedMaterials_[ currentObject() ][i] == _materialName )
for (unsigned int i=0; i < usedMaterials_[ currentGroup() ].size(); i++ )
if ( usedMaterials_[ currentGroup() ][i] == _materialName )
return;
usedMaterials_[ currentObject() ].push_back( _materialName );
usedMaterials_[ currentGroup() ].push_back( _materialName );
objectOptions_[ currentObject() ] |= TEXTURE;
objectOptions_[ currentGroup() ] |= TEXTURE;
}
///used vertices
......
......@@ -134,8 +134,15 @@ class OBJImporter
/// add an object
void setObject( BaseObject* _object, int _groupId );
/// get id of the active object
int currentObject();
/**\brief Get the id of the current group
*
* OBJ files can contain groups which are handled inside OpenFlipper as separate Objects.
* When loading a file, the importer has to keep track of the current group and store a state
* for each group with its properties (materials) )
*
* @return Id of the current group
*/
int currentGroup();
/// get a pointer to the active polyMesh
PolyMesh* currentPolyMesh();
......@@ -201,16 +208,29 @@ class OBJImporter
bool isPolyMesh(int _objectID);
bool isCurve(int _objectID);
bool isSurface(int _objectID);
bool isNone(int _objectID);
/// Set Object Option
void setOption( ObjectOptionsE _option);
/// Set Object Option
void setOption( ObjectOptionsE _option, int _groupId);
/// Global Properties
unsigned int n_vertices();
unsigned int n_normals();
unsigned int n_texCoords();
unsigned int objectCount();
/** \brief Number of groups currently stored in the importer
*
* As OBJ files can contain several groups, we need to store information per group.
*
* @return Number of groups currently found by the importer
*/
unsigned int groupCount();
/// return object with given index
BaseObject* object(int _objectID );
/// return object for the given group
BaseObject* object(int _groupId );
/// return all loaded materials
MaterialList& materials();
......@@ -233,8 +253,8 @@ class OBJImporter
/// containing info about the meshType
void setObjectOptions(ObjectOptions _options);
/// Object Options for all objects
std::vector< ObjectOptions >& objectOptions();
/// Return true if the importer has no options stored
bool noOptions();
/// check if object with given id has given option
bool hasOption( unsigned int _id, ObjectOptions _option );
......@@ -258,7 +278,7 @@ class OBJImporter
void finish();
private:
bool vertexListIsManifold(const std::vector<PolyMesh::VertexHandle>& _vertices) const;
// general data
......
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