First Release with OBJ import support

This commit is contained in:
Paolo Cignoni 2006-03-07 13:19:29 +00:00
parent 5dee0e155b
commit e71a29248f
4 changed files with 1326 additions and 21 deletions

View File

@ -25,6 +25,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.2 2006/02/28 14:38:09 corsini
remove qt include
Revision 1.1 2006/02/16 19:28:36 fmazzant
transfer of Export_3ds.h, Export_obj.h, Io_3ds_obj_material.h from Meshlab to vcg
@ -75,7 +78,7 @@
#include <wrap/callback.h>
#include <vcg/complex/trimesh/allocate.h>
#include <wrap/io_trimesh/io_mask.h>
#include "io_3ds_obj_material.h"
#include "io_material.h"
#include <iostream>
#include <fstream>
#include <map>
@ -151,10 +154,8 @@ namespace io {
/*
function which saves in OBJ file format
*/
static int SaveASCII(SaveMeshType &m, const char * filename, ObjInfo &oi)
static int SaveASCII(SaveMeshType &m, const char * filename, int mask, CallBackPos *cb=0)
{
CallBackPos *cb = oi.cb;
if(m.vert.size() == 0)
return E_NOTVEXTEXVALID;
if(m.face.size() == 0)
@ -177,7 +178,7 @@ namespace io {
fprintf(fp,"# Object %s\n#\n# Vertices: %d\n# Faces: %d\n#\n####\n",fn.substr(i+1).c_str(),m.vert.size(),m.face.size());
//library materials
if(oi.mask & vcg::tri::io::Mask::IOM_FACECOLOR)
if(mask & vcg::tri::io::Mask::IOM_FACECOLOR)
fprintf(fp,"mtllib ./%s.mtl\n\n",fn.substr(i+1).c_str());
//vertexs + normal
@ -188,7 +189,7 @@ namespace io {
for(vi=m.vert.begin(); vi!=m.vert.end(); ++vi) if( !(*vi).IsD() )
{
//saves normal per vertex
if(oi.mask & vcg::tri::io::Mask::IOM_VERTNORMAL | oi.mask & vcg::tri::io::Mask::IOM_WEDGNORMAL)
if(mask & vcg::tri::io::Mask::IOM_VERTNORMAL | mask & vcg::tri::io::Mask::IOM_WEDGNORMAL)
{
if(AddNewNormalVertex(NormalVertex,(*vi).N(),value))
{
@ -201,7 +202,7 @@ namespace io {
fprintf(fp,"v %f %f %f\n",(*vi).P()[0],(*vi).P()[1],(*vi).P()[2]);
if (cb !=NULL)
(*cb)(100.0 * (float)++current/(float)max, "writing vertices ");
(*cb)((100*++current)/max, "writing vertices ");
else
{ fclose(fp); return E_ABORTED;}
}
@ -215,7 +216,7 @@ namespace io {
/*int*/ value = 1;//tmp
for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() )
{
if(oi.mask & vcg::tri::io::Mask::IOM_FACECOLOR)
if(mask & vcg::tri::io::Mask::IOM_FACECOLOR)
{
int index = vcg::tri::io::Materials<SaveMeshType>::CreateNewMaterial(m,materials,material_num,fi);
@ -239,7 +240,7 @@ namespace io {
unsigned int MAX = 3;
for(unsigned int k=0;k<MAX;k++)
{
if(m.HasPerWedgeTexture() && oi.mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD)
if(m.HasPerWedgeTexture() && mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD)
{
if(AddNewTextureCoord(CoordIndexTexture,(*fi).WT(k),value))
{
@ -257,11 +258,11 @@ namespace io {
v = GetIndexVertex(m, (*fi).V(k)) + 1;//index of vertex per face
int vt = -1;
if(oi.mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD)
if(mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD)
vt = GetIndexVertexTexture(CoordIndexTexture,(*fi).WT(k));//index of vertex texture per face
int vn = -1;
if(oi.mask & vcg::tri::io::Mask::IOM_VERTNORMAL | oi.mask & vcg::tri::io::Mask::IOM_WEDGNORMAL)
if(mask & vcg::tri::io::Mask::IOM_VERTNORMAL | mask & vcg::tri::io::Mask::IOM_WEDGNORMAL)
vn = GetIndexVertexNormal(m, NormalVertex, v);//index of vertex normal per face.
//writes elements on file obj
@ -273,7 +274,7 @@ namespace io {
fprintf(fp,"\n");
}
if (cb !=NULL)
(*cb)(100.0 * (float)++current/(float)max, "writing faces ");
(*cb)((100*++current)/max, "writing faces ");
else
{ fclose(fp); return E_ABORTED;}
}//for
@ -283,7 +284,7 @@ namespace io {
fclose(fp);
int r = 0;
if(oi.mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD | oi.mask & vcg::tri::io::Mask::IOM_FACECOLOR)
if(mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD | mask & vcg::tri::io::Mask::IOM_FACECOLOR)
r = WriteMaterials(materials, filename,cb);//write material
if(r!= E_NOERROR)
@ -294,7 +295,7 @@ namespace io {
/*
function which saves in OBJ file format
*/
static int SaveBinary(SaveMeshType &m, const char * filename, ObjInfo &oi)
static int SaveBinary(SaveMeshType &m, const char * filename)
{
return E_NOTDEFINITION;
}
@ -304,10 +305,7 @@ namespace io {
*/
static int Save(SaveMeshType &m, const char * filename, const int &mask, CallBackPos *cb=0)
{
ObjInfo oi;
oi.cb=cb;
oi.mask=mask;
return SaveASCII(m,filename,oi);
return SaveASCII(m,filename,mask,cb);
}
/*
@ -397,7 +395,7 @@ namespace io {
for(unsigned int i=0;i<materials.size();i++)
{
if (cb !=NULL)
(*cb)(100.0 * (float)++current/(float)materials.size(), "saving material file ");
(*cb)((100 * ++current)/materials.size(), "saving material file ");
else
{ fclose(fp); return E_ABORTED;}

View File

@ -25,6 +25,9 @@
History
$Log: not supported by cvs2svn $
Revision 1.7 2006/02/28 14:50:00 corsini
Fix comments
Revision 1.6 2006/02/10 16:14:53 corsini
Fix typo
@ -49,6 +52,7 @@ Initial Update
#ifndef __VCGLIB_IMPORT
#define __VCGLIB_IMPORT
#include <wrap/io_trimesh/import_obj.h>
#include <wrap/io_trimesh/import_ply.h>
#include <wrap/io_trimesh/import_stl.h>
#include <wrap/io_trimesh/import_off.h>
@ -68,7 +72,7 @@ template <class OpenMeshType>
class Importer
{
private:
enum KnownTypes { KT_UNKNOWN, KT_PLY, KT_STL, KT_OFF };
enum KnownTypes { KT_UNKNOWN, KT_PLY, KT_STL, KT_OFF, KT_OBJ };
static int &LastType()
{
static int lastType= KT_UNKNOWN;
@ -112,7 +116,12 @@ static int Open(OpenMeshType &m, const char *filename, int &loadmask, CallBackPo
err = ImporterOFF<OpenMeshType>::Open(m, filename, loadmask, cb);
LastType()=KT_OFF;
}
else {
else if(FileExtension(filename,"obj"))
{
err = ImporterOBJ<OpenMeshType>::Open(m, filename, loadmask, cb);
LastType()=KT_OBJ;
}
else {
err=1;
LastType()=KT_UNKNOWN;
}

1162
wrap/io_trimesh/import_obj.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,136 @@
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
Revision 1.1 2006/02/16 19:28:36 fmazzant
transfer of Export_3ds.h, Export_obj.h, Io_3ds_obj_material.h from Meshlab to vcg
Revision 1.1 2006/02/06 11:04:40 fmazzant
added file material.h. it include struct Material, CreateNewMaterial(...) and MaterialsCompare(...)
****************************************************************************/
#ifndef __VCGLIB_MATERIAL
#define __VCGLIB_MATERIAL
namespace vcg {
namespace tri {
namespace io {
/*
structures material
*/
struct Material
{
unsigned int index;//index of material
Point3f Ka;//ambient
Point3f Kd;//diffuse
Point3f Ks;//specular
float d;//alpha
float Tr;//alpha
int illum;//specular illumination
float Ns;
std::string map_Kd; //filename texture
};
template <class SaveMeshType>
class Materials
{
public:
typedef typename SaveMeshType::FaceIterator FaceIterator;
typedef typename SaveMeshType::VertexIterator VertexIterator;
typedef typename SaveMeshType::VertexType VertexType;
/*
creates a new meterial
*/
inline static int CreateNewMaterial(SaveMeshType &m, std::vector<Material> &materials, unsigned int index, FaceIterator &fi)
{
unsigned char r = (*fi).C()[0];
unsigned char g = (*fi).C()[1];
unsigned char b = (*fi).C()[2];
unsigned char alpha = (*fi).C()[3];
Point3f diffuse = Point3f((float)r/255.0f,(float)g/255.0f,(float)b/255.0f);//diffuse
float Tr = (float)alpha/255.0f;//alpha
int illum = 2; //default not use Ks!
float ns = 0.0; //default
Material mtl;
mtl.index = index;//index of materials
mtl.Ka = Point3f(0.2f,0.2f,0.2f);//ambient
mtl.Kd = diffuse;//diffuse
mtl.Ks = Point3f(1.0f,1.0f,1.0f);//specular
mtl.Tr = Tr;//alpha
mtl.Ns = ns;
mtl.illum = illum;//illumination
if(m.textures.size() && (*fi).WT(0).n() >=0 )
mtl.map_Kd = m.textures[(*fi).WT(0).n()];
else
mtl.map_Kd = "";
int i = -1;
if((i = MaterialsCompare(materials,mtl)) == -1)
{
materials.push_back(mtl);
return materials.size();
}
return i;
}
/*
returns the index of the material if it exists inside the list of the materials,
otherwise it returns -1.
*/
inline static int MaterialsCompare(std::vector<Material> &materials, Material mtl)
{
for(int i=0;i<materials.size();i++)
{
bool ka = materials[i].Ka == mtl.Ka;
bool kd = materials[i].Kd == mtl.Kd;
bool ks = materials[i].Ks == mtl.Ks;
bool tr = materials[i].Tr == mtl.Tr;
bool illum = materials[i].illum == mtl.illum;
bool ns = materials[i].Ns == mtl.Ns;
bool map = materials[i].map_Kd == mtl.map_Kd;
if(ka & kd & ks & tr & illum & ns & map){return i;}
}
return -1;
}
};
}
}
}
#endif __VCGLIB_MATERIAL