Skip to content

Commit d13d35f

Browse files
author
alfonse
committed
Tut14: Basic texturing works.
1 parent 65fff7a commit d13d35f

File tree

12 files changed

+1041
-3
lines changed

12 files changed

+1041
-3
lines changed

Tut 14 Textures Are Not Pictures/Basic Texture.cpp

Lines changed: 493 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
require "XmlWriter"
2+
require "vmath"
3+
4+
local function GenStringFromArray(theArray, bAsInt)
5+
local array = {" "}
6+
for i, vector in ipairs(theArray) do
7+
local elements = vector;
8+
if(bAsInt) then
9+
elements = {};
10+
for i, value in ipairs(vector) do
11+
elements[#elements + 1] = string.format("%i", value);
12+
end
13+
end
14+
15+
array[#array + 1] = " " .. table.concat(vector, " ");
16+
end
17+
18+
return table.concat(array, "\n");
19+
end
20+
21+
local positions =
22+
{
23+
--Front
24+
vmath.vec3( 0.5, 0.5, 0.5),
25+
vmath.vec3( 0.5, -0.5, 0.5),
26+
vmath.vec3(-0.5, -0.5, 0.5),
27+
vmath.vec3(-0.5, 0.5, 0.5),
28+
29+
--Top
30+
vmath.vec3( 0.5, 0.5, 0.5),
31+
vmath.vec3(-0.5, 0.5, 0.5),
32+
vmath.vec3(-0.5, 0.5, -0.5),
33+
vmath.vec3( 0.5, 0.5, -0.5),
34+
35+
--Left
36+
vmath.vec3( 0.5, 0.5, 0.5),
37+
vmath.vec3( 0.5, 0.5, -0.5),
38+
vmath.vec3( 0.5, -0.5, -0.5),
39+
vmath.vec3( 0.5, -0.5, 0.5),
40+
41+
--Back
42+
vmath.vec3( 0.5, 0.5, -0.5),
43+
vmath.vec3(-0.5, 0.5, -0.5),
44+
vmath.vec3(-0.5, -0.5, -0.5),
45+
vmath.vec3( 0.5, -0.5, -0.5),
46+
47+
--Bottom
48+
vmath.vec3( 0.5, -0.5, 0.5),
49+
vmath.vec3( 0.5, -0.5, -0.5),
50+
vmath.vec3(-0.5, -0.5, -0.5),
51+
vmath.vec3(-0.5, -0.5, 0.5),
52+
53+
--Right
54+
vmath.vec3(-0.5, 0.5, 0.5),
55+
vmath.vec3(-0.5, -0.5, 0.5),
56+
vmath.vec3(-0.5, -0.5, -0.5),
57+
vmath.vec3(-0.5, 0.5, -0.5),
58+
};
59+
60+
local normals =
61+
{
62+
--Front
63+
vmath.vec3(0.0, 0.0, 1.0),
64+
vmath.vec3(0.0, 0.0, 1.0),
65+
vmath.vec3(0.0, 0.0, 1.0),
66+
vmath.vec3(0.0, 0.0, 1.0),
67+
68+
--Top
69+
vmath.vec3(0.0, 1.0, 0.0),
70+
vmath.vec3(0.0, 1.0, 0.0),
71+
vmath.vec3(0.0, 1.0, 0.0),
72+
vmath.vec3(0.0, 1.0, 0.0),
73+
74+
--Left
75+
vmath.vec3(1.0, 0.0, 0.0),
76+
vmath.vec3(1.0, 0.0, 0.0),
77+
vmath.vec3(1.0, 0.0, 0.0),
78+
vmath.vec3(1.0, 0.0, 0.0),
79+
80+
--Back
81+
vmath.vec3(0.0, 0.0, -1.0),
82+
vmath.vec3(0.0, 0.0, -1.0),
83+
vmath.vec3(0.0, 0.0, -1.0),
84+
vmath.vec3(0.0, 0.0, -1.0),
85+
86+
--Bottom
87+
vmath.vec3(0.0, -1.0, 0.0),
88+
vmath.vec3(0.0, -1.0, 0.0),
89+
vmath.vec3(0.0, -1.0, 0.0),
90+
vmath.vec3(0.0, -1.0, 0.0),
91+
92+
--Right
93+
vmath.vec3(-1.0, 0.0, 0.0),
94+
vmath.vec3(-1.0, 0.0, 0.0),
95+
vmath.vec3(-1.0, 0.0, 0.0),
96+
vmath.vec3(-1.0, 0.0, 0.0),
97+
}
98+
99+
local colors =
100+
{
101+
vmath.vec4(0.25, 1.0, 0.25, 1.0),
102+
vmath.vec4(0.25, 1.0, 0.25, 1.0),
103+
vmath.vec4(0.25, 1.0, 0.25, 1.0),
104+
vmath.vec4(0.25, 1.0, 0.25, 1.0),
105+
106+
vmath.vec4(0.5, 0.5, 1.0, 1.0),
107+
vmath.vec4(0.5, 0.5, 1.0, 1.0),
108+
vmath.vec4(0.5, 0.5, 1.0, 1.0),
109+
vmath.vec4(0.5, 0.5, 1.0, 1.0),
110+
111+
vmath.vec4(1.0, 0.5, 0.5, 1.0),
112+
vmath.vec4(1.0, 0.5, 0.5, 1.0),
113+
vmath.vec4(1.0, 0.5, 0.5, 1.0),
114+
vmath.vec4(1.0, 0.5, 0.5, 1.0),
115+
116+
vmath.vec4(1.0, 1.0, 0.5, 1.0),
117+
vmath.vec4(1.0, 1.0, 0.5, 1.0),
118+
vmath.vec4(1.0, 1.0, 0.5, 1.0),
119+
vmath.vec4(1.0, 1.0, 0.5, 1.0),
120+
121+
vmath.vec4(0.5, 1.0, 1.0, 1.0),
122+
vmath.vec4(0.5, 1.0, 1.0, 1.0),
123+
vmath.vec4(0.5, 1.0, 1.0, 1.0),
124+
vmath.vec4(0.5, 1.0, 1.0, 1.0),
125+
126+
vmath.vec4(1.0, 0.5, 1.0, 1.0),
127+
vmath.vec4(1.0, 0.5, 1.0, 1.0),
128+
vmath.vec4(1.0, 0.5, 1.0, 1.0),
129+
vmath.vec4(1.0, 0.5, 1.0, 1.0),
130+
}
131+
132+
local indices =
133+
{
134+
vmath.vec3(0, 1, 2),
135+
vmath.vec3(2, 3, 0),
136+
137+
vmath.vec3(4, 5, 6),
138+
vmath.vec3(6, 7, 4),
139+
140+
vmath.vec3(8, 9, 10),
141+
vmath.vec3(10, 11, 8),
142+
143+
vmath.vec3(12, 13, 14),
144+
vmath.vec3(14, 15, 12),
145+
146+
vmath.vec3(16, 17, 18),
147+
vmath.vec3(18, 19, 16),
148+
149+
vmath.vec3(20, 21, 22),
150+
vmath.vec3(22, 23, 20),
151+
};
152+
153+
do
154+
local writer = XmlWriter.XmlWriter("UnitCube.xml");
155+
writer:AddPI("oxygen", [[RNGSchema="../../Documents/meshFormat.rnc" type="compact"]]);
156+
writer:PushElement("mesh", "http://www.arcsynthesis.com/gltut/mesh");
157+
writer:PushElement("attribute");
158+
writer:AddAttribute("index", "0");
159+
writer:AddAttribute("type", "float");
160+
writer:AddAttribute("size", "3");
161+
writer:AddText(GenStringFromArray(positions));
162+
writer:PopElement();
163+
writer:PushElement("attribute");
164+
writer:AddAttribute("index", "1");
165+
writer:AddAttribute("type", "float");
166+
writer:AddAttribute("size", "4");
167+
writer:AddText(GenStringFromArray(colors));
168+
writer:PopElement();
169+
writer:PushElement("attribute");
170+
writer:AddAttribute("index", "2");
171+
writer:AddAttribute("type", "float");
172+
writer:AddAttribute("size", "3");
173+
writer:AddText(GenStringFromArray(normals));
174+
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();
194+
writer:PopElement();
195+
writer:PushElement("indices");
196+
writer:AddAttribute("cmd", "triangles");
197+
writer:AddAttribute("type", "ushort");
198+
writer:AddText(GenStringFromArray(indices, true));
199+
writer:PopElement();
200+
writer:PopElement();
201+
writer:Close();
202+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#version 330
2+
3+
layout(std140) uniform;
4+
5+
layout(location = 0) in vec3 position;
6+
layout(location = 2) in vec3 normal;
7+
8+
out vec3 vertexNormal;
9+
out vec3 cameraSpacePosition;
10+
11+
uniform Projection
12+
{
13+
mat4 cameraToClipMatrix;
14+
};
15+
16+
uniform mat4 modelToCameraMatrix;
17+
uniform mat3 normalModelToCameraMatrix;
18+
19+
void main()
20+
{
21+
vec4 tempCamPosition = (modelToCameraMatrix * vec4(position, 1.0));
22+
gl_Position = cameraToClipMatrix * tempCamPosition;
23+
24+
vertexNormal = normalize(normalModelToCameraMatrix * normal);
25+
cameraSpacePosition = vec3(tempCamPosition);
26+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#version 330
2+
3+
in vec3 vertexNormal;
4+
in vec3 cameraSpacePosition;
5+
6+
out vec4 outputColor;
7+
8+
layout(std140) uniform;
9+
10+
uniform Material
11+
{
12+
vec4 diffuseColor;
13+
vec4 specularColor;
14+
float specularShininess;
15+
} Mtl;
16+
17+
struct PerLight
18+
{
19+
vec4 cameraSpaceLightPos;
20+
vec4 lightIntensity;
21+
};
22+
23+
const int numberOfLights = 2;
24+
25+
uniform Light
26+
{
27+
vec4 ambientIntensity;
28+
float lightAttenuation;
29+
PerLight lights[numberOfLights];
30+
} Lgt;
31+
32+
33+
float CalcAttenuation(in vec3 cameraSpacePosition,
34+
in vec3 cameraSpaceLightPos,
35+
out vec3 lightDirection)
36+
{
37+
vec3 lightDifference = cameraSpaceLightPos - cameraSpacePosition;
38+
float lightDistanceSqr = dot(lightDifference, lightDifference);
39+
lightDirection = lightDifference * inversesqrt(lightDistanceSqr);
40+
41+
return (1 / ( 1.0 + Lgt.lightAttenuation * lightDistanceSqr));
42+
}
43+
44+
vec4 ComputeLighting(in PerLight lightData, in vec3 cameraSpacePosition,
45+
in vec3 cameraSpaceNormal)
46+
{
47+
vec3 lightDir;
48+
vec4 lightIntensity;
49+
if(lightData.cameraSpaceLightPos.w == 0.0)
50+
{
51+
lightDir = vec3(lightData.cameraSpaceLightPos);
52+
lightIntensity = lightData.lightIntensity;
53+
}
54+
else
55+
{
56+
float atten = CalcAttenuation(cameraSpacePosition,
57+
lightData.cameraSpaceLightPos.xyz, lightDir);
58+
lightIntensity = atten * lightData.lightIntensity;
59+
}
60+
61+
vec3 surfaceNormal = normalize(cameraSpaceNormal);
62+
float cosAngIncidence = dot(surfaceNormal, lightDir);
63+
cosAngIncidence = cosAngIncidence < 0.0001 ? 0.0 : cosAngIncidence;
64+
65+
vec3 viewDirection = normalize(-cameraSpacePosition);
66+
67+
vec3 halfAngle = normalize(lightDir + viewDirection);
68+
float angleNormalHalf = acos(dot(halfAngle, surfaceNormal));
69+
float exponent = angleNormalHalf / Mtl.specularShininess;
70+
exponent = -(exponent * exponent);
71+
float gaussianTerm = exp(exponent);
72+
73+
gaussianTerm = cosAngIncidence != 0.0 ? gaussianTerm : 0.0;
74+
75+
vec4 lighting = Mtl.diffuseColor * lightIntensity * cosAngIncidence;
76+
lighting += Mtl.specularColor * lightIntensity * gaussianTerm;
77+
78+
return lighting;
79+
}
80+
81+
void main()
82+
{
83+
vec4 accumLighting = Mtl.diffuseColor * Lgt.ambientIntensity;
84+
for(int light = 0; light < numberOfLights; light++)
85+
{
86+
accumLighting += ComputeLighting(Lgt.lights[light],
87+
cameraSpacePosition, vertexNormal);
88+
}
89+
90+
outputColor = sqrt(accumLighting); //2.0 gamma correction
91+
}

0 commit comments

Comments
 (0)