Commits

Jason McKesson committed ec2af32

Tut17: Full scene for double projection.

Comments (0)

Files changed (8)

Tut 17 Spotlight on Textures/Double Projection.cpp

 {
 	glm::vec3(0.0f, 0.0f, 0.0f),
 	glm::fquat(0.98481f, 0.173648f, 0.0f, 0.0f),
-	15.0f,
+	25.0f,
 	0.0f
 };
 
 GLint g_unlitObjectColorUnif;
 GLuint g_unlitProg;
 Framework::Mesh *g_pSphereMesh = NULL;
+glm::fquat g_spinBarOrient;
 
 void LoadAndSetupScene()
 {
 	std::auto_ptr<Framework::Scene> pScene(new Framework::Scene("dp_scene.xml"));
 
 	std::vector<Framework::NodeRef> nodes;
-	nodes.push_back(pScene->FindNode("blueObj"));
-	nodes.push_back(pScene->FindNode("redObj"));
+	nodes.push_back(pScene->FindNode("cube"));
+	nodes.push_back(pScene->FindNode("rightBar"));
+	nodes.push_back(pScene->FindNode("leaningBar"));
+	nodes.push_back(pScene->FindNode("spinBar"));
+
+	AssociateUniformWithNodes(nodes, g_lightNumBinder, "numberOfLights");
+	SetStateBinderWithNodes(nodes, g_lightNumBinder);
+
+	g_stoneTexBinder.SetTexture(0, GL_TEXTURE_2D, g_stoneTex, g_samplers[1]);
+	nodes[0].SetStateBinder(&g_stoneTexBinder);
+	nodes[1].SetStateBinder(&g_stoneTexBinder);
+	nodes[2].SetStateBinder(&g_stoneTexBinder);
+	nodes[3].SetStateBinder(&g_stoneTexBinder);
 
 	GLuint unlit = pScene->FindProgram("p_unlit");
 	Framework::Mesh *pSphereMesh = pScene->FindMesh("m_sphere");
 
-	g_lightNumBinder.AssociateWithProgram(nodes[0].GetProgram(), "numberOfLights");
-	g_lightNumBinder.AssociateWithProgram(nodes[1].GetProgram(), "numberOfLights");
-	nodes[0].SetStateBinder(&g_lightNumBinder);
-	nodes[1].SetStateBinder(&g_lightNumBinder);
-
-	g_blueBinder.AssociateWithProgram(nodes[0].GetProgram(), "objectColor");
-	g_blueBinder.SetValue(glm::vec4(0.3f, 0.3f, 1.0f, 1.0f));
-	nodes[0].SetStateBinder(&g_blueBinder);
-	g_redBinder.AssociateWithProgram(nodes[1].GetProgram(), "objectColor");
-	g_redBinder.SetValue(glm::vec4(1.0f, 0.1f, 0.1f, 1.0f));
-	nodes[1].SetStateBinder(&g_redBinder);
-
-	g_stoneTexBinder.SetTexture(0, GL_TEXTURE_2D, g_stoneTex, g_samplers[1]);
-	nodes[0].SetStateBinder(&g_stoneTexBinder);
-	nodes[1].SetStateBinder(&g_stoneTexBinder);
-
 	//No more things that can throw.
+	g_spinBarOrient = nodes[3].NodeGetOrient();
 	g_unlitProg = unlit;
 	g_unlitModelToCameraMatrixUnif = glGetUniformLocation(unlit, "modelToCameraMatrix");
 	g_unlitObjectColorUnif = glGetUniformLocation(unlit, "objectColor");
 		0, sizeof(LightBlock));
 
 	glBindBuffer(GL_UNIFORM_BUFFER, 0);
-
 }
 
 using Framework::Timer;
 	lightData.maxIntensity = 3.0f;
 	lightData.lights[0].lightIntensity = glm::vec4(2.0, 2.0, 2.5, 1.0);
 	lightData.lights[0].cameraSpaceLightPos = camMatrix *
