Panagiotis Christopoulos Charitos před 14 roky
rodič
revize
23886554e1
7 změnil soubory, kde provedl 180 přidání a 41 odebrání
  1. 1 1
      license
  2. 6 6
      shaders/IsLpGeneric.glsl
  3. 1 7
      shaders/Pack.glsl
  4. 8 7
      src/Main.cpp
  5. 132 13
      src/Resources/SProgLoader.cpp
  6. 14 1
      src/Resources/SProgLoader.h
  7. 18 6
      src/Util/Util.h

+ 1 - 1
license

@@ -1,5 +1,5 @@
 AnKi 3D Engine
-Copyright (c) 2009, 2010 Panagiotis Christopoulos-Charitos
+Copyright (c) 2009, 2011 Panagiotis Christopoulos-Charitos
 All rights reserved.
 
 AnKi 3D Engine source code is available under dual license. GPLv3 and commercial

+ 6 - 6
shaders/IsLpGeneric.glsl

@@ -23,12 +23,12 @@ uniform float lightRadius;
 uniform vec3 lightDiffuseCol;
 uniform vec3 lightSpecularCol;
 #if defined(SPOT_LIGHT_ENABLED)
-	uniform sampler2D lightTex;
-	uniform mat4 texProjectionMat;
-	#if defined(SHADOW_ENABLED)
-		uniform sampler2DShadow shadowMap;
-		uniform float shadowMapSize;
-	#endif
+uniform sampler2D lightTex;
+uniform mat4 texProjectionMat;
+#	if defined(SHADOW_ENABLED)
+uniform sampler2DShadow shadowMap;
+uniform float shadowMapSize;
+#	endif
 #endif
 /// @}
 

+ 1 - 7
shaders/Pack.glsl

@@ -1,13 +1,7 @@
 /// Pack 3D normal to 2D vector
