Browse Source

- Converting Model file format to XML

Panagiotis Christopoulos Charitos 15 years ago
parent
commit
271efadff7
4 changed files with 80 additions and 259 deletions
  1. 15 14
      build/debug/Makefile
  2. 46 121
      src/Resources/Model.cpp
  3. 19 21
      src/Resources/Model.h
  4. 0 103
      src/Util/BldfTree.h

+ 15 - 14
build/debug/Makefile

@@ -1772,7 +1772,8 @@ Renderer.o: ../../src/Renderer/Renderer.cpp ../../src/Renderer/Renderer.h \
  ../../extern/include/bullet/BulletDynamics/Dynamics/btRigidBody.h \
  ../../src/Physics/BtAndAnkiConvertors.h ../../src/Physics/DebugDrawer.h \
  ../../extern/include/bullet/LinearMath/btIDebugDraw.h \
- ../../src/Scene/ModelNode.h
+ ../../src/Scene/ModelNode.h ../../src/Resources/Model.h \
+ ../../src/Resources/Mesh.h
 	@echo Compiling ../../src/Renderer/Renderer.cpp...
 	@$(CXX) $(INCPATH) $(CFLAGS) ../../src/Renderer/Renderer.cpp -o Renderer.o
 
@@ -2746,8 +2747,6 @@ Model.o: ../../src/Resources/Model.cpp ../../src/Resources/Model.h \
  ../../src/Util/StdTypes.h ../../src/Util/Properties.h \
  ../../src/Util/StdTypes.h ../../src/Resources/Core/RsrcPtr.h \
  ../../src/Util/Exception.h ../../src/Core/Object.h ../../src/Util/Vec.h \
- ../../src/Misc/Parser.h ../../src/Util/Scanner.h \
- ../../src/Misc/Parser.inl.h ../../src/Misc/Parser.h \
  ../../src/Resources/Material.h ../../src/Math/Math.h \
  ../../src/Math/Vec2.h ../../src/Math/MathCommon.h \
  ../../src/Math/Vec2.inl.h ../../src/Math/MathCommon.inl.h \
@@ -2886,7 +2885,8 @@ RsrcPtr.o: ../../src/Resources/Core/RsrcPtr.cpp \
  ../../src/Resources/ShaderProg.h ../../src/Resources/Mesh.h \
  ../../src/Core/Object.h ../../src/Resources/Skeleton.h \
  ../../src/Resources/SkelAnim.h ../../src/Resources/LightData.h \
- ../../src/Resources/ParticleEmitterProps.h ../../src/Resources/Script.h
+ ../../src/Resources/ParticleEmitterProps.h ../../src/Resources/Script.h \
+ ../../src/Resources/Model.h
 	@echo Compiling ../../src/Resources/Core/RsrcPtr.cpp...
 	@$(CXX) $(INCPATH) $(CFLAGS) ../../src/Resources/Core/RsrcPtr.cpp -o RsrcPtr.o
 
@@ -2950,7 +2950,8 @@ RsrcContainers.o: ../../src/Resources/Core/RsrcContainers.cpp \
  ../../src/Resources/ShaderProg.h ../../src/Resources/Mesh.h \
  ../../src/Core/Object.h ../../src/Resources/Skeleton.h \
  ../../src/Resources/SkelAnim.h ../../src/Resources/LightData.h \
- ../../src/Resources/ParticleEmitterProps.h ../../src/Resources/Script.h
+ ../../src/Resources/ParticleEmitterProps.h ../../src/Resources/Script.h \
+ ../../src/Resources/Model.h
 	@echo Compiling ../../src/Resources/Core/RsrcContainers.cpp...
 	@$(CXX) $(INCPATH) $(CFLAGS) ../../src/Resources/Core/RsrcContainers.cpp -o RsrcContainers.o
 
@@ -3669,16 +3670,15 @@ ParticleEmitter.o: ../../src/Scene/ParticleEmitter.cpp \
  ../../src/Scene/GhostNode.h ../../src/Resources/ParticleEmitterProps.h \
  ../../src/Resources/Core/Resource.h ../../src/Util/Util.h \
  ../../src/Util/Vec.h ../../src/Util/StdTypes.h \
