Skip to content

Commit 50e56da

Browse files
author
alfonse
committed
Tut17: Textured objects.
1 parent 39de236 commit 50e56da

File tree

8 files changed

+137
-66
lines changed

8 files changed

+137
-66
lines changed

License.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ The following files are in the public domain and therefore are not copywritten:
1515

1616
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:
1717

18-
19-
18+
Attributed to Etory, of OpenGameArt.org:
19+
* data/seamless_rock1_small.dds
2020

Tut 17 Spotlight on Textures/Double Projection.cpp

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct ProjectionBlock
3838

3939
GLuint g_projectionUniformBuffer = 0;
4040
GLuint g_lightUniformBuffer = 0;
41+
GLuint g_stoneTex;
4142

4243
const int NUM_SAMPLERS = 2;
4344
GLuint g_samplers[NUM_SAMPLERS];
@@ -69,30 +70,10 @@ void LoadTextures()
6970
{
7071
try
7172
{
72-
/*
73-
std::string filename(Framework::FindFileOrThrow("terrain_tex.dds"));
73+
std::string filename(Framework::FindFileOrThrow("seamless_rock1_small.dds"));
7474

7575
std::auto_ptr<glimg::ImageSet> pImageSet(glimg::loaders::dds::LoadFromFile(filename.c_str()));
76-
77-
glGenTextures(1, &g_linearTexture);
78-
glBindTexture(GL_TEXTURE_2D, g_linearTexture);
79-
80-
glimg::OpenGLPixelTransferParams xfer = glimg::GetUploadFormatType(pImageSet->GetFormat(), 0);
81-
82-
for(int mipmapLevel = 0; mipmapLevel < pImageSet->GetMipmapCount(); mipmapLevel++)
83-
{
84-
glimg::SingleImage image = pImageSet->GetImage(mipmapLevel, 0, 0);
85-
glimg::Dimensions dims = image.GetDimensions();
86-
87-
glTexImage2D(GL_TEXTURE_2D, mipmapLevel, GL_SRGB8_ALPHA8, dims.width, dims.height, 0,
88-
xfer.format, xfer.type, image.GetImageData());
89-
}
90-
91-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
92-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, pImageSet->GetMipmapCount() - 1);
93-
94-
glBindTexture(GL_TEXTURE_2D, 0);
95-
*/
76+
g_stoneTex = glimg::CreateTexture(pImageSet.get(), 0);
9677
}
9778
catch(std::exception &e)
9879
{
@@ -166,6 +147,7 @@ Framework::Timer g_timer(Framework::Timer::TT_LOOP, 10.0f);
166147
Framework::UniformVec4Binder g_blueBinder;
167148
Framework::UniformVec4Binder g_redBinder;
168149
Framework::UniformIntBinder g_lightNumBinder;
150+
Framework::TextureBinder g_stoneTexBinder;
169151

170152
GLint g_unlitModelToCameraMatrixUnif;
171153
GLint g_unlitObjectColorUnif;
@@ -179,8 +161,8 @@ void LoadAndSetupScene()
179161
std::auto_ptr<Framework::Scene> pScene(new Framework::Scene("dp_scene.xml"));
180162

181163
std::vector<Framework::NodeRef> nodes;
182-
nodes.push_back(pScene->FindNode("blueSphere"));
183-
nodes.push_back(pScene->FindNode("redSphere"));
164+
nodes.push_back(pScene->FindNode("blueObj"));
165+
nodes.push_back(pScene->FindNode("redObj"));
184166

185167
GLuint unlit = pScene->FindProgram("p_unlit");
186168
Framework::Mesh *pSphereMesh = pScene->FindMesh("m_sphere");
@@ -197,6 +179,10 @@ void LoadAndSetupScene()
197179
g_redBinder.SetValue(glm::vec4(1.0f, 0.1f, 0.1f, 1.0f));
198180
nodes[1].SetStateBinder(&g_redBinder);
199181

182+
g_stoneTexBinder.SetTexture(0, GL_TEXTURE_2D, g_stoneTex, g_samplers[1]);
183+
nodes[0].SetStateBinder(&g_stoneTexBinder);
184+
nodes[1].SetStateBinder(&g_stoneTexBinder);
185+
200186
//No more things that can throw.
201187
g_unlitProg = unlit;
202188
g_unlitModelToCameraMatrixUnif = glGetUniformLocation(unlit, "modelToCameraMatrix");
@@ -259,6 +245,9 @@ void init()
259245
glBindBufferRange(GL_UNIFORM_BUFFER, g_projectionBlockIndex, g_projectionUniformBuffer,
260246
0, sizeof(ProjectionBlock));
261247

248+
CreateSamplers();
249+
LoadTextures();
250+
262251
try
263252
{
264253
LoadAndSetupScene();
@@ -278,8 +267,6 @@ void init()
278267

279268
glBindBuffer(GL_UNIFORM_BUFFER, 0);
280269

281-
// LoadTextures();
282-
// CreateSamplers();
283270
}
284271

285272
using Framework::Timer;
@@ -300,9 +287,9 @@ void BuildLights( const glm::mat4 &camMatrix )
300287
lightData.lights[0].lightIntensity = glm::vec4(2.0, 2.0, 2.5, 1.0);
301288
lightData.lights[0].cameraSpaceLightPos = camMatrix *
302289
glm::normalize(glm::vec4(0.0f, 0.5f, 0.5f, 0.0f));
303-
lightData.lights[1].lightIntensity = glm::vec4(7.0, 9.0, 6.5, 1.0);
290+
lightData.lights[1].lightIntensity = glm::vec4(3.5, 6.5, 3.0, 1.0);
304291
lightData.lights[1].cameraSpaceLightPos = camMatrix *
305-
glm::vec4(5.0f, 4.0f, 0.5f, 1.0f);
292+
glm::vec4(5.0f, 6.0f, 0.5f, 1.0f);
306293

307294
g_lightNumBinder.SetValue(2);
308295

Tut 17 Spotlight on Textures/data/dp_scene.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@
1818
normal-model-to-camera="normalModelToCameraMatrix">
1919
<block name="Projection" binding="0"/>
2020
<block name="Light" binding="1"/>
21+
<sampler name="diffuseColorTex" unit="0"/>
2122
</prog>
2223
<node
23-
name="blueSphere"
24+
name="blueObj"
2425
mesh="m_cube"
2526
prog="p_lit"
2627
pos="0 1 0"
2728
orient="0.0 0.0 0.0 1.0"
2829
scale="3"/>
2930
<node
30-
name="redSphere"
31+
name="redObj"
3132
mesh="m_cube"
3233
prog="p_lit"
3334
pos="7 0 0"

Tut 17 Spotlight on Textures/data/litTexture.frag

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#version 330
22

3-
//in vec2 colorCoord;
3+
in vec2 colorCoord;
44
in vec3 cameraSpacePosition;
55
in vec3 cameraSpaceNormal;
66

@@ -14,8 +14,6 @@ struct PerLight
1414
vec4 lightIntensity;
1515
};
1616

17-
uniform vec4 objectColor;
18-
1917
uniform Light
2018
{
2119
vec4 ambientIntensity;
@@ -62,20 +60,20 @@ vec4 ComputeLighting(in vec4 diffuseColor, in PerLight lightData)
6260
return lighting;
6361
}
6462

65-
//uniform sampler2D diffuseColorTex;
63+
uniform sampler2D diffuseColorTex;
6664

6765
void main()
6866
{
69-
// vec4 diffuseColor = texture(diffuseColorTex, colorCoord);
67+
vec4 diffuseColor = texture(diffuseColorTex, colorCoord);
7068

7169
PerLight currLight;
7270
currLight.cameraSpaceLightPos = normalize(vec4(0.0, 0.5, 0.5, 0.0));
7371
currLight.lightIntensity = vec4(2.0, 2.0, 2.5, 1.0);
7472

75-
vec4 accumLighting = objectColor * Lgt.ambientIntensity;
73+
vec4 accumLighting = diffuseColor * Lgt.ambientIntensity;
7674
for(int light = 0; light < numberOfLights; light++)
7775
{
78-
accumLighting += ComputeLighting(objectColor, Lgt.lights[light]);
76+
accumLighting += ComputeLighting(diffuseColor, Lgt.lights[light]);
7977
}
8078

8179
outputColor = accumLighting / Lgt.maxIntensity;

Tut 17 Spotlight on Textures/data/litTexture.vert

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ layout(std140) uniform;
44

55
layout(location = 0) in vec3 position;
66
layout(location = 2) in vec3 normal;
7-
//layout(location = 5) in vec2 texCoord;
7+
layout(location = 5) in vec2 texCoord;
88

9-
//out vec2 colorCoord;
9+
out vec2 colorCoord;
1010
out vec3 cameraSpacePosition;
1111
out vec3 cameraSpaceNormal;
1212

@@ -22,7 +22,7 @@ void main()
2222
{
2323
cameraSpacePosition = (modelToCameraMatrix * vec4(position, 1.0)).xyz;
2424
gl_Position = cameraToClipMatrix * vec4(cameraSpacePosition, 1.0);
25-
//Assume the modelToCameraMatrix contains no scaling.
2625
cameraSpaceNormal = normalize(normalModelToCameraMatrix * normal);
27-
// colorCoord = texCoord;
26+
27+
colorCoord = texCoord;
2828
}

data/GenCube.lua

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ local positions =
3232
vmath.vec3(-0.5, 0.5, -0.5),
3333
vmath.vec3( 0.5, 0.5, -0.5),
3434

35-
--Left
35+
--Right
3636
vmath.vec3( 0.5, 0.5, 0.5),
3737
vmath.vec3( 0.5, 0.5, -0.5),
3838
vmath.vec3( 0.5, -0.5, -0.5),
@@ -50,7 +50,7 @@ local positions =
5050
vmath.vec3(-0.5, -0.5, -0.5),
5151
vmath.vec3(-0.5, -0.5, 0.5),
5252

53-
--Right
53+
--Left
5454
vmath.vec3(-0.5, 0.5, 0.5),
5555
vmath.vec3(-0.5, -0.5, 0.5),
5656
vmath.vec3(-0.5, -0.5, -0.5),
@@ -71,7 +71,7 @@ local normals =
7171
vmath.vec3(0.0, 1.0, 0.0),
7272
vmath.vec3(0.0, 1.0, 0.0),
7373

74-
--Left
74+
--Right
7575
vmath.vec3(1.0, 0.0, 0.0),
7676
vmath.vec3(1.0, 0.0, 0.0),
7777
vmath.vec3(1.0, 0.0, 0.0),
@@ -89,7 +89,7 @@ local normals =
8989
vmath.vec3(0.0, -1.0, 0.0),
9090
vmath.vec3(0.0, -1.0, 0.0),
9191

92-
--Right
92+
--Left
9393
vmath.vec3(-1.0, 0.0, 0.0),
9494
vmath.vec3(-1.0, 0.0, 0.0),
9595
vmath.vec3(-1.0, 0.0, 0.0),
@@ -129,6 +129,39 @@ local colors =
129129
vmath.vec4(1.0, 0.5, 1.0, 1.0),
130130
}
131131

132+
local texCoords =
133+
{
134+
vmath.vec2(1.0, 1.0),
135+
vmath.vec2(1.0, 0.0),
136+
vmath.vec2(0.0, 0.0),
137+
vmath.vec2(0.0, 1.0),
138+
139+
vmath.vec2(1.0, 1.0),
140+
vmath.vec2(1.0, 0.0),
141+
vmath.vec2(0.0, 0.0),
142+
vmath.vec2(0.0, 1.0),
143+
144+
vmath.vec2(0.0, 1.0),
145+
vmath.vec2(1.0, 1.0),
146+
vmath.vec2(1.0, 0.0),
147+
vmath.vec2(0.0, 0.0),
148+
149+
vmath.vec2(0.0, 1.0),
150+
vmath.vec2(1.0, 1.0),
151+
vmath.vec2(1.0, 0.0),
152+
vmath.vec2(0.0, 0.0),
153+
154+
vmath.vec2(1.0, 1.0),
155+
vmath.vec2(1.0, 0.0),
156+
vmath.vec2(0.0, 0.0),
157+
vmath.vec2(0.0, 1.0),
158+
159+
vmath.vec2(1.0, 1.0),
160+
vmath.vec2(1.0, 0.0),
161+
vmath.vec2(0.0, 0.0),
162+
vmath.vec2(0.0, 1.0),
163+
}
164+
132165
local indices =
133166
{
134167
vmath.vec3(0, 1, 2),
@@ -150,6 +183,19 @@ local indices =
150183
vmath.vec3(22, 23, 20),
151184
};
152185

186+
local function WriteVAO(writer, name, ...)
187+
local attribs = {...}
188+
189+
writer:PushElement("vao");
190+
writer:AddAttribute("name", name);
191+
for i, attrib in ipairs(attribs) do
192+
writer:PushElement("source");
193+
writer:AddAttribute("attrib", tostring(attrib));
194+
writer:PopElement();
195+
end
196+
writer:PopElement();
197+
end
198+
153199
do
154200
local writer = XmlWriter.XmlWriter("UnitCube.xml");
155201
writer:AddPI("oxygen", [[RNGSchema="../../Documents/meshFormat.rnc" type="compact"]]);
@@ -172,26 +218,20 @@ do
172218
writer:AddAttribute("size", "3");
173219
writer:AddText(GenStringFromArray(normals));
174220
writer:PopElement();
175-
writer:PushElement("vao");
176-
writer:AddAttribute("name", "lit");
177-
writer:PushElement("source"); writer:AddAttribute("attrib", "0"); writer:PopElement();
178-
writer:PushElement("source"); writer:AddAttribute("attrib", "2"); writer:PopElement();
179-
writer:PopElement();
180-
writer:PushElement("vao");
181-
writer:AddAttribute("name", "lit-color");
182-
writer:PushElement("source"); writer:AddAttribute("attrib", "0"); writer:PopElement();
183-
writer:PushElement("source"); writer:AddAttribute("attrib", "1"); writer:PopElement();
184-
writer:PushElement("source"); writer:AddAttribute("attrib", "2"); writer:PopElement();
185-
writer:PopElement();
186-
writer:PushElement("vao");
187-
writer:AddAttribute("name", "color");
188-
writer:PushElement("source"); writer:AddAttribute("attrib", "0"); writer:PopElement();
189-
writer:PushElement("source"); writer:AddAttribute("attrib", "1"); writer:PopElement();
190-
writer:PopElement();
191-
writer:PushElement("vao");
192-
writer:AddAttribute("name", "flat");
193-
writer:PushElement("source"); writer:AddAttribute("attrib", "0"); writer:PopElement();
221+
writer:PushElement("attribute");
222+
writer:AddAttribute("index", "5");
223+
writer:AddAttribute("type", "float");
224+
writer:AddAttribute("size", "2");
225+
writer:AddText(GenStringFromArray(texCoords));
194226
writer:PopElement();
227+
WriteVAO(writer, "lit", 0, 2);
228+
WriteVAO(writer, "lit-color", 0, 1, 2);
229+
WriteVAO(writer, "color", 0, 1);
230+
WriteVAO(writer, "lit-tex", 0, 2, 5);
231+
WriteVAO(writer, "lit-color-tex", 0, 1, 2, 5);
232+
WriteVAO(writer, "color-tex", 0, 1, 5);
233+
WriteVAO(writer, "tex", 0, 5);
234+
WriteVAO(writer, "flat", 0);
195235
writer:PushElement("indices");
196236
writer:AddAttribute("cmd", "triangles");
197237
writer:AddAttribute("type", "ushort");

data/UnitCube.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,31 @@
7777
-1 0 0
7878
-1 0 0
7979
-1 0 0</attribute>
80+
<attribute index="5" type="float" size="2" >
81+
1 1
82+
1 0
83+
0 0
84+
0 1
85+
1 1
86+
1 0
87+
0 0
88+
0 1
89+
0 1
90+
1 1
91+
1 0
92+
0 0
93+
0 1
94+
1 1
95+
1 0
96+
0 0
97+
1 1
98+
1 0
99+
0 0
100+
0 1
101+
1 1
102+
1 0
103+
0 0
104+
0 1</attribute>
80105
<vao name="lit" >
81106
<source attrib="0" />
82107
<source attrib="2" />
@@ -90,6 +115,26 @@
90115
<source attrib="0" />
91116
<source attrib="1" />
92117
</vao>
118+
<vao name="lit-tex" >
119+
<source attrib="0" />
120+
<source attrib="2" />
121+
<source attrib="5" />
122+
</vao>
123+
<vao name="lit-color-tex" >
124+
<source attrib="0" />
125+
<source attrib="1" />
126+
<source attrib="2" />
127+
<source attrib="5" />
128+
</vao>
129+
<vao name="color-tex" >
130+
<source attrib="0" />
131+
<source attrib="1" />
132+
<source attrib="5" />
133+
</vao>
134+
<vao name="tex" >
135+
<source attrib="0" />
136+
<source attrib="5" />
137+
</vao>
93138
<vao name="flat" >
94139
<source attrib="0" />
95140
</vao>

data/seamless_rock1_small.dds

171 KB
Binary file not shown.

0 commit comments

Comments
 (0)