Browse Source

Add the skeleton for the image importer

Panagiotis Christopoulos Charitos 4 years ago
parent
commit
5b3aefd66e

+ 1 - 0
AnKi/Importer.h

@@ -6,5 +6,6 @@
 #pragma once
 
 #include <AnKi/Importer/GltfImporter.h>
+#include <AnKi/Importer/ImageImporter.h>
 
 /// @defgroup importer Importers

+ 21 - 0
AnKi/Importer/Common.h

@@ -0,0 +1,21 @@
+// Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#include <AnKi/Util/Logger.h>
+
+namespace anki
+{
+
+/// @addtogroup importer
+/// @{
+
+#define ANKI_IMPORTER_LOGI(...) ANKI_LOG("IMPR", NORMAL, __VA_ARGS__)
+#define ANKI_IMPORTER_LOGE(...) ANKI_LOG("IMPR", ERROR, __VA_ARGS__)
+#define ANKI_IMPORTER_LOGW(...) ANKI_LOG("IMPR", WARNING, __VA_ARGS__)
+#define ANKI_IMPORTER_LOGF(...) ANKI_LOG("IMPR", FATAL, __VA_ARGS__)
+
+/// @}
+
+} // end namespace anki

+ 28 - 28
AnKi/Importer/GltfImporter.cpp

@@ -41,7 +41,7 @@ static ANKI_USE_RESULT Error getUniformScale(const Mat4& m, F32& out)
 	const F32 scale = xAxis.getLength();
 	if(absolute(scale - yAxis.getLength()) > SCALE_THRESHOLD || absolute(scale - zAxis.getLength()) > SCALE_THRESHOLD)
 	{
-		ANKI_GLTF_LOGE("No uniform scale in the matrix");
+		ANKI_IMPORTER_LOGE("No uniform scale in the matrix");
 		return Error::USER_DATA;
 	}
 
@@ -125,7 +125,7 @@ static ANKI_USE_RESULT Error getNodeTransform(const cgltf_node& node, Transform&
 	const F32 scaleEpsilon = 0.01f;
 	if(absolute(scale[0] - scale[1]) > scaleEpsilon || absolute(scale[0] - scale[2]) > scaleEpsilon)
 	{
-		ANKI_GLTF_LOGE("Expecting uniform scale");
+		ANKI_IMPORTER_LOGE("Expecting uniform scale");
 		return Error::USER_DATA;
 	}
 
@@ -169,7 +169,7 @@ Error GltfImporter::init(const GltfImporterInitInfo& initInfo)
 	m_lodFactor = clamp(initInfo.m_lodFactor, 0.0f, 1.0f);
 	if(m_lodFactor * F32(m_lodCount - 1) > 0.7f)
 	{
-		ANKI_GLTF_LOGE("LOD factor is too high %f", m_lodFactor);
+		ANKI_IMPORTER_LOGE("LOD factor is too high %f", m_lodFactor);
 		return Error::USER_DATA;
 	}
 
@@ -179,20 +179,20 @@ Error GltfImporter::init(const GltfImporterInitInfo& initInfo)
 		m_lodFactor = 0.0f;
 	}
 
-	ANKI_GLTF_LOGI("Having %u LODs with LOD factor %f", m_lodCount, m_lodFactor);
+	ANKI_IMPORTER_LOGI("Having %u LODs with LOD factor %f", m_lodCount, m_lodFactor);
 
 	cgltf_options options = {};
 	cgltf_result res = cgltf_parse_file(&options, m_inputFname.cstr(), &m_gltf);
 	if(res != cgltf_result_success)
 	{
-		ANKI_GLTF_LOGE("Failed to open the GLTF file. Code: %d", res);
+		ANKI_IMPORTER_LOGE("Failed to open the GLTF file. Code: %d", res);
 		return Error::FUNCTION_FAILED;
 	}
 
 	res = cgltf_load_buffers(&options, m_gltf, m_inputFname.cstr());
 	if(res != cgltf_result_success)
 	{
-		ANKI_GLTF_LOGE("Failed to load GLTF data. Code: %d", res);
+		ANKI_IMPORTER_LOGE("Failed to load GLTF data. Code: %d", res);
 		return Error::FUNCTION_FAILED;
 	}
 
@@ -238,14 +238,14 @@ Error GltfImporter::writeAll()
 	// Check error
 	if(err)
 	{
-		ANKI_GLTF_LOGE("Error happened in main thread");
+		ANKI_IMPORTER_LOGE("Error happened in main thread");
 		return err;
 	}
 
 	const Error threadErr = m_errorInThread.load();
 	if(threadErr)
 	{
-		ANKI_GLTF_LOGE("Error happened in a thread");
+		ANKI_IMPORTER_LOGE("Error happened in a thread");
 		return threadErr;
 	}
 
