Browse Source

Exporter updates

Panagiotis Christopoulos Charitos 11 years ago
parent
commit
6a1390528e

+ 2 - 0
shaders/IsLp.frag.glsl

@@ -165,6 +165,8 @@ void main()
 	readGBuffer(uMsRt0, uMsRt1,
 		inTexCoord, diffColor, normal, specColor, specPower);
 
+	specPower *= 128.0;
+
 	// Ambient color
 	outColor = diffColor * uSceneAmbientColor.rgb;
 

+ 2 - 4
shaders/Pack.glsl

@@ -45,15 +45,13 @@ vec4 unpackUnorm4x8(in highp uint u)
 }
 #endif
 
-#define MAX_SPECULARITY 128.0
-
 // Populate the G buffer
 void writeGBuffer(
 	in vec3 diffColor, in vec3 normal, in float specColor, in float specPower,
 	out vec4 fai0, out vec4 fai1)
 {
 	vec3 unorm = normal * 0.5 + 0.5;
-	fai0 = vec4(diffColor, specPower / MAX_SPECULARITY);
+	fai0 = vec4(diffColor, specPower);
 	fai1 = vec4(unorm.xyz, specColor);
 }
 
@@ -66,7 +64,7 @@ void readGBuffer(
 {
 	vec4 comp = textureRt(fai0, texCoord);
 	diffColor = comp.rgb;
-	specPower = comp.w * MAX_SPECULARITY;
+	specPower = comp.a;
 
 	comp = textureRt(fai1, texCoord);
 	normal = normalize(comp.xyz * 2.0 - 1.0);

+ 1 - 1
src/resource/MeshLoader.cpp

@@ -233,7 +233,7 @@ Error MeshLoader::doPostLoad()
 	}
 
 	ANKI_CHECK(createAllNormals());
-	//fixNormals();
+	fixNormals();
 
 	if(m_texCoords.getSize() > 0)
 	{

+ 23 - 1
testapp/Main.cpp

@@ -225,11 +225,33 @@ Error init()
 		0.7));*/
 #endif
 
