Browse Source

Reworking material

Panagiotis Christopoulos Charitos 13 years ago
parent
commit
55e8fbe7b2

+ 18 - 33
include/anki/resource/Material.h

@@ -7,16 +7,14 @@
 #include "anki/util/StringList.h"
 #include "anki/math/Math.h"
 #include "anki/util/Visitor.h"
-#include "anki/gl/gl.h"
+#include "anki/util/NonCopyable.h"
+#include "anki/gl/Gl.h"
 #include <boost/ptr_container/ptr_vector.hpp>
-#include <boost/array.hpp>
-#include <boost/scoped_ptr.hpp>
 #include <boost/property_tree/ptree_fwd.hpp>
-#include <boost/range/iterator_range.hpp>
-#include <boost/variant.hpp>
 
 namespace anki {
 
+// Forward
 class ShaderProgram;
 class ShaderProgramUniformVariable;
 
@@ -30,7 +28,7 @@ class MaterialVariableTemplate;
 
 /// Holds the shader variables. Its a container for shader program variables
 /// that share the same name
-class MaterialVariable: public MateriaVariableBase
+class MaterialVariable: public MateriaVariableBase, public NonCopyable
 {
 public:
 	typedef MateriaVariableBase Base;
@@ -40,15 +38,11 @@ public:
 	typedef PassLevelHashMap<ShaderProgramUniformVariable*>::Type
 		PassLevelToShaderProgramUniformVariableHashMap;
 
-	// Non-copyable
-	MaterialVariable(const MaterialVariable&) = delete;
-	MaterialVariable& operator=(const MaterialVariable&) = delete;
-
 	/// @name Constructors & destructor
 	/// @{
 	MaterialVariable(
 		const char* shaderProgVarName,
-		const PassLevelToShaderProgramHashMap& sProgs,
+		PassLevelToShaderProgramHashMap& sProgs,
 		bool init_,
 		uint8_t type_)
 		: type(type_), initialized(init_)
@@ -111,7 +105,7 @@ private:
 
 	/// Common constructor code
 	void init(const char* shaderProgVarName,
-		const PassLevelToShaderProgramHashMap& shaderProgsArr);
+		PassLevelToShaderProgramHashMap& shaderProgsArr);
 };
 
 /// XXX
@@ -138,7 +132,7 @@ public:
 
 	/// @name Accessors
 	/// @{
-	const Data& get() const;
+	const Data& get() const
 	{
 		return data;
 	}
@@ -158,18 +152,6 @@ private:
 class MaterialProperties
 {
 public:
-	/// Initialize with default values
-	MaterialProperties()
-	{
-		renderingStage = 0;
-		levelsOfDetail = 1;
-		shadow = false;
-		blendingSfactor = GL_ONE;
-		blendingDfactor = GL_ZERO;
-		depthTesting = true;
-		wireframe = false;
-	}
-
 	/// @name Accessors
 	/// @{
 	uint getRenderingStage() const
@@ -219,20 +201,20 @@ public:
 		return blendingSfactor != GL_ONE || blendingDfactor != GL_ZERO;
 	}
 protected:
-	uint renderingStage;
+	uint renderingStage = 0;
 
 	StringList passes;
 
-	uint levelsOfDetail;
+	uint levelsOfDetail = 1;
 
-	bool shadow;
+	bool shadow = true;
 
-	int blendingSfactor; ///< Default GL_ONE
-	int blendingDfactor; ///< Default GL_ZERO
+	int blendingSfactor = GL_ONE; ///< Default GL_ONE
+	int blendingDfactor = GL_ZERO; ///< Default GL_ZERO
 
-	bool depthTesting;
+	bool depthTesting = true;
 
-	bool wireframe;
+	bool wireframe = false;
 };
 
 /// Material resource
@@ -312,7 +294,7 @@ protected:
 /// (4): AKA uniforms
 ///
 /// (5): The order of the shaders is crucial
-class Material: public MaterialProperties, public boost::noncopyable
+class Material: public MaterialProperties, public NonCopyable
 {
 public:
 	typedef boost::ptr_vector<MaterialVariable> VarsContainer;
@@ -414,6 +396,9 @@ private:
 	/// math type
 	template<typename Type, size_t n>
 	static Type setMathType(const char* str);
+
+	/// Given a string that defines blending return the GLenum
+	static GLenum blendToEnum(const char* str);
 };
 
 } // end namespace