-		glm::normalize(glm::vec4(0.0f, 0.5f, 0.5f, 0.0f));
+		glm::normalize(glm::vec4(-0.2f, 0.5f, 0.5f, 0.0f));
 	lightData.lights[1].lightIntensity = glm::vec4(3.5, 6.5, 3.0, 1.0);
 	lightData.lights[1].cameraSpaceLightPos = camMatrix *
 		glm::vec4(5.0f, 6.0f, 0.5f, 1.0f);
 	g_nodes[0].NodeSetOrient(glm::rotate(glm::fquat(),
 		360.0f * g_timer.GetAlpha(), glm::vec3(0.0f, 1.0f, 0.0f)));
 
+	g_nodes[3].NodeSetOrient(g_spinBarOrient * glm::rotate(glm::fquat(),
+		360.0f * g_timer.GetAlpha(), glm::vec3(0.0f, 0.0f, 1.0f)));
+
 	glm::ivec2 displaySize(g_displayWidth / 2, g_displayHeight);
 
 	{

Tut 17 Spotlight on Textures/data/GenBars.lua

+require "XmlWriter"
+require "vmath"
+
+local function GenStringFromArray(theArray, bAsInt)
+	local array = {" "}
+	for i, vector in ipairs(theArray) do
+		local elements = vector;
+		if(bAsInt) then
+			elements = {};
+			for i, value in ipairs(vector) do
+				elements[#elements + 1] = string.format("%i", value);
+			end
+		end
+		
+		array[#array + 1] = "        " .. table.concat(vector, " ");
+	end
+	
+	return table.concat(array, "\n");
+end
+
+local function MakeBar(length)
+	local bar = {}
+	
+	bar.positions =
+	{
+		--Front
+		vmath.vec3( 0.5,  0.5,  0.5 * length),
+		vmath.vec3( 0.5, -0.5,  0.5 * length),
+		vmath.vec3(-0.5, -0.5,  0.5 * length),
+		vmath.vec3(-0.5,  0.5,  0.5 * length),
+
+		--Top
+		vmath.vec3( 0.5,  0.5,  0.5 * length),
+		vmath.vec3(-0.5,  0.5,  0.5 * length),
+		vmath.vec3(-0.5,  0.5, -0.5 * length),
+		vmath.vec3( 0.5,  0.5, -0.5 * length),
+
+		--Right
+		vmath.vec3( 0.5,  0.5,  0.5 * length),
+		vmath.vec3( 0.5,  0.5, -0.5 * length),
+		vmath.vec3( 0.5, -0.5, -0.5 * length),
+		vmath.vec3( 0.5, -0.5,  0.5 * length),
+
+		--Back
+		vmath.vec3( 0.5,  0.5, -0.5 * length),
+		vmath.vec3(-0.5,  0.5, -0.5 * length),
+		vmath.vec3(-0.5, -0.5, -0.5 * length),
+		vmath.vec3( 0.5, -0.5, -0.5 * length),
+
+		--Bottom
+		vmath.vec3( 0.5, -0.5,  0.5 * length),
+		vmath.vec3( 0.5, -0.5, -0.5 * length),
+		vmath.vec3(-0.5, -0.5, -0.5 * length),
+		vmath.vec3(-0.5, -0.5,  0.5 * length),
+
+		--Left
+		vmath.vec3(-0.5,  0.5,  0.5 * length),
+		vmath.vec3(-0.5, -0.5,  0.5 * length),
+		vmath.vec3(-0.5, -0.5, -0.5 * length),
+		vmath.vec3(-0.5,  0.5, -0.5 * length),
+	};
+
+	bar.normals =
+	{
+		--Front
+		vmath.vec3(0.0,  0.0,  1.0),
+		vmath.vec3(0.0,  0.0,  1.0),
+		vmath.vec3(0.0,  0.0,  1.0),
+		vmath.vec3(0.0,  0.0,  1.0),
+
+		--Top
+		vmath.vec3(0.0,  1.0,  0.0),
+		vmath.vec3(0.0,  1.0,  0.0),
+		vmath.vec3(0.0,  1.0,  0.0),
+		vmath.vec3(0.0,  1.0,  0.0),
+
+		--Right
+		vmath.vec3(1.0,  0.0,  0.0),
+		vmath.vec3(1.0,  0.0,  0.0),
+		vmath.vec3(1.0,  0.0,  0.0),
+		vmath.vec3(1.0,  0.0,  0.0),
+
+		--Back
+		vmath.vec3(0.0,  0.0, -1.0),
+		vmath.vec3(0.0,  0.0, -1.0),
+		vmath.vec3(0.0,  0.0, -1.0),
+		vmath.vec3(0.0,  0.0, -1.0),
+
+		--Bottom
+		vmath.vec3(0.0, -1.0,  0.0),
+		vmath.vec3(0.0, -1.0,  0.0),
+		vmath.vec3(0.0, -1.0,  0.0),
+		vmath.vec3(0.0, -1.0,  0.0),
+
+		--Left
+		vmath.vec3(-1.0,  0.0,  0.0),
+		vmath.vec3(-1.0,  0.0,  0.0),
+		vmath.vec3(-1.0,  0.0,  0.0),
+		vmath.vec3(-1.0,  0.0,  0.0),
+	}
+
+	bar.texCoords =
+	{
+		--Front
+		vmath.vec2(1.0, 1.0),
+		vmath.vec2(1.0, 0.0),
+		vmath.vec2(0.0, 0.0),
+		vmath.vec2(0.0, 1.0),
+
+		--Top
+		vmath.vec2(1.0 * length, 1.0),
+		vmath.vec2(1.0 * length, 0.0),
+		vmath.vec2(0.0 * length, 0.0),
+		vmath.vec2(0.0 * length, 1.0),
+
+		--Right
+		vmath.vec2(0.0 * length, 1.0),
+		vmath.vec2(1.0 * length, 1.0),
+		vmath.vec2(1.0 * length, 0.0),
+		vmath.vec2(0.0 * length, 0.0),
+		
+		--Back
+		vmath.vec2(0.0, 1.0),
+		vmath.vec2(1.0, 1.0),
+		vmath.vec2(1.0, 0.0),
+		vmath.vec2(0.0, 0.0),
+		
+		--Bottom
+		vmath.vec2(1.0, 1.0 * length),
+		vmath.vec2(1.0, 0.0 * length),
+		vmath.vec2(0.0, 0.0 * length),
+		vmath.vec2(0.0, 1.0 * length),
+
+		--Left
+		vmath.vec2(1.0 * length, 1.0),
+		vmath.vec2(1.0 * length, 0.0),
+		vmath.vec2(0.0 * length, 0.0),
+		vmath.vec2(0.0 * length, 1.0),
+	}
+
+	bar.indices =
+	{
+		vmath.vec3(0, 1, 2),
+		vmath.vec3(2, 3, 0),
+
+		vmath.vec3(4, 5, 6),
+		vmath.vec3(6, 7, 4),
+
+		vmath.vec3(8, 9, 10),
+		vmath.vec3(10, 11, 8),
+
+		vmath.vec3(12, 13, 14),
+		vmath.vec3(14, 15, 12),
+
+		vmath.vec3(16, 17, 18),
+		vmath.vec3(18, 19, 16),
+
+		vmath.vec3(20, 21, 22),
+		vmath.vec3(22, 23, 20),
+	}
+	
+	return bar;
+end
+
+local function WriteVAO(writer, name, ...)
+	local attribs = {...}
+	
+	writer:PushElement("vao");
+		writer:AddAttribute("name", name);
+		for i, attrib in ipairs(attribs) do
+			writer:PushElement("source");
+			writer:AddAttribute("attrib", tostring(attrib));
+			writer:PopElement();
+		end
+	writer:PopElement();
+end
+
+local function WriteBar(filename, bar)
+	if(type(bar) == "number") then
+		bar = MakeBar(bar);
+	end
+
+	do
+		local writer = XmlWriter.XmlWriter(filename);
+		writer:AddPI("oxygen", [[RNGSchema="../../Documents/meshFormat.rnc" type="compact"]]);
+		writer:PushElement("mesh", "http://www.arcsynthesis.com/gltut/mesh");
+			writer:PushElement("attribute");
+				writer:AddAttribute("index", "0");
+				writer:AddAttribute("type", "float");
+				writer:AddAttribute("size", "3");
+				writer:AddText(GenStringFromArray(bar.positions));
+			writer:PopElement();
+			writer:PushElement("attribute");
+				writer:AddAttribute("index", "2");
+				writer:AddAttribute("type", "float");
+				writer:AddAttribute("size", "3");
+				writer:AddText(GenStringFromArray(bar.normals));
+			writer:PopElement();
+			writer:PushElement("attribute");
+				writer:AddAttribute("index", "5");
+				writer:AddAttribute("type", "float");
+				writer:AddAttribute("size", "2");
+				writer:AddText(GenStringFromArray(bar.texCoords));
+			writer:PopElement();
+			WriteVAO(writer, "lit", 0, 2);
+			WriteVAO(writer, "lit-tex", 0, 2, 5);
+			WriteVAO(writer, "tex", 0, 5);
+			WriteVAO(writer, "flat", 0);
+			writer:PushElement("indices");
+				writer:AddAttribute("cmd", "triangles");
+				writer:AddAttribute("type", "ushort");
+				writer:AddText(GenStringFromArray(bar.indices, true));
+			writer:PopElement();
+		writer:PopElement();
+		writer:Close();
+	end
+end
+
+WriteBar("ShortBar.xml", 3);
+WriteBar("LongBar.xml", 9);
+

Tut 17 Spotlight on Textures/data/LongBar.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<?oxygen RNGSchema="../../Documents/meshFormat.rnc" type="compact"?>
+
+<mesh xmlns="http://www.arcsynthesis.com/gltut/mesh" >
+	<attribute index="0" type="float" size="3" > 
+        0.5 0.5 4.5
+        0.5 -0.5 4.5
+        -0.5 -0.5 4.5
+        -0.5 0.5 4.5
+        0.5 0.5 4.5
+        -0.5 0.5 4.5
+        -0.5 0.5 -4.5
+        0.5 0.5 -4.5
+        0.5 0.5 4.5
+        0.5 0.5 -4.5
+        0.5 -0.5 -4.5
+        0.5 -0.5 4.5
+        0.5 0.5 -4.5
+        -0.5 0.5 -4.5
+        -0.5 -0.5 -4.5
+        0.5 -0.5 -4.5
+        0.5 -0.5 4.5
+        0.5 -0.5 -4.5
+        -0.5 -0.5 -4.5
+        -0.5 -0.5 4.5
+        -0.5 0.5 4.5
+        -0.5 -0.5 4.5
+        -0.5 -0.5 -4.5
+        -0.5 0.5 -4.5</attribute>
+	<attribute index="2" type="float" size="3" > 
+        0 0 1
+        0 0 1
+        0 0 1
+        0 0 1
+        0 1 0
+        0 1 0
+        0 1 0
+        0 1 0
+        1 0 0
+        1 0 0
+        1 0 0
+        1 0 0
+        0 0 -1
+        0 0 -1
+        0 0 -1
+        0 0 -1
+        0 -1 0
+        0 -1 0
+        0 -1 0
+        0 -1 0
+        -1 0 0
+        -1 0 0
+        -1 0 0
+        -1 0 0</attribute>
+	<attribute index="5" type="float" size="2" > 
+        1 1
+        1 0
+        0 0
+        0 1
+        9 1
+        9 0
+        0 0
+        0 1
+        0 1
+        9 1
+        9 0
+        0 0
+        0 1
+        1 1
+        1 0
+        0 0
+        1 9
+        1 0
+        0 0
+        0 9
+        9 1
+        9 0
+        0 0
+        0 1</attribute>
+	<vao name="lit" >
+		<source attrib="0" />
+		<source attrib="2" />
+	</vao>
+	<vao name="lit-tex" >
+		<source attrib="0" />
+		<source attrib="2" />
+		<source attrib="5" />
+	</vao>
+	<vao name="tex" >
+		<source attrib="0" />
+		<source attrib="5" />
+	</vao>
+	<vao name="flat" >
+		<source attrib="0" />
+	</vao>
+	<indices cmd="triangles" type="ushort" > 
+        0 1 2
+        2 3 0
+        4 5 6
+        6 7 4
+        8 9 10
+        10 11 8
+        12 13 14
+        14 15 12
+        16 17 18
+        18 19 16
+        20 21 22
+        22 23 20</indices>
+</mesh>

Tut 17 Spotlight on Textures/data/ShortBar.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<?oxygen RNGSchema="../../Documents/meshFormat.rnc" type="compact"?>
+
+<mesh xmlns="http://www.arcsynthesis.com/gltut/mesh" >
+	<attribute index="0" type="float" size="3" > 
+        0.5 0.5 1.5
+        0.5 -0.5 1.5
+        -0.5 -0.5 1.5
+        -0.5 0.5 1.5
+        0.5 0.5 1.5
+        -0.5 0.5 1.5
+        -0.5 0.5 -1.5
+        0.5 0.5 -1.5
+        0.5 0.5 1.5
+        0.5 0.5 -1.5
+        0.5 -0.5 -1.5
+        0.5 -0.5 1.5
+        0.5 0.5 -1.5
+        -0.5 0.5 -1.5
+        -0.5 -0.5 -1.5
+        0.5 -0.5 -1.5
+        0.5 -0.5 1.5
+        0.5 -0.5 -1.5
+        -0.5 -0.5 -1.5
+        -0.5 -0.5 1.5
+        -0.5 0.5 1.5
+        -0.5 -0.5 1.5
+        -0.5 -0.5 -1.5
+        -0.5 0.5 -1.5</attribute>
+	<attribute index="2" type="float" size="3" > 
+        0 0 1
+        0 0 1
+        0 0 1
+        0 0 1
+        0 1 0
+        0 1 0
+        0 1 0
+        0 1 0
+        1 0 0
+        1 0 0
+        1 0 0
+        1 0 0
+        0 0 -1
+        0 0 -1
+        0 0 -1
+        0 0 -1
+        0 -1 0
+        0 -1 0
+        0 -1 0
+        0 -1 0
+        -1 0 0
+        -1 0 0
+        -1 0 0
+        -1 0 0</attribute>
+	<attribute index="5" type="float" size="2" > 
+        1 1
+        1 0
+        0 0
+        0 1
+        3 1
+        3 0
+        0 0
+        0 1
+        0 1
+        3 1
+        3 0
+        0 0
+        0 1
+        1 1
+        1 0
+        0 0
+        1 3
+        1 0
+        0 0
+        0 3
+        3 1
+        3 0
+        0 0
+        0 1</attribute>
+	<vao name="lit" >
+		<source attrib="0" />
+		<source attrib="2" />
+	</vao>
+	<vao name="lit-tex" >
+		<source attrib="0" />
+		<source attrib="2" />
+		<source attrib="5" />
+	</vao>
+	<vao name="tex" >
+		<source attrib="0" />
+		<source attrib="5" />
+	</vao>
+	<vao name="flat" >
+		<source attrib="0" />
+	</vao>
+	<indices cmd="triangles" type="ushort" > 
+        0 1 2
+        2 3 0
+        4 5 6
+        6 7 4
+        8 9 10
+        10 11 8
+        12 13 14
+        14 15 12
+        16 17 18
+        18 19 16
+        20 21 22
+        22 23 20</indices>
+</mesh>

Tut 17 Spotlight on Textures/data/dp_scene.xml

 <scene xmlns="http://www.arcsynthesis.com/gltut/scene">
     <mesh xml:id="m_sphere" file="UnitSphere.xml"/>
     <mesh xml:id="m_cube" file="UnitCube.xml"/>
+	<mesh xml:id="m_shortBar" file="ShortBar.xml"/>
+	<mesh xml:id="m_longBar" file="LongBar.xml"/>
     <prog
         xml:id="p_unlit"
         vert="Unlit.vert"
         <sampler name="diffuseColorTex" unit="0"/>
     </prog>
     <node
-        name="blueObj"
+        name="cube"
         mesh="m_cube"
         prog="p_lit"
         pos="0 1 0"
         orient="0.0 0.0 0.0 1.0"
         scale="3"/>
     <node
-        name="redObj"
-        mesh="m_cube"
+        name="rightBar"
+        mesh="m_shortBar"
         prog="p_lit"
-        pos="7 0 0"
+        pos="13 -2 0"
         orient="0.0 0.0 0.0 1.0"
-        scale="3 3 9"/>
-    
+        scale="3"/>
+    <node
+        name="leaningBar"
+        mesh="m_longBar"
+        prog="p_lit"
+        pos="3 -7 -10"
+        orient="0.64278 0 0 0.76604"
+        scale="5"/>
+    <node
+        name="spinBar"
+        mesh="m_longBar"
+        prog="p_lit"
+        pos="-7 0 8"
+        orient="-0.148446 0.554035 0.212003 0.791242"
+        scale="4"/>
 </scene>

framework/Scene.cpp

 			m_nodeTm.m_orient = orient;
 		}
 
+		glm::fquat NodeGetOrient() const {return m_nodeTm.m_orient;}
+
 		void SetNodeOrient(const glm::fquat &nodeOrient)
 		{
 			m_nodeTm.m_orient = glm::normalize(nodeOrient);
 		m_pNode->NodeSetOrient(orient);
 	}
 
+	glm::fquat NodeRef::NodeGetOrient() const
+	{
+		return m_pNode->NodeGetOrient();
+	}
+
 	void NodeRef::NodeOffset( const glm::vec3 &offset )
 	{
 		m_pNode->NodeOffset(offset);

framework/Scene.h

 		//Sets the current orientation to the given one.
 		void NodeSetOrient(const glm::fquat &orient);
 
+		glm::fquat NodeGetOrient() const;
+
 		//Adds the offset to the current translation.
 		void NodeOffset(const glm::vec3 &offset);
 		//Sets the current translation to the given one.

framework/SceneBinders.h

 #include <map>
 #include <glm/glm.hpp>
 #include <glm/gtc/type_ptr.hpp>
+#include "Scene.h"
 
 namespace Framework
 {
 		GLsizeiptr m_buffSize;
 	};
 
+	template<typename NodeForwardIterator, typename AssociationBinder>
+	void AssociateUniformWithNodes(NodeForwardIterator start, NodeForwardIterator final,
+		AssociationBinder &binder, const std::string &unifName)
+	{
+		for(NodeForwardIterator currIt = start; currIt != final; ++currIt)
+		{
+			binder.AssociateWithProgram(currIt->GetProgram(), unifName);
+		}
+	}
 
+	template<typename NodeForwardRange>
+	void AssociateUniformWithNodes(const NodeForwardRange &range,
+		UniformBinderBase &binder, const std::string &unifName)
+	{
+		AssociateUniformWithNodes(range.begin(), range.end(), binder, unifName);
+	}
+
+	template<typename NodeForwardIterator>
+	void SetStateBinderWithNodes(NodeForwardIterator start, NodeForwardIterator final,
+		StateBinder &binder)
+	{
+		for(NodeForwardIterator currIt = start; currIt != final; ++currIt)
+		{
+			currIt->SetStateBinder(&binder);
+		}
+	}
+
+	template<typename NodeForwardRange>
+	void SetStateBinderWithNodes(NodeForwardRange &range,
+		StateBinder &binder)
+	{
+		SetStateBinderWithNodes(range.begin(), range.end(), binder);
+	}
 }