2
0
Panagiotis Christopoulos Charitos 12 жил өмнө
parent
commit
3eed0588a0

+ 7 - 2
include/anki/misc/Xml.h

@@ -7,6 +7,7 @@
 #	error "Wrong tinyxml2 included"
 #endif
 #include <string>
+#include "anki/Math.h"
 
 namespace anki {
 
@@ -16,6 +17,7 @@ struct XmlElement
 	friend class XmlDocument;
 public:
 	XmlElement()
+		: el(nullptr)
 	{}
 	XmlElement(const XmlElement& b)
 		: el(b.el)
@@ -42,7 +44,10 @@ public:
 	}
 
 	/// Return the text inside as a string
-	int getInt() const;
+	I getInt() const;
+
+	/// Return a Mat4
+	Mat4 getMat4() const;
 
 	/// Get a child element quietly
 	XmlElement getChildElementOptional(const char* name) const;
@@ -55,7 +60,7 @@ public:
 	XmlElement getNextSiblingElement(const char* name) const;
 
 private:
-	tinyxml2::XMLElement* el = nullptr;
+	tinyxml2::XMLElement* el;
 
 	void check() const
 	{

+ 36 - 1
src/misc/Xml.cpp

@@ -1,9 +1,10 @@
 #include "anki/misc/Xml.h"
+#include "anki/util/StringList.h"
 
 namespace anki {
 
 //==============================================================================
-int XmlElement::getInt() const
+I XmlElement::getInt() const
 {
 	check();
 	const char* txt = getText();
@@ -15,6 +16,40 @@ int XmlElement::getInt() const
 	return std::stoi(txt);
 }
 
+//==============================================================================
+Mat4 XmlElement::getMat4() const
+{
+	check();
+
+	const char* txt = getText();
+	if(txt == nullptr)
+	{
+		throw ANKI_EXCEPTION("Failed to return Mat4");
+	}
+
+	StringList list = StringList::splitString(txt, ' ');
+	if(list.size() != 16)
+	{
+		throw ANKI_EXCEPTION("Expecting 16 elements for Mat4");
+	}
+
+	Mat4 out;
+
+	try
+	{
+		for(U i = 0; i < 16; i++)
+		{
+			out[i] = std::stof(list[i]);
+		}
+	}
+	catch(const std::exception& e)
+	{
+		throw ANKI_EXCEPTION("Found non-float element for Mat4");
+	}
+
+	return out;
+}
+
 //==============================================================================
 XmlElement XmlElement::getChildElementOptional(const char* name) const
 {

+ 11 - 2
src/scene/SceneGraph.cpp

@@ -238,13 +238,22 @@ void SceneGraph::load(const char* filename)
 
 		do
 		{
-			XmlElement el;
+			XmlElement el, el1;
 	
 			// <model>
 			el = mdlNodeEl.getChildElement("model");
 
+			// <instancesCount>
+			el1 = mdlNodeEl.getChildElementOptional("instancesCount");
+			U32 instancesCount = (el1) ? el1.getInt() : 1;
+
+			if(instancesCount > ANKI_MAX_INSTANCES)
+			{
+				throw ANKI_EXCEPTION("Too many instances");
+			}
+
 			ModelNode* node = new ModelNode("name", this, nullptr,
-				Movable::MF_NONE, el.getText());
+				Movable::MF_NONE, el.getText(), instancesCount);
 
 			// <transform>
 			el = mdlNodeEl.getChildElement("transform");

+ 8 - 5
tools/2anki/Main.cpp

@@ -838,16 +838,20 @@ static void exportScene(const aiScene& scene, Config& config)
 				<< "</model>\n";
 		}
 
+		// Node name
+		std::string nodeName = getMaterialName(aimtl) + "_instanced_" 
+			+ std::to_string(i);
+
 		// Write the scene file
 		file << "\t<modelNode>\n"
+			<< "\t\t<name>" << nodeName << "</name>\n"
 			<< "\t\t<model>" << config.outDir << modelName << ".mdl</model>\n"
 			<< "\t\t<instancesCount>" 
-			<< mesh.transforms.size() << "</instancesCount>\n"
-			<< "\t\t<transforms>\n";
+			<< mesh.transforms.size() << "</instancesCount>\n";
 
 		for(uint32_t j = 0; j < mesh.transforms.size(); j++)
 		{
-			file << "\t\t\t<transform>";
+			file << "\t\t<transform>";
 
 			aiMatrix4x4 trf = mesh.transforms[j];
 			for(uint32_t a = 0; a < 4; a++)
@@ -861,8 +865,7 @@ static void exportScene(const aiScene& scene, Config& config)
 			file << "</transform>\n";
 		}
 
-		file << "\t\t</transforms>\n"
-			"\t</modelNode>\n";
+		file << "\t</modelNode>\n";
 	}