+ 1 - 2
include/anki/util/HighRezTimer.h

@@ -1,7 +1,6 @@
 #ifndef ANKI_UTIL_HIGH_REZ_TIMER_H
 #define ANKI_UTIL_HIGH_REZ_TIMER_H
 
-
 namespace anki {
 
 /// High resolution timer. All time in seconds
@@ -22,7 +21,7 @@ public:
 	Scalar getElapsedTime() const;
 
 	/// Get the current date's seconds
-	static Scalar getCrntTime();
+	static Scalar getCurrentTime();
 
 private:
 	Scalar startTime = 0.0;

+ 21 - 0
include/anki/util/NonCopyable.h

@@ -0,0 +1,21 @@
+#ifndef ANKI_UTIL_NON_COPYABLE_H
+#define ANKI_UTIL_NON_COPYABLE_H
+
+namespace anki {
+
+/// Makes a derived class non copyable
+struct NonCopyable
+{
+	NonCopyable()
+	{}
+
+	NonCopyable(const NonCopyable&) = delete;
+	NonCopyable& operator=(const NonCopyable&) = delete;
+
+	~NonCopyable()
+	{}
+};
+
+} // end namespace anki
+
+#endif

+ 9 - 5
include/anki/util/Visitor.h

@@ -9,6 +9,8 @@ namespace anki {
 /// @addtogroup util
 /// @{
 
+namespace detail {
+
 //==============================================================================
 // ConstVisitor                                                                =
 //==============================================================================
@@ -152,6 +154,8 @@ struct GetVisitableId<Type, Type, Types...>
 	static const int ID = sizeof...(Types);
 };
 
+} // end namespace detail
+
 //==============================================================================
 // Visitable                                                                   =
 //==============================================================================
@@ -160,10 +164,10 @@ struct GetVisitableId<Type, Type, Types...>
 template<typename... Types>
 struct Visitable
 {
-	using MutableVisitor = MutableVisitor<Types...>;
-	using ConstVisitor = ConstVisitor<Types...>;
-	using GetTypeIdVisitor = typename GetTypeIdVisitor<Types...>::Type;
-	using DummyVisitor = typename DummyVisitor<Types...>::Type;
+	using MutableVisitor = detail::MutableVisitor<Types...>;
+	using ConstVisitor = detail::ConstVisitor<Types...>;
+	using GetTypeIdVisitor = typename detail::GetTypeIdVisitor<Types...>::Type;
+	using DummyVisitor = typename detail::DummyVisitor<Types...>::Type;
 
 	/// Visitor accept
 	virtual void accept(MutableVisitor& v) = 0;
@@ -174,7 +178,7 @@ struct Visitable
 	template<typename T>
 	static constexpr int getTypeId()
 	{
-		return sizeof...(Types) - GetVisitableId<T, Types...>::ID - 1;
+		return sizeof...(Types) - detail::GetVisitableId<T, Types...>::ID - 1;
 	}
 };
 /// @}

+ 13 - 11
src/gl/GlException.cpp

@@ -1,7 +1,7 @@
 #include "anki/gl/GlException.h"
 #include "anki/gl/Gl.h"
 #include "anki/util/Exception.h"
-#include <string>
+#include <cstring>
 
 namespace anki {
 
@@ -13,31 +13,33 @@ void glConditionalThrowException(const char* file, int line, const char* func)
 		return;
 	}
 
-	const errStr = nullptr;
-
+	const char* glerr;
 	switch(errId)
 	{
 	case GL_INVALID_ENUM:
-		errStr = "GL_INVALID_ENUM";
+		glerr = "GL_INVALID_ENUM";
 		break;
 	case GL_INVALID_VALUE:
-		errStr = "GL_INVALID_VALUE";
+		glerr = "GL_INVALID_VALUE";
 		break;
 	case GL_INVALID_OPERATION:
-		errStr = "GL_INVALID_OPERATION";
+		glerr = "GL_INVALID_OPERATION";
 		break;
 	case GL_INVALID_FRAMEBUFFER_OPERATION:
-		errStr = "GL_INVALID_FRAMEBUFFER_OPERATION";
+		glerr = "GL_INVALID_FRAMEBUFFER_OPERATION";
 		break;
 	case GL_OUT_OF_MEMORY:
-		errStr = "GL_OUT_OF_MEMORY";
+		glerr = "GL_OUT_OF_MEMORY";
 		break;
 	default:
-		errStr = "unknown";
+		glerr = "unknown";
 	};
 
-	std::string err = std::string("OpenGL exception: ") + errStr;
-	throw Exception(err.c_str(), file, line, func);
+	char errStr[256];
+	const char tmp[] = "OpenGL exception: ";
+	memcpy(errStr, tmp, sizeof(tmp));
+	strcat(errStr, glerr);
+	throw Exception(errStr, file, line, func);
 }
 
 } // end namespace

