[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [47352] branches/soc-2012-bratwurst/source /blender/assimp: - further work on assimp integration.

Alexander Gessler alexander.gessler at gmx.net
Sat Jun 2 21:52:52 CEST 2012


Revision: 47352
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=47352
Author:   aramis_acg
Date:     2012-06-02 19:52:47 +0000 (Sat, 02 Jun 2012)
Log Message:
-----------
- further work on assimp integration. Work on material assignment and UV handling. 

Apart from animations (which I didn't start yet), assimp is fully integrated now. There will be some changes to the way how meshes and nodes are imported, though.

Some issues remain, though and I with some model files I'm getting crashes in Windows release builds, so I'm suspecting there is a memory management error somewhere.

Modified Paths:
--------------
    branches/soc-2012-bratwurst/source/blender/assimp/MaterialImporter.cpp
    branches/soc-2012-bratwurst/source/blender/assimp/MaterialImporter.h
    branches/soc-2012-bratwurst/source/blender/assimp/MeshImporter.cpp
    branches/soc-2012-bratwurst/source/blender/assimp/MeshImporter.h
    branches/soc-2012-bratwurst/source/blender/assimp/SceneImporter.cpp
    branches/soc-2012-bratwurst/source/blender/assimp/SceneImporter.h

Modified: branches/soc-2012-bratwurst/source/blender/assimp/MaterialImporter.cpp
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/MaterialImporter.cpp	2012-06-02 19:25:12 UTC (rev 47351)
+++ branches/soc-2012-bratwurst/source/blender/assimp/MaterialImporter.cpp	2012-06-02 19:52:47 UTC (rev 47352)
@@ -86,9 +86,23 @@
 }
 
 
+const UVTextureInfo* MaterialImporter::get_uv_texture(unsigned int index) const
+{
+	if (index >= uv_textures.size()) {
+		return NULL;
+	}
+	return &uv_textures[index];
+}
 
-Material* MaterialImporter::extract_material()
+
+Material* MaterialImporter::get_material() const
 {
+	return mat;
+}
+
+
+Material* MaterialImporter::disown_material()
+{
 	Material* m = mat;
 	mat = NULL;
 	return m;
@@ -147,19 +161,19 @@
 void MaterialImporter::convert_textures()
 {
 	aiString path;
-	aiTextureMapping mapping;
-	unsigned int uv;
-	aiTextureOp op;
-	aiTextureMapMode mapMode;
-	unsigned int flags;
-	float blend;
+	aiTextureMapping mapping = aiTextureMapping_UV;
+	unsigned int uv = 0;
+	aiTextureOp op = aiTextureOp_Add;
+	aiTextureMapMode mapMode[2] = {aiTextureMapMode_Wrap,aiTextureMapMode_Wrap};
+	unsigned int flags = 0;
+	float blend = 1.0f;
 
 	const unsigned int lasttex = static_cast<unsigned int>(aiTextureType_UNKNOWN);
 	for (unsigned int t = 0; t <= lasttex; ++t) {
 		for (unsigned int i = 0; ; ++i) {
 
 			const aiTextureType type = static_cast<aiTextureType>(t);
-			if(aiGetMaterialTexture(in_mat,type,i,&path,&mapping,&uv,&blend,&op,&mapMode,&flags) == AI_SUCCESS) {
+			if(aiGetMaterialTexture(in_mat,type,i,&path,&mapping,&uv,&blend,&op,mapMode,&flags) == AI_SUCCESS) {
 				convert_texture(type,i,path,mapping,uv,op,mapMode,flags,blend);
 			}
 			else {
@@ -173,7 +187,7 @@
 void MaterialImporter::convert_texture(aiTextureType type, unsigned int index, const aiString& path, aiTextureMapping mapping,
 	unsigned int uv,
 	aiTextureOp op,
-	aiTextureMapMode mapMode,
+	aiTextureMapMode mapMode[2],
 	unsigned int flags,
 	float blend)
 {
@@ -183,6 +197,8 @@
 	mtex->tex->type = TEX_IMAGE;
 	mtex->tex->imaflag &= ~TEX_USEALPHA;
 	mtex->tex->ima = convert_image(path);
+
+	// XXX convert further material properties
 }
 
 

Modified: branches/soc-2012-bratwurst/source/blender/assimp/MaterialImporter.h
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/MaterialImporter.h	2012-06-02 19:25:12 UTC (rev 47351)
+++ branches/soc-2012-bratwurst/source/blender/assimp/MaterialImporter.h	2012-06-02 19:52:47 UTC (rev 47352)
@@ -27,10 +27,19 @@
 #ifndef INCLUDED_MATERIALIMPORTER_H
 #define INCLUDED_MATERIALIMPORTER_H
 
+#include <string>
 #include "bassimp_shared.h"
 
 namespace bassimp {
 
+
+struct UVTextureInfo
+{
+	std::string name;
+	Tex* texture;
+};
+
+
 class MaterialImporter 
 {
 private:
@@ -46,6 +55,8 @@
 	const SceneImporter& scene_imp;
 	std::string name;
 
+	std::vector<UVTextureInfo> uv_textures;
+
 private:
 
 	void error(const char* message);
@@ -55,7 +66,7 @@
 	void convert_texture(aiTextureType type, unsigned int index, const aiString& path, aiTextureMapping mapping,
 		unsigned int uv,
 		aiTextureOp op,
-		aiTextureMapMode mapMode,
+		aiTextureMapMode mapMode[2],
 		unsigned int flags,
 		float blend);
 
@@ -72,9 +83,13 @@
 	// run conversion
 	void convert();
 
-	// get converted material, this resets the cached conversion
-	// result and transfers ownership of it to the caller.
-	Material* extract_material();
+	const UVTextureInfo* get_uv_texture(unsigned int index) const;
+
+	// get converted material, but possesion of the Material* stays here
+	Material* get_material() const;
+
+	// disown the converted material (i.e. prevent deletion in constructor)
+	Material* disown_material();
 };
 
 }

Modified: branches/soc-2012-bratwurst/source/blender/assimp/MeshImporter.cpp
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/MeshImporter.cpp	2012-06-02 19:25:12 UTC (rev 47351)
+++ branches/soc-2012-bratwurst/source/blender/assimp/MeshImporter.cpp	2012-06-02 19:52:47 UTC (rev 47352)
@@ -29,6 +29,7 @@
 
 #include "SceneImporter.h"
 #include "MeshImporter.h"
+#include "MaterialImporter.h"
 #include "bassimp_internal.h"
 
 extern "C" {
@@ -37,6 +38,7 @@
 #	include "BKE_mesh.h"
 #	include "BKE_displist.h"
 #	include "BKE_library.h"
+#	include "BKE_material.h"
 
 #	include "MEM_guardedalloc.h"
 
@@ -60,6 +62,8 @@
 
 	// XXX copypaste from collada
 	mesh->id.us--; // is already 1 here, but will be set later in set_mesh
+
+	reverseMatIDs.reserve(16);
 }
 
 
@@ -113,6 +117,12 @@
 
 	// give up ownership of the mesh object
 	mesh = NULL;
+
+	// assign materials
+	for (size_t i = 0; i < reverseMatIDs.size(); ++i) {
+		assign_material(ob,scene_imp.get_material(reverseMatIDs[i]).get_material(),ob->totcol + 1);
+	}
+
 	return ob;
 }
 
@@ -290,10 +300,13 @@
 	for (std::vector<const aiMesh*>::const_iterator it = in_meshes.begin(), end = in_meshes.end(); it != end; ++it) {
 		const aiMesh& m = **it;
 
+		MFace* const start = mface;
+		unsigned int mesh_face_index_start = face_index;
+
 		for (unsigned int i = 0, e = m.mNumFaces; i < e; ++i) {
 			const aiFace& f = m.mFaces[i];
 
-			// skip over POINT and LINE primitives
+			// skip over POINT and LINE primitives at this stage
 			if (f.mNumIndices < 3) {
 				continue;
 			}
@@ -369,10 +382,41 @@
 					nind += vcount; */
 			}
 		}
+
+		if (matIDs.find(m.mMaterialIndex) == matIDs.end()) {
+			matIDs[m.mMaterialIndex] = reverseMatIDs.size();
+			reverseMatIDs.push_back(m.mMaterialIndex);
+		}
+
+		const short mat = static_cast<short>( matIDs[m.mMaterialIndex] );
+
+		// assign material indices to each MFace
+		for (MFace* p = start; p != mface; ++p) {
+			p->mat_nr = mat;
+		}
+
+		// assign textures to each MTFace
+		for (unsigned int k = 0; k < uv_count; k++) {
+			MTFace *mtface = static_cast<MTFace*>(CustomData_get_layer_n(&mesh->fdata, CD_MTFACE, k)) + mesh_face_index_start;
+
+			for (const MFace* p = start; p != mface; ++p, ++mtface) {
+				assign_textures_to_face(*p,*mtface,m.mMaterialIndex,k);
+			}
+		}
 	}
+
 }
 
+void MeshImporter::assign_textures_to_face(const MFace& fac, MTFace& tfac, unsigned int assimp_mat_id, unsigned int uv_index)
+{
+	// bind texture images to faces
+	const UVTextureInfo* uvtex = scene_imp.get_material(assimp_mat_id).get_uv_texture(uv_index);
+	if(uvtex) {
+		tfac.tpage = uvtex->texture->ima;
+	}
+}
 
+
 // initiate the conversion from aiMesh to BlenMesh.
 void MeshImporter::convert()
 {

Modified: branches/soc-2012-bratwurst/source/blender/assimp/MeshImporter.h
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/MeshImporter.h	2012-06-02 19:25:12 UTC (rev 47351)
+++ branches/soc-2012-bratwurst/source/blender/assimp/MeshImporter.h	2012-06-02 19:52:47 UTC (rev 47352)
@@ -27,11 +27,14 @@
 #ifndef INCLUDED_MESHIMPORTER_H
 #define INCLUDED_MESHIMPORTER_H
 
+#include <map>
 #include "bassimp_shared.h"
 
 namespace bassimp {
 
 	class SceneImporter;
+	class MaterialImporter;
+	struct UVTextureInfo;
 
 class MeshImporter 
 {
@@ -44,6 +47,12 @@
 	const SceneImporter& scene_imp;
 	std::string name;
 
+	// assimp-to-mesh local IDs
+	std::map<unsigned int, unsigned int> matIDs;
+
+	// mesh-local-to-assimp IDs
+	std::vector<unsigned int> reverseMatIDs;
+
 private:
 
 	void error(const char* verbose);
@@ -63,6 +72,8 @@
 
 	// polygon triangulation
 	int triangulate_poly(const unsigned int* indices, int totvert, MVert* verts, std::vector<unsigned int>& tri);
+
+	void assign_textures_to_face(const MFace& fac, MTFace& tfac,unsigned int assimp_mat_id, unsigned int uv_index);
 	
 public:
 

Modified: branches/soc-2012-bratwurst/source/blender/assimp/SceneImporter.cpp
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/SceneImporter.cpp	2012-06-02 19:25:12 UTC (rev 47351)
+++ branches/soc-2012-bratwurst/source/blender/assimp/SceneImporter.cpp	2012-06-02 19:52:47 UTC (rev 47352)
@@ -30,6 +30,7 @@
 
 #include "SceneImporter.h"
 #include "MeshImporter.h"
+#include "MaterialImporter.h"
 
 #include "bassimp_internal.h"
 
@@ -62,6 +63,20 @@
 }
 
 
+SceneImporter::~SceneImporter()
+{
+	for (size_t i = 0; i < materials.size(); ++i) {
+		if (materials[i]) {
+			// is the material actually used? if so, make sure the object is preserved
+			if (materials_used[i]) {
+				materials[i]->disown_material();
+			}
+			delete materials[i];
+		}
+	}
+}
+
+
 void SceneImporter::error(const char* what) const
 {
 	std::cerr << "bassimp error: " << what << std::endl;
@@ -109,6 +124,8 @@
 		return false;
 	}
 
+	convert_materials();
+
 	const aiNode* nd = scene->mRootNode;
 	convert_node(nd,NULL);
 
@@ -117,6 +134,38 @@
 }
 
 
+void SceneImporter::convert_materials() 
+{
+	materials.resize(scene->mNumMaterials);
+	materials_used.resize(scene->mNumMaterials);
+
+	for (unsigned int i = 0; i < scene->mNumMaterials; ++i) {
+		materials[i] = new MaterialImporter(*this,scene->mMaterials[i],scene,out_scene,i);
+		materials[i]->convert();
+	}
+}
+
+
+unsigned int SceneImporter::resolve_matid(unsigned int src) const
+{
+	if (src >= materials.size()) {
+		error("material index out of range, ignoring");
+		// assimp does create a default material so we can rely on this
+		return 0;
+	}
+
+	materials_used[src] = true;
+	return src;
+}
+
+
+const MaterialImporter& SceneImporter::get_material(unsigned int idx) const
+{
+	const unsigned int i = resolve_matid(idx);
+	return *materials[i];
+}
+
+
 void SceneImporter::convert_node(const aiNode* in_node, Object* out_parent) const
 {
 	assert(in_node != NULL);

Modified: branches/soc-2012-bratwurst/source/blender/assimp/SceneImporter.h
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/SceneImporter.h	2012-06-02 19:25:12 UTC (rev 47351)
+++ branches/soc-2012-bratwurst/source/blender/assimp/SceneImporter.h	2012-06-02 19:52:47 UTC (rev 47352)
@@ -31,6 +31,8 @@
 
 namespace bassimp {
 
+	class MaterialImporter;
+
 class SceneImporter
 {
 private:
@@ -42,9 +44,13 @@
 	Assimp::Importer importer;
 	const aiScene* scene;
 
+	std::vector<MaterialImporter*> materials;
+	mutable std::vector<bool> materials_used;
+
 private:
 
 	unsigned int get_assimp_flags() const;
+	void convert_materials();
 	void convert_node(const aiNode* in_node, Object* out_parent) const;
 	void convert_node_transform(const aiNode* node_in, Object* out_obj) const;

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list