@@ -266,7 +266,7 @@ Error GltfImporter::getExtras(const cgltf_extras& extras, HashMapAuto<CString, S
 	cgltf_result res = cgltf_copy_extras_json(m_gltf, &extras, &json[0], &extrasSize);
 	if(res != cgltf_result_success)
 	{
-		ANKI_GLTF_LOGE("cgltf_copy_extras_json failed: %d", res);
+		ANKI_IMPORTER_LOGE("cgltf_copy_extras_json failed: %d", res);
 		return Error::FUNCTION_FAILED;
 	}
 
@@ -304,7 +304,7 @@ Error GltfImporter::getExtras(const cgltf_extras& extras, HashMapAuto<CString, S
 
 	if((tokenStrings.getSize() % 2) != 0)
 	{
-		ANKI_GLTF_LOGE("Unable to parse: %s", jsonTxt.cstr());
+		ANKI_IMPORTER_LOGE("Unable to parse: %s", jsonTxt.cstr());
 		return Error::FUNCTION_FAILED;
 	}
 
@@ -383,13 +383,13 @@ Error GltfImporter::parseArrayOfNumbers(CString str, DynamicArrayAuto<F64>& out,
 
 	if(err)
 	{
-		ANKI_GLTF_LOGE("Failed to parse floats: %s", str.cstr());
+		ANKI_IMPORTER_LOGE("Failed to parse floats: %s", str.cstr());
 	}
 
 	if(expectedArraySize && *expectedArraySize != out.getSize())
 	{
-		ANKI_GLTF_LOGE("Failed to parse floats. Expecting %u floats got %u: %s", *expectedArraySize, out.getSize(),
-					   str.cstr());
+		ANKI_IMPORTER_LOGE("Failed to parse floats. Expecting %u floats got %u: %s", *expectedArraySize, out.getSize(),
+						   str.cstr());
 	}
 
 	return Error::NONE;
@@ -402,7 +402,7 @@ Error GltfImporter::visitNode(const cgltf_node& node, const Transform& parentTrf
 	const Error threadErr = m_errorInThread.load();
 	if(threadErr)
 	{
-		ANKI_GLTF_LOGE("Error happened in a thread");
+		ANKI_IMPORTER_LOGE("Error happened in a thread");
 		return threadErr;
 	}
 
@@ -709,7 +709,7 @@ Error GltfImporter::visitNode(const cgltf_node& node, const Transform& parentTrf
 	}
 	else
 	{
-		ANKI_GLTF_LOGW("Ignoring node %s. Assuming transform node", getNodeName(node).cstr());
+		ANKI_IMPORTER_LOGW("Ignoring node %s. Assuming transform node", getNodeName(node).cstr());
 		ANKI_CHECK(getExtras(node.extras, outExtras));
 	}
 
@@ -754,7 +754,7 @@ Error GltfImporter::writeTransform(const Transform& trf)
 Error GltfImporter::writeModel(const cgltf_mesh& mesh)
 {
 	const StringAuto modelFname = computeModelResourceFilename(mesh);
-	ANKI_GLTF_LOGI("Importing model %s", modelFname.cstr());
+	ANKI_IMPORTER_LOGI("Importing model %s", modelFname.cstr());
 
 	HashMapAuto<CString, StringAuto> extras(m_alloc);
 	ANKI_CHECK(getExtras(mesh.extras, extras));
@@ -896,7 +896,7 @@ Error GltfImporter::writeAnimation(const cgltf_animation& anim)
 	StringAuto fname(m_alloc);
 	fname.sprintf("%s%s.ankianim", m_outDir.cstr(), anim.name);
 	fname = fixFilename(fname);
-	ANKI_GLTF_LOGI("Importing animation %s", fname.cstr());
+	ANKI_IMPORTER_LOGI("Importing animation %s", fname.cstr());
 
 	// Gather the channels
 	HashMapAuto<CString, Array<const cgltf_animation_channel*, 3>> channelMap(m_alloc);
@@ -958,7 +958,7 @@ Error GltfImporter::writeAnimation(const cgltf_animation& anim)
 			readAccessor(*channel.sampler->output, positions);
 			if(keys.getSize() != positions.getSize())
 			{
-				ANKI_GLTF_LOGE("Position count should match they keyframes");
+				ANKI_IMPORTER_LOGE("Position count should match they keyframes");
 				return Error::USER_DATA;
 			}
 
@@ -982,7 +982,7 @@ Error GltfImporter::writeAnimation(const cgltf_animation& anim)
 			readAccessor(*channel.sampler->output, rotations);
 			if(keys.getSize() != rotations.getSize())
 			{
-				ANKI_GLTF_LOGE("Rotation count should match they keyframes");
+				ANKI_IMPORTER_LOGE("Rotation count should match they keyframes");
 				return Error::USER_DATA;
 			}
 
@@ -1006,7 +1006,7 @@ Error GltfImporter::writeAnimation(const cgltf_animation& anim)
 			readAccessor(*channel.sampler->output, scales);
 			if(keys.getSize() != scales.getSize())
 			{
-				ANKI_GLTF_LOGE("Scale count should match they keyframes");
+				ANKI_IMPORTER_LOGE("Scale count should match they keyframes");
 				return Error::USER_DATA;
 			}
 
@@ -1016,7 +1016,7 @@ Error GltfImporter::writeAnimation(const cgltf_animation& anim)
 				if(absolute(scales[i][0] - scales[i][1]) > scaleEpsilon
 				   || absolute(scales[i][0] - scales[i][2]) > scaleEpsilon)
 				{
-					ANKI_GLTF_LOGE("Expecting uniform scale");
+					ANKI_IMPORTER_LOGE("Expecting uniform scale");
 					return Error::USER_DATA;
 				}
 
@@ -1111,14 +1111,14 @@ Error GltfImporter::writeSkeleton(const cgltf_skin& skin)
 {
 	StringAuto fname(m_alloc);
 	fname.sprintf("%s%s.ankiskel", m_outDir.cstr(), skin.name);
-	ANKI_GLTF_LOGI("Importing skeleton %s", fname.cstr());
+	ANKI_IMPORTER_LOGI("Importing skeleton %s", fname.cstr());
 
 	// Get matrices
 	DynamicArrayAuto<Mat4> boneMats(m_alloc);
 	readAccessor(*skin.inverse_bind_matrices, boneMats);
 	if(boneMats.getSize() != skin.joints_count)
 	{
-		ANKI_GLTF_LOGE("Bone matrices should match the joint count");
+		ANKI_IMPORTER_LOGE("Bone matrices should match the joint count");
 		return Error::USER_DATA;
 	}
 
@@ -1176,7 +1176,7 @@ Error GltfImporter::writeLight(const cgltf_node& node, const HashMapAuto<CString
 {
 	const cgltf_light& light = *node.light;
 	StringAuto nodeName = getNodeName(node);
-	ANKI_GLTF_LOGI("Importing light %s", nodeName.cstr());
+	ANKI_IMPORTER_LOGI("Importing light %s", nodeName.cstr());
 
 	HashMapAuto<CString, StringAuto> extras(parentExtras);
 	ANKI_CHECK(getExtras(light.extras, extras));
@@ -1194,7 +1194,7 @@ Error GltfImporter::writeLight(const cgltf_node& node, const HashMapAuto<CString
 		lightTypeStr = "Directional";
 		break;
 	default:
-		ANKI_GLTF_LOGE("Unsupporter light type %d", light.type);
+		ANKI_IMPORTER_LOGE("Unsupporter light type %d", light.type);
 		return Error::USER_DATA;
 	}
 
@@ -1314,12 +1314,12 @@ Error GltfImporter::writeCamera(const cgltf_node& node, const HashMapAuto<CStrin
 {
 	if(node.camera->type != cgltf_camera_type_perspective)
 	{
-		ANKI_GLTF_LOGW("Unsupported camera type: %s", getNodeName(node).cstr());
+		ANKI_IMPORTER_LOGW("Unsupported camera type: %s", getNodeName(node).cstr());
 		return Error::NONE;
 	}
 
 	const cgltf_camera_perspective& cam = node.camera->perspective;
-	ANKI_GLTF_LOGI("Importing camera %s", getNodeName(node).cstr());
+	ANKI_IMPORTER_LOGI("Importing camera %s", getNodeName(node).cstr());
 
 	ANKI_CHECK(m_sceneFile.writeText("\nnode = scene:newPerspectiveCameraNode(\"%s\")\n", getNodeName(node).cstr()));
 	ANKI_CHECK(m_sceneFile.writeText("scene:setActiveCameraNode(node:getSceneNodeBase())\n"));
@@ -1335,7 +1335,7 @@ Error GltfImporter::writeCamera(const cgltf_node& node, const HashMapAuto<CStrin
 
 Error GltfImporter::writeModelNode(const cgltf_node& node, const HashMapAuto<CString, StringAuto>& parentExtras)
 {
-	ANKI_GLTF_LOGI("Importing model node %s", getNodeName(node).cstr());
+	ANKI_IMPORTER_LOGI("Importing model node %s", getNodeName(node).cstr());
 
 	HashMapAuto<CString, StringAuto> extras(parentExtras);
 	ANKI_CHECK(getExtras(node.extras, extras));

+ 2 - 5
AnKi/Importer/GltfImporter.h

@@ -5,6 +5,7 @@
 
 #pragma once
 
+#include <AnKi/Importer/Common.h>
 #include <AnKi/Util/String.h>
 #include <AnKi/Util/StringList.h>
 #include <AnKi/Util/File.h>
@@ -19,11 +20,7 @@ namespace anki
 /// @addtogroup importer
 /// @{
 
-#define ANKI_GLTF_LOGI(...) ANKI_LOG("GLTF", NORMAL, __VA_ARGS__)
-#define ANKI_GLTF_LOGE(...) ANKI_LOG("GLTF", ERROR, __VA_ARGS__)
-#define ANKI_GLTF_LOGW(...) ANKI_LOG("GLTF", WARNING, __VA_ARGS__)
-#define ANKI_GLTF_LOGF(...) ANKI_LOG("GLTF", FATAL, __VA_ARGS__)
-
+/// @memberof GltfImporter
 class GltfImporterInitInfo
 {
 public:

+ 3 - 3
AnKi/Importer/GltfImporterMaterial.cpp

@@ -101,11 +101,11 @@ Error GltfImporter::writeMaterial(const cgltf_material& mtl, RayTypeBit usedRayT
 {
 	StringAuto fname(m_alloc);
 	fname.sprintf("%s%s.ankimtl", m_outDir.cstr(), mtl.name);
-	ANKI_GLTF_LOGI("Importing material %s", fname.cstr());
+	ANKI_IMPORTER_LOGI("Importing material %s", fname.cstr());
 
 	if(!mtl.has_pbr_metallic_roughness)
 	{
-		ANKI_GLTF_LOGE("Expecting PBR metallic roughness");
+		ANKI_IMPORTER_LOGE("Expecting PBR metallic roughness");
 		return Error::USER_DATA;
 	}
 
@@ -152,7 +152,7 @@ Error GltfImporter::writeMaterial(const cgltf_material& mtl, RayTypeBit usedRayT
 			tokens.splitString(it->toCString(), ' ');
 			if(tokens.getSize() != 3)
 			{
-				ANKI_GLTF_LOGE("Wrong specular: %s", it->cstr());
+				ANKI_IMPORTER_LOGE("Wrong specular: %s", it->cstr());
 				return Error::USER_DATA;
 			}
 

+ 12 - 12
AnKi/Importer/GltfImporterMesh.cpp

@@ -70,13 +70,13 @@ static Error checkAttribute(const cgltf_attribute& attrib)
 {
 	if(cgltfComponentCount(attrib.data->type) != T::COMPONENT_COUNT)
 	{
-		ANKI_GLTF_LOGE("Wrong component count for attribute: %s", attrib.name);
+		ANKI_IMPORTER_LOGE("Wrong component count for attribute: %s", attrib.name);
 		return Error::USER_DATA;
 	}
 
 	if(cgltfComponentSize(attrib.data->component_type) != sizeof(typename T::Scalar))
 	{
-		ANKI_GLTF_LOGE("Incompatible type: %s", attrib.name);
+		ANKI_IMPORTER_LOGE("Incompatible type: %s", attrib.name);
 		return Error::USER_DATA;
 	}
 
@@ -84,7 +84,7 @@ static Error checkAttribute(const cgltf_attribute& attrib)
 	const U count = attrib.data->count;
 	if(count == 0)
 	{
-		ANKI_GLTF_LOGE("Zero vertex count");
+		ANKI_IMPORTER_LOGE("Zero vertex count");
 		return Error::USER_DATA;
 	}
 
@@ -276,8 +276,8 @@ Error GltfImporter::writeMesh(const cgltf_mesh& mesh, CString nameOverride, F32
 {
 	StringAuto fname(m_alloc);
 	fname.sprintf("%s%s.ankimesh", m_outDir.cstr(), (nameOverride.isEmpty()) ? mesh.name : nameOverride.cstr());
-	ANKI_GLTF_LOGI("Importing mesh (%s, decimate factor %f): %s", (m_optimizeMeshes) ? "optimze" : "WON'T optimize",
-				   decimateFactor, fname.cstr());
+	ANKI_IMPORTER_LOGI("Importing mesh (%s, decimate factor %f): %s", (m_optimizeMeshes) ? "optimze" : "WON'T optimize",
+					   decimateFactor, fname.cstr());
 
 	ListAuto<SubMesh> submeshes(m_alloc);
 	U32 totalIndexCount = 0;
@@ -294,7 +294,7 @@ Error GltfImporter::writeMesh(const cgltf_mesh& mesh, CString nameOverride, F32
 	{
 		if(primitive->type != cgltf_primitive_type_triangles)
 		{
-			ANKI_GLTF_LOGE("Expecting triangles got %d", primitive->type);
+			ANKI_IMPORTER_LOGE("Expecting triangles got %d", primitive->type);
 			return Error::USER_DATA;
 		}
 
@@ -311,7 +311,7 @@ Error GltfImporter::writeMesh(const cgltf_mesh& mesh, CString nameOverride, F32
 
 		if(maxVertCount == 0 || minVertCount != maxVertCount)
 		{
-			ANKI_GLTF_LOGE("Wrong number of vertices");
+			ANKI_IMPORTER_LOGE("Wrong number of vertices");
 			return Error::USER_DATA;
 		}
 
@@ -368,7 +368,7 @@ Error GltfImporter::writeMesh(const cgltf_mesh& mesh, CString nameOverride, F32
 			}
 			else
 			{
-				ANKI_GLTF_LOGW("Ignoring attribute: %s", attrib->name);
+				ANKI_IMPORTER_LOGW("Ignoring attribute: %s", attrib->name);
 			}
 		}
 
@@ -418,7 +418,7 @@ Error GltfImporter::writeMesh(const cgltf_mesh& mesh, CString nameOverride, F32
 			ANKI_ASSERT(primitive->indices);
 			if(primitive->indices->count == 0 || (primitive->indices->count % 3) != 0)
 			{
-				ANKI_GLTF_LOGE("Incorect index count: %lu", primitive->indices->count);
+				ANKI_IMPORTER_LOGE("Incorect index count: %lu", primitive->indices->count);
 				return Error::USER_DATA;
 			}
 			submesh.m_indices.create(U32(primitive->indices->count));
@@ -568,7 +568,7 @@ Error GltfImporter::writeMesh(const cgltf_mesh& mesh, CString nameOverride, F32
 
 	if(submeshes.getSize() == 0)
 	{
-		ANKI_GLTF_LOGE("Mesh contains degenerate geometry");
+		ANKI_IMPORTER_LOGE("Mesh contains degenerate geometry");
 		return Error::USER_DATA;
 	}
 
@@ -732,7 +732,7 @@ Error GltfImporter::writeMesh(const cgltf_mesh& mesh, CString nameOverride, F32
 			const U32 idx = submesh.m_indices[i] + vertCount;
 			if(idx > MAX_U16)
 			{
-				ANKI_GLTF_LOGE("Only supports 16bit indices for now");
+				ANKI_IMPORTER_LOGE("Only supports 16bit indices for now");
 				return Error::USER_DATA;
 			}
 
@@ -797,7 +797,7 @@ Error GltfImporter::writeMesh(const cgltf_mesh& mesh, CString nameOverride, F32
 				{
 					if(submesh.m_verts[i].m_boneIds[c] > 0XFF)
 					{
-						ANKI_GLTF_LOGE("Only 256 bones are supported");
+						ANKI_IMPORTER_LOGE("Only 256 bones are supported");
 						return Error::USER_DATA;
 					}
 

+ 6 - 0
AnKi/Importer/ImageImporter.cpp

@@ -0,0 +1,6 @@
+// Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#include <AnKi/Importer/ImageImporter.h>

+ 25 - 0
AnKi/Importer/ImageImporter.h

@@ -0,0 +1,25 @@
+// Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
+// All rights reserved.
+// Code licensed under the BSD License.
+// http://www.anki3d.org/LICENSE
+
+#include <AnKi/Importer/Common.h>
+#include <AnKi/Util/Allocator.h>
+
+namespace anki
+{
+
+/// @addtogroup importer
+/// @{
+
+class ImageImporterInfo
+{
+public:
+	GenericMemoryPoolAllocator<U8> m_allocator;
+};
+
+/// Converts images to AnKi's specific format.
+ANKI_USE_RESULT Error importImage(const ImageImporterInfo& info);
+/// @}
+
+} // end namespace anki

+ 1 - 1
Tools/GltfImporter/Main.cpp

@@ -184,7 +184,7 @@ int main(int argc, char** argv)
 	CmdLineArgs cmdArgs;
 	if(parseCommandLineArgs(argc, argv, cmdArgs))
 	{
-		ANKI_GLTF_LOGE(USAGE, argv[0]);
+		ANKI_IMPORTER_LOGE(USAGE, argv[0]);
 		return 1;
 	}