Commits

Jason McKesson  committed c2656a2

Tut17: Textured objects.

  • Participants
  • Parent commits 855ad5e

Comments (0)

Files changed (8)

 
 The following files are copywritten and distributed under the Creative Commons Attribution 3.0 Unported (CC BY 3.0) license, as described in the "./CC BY 3.0 legalcode.txt" file. Attribution for these works is presented here:
 
+Attributed to Etory, of OpenGameArt.org:
+* data/seamless_rock1_small.dds
 
-
-

File Tut 17 Spotlight on Textures/Double Projection.cpp

 
 GLuint g_projectionUniformBuffer = 0;
 GLuint g_lightUniformBuffer = 0;
+GLuint g_stoneTex;
 
 const int NUM_SAMPLERS = 2;
 GLuint g_samplers[NUM_SAMPLERS];
 {
 	try
 	{
-/*
-		std::string filename(Framework::FindFileOrThrow("terrain_tex.dds"));
+		std::string filename(Framework::FindFileOrThrow("seamless_rock1_small.dds"));
 
 		std::auto_ptr<glimg::ImageSet> pImageSet(glimg::loaders::dds::LoadFromFile(filename.c_str()));
-
-		glGenTextures(1, &g_linearTexture);
-		glBindTexture(GL_TEXTURE_2D, g_linearTexture);
-
-		glimg::OpenGLPixelTransferParams xfer = glimg::GetUploadFormatType(pImageSet->GetFormat(), 0);
-
-		for(int mipmapLevel = 0; mipmapLevel < pImageSet->GetMipmapCount(); mipmapLevel++)
-		{
-			glimg::SingleImage image = pImageSet->GetImage(mipmapLevel, 0, 0);
-			glimg::Dimensions dims = image.GetDimensions();
-
-			glTexImage2D(GL_TEXTURE_2D, mipmapLevel, GL_SRGB8_ALPHA8, dims.width, dims.height, 0,
-				xfer.format, xfer.type, image.GetImageData());
-		}
-
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, pImageSet->GetMipmapCount() - 1);
-
-		glBindTexture(GL_TEXTURE_2D, 0);
-*/
+		g_stoneTex = glimg::CreateTexture(pImageSet.get(), 0);
 	}
 	catch(std::exception &e)
 	{
 Framework::UniformVec4Binder g_blueBinder;
 Framework::UniformVec4Binder g_redBinder;
 Framework::UniformIntBinder g_lightNumBinder;
+Framework::TextureBinder g_stoneTexBinder;
 
 GLint g_unlitModelToCameraMatrixUnif;
 GLint g_unlitObjectColorUnif;
 	std::auto_ptr<Framework::Scene> pScene(new Framework::Scene("dp_scene.xml"));
 
 	std::vector<Framework::NodeRef> nodes;
-	nodes.push_back(pScene->FindNode("blueSphere"));
-	nodes.push_back(pScene->FindNode("redSphere"));
+	nodes.push_back(pScene->FindNode("blueObj"));
+	nodes.push_back(pScene->FindNode("redObj"));
 
 	GLuint unlit = pScene->FindProgram("p_unlit");
 	Framework::Mesh *pSphereMesh = pScene->FindMesh("m_sphere");
 	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_unlitProg = unlit;
 	g_unlitModelToCameraMatrixUnif = glGetUniformLocation(unlit, "modelToCameraMatrix");
 	glBindBufferRange(GL_UNIFORM_BUFFER, g_projectionBlockIndex, g_projectionUniformBuffer,
 		0, sizeof(ProjectionBlock));
 
+	CreateSamplers();
+	LoadTextures();
+
 	try
 	{
 		LoadAndSetupScene();
 
 	glBindBuffer(GL_UNIFORM_BUFFER, 0);
 
-//	LoadTextures();
-//	CreateSamplers();
 }
 
 using Framework::Timer;
 	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));
-	lightData.lights[1].lightIntensity = glm::vec4(7.0, 9.0, 6.5, 1.0);
+	lightData.lights[1].lightIntensity = glm::vec4(3.5, 6.5, 3.0, 1.0);
 	lightData.lights[1].cameraSpaceLightPos = camMatrix *
-		glm::vec4(5.0f, 4.0f, 0.5f, 1.0f);
+		glm::vec4(5.0f, 6.0f, 0.5f, 1.0f);
 
 	g_lightNumBinder.SetValue(2);
 

File Tut 17 Spotlight on Textures/data/dp_scene.xml

         normal-model-to-camera="normalModelToCameraMatrix">
         <block name="Projection" binding="0"/>
 		<block name="Light" binding="1"/>
