Prechádzať zdrojové kódy

Added uv and colour support

Kester Maddock 12 rokov pred
rodič
commit
3713383b0f
4 zmenil súbory, kde vykonal 129 pridanie a 2 odobranie
  1. 33 2
      code/BlenderLoader.cpp
  2. 55 0
      code/BlenderScene.cpp
  3. 23 0
      code/BlenderScene.h
  4. 18 0
      code/BlenderSceneGen.h

+ 33 - 2
code/BlenderLoader.cpp

@@ -838,7 +838,7 @@ void BlenderImporter::ConvertMesh(const Scene& /*in*/, const Object* /*obj*/, co
 	}
 	
 	// collect texture coordinates, they're stored in a separate per-face buffer
-	if (mesh->mtface) {
+	if (mesh->mtface || mesh->mloopuv) {
 		if (mesh->totface > static_cast<int> ( mesh->mtface.size())) {
 			ThrowException("Number of UV faces is larger than the corresponding UV face array (#1)");
 		}
@@ -861,6 +861,20 @@ void BlenderImporter::ConvertMesh(const Scene& /*in*/, const Object* /*obj*/, co
 				vo->y = v->uv[i][1];
 			}
 		}
+		
+		for (int i = 0; i < mesh->totpoly; ++i) {
+			const MPoly& v = mesh->mpoly[i];
+			aiMesh* const out = temp[ mat_num_to_mesh_idx[ v.mat_nr ] ];
+			const aiFace& f = out->mFaces[out->mNumFaces++];
+			
+			aiVector3D* vo = &out->mTextureCoords[0][out->mNumVertices];
+			for (unsigned int j = 0; j < f.mNumIndices; ++j,++vo,++out->mNumVertices) {
+				const MLoopUV& uv = mesh->mloopuv[v.loopstart + j];
+				vo->x = uv.uv[0];
+				vo->y = uv.uv[1];
+			}
+			
+		}
 	}
 
 	// collect texture coordinates, old-style (marked as deprecated in current blender sources)
@@ -890,7 +904,7 @@ void BlenderImporter::ConvertMesh(const Scene& /*in*/, const Object* /*obj*/, co
 	}
 
 	// collect vertex colors, stored separately as well
-	if (mesh->mcol) {
+	if (mesh->mcol || mesh->mloopcol) {
 		if (mesh->totface > static_cast<int> ( (mesh->mcol.size()/4)) ) {
 			ThrowException("Number of faces is larger than the corresponding color face array");
 		}
@@ -917,6 +931,23 @@ void BlenderImporter::ConvertMesh(const Scene& /*in*/, const Object* /*obj*/, co
 			}
 			for (unsigned int n = f.mNumIndices; n < 4; ++n);
 		}
+		
+		for (int i = 0; i < mesh->totpoly; ++i) {
+			const MPoly& v = mesh->mpoly[i];
+			aiMesh* const out = temp[ mat_num_to_mesh_idx[ v.mat_nr ] ];
+			const aiFace& f = out->mFaces[out->mNumFaces++];
+			
+			aiColor4D* vo = &out->mColors[0][out->mNumVertices];
+			for (unsigned int j = 0; j < f.mNumIndices; ++j,++vo,++out->mNumVertices) {
+				const MLoopCol& col = mesh->mloopcol[v.loopstart + j];
+				vo->r = col.r;
+				vo->g = col.g;
+				vo->b = col.b;
+				vo->a = col.a;
+			}
+			
+		}
+
 	}
 
 	return;

+ 55 - 0
code/BlenderScene.cpp

@@ -305,6 +305,27 @@ template <> void Structure :: Convert<Material> (
 	db.reader->IncPtr(size);
 }
 