- ../../src/Physics/RigidBody.h ../../src/Renderer/MainRenderer.h \
- ../../src/Renderer/Renderer.h ../../src/Renderer/BufferObjects/Fbo.h \
- ../../extern/include/GL/glew.h ../../src/Resources/Texture.h \
- ../../src/Resources/ShaderProg.h ../../src/Util/CharPtrHashMap.h \
- ../../src/Renderer/BufferObjects/Vbo.h \
+ ../../src/Resources/Core/RsrcPtr.h ../../src/Physics/RigidBody.h \
+ ../../src/Renderer/MainRenderer.h ../../src/Renderer/Renderer.h \
+ ../../src/Renderer/BufferObjects/Fbo.h ../../extern/include/GL/glew.h \
+ ../../src/Resources/Texture.h ../../src/Resources/ShaderProg.h \
+ ../../src/Util/CharPtrHashMap.h ../../src/Renderer/BufferObjects/Vbo.h \
  ../../src/Renderer/BufferObjects/BufferObject.h \
  ../../src/Renderer/BufferObjects/Vao.h ../../src/Misc/GlException.h \
- ../../src/Resources/Core/RsrcPtr.h ../../src/Renderer/Ms.h \
- ../../src/Renderer/RenderingPass.h ../../src/Renderer/Is.h \
- ../../src/Renderer/Pps.h ../../src/Renderer/Bs.h \
+ ../../src/Renderer/Ms.h ../../src/Renderer/RenderingPass.h \
+ ../../src/Renderer/Is.h ../../src/Renderer/Pps.h ../../src/Renderer/Bs.h \
  ../../src/Renderer/Dbg.h ../../src/Core/App.h \
  ../../extern/include/SDL/SDL.h ../../extern/include/SDL/SDL_main.h \
  ../../extern/include/SDL/SDL_stdinc.h \
@@ -3923,7 +3923,8 @@ ModelNode.o: ../../src/Scene/ModelNode.cpp ../../src/Scene/ModelNode.h \
  ../../src/Core/Object.h ../../src/Util/Properties.h \
  ../../src/Resources/Core/RsrcPtr.h ../../src/Resources/Model.h \
  ../../src/Resources/Core/Resource.h ../../src/Util/Util.h \
- ../../src/Util/Vec.h ../../src/Util/StdTypes.h
+ ../../src/Util/Vec.h ../../src/Util/StdTypes.h \
+ ../../src/Resources/Skeleton.h
 	@echo Compiling ../../src/Scene/ModelNode.cpp...
 	@$(CXX) $(INCPATH) $(CFLAGS) ../../src/Scene/ModelNode.cpp -o ModelNode.o
 

+ 46 - 121
src/Resources/Model.cpp

@@ -1,5 +1,8 @@
+#include <boost/property_tree/ptree.hpp>
+#include <boost/property_tree/xml_parser.hpp>
+#include <boost/foreach.hpp>
+#include <boost/lexical_cast.hpp>
 #include "Model.h"
-#include "Parser.h"
 #include "Material.h"
 #include "Mesh.h"
 #include "SkelAnim.h"