+        <sampler name="diffuseColorTex" unit="0"/>
     </prog>
     <node
-        name="blueSphere"
+        name="blueObj"
         mesh="m_cube"
         prog="p_lit"
         pos="0 1 0"
         orient="0.0 0.0 0.0 1.0"
         scale="3"/>
     <node
-        name="redSphere"
+        name="redObj"
         mesh="m_cube"
         prog="p_lit"
         pos="7 0 0"

File Tut 17 Spotlight on Textures/data/litTexture.frag

 #version 330
 
-//in vec2 colorCoord;
+in vec2 colorCoord;
 in vec3 cameraSpacePosition;
 in vec3 cameraSpaceNormal;
 
 	vec4 lightIntensity;
 };
 
-uniform vec4 objectColor;
-
 uniform Light
 {
 	vec4 ambientIntensity;
 	return lighting;
 }
 
-//uniform sampler2D diffuseColorTex;
+uniform sampler2D diffuseColorTex;
 
 void main()
 {
-//	vec4 diffuseColor = texture(diffuseColorTex, colorCoord);
+	vec4 diffuseColor = texture(diffuseColorTex, colorCoord);
 
 	PerLight currLight;
 	currLight.cameraSpaceLightPos = normalize(vec4(0.0, 0.5, 0.5, 0.0));
 	currLight.lightIntensity = vec4(2.0, 2.0, 2.5, 1.0);
 	
-	vec4 accumLighting = objectColor * Lgt.ambientIntensity;
+	vec4 accumLighting = diffuseColor * Lgt.ambientIntensity;
 	for(int light = 0; light < numberOfLights; light++)
 	{
-		accumLighting += ComputeLighting(objectColor, Lgt.lights[light]);
+		accumLighting += ComputeLighting(diffuseColor, Lgt.lights[light]);
 	}
 	
 	outputColor = accumLighting / Lgt.maxIntensity;

File Tut 17 Spotlight on Textures/data/litTexture.vert

 
 layout(location = 0) in vec3 position;
 layout(location = 2) in vec3 normal;
-//layout(location = 5) in vec2 texCoord;
+layout(location = 5) in vec2 texCoord;
 
-//out vec2 colorCoord;
+out vec2 colorCoord;
 out vec3 cameraSpacePosition;
 out vec3 cameraSpaceNormal;
 
 {
 	cameraSpacePosition = (modelToCameraMatrix * vec4(position, 1.0)).xyz;
 	gl_Position = cameraToClipMatrix * vec4(cameraSpacePosition, 1.0);
-	//Assume the modelToCameraMatrix contains no scaling.
 	cameraSpaceNormal = normalize(normalModelToCameraMatrix * normal);
-//	colorCoord = texCoord;
+
+	colorCoord = texCoord;
 }

File data/GenCube.lua

 	vmath.vec3(-0.5,  0.5, -0.5),
 	vmath.vec3( 0.5,  0.5, -0.5),
 
-	--Left
+	--Right
 	vmath.vec3( 0.5,  0.5,  0.5),
 	vmath.vec3( 0.5,  0.5, -0.5),
 	vmath.vec3( 0.5, -0.5, -0.5),
 	vmath.vec3(-0.5, -0.5, -0.5),
 	vmath.vec3(-0.5, -0.5,  0.5),
 
-	--Right
+	--Left
 	vmath.vec3(-0.5,  0.5,  0.5),
 	vmath.vec3(-0.5, -0.5,  0.5),
 	vmath.vec3(-0.5, -0.5, -0.5),
 	vmath.vec3(0.0,  1.0,  0.0),
 	vmath.vec3(0.0,  1.0,  0.0),
 
-	--Left
+	--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(0.0, -1.0,  0.0),
 	vmath.vec3(0.0, -1.0,  0.0),
 
-	--Right
+	--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.vec4(1.0, 0.5, 1.0, 1.0),
 }
 