+ 34 - 50
src/resource/Material.cpp

@@ -7,10 +7,7 @@
 #include "anki/resource/TextureResource.h"
 #include <boost/property_tree/ptree.hpp>
 #include <boost/property_tree/xml_parser.hpp>
-#include <boost/assign/list_of.hpp>
-#include <boost/functional/hash.hpp>
-#include <boost/tokenizer.hpp>
-#include <boost/lexical_cast.hpp>
+#include <functional>
 #include <algorithm>
 
 namespace anki {
@@ -37,12 +34,12 @@ const std::string& MaterialVariable::getName() const
 
 //==============================================================================
 void MaterialVariable::init(const char* shaderProgVarName,
-	const PassLevelToShaderProgramHashMap& sProgs)
+	PassLevelToShaderProgramHashMap& sProgs)
 {
 	oneSProgVar = NULL;
 
 	// For all programs
-	PassLevelToShaderProgramHashMap::const_iterator it = sProgs.begin();
+	PassLevelToShaderProgramHashMap::iterator it = sProgs.begin();
 	for(; it != sProgs.end(); ++it)
 	{
 		const ShaderProgram& sProg = *(it->second);
@@ -85,27 +82,6 @@ void MaterialVariable::init(const char* shaderProgVarName,
 // Material                                                                    =
 //==============================================================================
 
-//==============================================================================
-
-// Dont make idiotic mistakes
-#define TXT_AND_ENUM(x) \
-	(#x, x)
-
-ConstCharPtrHashMap<GLenum>::Type Material::txtToBlengGlEnum =
-	boost::assign::map_list_of
-	TXT_AND_ENUM(GL_ZERO)
-	TXT_AND_ENUM(GL_ONE)
-	TXT_AND_ENUM(GL_DST_COLOR)
-	TXT_AND_ENUM(GL_ONE_MINUS_DST_COLOR)
-	TXT_AND_ENUM(GL_SRC_ALPHA)
-	TXT_AND_ENUM(GL_ONE_MINUS_SRC_ALPHA)
-	TXT_AND_ENUM(GL_DST_ALPHA)
-	TXT_AND_ENUM(GL_ONE_MINUS_DST_ALPHA)
-	TXT_AND_ENUM(GL_SRC_ALPHA_SATURATE)
-	TXT_AND_ENUM(GL_SRC_COLOR)
-	TXT_AND_ENUM(GL_ONE_MINUS_SRC_COLOR);
-
-
 //==============================================================================
 Material::Material()
 {}
@@ -186,32 +162,14 @@ void Material::parseMaterialTag(const boost::property_tree::ptree& pt)
 		{
 			const std::string& tmp =
 				blendFuncsTree.get().get<std::string>("sFactor");
-
-			ConstCharPtrHashMap<GLenum>::Type::const_iterator it =
-				txtToBlengGlEnum.find(tmp.c_str());
-
-			if(it == txtToBlengGlEnum.end())
-			{
-				throw ANKI_EXCEPTION("Incorrect blend enum: " + tmp);
-			}
-
-			blendingSfactor = it->second;
+			blendingSfactor = blendToEnum(tmp.c_str());
 		}
 
 		// dFactor
 		{
 			const std::string& tmp =
 				blendFuncsTree.get().get<std::string>("dFactor");
-
-			ConstCharPtrHashMap<GLenum>::Type::const_iterator it =
-				txtToBlengGlEnum.find(tmp.c_str());
-
-			if(it == txtToBlengGlEnum.end())
-			{
-				throw ANKI_EXCEPTION("Incorrect blend enum: " + tmp);
-			}
-
-			blendingDfactor = it->second;
+			blendingDfactor = blendToEnum(tmp.c_str());
 		}
 	}
 
@@ -270,7 +228,7 @@ void Material::parseMaterialTag(const boost::property_tree::ptree& pt)
 std::string Material::createShaderProgSourceToCache(const std::string& source)
 {
 	// Create the hash
-	boost::hash<std::string> stringHash;
+	std::hash<std::string> stringHash;
 	std::size_t h = stringHash(source);
 	std::string prefix = boost::lexical_cast<std::string>(h);
 
@@ -286,8 +244,8 @@ std::string Material::createShaderProgSourceToCache(const std::string& source)
 		std::ofstream f(newfPathName.string().c_str());
 		if(!f.is_open())
 		{
-			throw ANKI_EXCEPTION("Cannot open file for writing: " +
-				newfPathName.string());
+			throw ANKI_EXCEPTION("Cannot open file for writing: " 
+				+ newfPathName.string());
 		}
 
 		f.write(source.c_str(), source.length());
@@ -494,4 +452,30 @@ const MaterialVariable& Material::findVariableByName(const char* name) const
 	return *(it->second);
 }
 
+//==============================================================================
+GLenum Material::blendToEnum(const char* str)
+{
+// Dont make idiotic mistakes
+#define TXT_AND_ENUM(x) \
+	if(strcmp(str, #x) == 0) { \
+		return x; \
+	}
+
+	TXT_AND_ENUM(GL_ZERO)
+	TXT_AND_ENUM(GL_ONE)
+	TXT_AND_ENUM(GL_DST_COLOR)
+	TXT_AND_ENUM(GL_ONE_MINUS_DST_COLOR)
+	TXT_AND_ENUM(GL_SRC_ALPHA)
+	TXT_AND_ENUM(GL_ONE_MINUS_SRC_ALPHA)
+	TXT_AND_ENUM(GL_DST_ALPHA)
+	TXT_AND_ENUM(GL_ONE_MINUS_DST_ALPHA)
+	TXT_AND_ENUM(GL_SRC_ALPHA_SATURATE)
+	TXT_AND_ENUM(GL_SRC_COLOR)
+	TXT_AND_ENUM(GL_ONE_MINUS_SRC_COLOR);
+	ANKI_ASSERT(0);
+	throw ANKI_EXCEPTION("Incorrect blend enum: " + str);
+
+#undef TXT_AND_ENUM
+}
+
 } // end namespace

+ 2 - 2
src/script/util/HighRezTimer.cpp

@@ -5,7 +5,7 @@
 WRAP(HighRezTimer)
 {
 	class_<HighRezTimer>("HighRezTimer")
-		.def("getCrntTime", &HighRezTimer::getCrntTime)
-		.staticmethod("getCrntTime")
+		.def("getCurrentTime", &HighRezTimer::getCurrentTime)
+		.staticmethod("getCurrentTime")
 	;
 }

+ 4 - 4
src/util/HighRezTimer.cpp

@@ -9,7 +9,7 @@ void HighRezTimer::start()
 {
 	ANKI_ASSERT(startTime == 0);
 	ANKI_ASSERT(stopTime == 0);
-	startTime = getCrntTime();
+	startTime = getCurrentTime();
 	stopTime = 0.0;
 }
 
@@ -18,7 +18,7 @@ void HighRezTimer::stop()
 {
 	ANKI_ASSERT(startTime != 0.0);
 	ANKI_ASSERT(stopTime == 0.0);
-	stopTime = getCrntTime();
+	stopTime = getCurrentTime();
 }
 
 //==============================================================================
@@ -26,7 +26,7 @@ HighRezTimer::Scalar HighRezTimer::getElapsedTime() const
 {
 	if(stopTime == 0)
 	{
-		return getCrntTime() - startTime;
+		return getCurrentTime() - startTime;
 	}
 	else
 	{
@@ -35,7 +35,7 @@ HighRezTimer::Scalar HighRezTimer::getElapsedTime() const
 }
 
 //==============================================================================
-HighRezTimer::Scalar HighRezTimer::getCrntTime()
+HighRezTimer::Scalar HighRezTimer::getCurrentTime()
 {
 	/// XXX Remove the boost
 	using namespace boost::posix_time;