@@ -19,101 +22,50 @@
 //======================================================================================================================
 void Model::load(const char* filename)
 {
-	Scanner scanner(filename);
-	const Scanner::Token* token = &scanner.getCrntToken();
-
-	while(true)
+	//
+	// Load
+	//
+	try
+  {
+		using namespace boost::property_tree;
+		ptree pt;
+  	read_xml(filename, pt);
+
+  	// subModels
+  	BOOST_FOREACH(ptree::value_type& v, pt.get_child("subModels"))
+  	{
+  		const std::string& mesh = v.second.get<std::string>("mesh");
+  		const std::string& material = v.second.get<std::string>("material");
+  		const std::string& dpMaterial = v.second.get<std::string>("dpMaterial");
+
+  		subModels.push_back(SubModel());
+  		subModels.back().load(mesh.c_str(), material.c_str(), dpMaterial.c_str());
+  	}
+
+  	// skeleton
+  	boost::optional<std::string> skelName = pt.get_optional<std::string>("skeleton");
+  	if(skelName.is_initialized())
+  	{
+  		skeleton.loadRsrc(skelName.get().c_str());
+  	}
+
+
+  	boost::optional<ptree&> skelAnims_ = pt.get_child_optional("skelAnims");
+  	if(skelAnims_.is_initialized())
+  	{
+  		BOOST_FOREACH(ptree::value_type& v, skelAnims_.get())
+  		{
+  			const std::string& name = v.second.data();
+  			skelAnims.push_back(RsrcPtr<SkelAnim>());
+				skelAnims.back().loadRsrc(name.c_str());
+  		}
+  	}
+	}
+	catch(std::exception& e)
 	{
-		scanner.getNextToken();
-
-		//
-		// subModels
-		//
-		if(Parser::isIdentifier(token, "subModels"))
-		{
-			// {
-			scanner.getNextToken();
-			if(token->getCode() != Scanner::TC_LBRACKET)
-			{
-				throw PARSER_EXCEPTION_EXPECTED("{");
-			}
-
-			// For all subModels
-			while(true)
-			{
-				scanner.getNextToken();
-
-				// }
-				if(token->getCode() == Scanner::TC_RBRACKET)
-				{
-					break;
-				}
-				else if(Parser::isIdentifier(token, "subModel"))
-				{
-					parseSubModel(scanner);
-				}
-				else
-				{
-					throw PARSER_EXCEPTION_EXPECTED("{ or subModel");
-				}
-			} // End for all subModels
-		}
-		//
-		// skeleton
-		//
-		if(Parser::isIdentifier(token, "skeleton"))
-		{
-			skeleton.loadRsrc(Parser::parseString(scanner).c_str());
-		}
-		//
-		// skelAnims
-		//
-		if(Parser::isIdentifier(token, "skelAnims"))
-		{
-			scanner.getNextToken();
-
-			// {
-			if(token->getCode() != Scanner::TC_LBRACKET)
-			{
-				throw PARSER_EXCEPTION_EXPECTED("{");
-			}
-
-			while(true)
-			{
-				scanner.getNextToken();
-
-				// }
-				if(token->getCode() != Scanner::TC_RBRACKET)
-				{
-					break;
-				}
-				else if(token->getCode() == Scanner::TC_STRING)
-				{
-					skelAnims.push_back(RsrcPtr<SkelAnim>());
-					skelAnims.back().loadRsrc(token->getValue().getString());
-				}
-				else
-				{
-					throw PARSER_EXCEPTION_EXPECTED("} or string");
-				}
+		throw MDL_EXCEPTION(e.what());
+	}
 
-			}
-		}
-		//
-		// EOF
-		//
-		else if(token->getCode() == Scanner::TC_EOF)
-		{
-			break;
-		}
-		//
-		// other crap
-		//
-		else
-		{
-			throw PARSER_EXCEPTION_UNEXPECTED();
-		}
-	} // end while
 
 	//
 	// Sanity checks
@@ -158,33 +110,6 @@ void Model::load(const char* filename)
 }
 
 
-//======================================================================================================================
-// parseSubModel                                                                                                       =
-//======================================================================================================================
-void Model::parseSubModel(Scanner& scanner)
-{
-	const Scanner::Token* token = &scanner.getNextToken();
-
-	if(token->getCode() != Scanner::TC_LBRACKET)
-	{
-		throw PARSER_EXCEPTION_EXPECTED("{");
-	}
-
-	Parser::parseIdentifier(scanner, "mesh");
-	std::string mesh = Parser::parseString(scanner);
-
-	Parser::parseIdentifier(scanner, "material");
-	std::string material = Parser::parseString(scanner);
-
-	Parser::parseIdentifier(scanner, "dpMaterial");
-	std::string dpMaterial = Parser::parseString(scanner);
-
-	// Load the stuff
-	subModels.push_back(SubModel());
-	subModels.back().load(mesh.c_str(), material.c_str(), dpMaterial.c_str());
-}
-
-
 //======================================================================================================================
 // hasHwSkinning                                                                                                       =
 //======================================================================================================================

+ 19 - 21
src/Resources/Model.h