+local texCoords =
+{
+	vmath.vec2(1.0, 1.0),
+	vmath.vec2(1.0, 0.0),
+	vmath.vec2(0.0, 0.0),
+	vmath.vec2(0.0, 1.0),
+
+	vmath.vec2(1.0, 1.0),
+	vmath.vec2(1.0, 0.0),
+	vmath.vec2(0.0, 0.0),
+	vmath.vec2(0.0, 1.0),
+
+	vmath.vec2(0.0, 1.0),
+	vmath.vec2(1.0, 1.0),
+	vmath.vec2(1.0, 0.0),
+	vmath.vec2(0.0, 0.0),
+	
+	vmath.vec2(0.0, 1.0),
+	vmath.vec2(1.0, 1.0),
+	vmath.vec2(1.0, 0.0),
+	vmath.vec2(0.0, 0.0),
+	
+	vmath.vec2(1.0, 1.0),
+	vmath.vec2(1.0, 0.0),
+	vmath.vec2(0.0, 0.0),
+	vmath.vec2(0.0, 1.0),
+
+	vmath.vec2(1.0, 1.0),
+	vmath.vec2(1.0, 0.0),
+	vmath.vec2(0.0, 0.0),
+	vmath.vec2(0.0, 1.0),
+}
+
 local indices =
 {
 	vmath.vec3(0, 1, 2),
 	vmath.vec3(22, 23, 20),
 };
 
+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
+
 do
 	local writer = XmlWriter.XmlWriter("UnitCube.xml");
 	writer:AddPI("oxygen", [[RNGSchema="../../Documents/meshFormat.rnc" type="compact"]]);
 			writer:AddAttribute("size", "3");
 			writer:AddText(GenStringFromArray(normals));
 		writer:PopElement();
-		writer:PushElement("vao");
-			writer:AddAttribute("name", "lit");
-			writer:PushElement("source"); writer:AddAttribute("attrib", "0"); writer:PopElement();
-			writer:PushElement("source"); writer:AddAttribute("attrib", "2"); writer:PopElement();
+		writer:PushElement("attribute");
+			writer:AddAttribute("index", "5");
+			writer:AddAttribute("type", "float");
+			writer:AddAttribute("size", "2");
+			writer:AddText(GenStringFromArray(texCoords));
 		writer:PopElement();
-		writer:PushElement("vao");
-			writer:AddAttribute("name", "lit-color");
-			writer:PushElement("source"); writer:AddAttribute("attrib", "0"); writer:PopElement();
-			writer:PushElement("source"); writer:AddAttribute("attrib", "1"); writer:PopElement();
-			writer:PushElement("source"); writer:AddAttribute("attrib", "2"); writer:PopElement();
-		writer:PopElement();
-		writer:PushElement("vao");
-			writer:AddAttribute("name", "color");
-			writer:PushElement("source"); writer:AddAttribute("attrib", "0"); writer:PopElement();
-			writer:PushElement("source"); writer:AddAttribute("attrib", "1"); writer:PopElement();
-		writer:PopElement();
-		writer:PushElement("vao");
-			writer:AddAttribute("name", "flat");
-			writer:PushElement("source"); writer:AddAttribute("attrib", "0"); writer:PopElement();
-		writer:PopElement();
+		WriteVAO(writer, "lit", 0, 2);
+		WriteVAO(writer, "lit-color", 0, 1, 2);
+		WriteVAO(writer, "color", 0, 1);
+		WriteVAO(writer, "lit-tex", 0, 2, 5);
+		WriteVAO(writer, "lit-color-tex", 0, 1, 2, 5);
+		WriteVAO(writer, "color-tex", 0, 1, 5);
+		WriteVAO(writer, "tex", 0, 5);
+		WriteVAO(writer, "flat", 0);
 		writer:PushElement("indices");
 			writer:AddAttribute("cmd", "triangles");
 			writer:AddAttribute("type", "ushort");

File data/UnitCube.xml

         -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
+        1 1
+        1 0
+        0 0
+        0 1
+        0 1
+        1 1
+        1 0
+        0 0
+        0 1
+        1 1
+        1 0
+        0 0
+        1 1
+        1 0
+        0 0
+        0 1
+        1 1
+        1 0
+        0 0
+        0 1</attribute>
 	<vao name="lit" >
 		<source attrib="0" />
 		<source attrib="2" />
 		<source attrib="0" />
 		<source attrib="1" />
 	</vao>
+	<vao name="lit-tex" >
+		<source attrib="0" />
+		<source attrib="2" />
+		<source attrib="5" />
+	</vao>
+	<vao name="lit-color-tex" >
+		<source attrib="0" />
+		<source attrib="1" />
+		<source attrib="2" />
+		<source attrib="5" />
+	</vao>
+	<vao name="color-tex" >
+		<source attrib="0" />
+		<source attrib="1" />
+		<source attrib="5" />
+	</vao>
+	<vao name="tex" >
+		<source attrib="0" />
+		<source attrib="5" />
+	</vao>
 	<vao name="flat" >
 		<source attrib="0" />
 	</vao>

File data/seamless_rock1_small.dds

Binary file added.