[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [47270] branches/soc-2012-bratwurst/source /blender/assimp: - bf_assimp: WIP work on material and image importing.

Alexander Gessler alexander.gessler at gmx.net
Thu May 31 13:54:41 CEST 2012


Revision: 47270
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=47270
Author:   aramis_acg
Date:     2012-05-31 11:54:41 +0000 (Thu, 31 May 2012)
Log Message:
-----------
- bf_assimp: WIP work on material and image importing.
- bf_assimp: better error reporting and formatting.

Modified Paths:
--------------
    branches/soc-2012-bratwurst/source/blender/assimp/CMakeLists.txt
    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

Added Paths:
-----------
    branches/soc-2012-bratwurst/source/blender/assimp/ImageImporter.cpp
    branches/soc-2012-bratwurst/source/blender/assimp/ImageImporter.h
    branches/soc-2012-bratwurst/source/blender/assimp/MaterialImporter.cpp
    branches/soc-2012-bratwurst/source/blender/assimp/MaterialImporter.h

Modified: branches/soc-2012-bratwurst/source/blender/assimp/CMakeLists.txt
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/CMakeLists.txt	2012-05-31 10:54:27 UTC (rev 47269)
+++ branches/soc-2012-bratwurst/source/blender/assimp/CMakeLists.txt	2012-05-31 11:54:41 UTC (rev 47270)
@@ -54,6 +54,12 @@
 
 	MeshImporter.cpp
 	MeshImporter.h
+
+	MaterialImporter.cpp
+	MaterialImporter.h
+
+	ImageImporter.cpp
+	ImageImporter.h
 )
 
 if(WITH_BUILDINFO)

Added: branches/soc-2012-bratwurst/source/blender/assimp/ImageImporter.cpp
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/ImageImporter.cpp	                        (rev 0)
+++ branches/soc-2012-bratwurst/source/blender/assimp/ImageImporter.cpp	2012-05-31 11:54:41 UTC (rev 47270)
@@ -0,0 +1,182 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * 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 for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributor(s): Alexander Gessler
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/assimp/MeshImporter.cpp
+ *  \ingroup assimp
+ */
+
+#include <cassert>
+#include <sstream>
+
+#include "SceneImporter.h"
+#include "ImageImporter.h"
+#include "bassimp_internal.h"
+
+extern "C" {
+#	include "BKE_global.h"
+#	include "BKE_main.h"
+#	include "BKE_mesh.h"
+#	include "BKE_image.h"
+#	include "BKE_library.h"
+#	include "BKE_packedFile.h"
+#	include "DNA_packedFile_types.h"
+#	include "../imbuf/IMB_imbuf.h"
+#	include "../imbuf/IMB_imbuf_types.h"
+
+#	include "MEM_guardedalloc.h"
+
+#	include "BLI_listbase.h"
+#	include "BLI_math.h"
+#	include "BLI_string.h"
+}
+
+namespace bassimp {
+
+ImageImporter::ImageImporter(const SceneImporter& scene_imp,const aiString& path, const aiScene* in_scene)
+: path(path)
+, ima()
+, in_scene(in_scene)
+, scene_imp(scene_imp)
+{
+	assert(in_scene);
+}
+
+
+ImageImporter::~ImageImporter()
+{
+	if (ima) {
+		BKE_libblock_free(&G.main->image,ima);
+	}
+}
+
+
+void ImageImporter::error(const char* message)
+{
+	std::string m(message);
+	scene_imp.error((m + " (" + std::string(path.C_Str())+ ")").c_str());
+}
+
+
+void ImageImporter::verbose(const char* message)
+{
+	std::string m(message);
+	scene_imp.verbose((m + " (" + std::string(path.C_Str())+ ")").c_str());
+}
+
+
+Image* ImageImporter::extract_image()
+{
+	Image* m = ima;
+	ima = NULL;
+	return m;
+}
+
+
+bool ImageImporter::convert()
+{
+	if (!path.length) {
+		scene_imp.error("failed to convert image, no path given");
+		return false;
+	}
+
+	if (path.data[0] == '*') {
+		return convert_embedded();
+	}
+
+	return convert_file();
+}
+
+#define FILE_MAX 512
+
+bool ImageImporter::convert_file()
+{
+	const char *filename = path.C_Str();
+	char dir[FILE_MAX];
+	char full_path[FILE_MAX];
+
+	BLI_split_dir_part(filename, dir, sizeof(dir));
+	BLI_join_dirfile(full_path, sizeof(full_path), dir, scene_imp.get_file_path());
+	ima = BKE_image_load_exists(full_path);
+	if (!ima) {
+		error("failed to convert image, image load fails");
+		return false;
+	}
+
+	return true;
+}
+
+
+bool ImageImporter::convert_embedded()
+{
+	const unsigned int index = static_cast<unsigned int>(atoi(path.data+1));
+	if (index >= in_scene->mNumTextures)	{
+		error("failed to convert image, embedded texture index is out of range");
+	}
+
+	const aiTexture* const tex = in_scene->mTextures[index];
+	
+	// raw image data
+	if (tex->mHeight) {
+		if (!tex->mWidth) {
+			error("failed to convert image, embedded texture has empty width");
+		}
+
+		ImBuf* const buff = IMB_allocImBuf(tex->mWidth,tex->mWidth,1,IB_rect);
+		unsigned char* rect = reinterpret_cast<unsigned char*>(buff->rect);
+
+		aiTexel* t = tex->pcData;
+		for (unsigned int y  = 0; y < tex->mHeight; ++y) {
+			for (unsigned int x  = 0; x < tex->mWidth; ++x, rect += 4, ++t) {
+				rect[0] = t->r;
+				rect[1] = t->g;
+				rect[2] = t->b;
+				rect[3] = t->a;
+			}
+		}
+	}
+	// compressed (i.e. png) image data
+	else {
+
+		std::stringstream name;
+		name << "assimp-embedded-" << index;
+		ima = BKE_image_load_exists(name.str().c_str());
+
+		// copy the texture data to a PackedFile and assign it to the image, this will make Blender load it therefrom
+		PackedFile *pf = static_cast<PackedFile*>( MEM_callocN(sizeof(*pf), "PackedFile") );
+
+		void* buff = MEM_callocN(tex->mWidth,"EmbeddedTex");
+		memcpy(buff,tex->pcData,tex->mWidth);
+
+		pf->data = buff;
+		pf->size = tex->mWidth;
+		ima->packedfile = pf;
+		
+		ima->source = IMA_SRC_FILE;
+		ima->type = IMA_TYPE_IMAGE;
+
+		ima->packedfile = pf;
+	}
+
+	return true;
+}
+
+}