@@ -16,28 +16,29 @@ class Scanner;
 
 /// Model is an entity that acts as a container for other resources. Models are all the non static objects in a map.
 ///
-/// Text file format:
+/// XML file format:
 /// @code
-/// subModels {
-/// 	subModel {
-/// 		mesh <string>
-/// 		material <string>
-/// 		dpMaterial <string>
-/// 	}
+/// <subModels>
+/// 	<subModel>
+/// 		<mesh>path/to/mesh.mesh</mesh>
+/// 		<material>path/to/material.mtl</material>
+/// 		<dpMaterial>path/to/dp.mtl</dpMaterial>
+/// 	</subModel>
 /// 	...
-/// 	subModel {
-/// 		...
-/// 	}
-/// }
-///
-/// skeleton <string>
-///
-/// skelAnims {
-/// 	<string>
+/// 	<subModel>...</subModel>
+/// </subModels>
+/// <skeleton>path/to/skeleton.skel</skeleton>
+/// <skelAnims>
+/// 	<skelAnim>path/to/anim0.sanim</skelAnim>
 /// 	...
-/// 	<string>
-/// }
+/// 	<skelAnim>...</skelAnim>
+/// </skelAnims>
 /// @endcode
+///
+/// Requirements:
+/// - If the materials need texture coords or/and vertex weights then mesh should have them
+/// - The skeleton and skelAnims are optional
+/// - Its an error to have skelAnims without skeleton
 class Model: public Resource
 {
 	public:
@@ -93,9 +94,6 @@ class Model: public Resource
 		Vec<SubModel> subModels; ///< The vector of SubModel
 		RsrcPtr<Skeleton> skeleton; ///< The skeleton. It can be empty
 		Vec<RsrcPtr<SkelAnim> > skelAnims; ///< The standard skeleton animations
-
-		/// Parses a submodel from after the "subModel" until the closing bracket
-		void parseSubModel(Scanner& scanner);
 };
 
 

+ 0 - 103
src/Util/BldfTree.h

@@ -1,103 +0,0 @@
-#ifndef BLDF_TREE_H
-#define BLDF_TREE_H
-
-#include "Properties.h"
-#include "Vec.h"
-
-
-namespace Bldf {
-
-
-/// Entity as a base class
-class Entity
-{
-	public:
-		enum EntityType
-		{
-			ET_KEY,
-			ET_VALUE
-		};
-
-	PROPERTY_R(int, line, getLine)
-	PROPERTY_R(EntityType, type, getType)
-
-	public:
-		Entity(int line, EntityType type);
-};
-
-
-inline Entity::Entity(int line_, EntityType type_):
-	line(line_),
-	type(type_)
-{}
-
-
-/// Value
-class Value: public Entity
-{
-	public:
-		/// The type of the value
-		enum ValueType
-		{
-			VT_STRING,
-			VT_NUMBER,
-			VT_BOOL
-		};
-
-	PROPERTY_R(ValueType, type, getType) ///< Type
-
-	public:
-		Value(int line, const char* str);
-		Value(int line, double num);
-		Value(int line, bool b);
-
-	private:
-		std::string str;
-		double num;
-		bool b;
-};
-
-
-inline Value::Value(int line, const char* str_):
-	Entity(line, ET_VALUE),
-	type(VT_STRING),
-	str(str_)
-{}
-
-
-inline Value::Value(int line, double num_):
-	Entity(line, ET_VALUE),
-	type(VT_NUMBER),
-	num(num_)
-{}
-
-
-inline Value::Value(int line, bool b_):
-	Entity(line, ET_VALUE),
-	type(VT_BOOL),
-	b(b_)
-{}
-
-
-///
-class Key: public Element
-{
-	PROPERTY_R(std::string, name, getName)
-	PROPERTY_R(Vec<Key>, keys, getKeys)
-	PROPERTY_R(Vec<Value>, values, getValues)
-
-	public:
-		Key(int line): Element(line, ET_KEY) {}
-};
-
-
-/// Bracket loving data file (aka BLDF) tree structure with parser
-class Tree
-{
-
-};
-
-
-} // end namespace Bldf
-
-#endif