+/// See the clean code in commends in revision < r467
 vec2 packNormal(in vec3 normal)
 {
-    /*original code:
-    const float SCALE = 1.7777;
-    vec2 _enc = _normal.xy / (_normal.z+1.0);
-    _enc /= SCALE;
-    _enc = _enc*0.5 + 0.5;
-    return _enc;*/
-
     const float SCALE = 1.7777;
     float scalar1 = (normal.z + 1.0) * (SCALE * 2.0);
     return normal.xy / scalar1 + 0.5;

+ 8 - 7
src/Main.cpp

@@ -43,6 +43,7 @@
 #include "Events/Manager.h"
 #include "Events/SceneColor.h"
 #include "Events/MainRendererPpsHdr.h"
+#include "Resources/SProgLoader.h"
 
 
 // map (hard coded)
@@ -459,15 +460,14 @@ void mainLoop()
 //==============================================================================
 int main(int argc, char* argv[])
 {
-	/*std::stringstream ss("lala\n\n\n\n1a");
-	Scanner::Scanner scanner(ss);
-
-	scanner.getAllPrintAll();
-
-	return 0;*/
-
 	try
 	{
+		SProgLoader l("lala.sprog");
+		std::cout << "$$$" << l.getVertexShaderSource() << "$$$" << std::endl;
+		std::cerr << "$$$" << l.getFragmentShaderSource() << "$$$" << std::endl;
+
+		return 0;
+
 		AppSingleton::getInstance().init(argc, argv);
 		init();
 
@@ -480,6 +480,7 @@ int main(int argc, char* argv[])
 	catch(std::exception& e)
 	{
 		ERROR("Aborting: " << e.what());
+		std::cerr << "Aborting: " << e.what() << std::endl;
 		//abort();
 		return 1;
 	}

+ 132 - 13
src/Resources/SProgLoader.cpp

@@ -1,5 +1,6 @@
 #include "SProgLoader.h"
 #include "Util/Exception.h"
+#include "Util/Util.h"
 
 #include <boost/property_tree/ptree.hpp>
 #include <boost/property_tree/xml_parser.hpp>
@@ -7,36 +8,154 @@
 #include <boost/lexical_cast.hpp>
 
 
+//==============================================================================
+// Destructor                                                                  =
+//==============================================================================
+SProgLoader::~SProgLoader()
+{}
+
+
 //==============================================================================
 // parseShaderProgBlock                                                        =
 //==============================================================================
 void SProgLoader::parseShaderProgBlock(const boost::property_tree::ptree& pt,
 	Output& out)
 {
-	try
-	{
-		using namespace boost::property_tree;
+	using namespace boost::property_tree;
+	std::string allCode;
 
-		// <transformFeedbackVaryings> </transformFeedbackVaryings>
-		boost::optional<const ptree&> trfFeedbackVarsPt =
-			pt.get_child_optional("transformFeedbackVaryings");
-
-		if(trfFeedbackVarsPt)
+	BOOST_FOREACH(const ptree::value_type& v, pt)
+	{
+		// <transformFeedbackVaryings> ... </transformFeedbackVaryings>
+		if(v.first == "transformFeedbackVaryings")
 		{
-			BOOST_FOREACH(const ptree::value_type& v, trfFeedbackVarsPt.get())
+			BOOST_FOREACH(const ptree::value_type& v2, v.second)
 			{
-				if(v.first != "transformFeedbackVarying")
+				if(v2.first != "transformFeedbackVarying")
 				{
 					throw EXCEPTION("Expected transformFeedbackVarying"
-						" and not " + v.first);
+						" and not " + v2.first);
 				}
 
-				const std::string& name = v.second.data();
+				const std::string& name = v2.second.data();
 				out.tfbVaryings.push_back(name);
 			}
 		}
+		// <allShader> ... </allShader>
+		else if(v.first == "allShader")
+		{
+			parseShaderBlock(v.second, allCode);
+		}
+		// <vertexShader> ... </vertexShader>
+		else if(v.first == "vertexShader")
+		{
+			out.vertShaderSource += allCode;
+			parseShaderBlock(v.second, out.vertShaderSource);
+		}
+		// <geometryShader> ... </geometryShader>
+		else if(v.first == "geometryShader")
+		{
+			out.geomShaderSource += allCode;
+			parseShaderBlock(v.second, out.geomShaderSource);
+		}
+		// <fragmentShader> ... </fragmentShader>
+		else if(v.first == "fragmentShader")
+		{
+			out.fragShaderSource += allCode;
+			parseShaderBlock(v.second, out.fragShaderSource);
+		}
+		// <include> ... </include>
+		else if(v.first == "include")
+		{
+			SProgLoader spl(v.second.data().c_str());
 
-		//
+			BOOST_FOREACH(const std::string& trffbvar, spl.out.tfbVaryings)
+			{
+				out.tfbVaryings.push_back(trffbvar);
+			}
+
+			out.vertShaderSource += spl.out.vertShaderSource;
+			out.geomShaderSource += spl.out.geomShaderSource;
+			out.fragShaderSource += spl.out.fragShaderSource;
+		}
+		// error
+		else
+		{
+			throw EXCEPTION("Incorrect tag: " + v.first);
+		}
+	}
+}
+
+
+//==============================================================================
+// parseShaderBlock                                                            =
+//==============================================================================
+void SProgLoader::parseShaderBlock(const boost::property_tree::ptree& pt,
+	std::string& src)
+{
+	using namespace boost::property_tree;
+
+	BOOST_FOREACH(const ptree::value_type& v, pt)
+	{
+		if(v.first == "include")
+		{
+			src += Util::readFile(v.second.data().c_str());
+		}
+		else if(v.first == "code")
+		{
+			src += v.second.data();
+		}
+		else
+		{
+			throw EXCEPTION("Expecting <include> or <code> and not: " +
+				v.first);
+		}
+	}
+}
+
+
+//==============================================================================
+// parseFile                                                                   =
+//==============================================================================
+void SProgLoader::parseFile(const char* filename)
+{
+	try
+	{
+		using namespace boost::property_tree;
+		ptree pt;
+		read_xml(filename, pt);
+		parsePtree(pt.get_child("shaderProgram"));
+	}
+	catch(std::exception& e)
+	{
+		throw EXCEPTION("File \"" + filename + "\" failed: " + e.what());
+	}
+	catch(...)
+	{
+		throw EXCEPTION("Fucken boost");
+	}
+}
+
+
+//==============================================================================
+// parsePtree                                                                  =
+//==============================================================================
+void SProgLoader::parsePtree(const boost::property_tree::ptree& pt_)
+{
+	try
+	{
+		using namespace boost::property_tree;
+
+		boost::optional<const ptree&> pt =
+			pt_.get_child_optional("shaderProgram");
+		if(pt)
+		{
+			parseShaderProgBlock(pt.get(), out);
+		}
+		else
+		{
+			parseShaderProgBlock(pt_, out);
+		}
 	}
 	catch(std::exception& e)
 	{

+ 14 - 1
src/Resources/SProgLoader.h

@@ -2,6 +2,7 @@
 #define S_PROG_LOADER_H
 
 #include "Util/Vec.h"
+#include "Util/Accessors.h"
 #include <string>
 #include <boost/property_tree/ptree_fwd.hpp>
 
@@ -15,6 +16,14 @@ class SProgLoader
 		SProgLoader(const boost::property_tree::ptree& pt) {parsePtree(pt);}
 		~SProgLoader();
 
+		/// @name Accessors
+		/// @{
+		GETTER_R(Vec<std::string>, out.tfbVaryings, getTranformFeedbackVaryings)
+		GETTER_R(std::string, out.vertShaderSource, getVertexShaderSource)
+		GETTER_R(std::string, out.geomShaderSource, getGeometryShaderSource)
+		GETTER_R(std::string, out.fragShaderSource, getFragmentShaderSource)
+		/// @}
+
 		void parseFile(const char* filename);
 		void parsePtree(const boost::property_tree::ptree& pt);
 
@@ -28,10 +37,14 @@ class SProgLoader
 			std::string fragShaderSource; ///< The frag shader source
 		};
 
-		/// Parse the: @code <shaderProg>...</shaderProg> @endcode
+		Output out;
+
+		/// Parse what is inside the:
+		/// @code <shaderProgram>...</shaderProgram> @endcode
 		static void parseShaderProgBlock(const boost::property_tree::ptree& pt,
 			Output& out);
 
+		/// Parse what is inside a shader block. It appends the src string
 		static void parseShaderBlock(const boost::property_tree::ptree& pt,
 			std::string& src);
 };

+ 18 - 6
src/Util/Util.h

@@ -9,14 +9,26 @@
 /// The namespace contains a few useful functions
 namespace Util {
 
-extern int randRange(int min, int max); ///< Pick a random number from min to max
-extern uint randRange(uint min, uint max); ///< Pick a random number from min to max
-extern float randRange(float min, float max); ///< Pick a random number from min to max
-extern double randRange(double min, double max); ///< Pick a random number from min to max
+/// Pick a random number from min to max
+extern int randRange(int min, int max);
+
+/// Pick a random number from min to max
+extern uint randRange(uint min, uint max);
+
+/// Pick a random number from min to max
+extern float randRange(float min, float max);
+
+/// Pick a random number from min to max
+extern double randRange(double min, double max);
+
 extern float randFloat(float max);
 
-extern std::string readFile(const char* filename); ///< Open a text file and return its contents into a string
-extern Vec<std::string> getFileLines(const char* filename); ///< Open a text file and return its lines into a string vector
+/// Open a text file and return its contents into a string
+extern std::string readFile(const char* filename);
+
+/// Open a text file and return its lines into a string vector
+extern Vec<std::string> getFileLines(const char* filename);
+
 
 }