Added: branches/soc-2012-bratwurst/source/blender/assimp/ImageImporter.h
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/ImageImporter.h	                        (rev 0)
+++ branches/soc-2012-bratwurst/source/blender/assimp/ImageImporter.h	2012-05-31 11:54:41 UTC (rev 47270)
@@ -0,0 +1,70 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * 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 for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributor(s): Alexander Gessler
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file MaterialImporter.h
+ *  \ingroup assimp
+ */
+
+#ifndef INCLUDED_IMAGEIMPORTER_H
+#define INCLUDED_IMAGEIMPORTER_H
+
+#include "bassimp_shared.h"
+
+namespace bassimp {
+
+	class SceneImporter;
+
+class ImageImporter 
+{
+private:
+
+	aiString path;
+	Image* ima;
+	const aiScene* in_scene;
+
+	// injected SceneImporter
+	const SceneImporter& scene_imp;
+
+private:
+
+	bool convert_file();
+	bool convert_embedded();
+
+	void error(const char* message);
+	void verbose(const char* message);
+	
+public:
+
+	ImageImporter( const SceneImporter& scene_imp, const aiString& path, const aiScene* in_scene);
+	~ImageImporter ();
+
+	// run conversion
+	bool convert();
+
+	// get converted material, this resets the cached conversion
+	// result and transfers ownership of it to the caller.
+	Image* extract_image();
+};
+
+}
+
+#endif

Added: branches/soc-2012-bratwurst/source/blender/assimp/MaterialImporter.cpp
===================================================================
--- branches/soc-2012-bratwurst/source/blender/assimp/MaterialImporter.cpp	                        (rev 0)
+++ branches/soc-2012-bratwurst/source/blender/assimp/MaterialImporter.cpp	2012-05-31 11:54:41 UTC (rev 47270)
@@ -0,0 +1,301 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * 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 for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributor(s): Alexander Gessler
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/assimp/MeshImporter.cpp
+ *  \ingroup assimp
+ */
+
+#include <cassert>
+#include <sstream>
+
+#include "SceneImporter.h"
+#include "MaterialImporter.h"
+#include "ImageImporter.h"
+#include "bassimp_internal.h"
+
+extern "C" {
+#	include "BKE_global.h"
+#	include "BKE_main.h"
+#	include "BKE_mesh.h"
+#	include "BKE_displist.h"
+#	include "BKE_library.h"
+#	include "BKE_material.h"
+#	include "BKE_texture.h"
+
+#	include "MEM_guardedalloc.h"
+
+#	include "BLI_listbase.h"
+#	include "BLI_math.h"
+#	include "BLI_string.h"
+}
+
+namespace bassimp {
+
+MaterialImporter::MaterialImporter(const SceneImporter& scene_imp,  const aiMaterial* in_mat, const aiScene* in_scene, Scene *out_scene, unsigned int in_mat_index)
+: in_mat(in_mat)
+, out_scene(out_scene)
+, mat()
+, in_mat_index(in_mat_index)
+, in_scene(in_scene)
+, next_texture()
+, scene_imp(scene_imp)
+{
+	assert(in_mat);
+	assert(out_scene);
+	assert(in_scene);
+	assert(in_scene);
+}
+
+
+MaterialImporter::~MaterialImporter()
+{
+	if (mat) {
+		BKE_libblock_free(&G.main->mat,mat);
+	}
+}
+
+
+void MaterialImporter::error(const char* message)
+{
+	scene_imp.error((message + (" (mat: " + name + ")")).c_str());
+}
+
+
+void MaterialImporter::verbose(const char* message)
+{
+	scene_imp.verbose((message + (" (mat: " + name + ")")).c_str());
+}
+
+
+
+Material* MaterialImporter::extract_material()
+{
+	Material* m = mat;
+	mat = NULL;
+	return m;
+}
+
+
+void MaterialImporter::convert()
+{
+	aiString s;
+	if(aiGetMaterialString(in_mat,AI_MATKEY_NAME,&s) == AI_SUCCESS) {
+		mat = BKE_material_add(s.C_Str());
+	}
+	else {
+		std::stringstream ss;
+		ss << "assimpmat-" << in_mat_index;
+		mat = BKE_material_add(ss.str().c_str());
+	}
+
+	aiColor4D c;
+	if (aiGetMaterialColor(in_mat,AI_MATKEY_COLOR_DIFFUSE,&c) == AI_SUCCESS) {
+		mat->r = c.r;
+		mat->g = c.g;
+		mat->b = c.b;
+	}
+
+	if (aiGetMaterialColor(in_mat,AI_MATKEY_COLOR_SPECULAR,&c) == AI_SUCCESS) {
+		mat->specr = c.r;
+		mat->specg = c.g;

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list