+//--------------------------------------------------------------------------------
+template <> void Structure :: Convert<MTexPoly> (
+    MTexPoly& dest,
+    const FileDatabase& db
+    ) const
+{ 
+
+    {
+        boost::shared_ptr<Image> tpage;
+        ReadFieldPtr<ErrorPolicy_Igno>(tpage,"*tpage",db);
+        dest.tpage = tpage.get();
+    }
+    ReadField<ErrorPolicy_Igno>(dest.flag,"flag",db);
+    ReadField<ErrorPolicy_Igno>(dest.transp,"transp",db);
+    ReadField<ErrorPolicy_Igno>(dest.mode,"mode",db);
+    ReadField<ErrorPolicy_Igno>(dest.tile,"tile",db);
+    ReadField<ErrorPolicy_Igno>(dest.pad,"pad",db);
+
+	db.reader->IncPtr(size);
+}
+
 //--------------------------------------------------------------------------------
 template <> void Structure :: Convert<Mesh> (
     Mesh& dest,
@@ -328,7 +349,10 @@ template <> void Structure :: Convert<Mesh> (
     ReadFieldPtr<ErrorPolicy_Fail>(dest.mvert,"*mvert",db);
     ReadFieldPtr<ErrorPolicy_Warn>(dest.medge,"*medge",db);
     ReadFieldPtr<ErrorPolicy_Igno>(dest.mloop,"*mloop",db);
+    ReadFieldPtr<ErrorPolicy_Igno>(dest.mloopuv,"*mloopuv",db);
+    ReadFieldPtr<ErrorPolicy_Igno>(dest.mloopcol,"*mloopcol",db);
     ReadFieldPtr<ErrorPolicy_Igno>(dest.mpoly,"*mpoly",db);
+    ReadFieldPtr<ErrorPolicy_Igno>(dest.mtpoly,"*mtpoly",db);
     ReadFieldPtr<ErrorPolicy_Igno>(dest.dvert,"*dvert",db);
     ReadFieldPtr<ErrorPolicy_Igno>(dest.mcol,"*mcol",db);
     ReadFieldPtr<ErrorPolicy_Fail>(dest.mat,"**mat",db);
@@ -361,6 +385,21 @@ template <> void Structure :: Convert<World> (
 	db.reader->IncPtr(size);
 }
 
+//--------------------------------------------------------------------------------
+template <> void Structure :: Convert<MLoopCol> (
+    MLoopCol& dest,
+    const FileDatabase& db
+    ) const
+{ 
+
+    ReadField<ErrorPolicy_Igno>(dest.r,"r",db);
+    ReadField<ErrorPolicy_Igno>(dest.g,"g",db);
+    ReadField<ErrorPolicy_Igno>(dest.b,"b",db);
+    ReadField<ErrorPolicy_Igno>(dest.a,"a",db);
+
+	db.reader->IncPtr(size);
+}
+
 //--------------------------------------------------------------------------------
 template <> void Structure :: Convert<MVert> (
     MVert& dest,
@@ -393,6 +432,19 @@ template <> void Structure :: Convert<MEdge> (
 	db.reader->IncPtr(size);
 }
 
+//--------------------------------------------------------------------------------
+template <> void Structure :: Convert<MLoopUV> (
+    MLoopUV& dest,
+    const FileDatabase& db
+    ) const
+{ 
+
+    ReadFieldArray<ErrorPolicy_Igno>(dest.uv,"uv",db);
+    ReadField<ErrorPolicy_Igno>(dest.flag,"flag",db);
+
+	db.reader->IncPtr(size);
+}
+
 //--------------------------------------------------------------------------------
 template <> void Structure :: Convert<GroupObject> (
     GroupObject& dest,
@@ -615,11 +667,14 @@ void DNA::RegisterConverters() {
     converters["Base"] = DNA::FactoryPair( &Structure::Allocate<Base>, &Structure::Convert<Base> );
     converters["MTFace"] = DNA::FactoryPair( &Structure::Allocate<MTFace>, &Structure::Convert<MTFace> );
     converters["Material"] = DNA::FactoryPair( &Structure::Allocate<Material>, &Structure::Convert<Material> );
+    converters["MTexPoly"] = DNA::FactoryPair( &Structure::Allocate<MTexPoly>, &Structure::Convert<MTexPoly> );
     converters["Mesh"] = DNA::FactoryPair( &Structure::Allocate<Mesh>, &Structure::Convert<Mesh> );
     converters["MDeformVert"] = DNA::FactoryPair( &Structure::Allocate<MDeformVert>, &Structure::Convert<MDeformVert> );
     converters["World"] = DNA::FactoryPair( &Structure::Allocate<World>, &Structure::Convert<World> );
+    converters["MLoopCol"] = DNA::FactoryPair( &Structure::Allocate<MLoopCol>, &Structure::Convert<MLoopCol> );
     converters["MVert"] = DNA::FactoryPair( &Structure::Allocate<MVert>, &Structure::Convert<MVert> );
     converters["MEdge"] = DNA::FactoryPair( &Structure::Allocate<MEdge>, &Structure::Convert<MEdge> );
+    converters["MLoopUV"] = DNA::FactoryPair( &Structure::Allocate<MLoopUV>, &Structure::Convert<MLoopUV> );
     converters["GroupObject"] = DNA::FactoryPair( &Structure::Allocate<GroupObject>, &Structure::Convert<GroupObject> );
     converters["ListBase"] = DNA::FactoryPair( &Structure::Allocate<ListBase>, &Structure::Convert<ListBase> );
     converters["MLoop"] = DNA::FactoryPair( &Structure::Allocate<MLoop>, &Structure::Convert<MLoop> );

+ 23 - 0
code/BlenderScene.h

@@ -94,6 +94,7 @@ namespace Assimp	{
 
 struct Object;
 struct MTex;
+struct Image;
 
 #define AI_BLEND_MESH_MAX_VERTS 2000000000L
 
@@ -161,6 +162,18 @@ struct MLoop : ElemBase {
 	int v, e;
 };
 
+// -------------------------------------------------------------------------------
+struct MLoopUV : ElemBase {
+	float uv[2];
+	int flag;
+};
+
+// -------------------------------------------------------------------------------
+// Note that red and blue are not swapped, as with MCol
+struct MLoopCol : ElemBase {
+	char r, g, b, a;
+};
+
 // -------------------------------------------------------------------------------
 struct MPoly : ElemBase {
 	int loopstart;
@@ -169,6 +182,13 @@ struct MPoly : ElemBase {
 	char flag;
 };
 
+// -------------------------------------------------------------------------------
+struct MTexPoly : ElemBase {
+	Image* tpage;
+	char flag, transp;
+	short mode, tile, pad;
+};
+
 // -------------------------------------------------------------------------------
 struct MCol : ElemBase {
 	char r,g,b,a FAIL;
@@ -262,7 +282,10 @@ struct Mesh : ElemBase {
 	vector<MVert> mvert FAIL;
 	vector<MEdge> medge WARN;
 	vector<MLoop> mloop;
+	vector<MLoopUV> mloopuv;
+	vector<MLoopCol> mloopcol;
 	vector<MPoly> mpoly;
+	vector<MTexPoly> mtpoly;
 	vector<MDeformVert> dvert;
 	vector<MCol> mcol;
 

+ 18 - 0
code/BlenderSceneGen.h

@@ -120,6 +120,12 @@ template <> void Structure :: Convert<Material> (
     ) const
 ;
 
+template <> void Structure :: Convert<MTexPoly> (
+    MTexPoly& dest,
+    const FileDatabase& db
+    ) const
+;
+
 template <> void Structure :: Convert<Mesh> (
     Mesh& dest,
     const FileDatabase& db
@@ -138,6 +144,12 @@ template <> void Structure :: Convert<World> (
     ) const
 ;
 
+template <> void Structure :: Convert<MLoopCol> (
+    MLoopCol& dest,
+    const FileDatabase& db
+    ) const
+;
+
 template <> void Structure :: Convert<MVert> (
     MVert& dest,
     const FileDatabase& db
@@ -150,6 +162,12 @@ template <> void Structure :: Convert<MEdge> (
     ) const
 ;
 
+template <> void Structure :: Convert<MLoopUV> (
+    MLoopUV& dest,
+    const FileDatabase& db
+    ) const
+;
+
 template <> void Structure :: Convert<GroupObject> (
     GroupObject& dest,
     const FileDatabase& db