+	if(0)
+	{
+		PointLight* point;
+		err = scene.newSceneNode<PointLight>("plight0", point);
+		point->setLocalOrigin(Vec4(0.5, 8.4, 7.6, 0.0));
+		point->setRadius(30.0);
+		point->setDiffuseColor(Vec4(1.0));
+		point->setSpecularColor(Vec4(1.0));
+		if(err) return err;
+	}
+
+	if(0)
+	{
+		PointLight* point;
+		err = scene.newSceneNode<PointLight>("plight1", point);
+		point->setLocalOrigin(Vec4(0.0, 1.1, -15.3, 0.0));
+		point->setRadius(30.0);
+		point->setDiffuseColor(Vec4(1.0));
+		point->setSpecularColor(Vec4(1.0, 0.0, 1.0, 0.0));
+		if(err) return err;
+	}
+
 #if 1
 	{
 		ScriptResourcePointer script;
 
-		err = script.load("maps/adis/scene.lua", &resources);
+		err = script.load("maps/sponza/scene.lua", &resources);
 		if(err) return err;
 
 		err = app->getScriptManager().evalString(script->getSource());

+ 110 - 49
tools/scene/Model.cpp

@@ -5,6 +5,7 @@
 
 #include "Common.h"
 #include <cassert>
+#include <algorithm>
 
 //==============================================================================
 static const aiMesh& getMesh(const Exporter& exporter, unsigned index)
@@ -271,23 +272,22 @@ void exportMaterial(
 	std::string shininessTex;
 	std::string dispTex;
 
+	aiString path;
+
 	std::string name = getMaterialName(mtl, instanced);
 	LOGI("Exporting material %s\n", name.c_str());
 
 	// Diffuse texture
-	if(mtl.GetTextureCount(aiTextureType_DIFFUSE) < 1)
-	{
-		ERROR("Material has no diffuse textures. Skipping\n");
-	}
-
-	aiString path;
-	if(mtl.GetTexture(aiTextureType_DIFFUSE, 0, &path) == AI_SUCCESS)
+	if(mtl.GetTextureCount(aiTextureType_DIFFUSE) > 0)
 	{
-		diffTex = getFilename(path.C_Str());
-	}
-	else
-	{
-		ERROR("Failed to retrieve texture\n");
+		if(mtl.GetTexture(aiTextureType_DIFFUSE, 0, &path) == AI_SUCCESS)
+		{
+			diffTex = getFilename(path.C_Str());
+		}
+		else
+		{
+			ERROR("Failed to retrieve texture\n");
+		}
 	}
 
 	// Normal texture
@@ -343,12 +343,6 @@ void exportMaterial(
 	}
 
 	// Write file
-	static const char* diffFragTemplate = 
-#include "templates/diffFrag.h"
-		;
-	static const char* diffNormFragTemplate = 
-#include "templates/diffNormFrag.h"
-		;
 	static const char* diffNormSpecFragTemplate = 
 #include "templates/diffNormSpecFrag.h"
 		;
@@ -358,6 +352,18 @@ void exportMaterial(
 	static const char* tessVertTemplate = 
 #include "templates/tessVert.h"
 		;
+
+	static const char* readRgbFromTextureTemplate = R"(
+				<operation>
+					<id>%id%</id>
+					<returnType>vec3</returnType>
+					<function>readRgbFromTexture</function>
+					<arguments>
+						<argument>%map%</argument>
+						<argument>out2</argument>
+					</arguments>
+				</operation>)";
+
 	static const char* readRFromTextureTemplate = R"(
 				<operation>
 					<id>%id%</id>
@@ -386,30 +392,90 @@ void exportMaterial(
 	materialStr += "\n";
 
 	// Then fragment part
-	if(normTex.empty())
+	materialStr += diffNormSpecFragTemplate;
+	materialStr += "\n\t</programs>\t</material>";
+
+	// Replace strings
+	if(!dispTex.empty())
 	{
-		materialStr += diffFragTemplate;
+		materialStr = replaceAllString(materialStr, "%dispMap%", 
+			exporter.texrpath + dispTex);
 	}
-	else
+
+	// Diffuse
+	if(!diffTex.empty())
 	{
-		materialStr += diffNormSpecFragTemplate;
+		materialStr = replaceAllString(materialStr, "%diffuseColorInput%", 
+			R"(<input><type>sampler2D</type><name>uDiffuseColor</name><value>)"
+			+ exporter.texrpath + diffTex
+			+ R"(</value></input>)");
+
+		materialStr = replaceAllString(materialStr, "%diffuseColorFunc%", 
+			readRgbFromTextureTemplate);
+
+		materialStr = replaceAllString(materialStr, "%id%", 
+			"10");
+
+		materialStr = replaceAllString(materialStr, "%map%", 
+			"uDiffuseColor");
+
+		materialStr = replaceAllString(materialStr, "%diffuseColorArg%", 
+			"out10");
 	}
+	else
+	{
+		aiColor3D diffCol = {0.0, 0.0, 0.0};
+		mtl.Get(AI_MATKEY_COLOR_DIFFUSE, diffCol);
+
+		materialStr = replaceAllString(materialStr, "%diffuseColorInput%", 
+			R"(<input><type>vec3</type><name>uDiffuseColor</name><value>)"
+			+ std::to_string(diffCol[0]) + " "
+			+ std::to_string(diffCol[1]) + " "
+			+ std::to_string(diffCol[2])
+			+ R"(</value></input>)");
 
-	materialStr += "\n\t</programs>\t</material>";
+		materialStr = replaceAllString(materialStr, "%diffuseColorFunc%", 
+			"");
 
-	// Replace strings
+		materialStr = replaceAllString(materialStr, "%diffuseColorArg%", 
+			"uDiffuseColor");
+	}
+
+	// Normal
 	if(!normTex.empty())
 	{
-		materialStr = replaceAllString(materialStr, "%normalMap%", 
-			exporter.texrpath + normTex);
-	}
+		materialStr = replaceAllString(materialStr, "%normalInput%", 
+			R"(<input><type>sampler2D</type><name>uNormal</name><value>)"
+			+ exporter.texrpath + normTex
+			+ R"(</value></input>)");
 
-	if(!dispTex.empty())
+		materialStr = replaceAllString(materialStr, "%normalFunc%", 
+				R"(
+				<operation>
+					<id>20</id>
+					<returnType>vec3</returnType>
+					<function>readNormalFromTexture</function>
+					<arguments>
+						<argument>out0</argument>
+						<argument>out1</argument>
+						<argument>uNormal</argument>
+						<argument>out2</argument>
+					</arguments>
+				</operation>)");
+
+		materialStr = replaceAllString(materialStr, "%normalArg%", 
+			"out20");
+	}
+	else
 	{
-		materialStr = replaceAllString(materialStr, "%dispMap%", 
-			exporter.texrpath + dispTex);
+		materialStr = replaceAllString(materialStr, "%normalInput%", " ");
+
+		materialStr = replaceAllString(materialStr, "%normalFunc%", " ");
+
+		materialStr = replaceAllString(materialStr, "%normalArg%", "out0");
 	}
 
+	// Specular
 	if(!specColTex.empty())
 	{
 		materialStr = replaceAllString(materialStr, "%specularColorInput%", 
@@ -451,29 +517,13 @@ void exportMaterial(
 		materialStr = replaceAllString(materialStr, "%specularPowerInput%", 
 			R"(<input><type>sampler2D</type><name>uSpecularPower</name><value>)"
 			+ exporter.texrpath + shininessTex
-			+ R"(</value></input>)" + "\n"
-			+ "\t\t\t\t" 
-			+ R"(<input><type>float</type><name>MAX_SPECULAR_POWER</name>)"
-			+ R"(<value>120.0</value><const>1</const></input>)");
+			+ R"(</value></input>)");
 
 		materialStr = replaceAllString(materialStr, "%specularPowerValue%", 
 			exporter.texrpath + shininessTex);
 
-			static const char* multiply = R"(
-				<operation>
-					<id>61</id>
-					<returnType>float</returnType>
-					<function>mul</function>
-					<arguments>
-						<argument>MAX_SPECULAR_POWER</argument>
-						<argument>out60</argument>
-					</arguments>
-				</operation>)";
-
-		std::string func = std::string(readRFromTextureTemplate) + multiply;
-
 		materialStr = replaceAllString(materialStr, "%specularPowerFunc%", 
-			func);
+			readRFromTextureTemplate);
 
 		materialStr = replaceAllString(materialStr, "%id%", 
 			"60");
@@ -482,12 +532,21 @@ void exportMaterial(
 			"uSpecularPower");
 
 		materialStr = replaceAllString(materialStr, "%specularPowerArg%", 
-			"out61");
+			"out60");
 	}
 	else
 	{
 		float shininess = 0.0;
 		mtl.Get(AI_MATKEY_SHININESS, shininess);
+		//shininess = std::min(128.0f, shininess)  128.0;
+		const int MAX_SHININESS = 511.0;
+		if(shininess > MAX_SHININESS)
+		{
+			LOGW("Shininness exceeds %d\n", MAX_SHININESS);
+		}
+
+		shininess = shininess / MAX_SHININESS;
+
 		materialStr = replaceAllString(materialStr, "%specularPowerInput%", 
 			R"(<input><type>float</type><name>uSpecularPower</name><value>)"
 			+ std::to_string(shininess)
@@ -510,6 +569,8 @@ void exportMaterial(
 	// Replace texture extensions with .anki
 	materialStr = replaceAllString(materialStr, ".tga", ".ankitex");
 	materialStr = replaceAllString(materialStr, ".png", ".ankitex");
+	materialStr = replaceAllString(materialStr, ".jpg", ".ankitex");
+	materialStr = replaceAllString(materialStr, ".jpeg", ".ankitex");
 
 	// Open and write file
 	std::fstream file;

+ 0 - 49
tools/scene/templates/diffFrag.h

@@ -1,49 +0,0 @@
-R"(		<program>
-			<type>frag</type>
-			<includes>
-				<include>shaders/MsCommonFrag.glsl</include>
-			</includes>
-
-			<inputs>
-				<input><type>float</type><name>uSpecularColor</name><value>%specularColor%</value></input>
-				<input><type>float</type><name>uSpecularPower</name><value>%specularPower%</value></input>
-				<input><type>float</type><name>uBlurring</name><value>0.0</value><const>1</const></input>
-				<input><type>sampler2D</type><name>uDiffuseMap</name><value>%diffuseMap%</value></input>
-			</inputs>
-			
-			<operations>
-				<operation>
-					<id>0</id>
-					<returnType>vec2</returnType>
-					<function>getTextureCoord</function>
-				</operation>
-				<operation>
-					<id>1</id>
-					<returnType>vec3</returnType>
-					<function>getNormal</function>
-				</operation>
-
-				<operation>
-					<id>10</id>
-					<returnType>vec3</returnType>
-					<function>readRgbFromTexture</function>
-					<arguments>
-						<argument>uDiffuseMap</argument>
-						<argument>out0</argument>
-					</arguments>
-				</operation>
-
-				<operation>
-					<id>20</id>
-					<returnType>void</returnType>
-					<function>writeRts</function>
-					<arguments>
-						<argument>out10</argument>
-						<argument>out1</argument>
-						<argument>uSpecularColor</argument>
-						<argument>uSpecularPower</argument>
-						<argument>uBlurring</argument>
-					</arguments>
-				</operation>
-			</operations>
-		</program>)"

+ 0 - 67
tools/scene/templates/diffNormFrag.h

@@ -1,67 +0,0 @@
-R"(		<program>
-			<type>frag</type>
-
-			<includes>
-				<include>shaders/MsCommonFrag.glsl</include>
-			</includes>
-
-			<inputs>
-				<input><type>float</type><name>uSpecularColor</name><value>%specularColor%</value></input>
-				<input><type>float</type><name>uSpecularPower</name><value>%specularPower%</value></input>
-				<input><type>float</type><name>uBlurring</name><value>0.0</value><const>1</const></input>
-				<input><type>sampler2D</type><name>uDiffuseMap</name><value>%diffuseMap%</value></input>
-				<input><type>sampler2D</type><name>uNormalMap</name><value>%normalMap%</value></input>
-			</inputs>
-			
-			<operations>
-				<operation>
-					<id>0</id>
-					<returnType>vec3</returnType>
-					<function>getNormal</function>
-				</operation>
-				<operation>
-					<id>1</id>
-					<returnType>vec4</returnType>
-					<function>getTangent</function>
-				</operation>
-				<operation>
-					<id>2</id>
-					<returnType>vec2</returnType>
-					<function>getTextureCoord</function>
-				</operation>
-
-				<operation>
-					<id>10</id>
-					<returnType>vec3</returnType>
-					<function>readRgbFromTexture</function>
-					<arguments>
-						<argument>uDiffuseMap</argument>
-						<argument>out2</argument>
-					</arguments>
-				</operation>
-				<operation>
-					<id>20</id>
-					<returnType>vec3</returnType>
-					<function>readNormalFromTexture</function>
-					<arguments>
-						<argument>out0</argument>
-						<argument>out1</argument>
-						<argument>uNormalMap</argument>
-						<argument>out2</argument>
-					</arguments>
-				</operation>
-				<operation>
-					<id>30</id>
-					<returnType>void</returnType>
-					<function>writeRts</function>
-					<arguments>
-						<argument>out10</argument>
-						<argument>out20</argument>
-						<argument>uSpecularColor</argument>
-						<argument>uSpecularPower</argument>
-						<argument>uBlurring</argument>
-					</arguments>
-				</operation>
-			</operations>
-		</program>)"
-

+ 6 - 27
tools/scene/templates/diffNormSpecFrag.h

@@ -9,8 +9,8 @@ R"(		<program>
 				%specularColorInput%
 				%specularPowerInput%
 				<input><type>float</type><name>uBlurring</name><value>0.0</value><const>1</const></input>
-				<input><type>sampler2D</type><name>uDiffuseMap</name><value>%diffuseMap%</value></input>
-				<input><type>sampler2D</type><name>uNormalMap</name><value>%normalMap%</value></input>
+				%diffuseColorInput%
+				%normalInput%
 			</inputs>
 			
 			<operations>
@@ -29,38 +29,17 @@ R"(		<program>
 					<returnType>vec2</returnType>
 					<function>getTextureCoord</function>
 				</operation>
-
-				<operation>
-					<id>10</id>
-					<returnType>vec3</returnType>
-					<function>readRgbFromTexture</function>
-					<arguments>
-						<argument>uDiffuseMap</argument>
-						<argument>out2</argument>
-					</arguments>
-				</operation>
-				<operation>
-					<id>20</id>
-					<returnType>vec3</returnType>
-					<function>readNormalFromTexture</function>
-					<arguments>
-						<argument>out0</argument>
-						<argument>out1</argument>
-						<argument>uNormalMap</argument>
-						<argument>out2</argument>
-					</arguments>
-				</operation>
-
+				%diffuseColorFunc%
+				%normalFunc%
 				%specularColorFunc%
 				%specularPowerFunc%
-				
 				<operation>
 					<id>100</id>
 					<returnType>void</returnType>
 					<function>writeRts</function>
 					<arguments>
-						<argument>out10</argument>
-						<argument>out20</argument>
+						<argument>%diffuseColorArg%</argument>
+						<argument>%normalArg%</argument>
 						<argument>%specularColorArg%</argument>
 						<argument>%specularPowerArg%</argument>
 						<argument>uBlurring</argument>