Skip to content

Commit 0a4e1e7

Browse files
committed
Lighting, textures, and normal mapping
1 parent 1a12501 commit 0a4e1e7

36 files changed

+905435
-163
lines changed

Source/Engine/Engine.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@
187187
<ClInclude Include="src\log\Log.h" />
188188
<ClInclude Include="src\object\Image.h" />
189189
<ClInclude Include="src\object\ImageLoader.h" />
190+
<ClInclude Include="src\object\Lights.h" />
190191
<ClInclude Include="src\object\Model.h" />
191192
<ClInclude Include="src\object\OBJLoader.h" />
192193
<ClInclude Include="src\object\UniformData.h" />

Source/Engine/Engine.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
<ClInclude Include="src\application\InputAction.h">
118118
<Filter>Header Files</Filter>
119119
</ClInclude>
120+
<ClInclude Include="src\object\Lights.h">
121+
<Filter>Header Files</Filter>
122+
</ClInclude>
120123
</ItemGroup>
121124
<ItemGroup>
122125
<ClCompile Include="src\event\EventSystem.cpp">

Source/Engine/src/object/Lights.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include "math/Math.h"
4+
5+
namespace Quartz
6+
{
7+
struct DirectionalLight
8+
{
9+
Vector4 direction;
10+
Vector4 color;
11+
};
12+
13+
struct PointLight
14+
{
15+
Vector4 position;
16+
Vector4 color;
17+
Vector4 attenuation;
18+
};
19+
}

Source/Engine/src/object/OBJLoader.cpp

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,11 @@ namespace Quartz
194194
{
195195
{ 0, VERTEX_ATTRIBUTE_POSITION, VERTEX_TYPE_FLOAT3 },
196196
{ 1, VERTEX_ATTRIBUTE_NORMAL, VERTEX_TYPE_FLOAT3 },
197-
{ 2, VERTEX_ATTRIBUTE_TEXCOORD, VERTEX_TYPE_FLOAT2 },
197+
{ 2, VERTEX_ATTRIBUTE_BINORMAL, VERTEX_TYPE_FLOAT3 },
198+
{ 3, VERTEX_ATTRIBUTE_TANGENT, VERTEX_TYPE_FLOAT3 },
199+
{ 4, VERTEX_ATTRIBUTE_TEXCOORD, VERTEX_TYPE_FLOAT2 },
198200
},
199-
3
201+
5
200202
};
201203

202204
VertexData vertexData{};
@@ -209,45 +211,80 @@ namespace Quartz
209211
Map<OBJIndex, UInt32> indexMap{};
210212
indexMap.Reserve(objModel.indices.Size());
211213

212-
for (const OBJIndex& index : objModel.indices)
214+
for (UInt32 i = 0; i < objModel.indices.Size(); i += 3)
213215
{
214-
UInt32* pIdx = indexMap.Get(index);
216+
const OBJIndex& index0 = objModel.indices[i];
217+
const OBJIndex& index1 = objModel.indices[i + 1];
218+
const OBJIndex& index2 = objModel.indices[i + 2];
215219

216-
if (pIdx != nullptr)
220+
Vector3 binormal = Vector3(0, 0, 0);
221+
Vector3 tangent = Vector3(0, 0, 0);
222+
223+
if (objModel.texCoords.Size() > 0 &&
224+
index0.texCoordIdx != 0 && index1.texCoordIdx != 0 && index2.texCoordIdx != 0)
217225
{
218-
indexData.buffer.Push(*pIdx);
226+
// Generate tangents/binormals *per-face*
227+
228+
Vector3 deltaPos1 = objModel.positions[index1.positionIdx - 1] - objModel.positions[index0.positionIdx - 1];
229+
Vector3 deltaPos2 = objModel.positions[index2.positionIdx - 1] - objModel.positions[index1.positionIdx - 1];
230+
Vector2 deltaTex1 = objModel.texCoords[index1.texCoordIdx - 1] - objModel.texCoords[index0.texCoordIdx - 1];
231+
Vector2 deltaTex2 = objModel.texCoords[index2.texCoordIdx - 1] - objModel.texCoords[index1.texCoordIdx - 1];
232+
233+
float f = 1.0f / (deltaTex1.x * deltaTex2.y - deltaTex2.x * deltaTex1.y);
234+
235+
tangent.x = f * (deltaTex2.y * deltaPos1.x - deltaTex1.y * deltaPos2.x);
236+
tangent.y = f * (deltaTex2.y * deltaPos1.y - deltaTex1.y * deltaPos2.y);
237+
tangent.z = f * (deltaTex2.y * deltaPos1.z - deltaTex1.y * deltaPos2.z);
238+
239+
binormal.x = f * (-deltaTex2.x * deltaPos1.x + deltaTex1.x * deltaPos2.x);
240+
binormal.y = f * (-deltaTex2.x * deltaPos1.y + deltaTex1.x * deltaPos2.y);
241+
binormal.z = f * (-deltaTex2.x * deltaPos1.z + deltaTex1.x * deltaPos2.z);
219242
}
220-
else
243+
244+
for (UInt32 j = 0; j < 3; j++)
221245
{
222-
Vector3& position = objModel.positions[index.positionIdx - 1];
223-
vertexData.buffer.Push(position);
246+
const OBJIndex& index = objModel.indices[i + j];
247+
UInt32* pIdx = indexMap.Get(index);
224248

225-
if (objModel.normals.Size() > 0 && index.normalIdx != 0)
249+
if (pIdx != nullptr)
226250
{
227-
Vector3& normal = objModel.normals[index.normalIdx - 1];
228-
vertexData.buffer.Push(normal);
251+
indexData.buffer.Push(*pIdx);
229252
}
230253
else
231254
{
232-
// Generate normals
233-
Vector3 normal = Vector3(0, 0, 0);
234-
vertexData.buffer.Push(normal);
235-
}
255+
Vector3& position = objModel.positions[index.positionIdx - 1];
256+
vertexData.buffer.Push(position);
236257

237-
if (objModel.texCoords.Size() > 0 && index.texCoordIdx != 0)
238-
{
239-
Vector2& texCoord = objModel.texCoords[index.texCoordIdx - 1];
240-
vertexData.buffer.Push(texCoord);
241-
}
242-
else
243-
{
244-
// Generate texCoords
245-
Vector2 texCoord = Vector2(0, 0);
246-
vertexData.buffer.Push(texCoord);
247-
}
258+
if (objModel.normals.Size() > 0 && index.normalIdx != 0)
259+
{
260+
Vector3& normal = objModel.normals[index.normalIdx - 1];
261+
vertexData.buffer.Push(normal);
262+
}
263+
else
264+
{
265+
// Generate normals
266+
Vector3 normal = Vector3(0, 0, 0);
267+
vertexData.buffer.Push(normal);
268+
}
269+
270+
vertexData.buffer.Push(binormal);
271+
vertexData.buffer.Push(tangent);
248272

249-
indexMap.Put(index, currentIndex);
250-
indexData.buffer.Push(currentIndex++);
273+
if (objModel.texCoords.Size() > 0 && index.texCoordIdx != 0)
274+
{
275+
Vector2& texCoord = objModel.texCoords[index.texCoordIdx - 1];
276+
vertexData.buffer.Push(texCoord);
277+
}
278+
else
279+
{
280+
// Generate texCoords
281+
Vector2 texCoord = Vector2(0, 0);
282+
vertexData.buffer.Push(texCoord);
283+
}
284+
285+
indexMap.Put(index, currentIndex);
286+
indexData.buffer.Push(currentIndex++);
287+
}
251288
}
252289
}
253290

Source/Sandbox/Sandbox.vcxproj

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@
9696
<AdditionalDependencies>core.lib;engine.lib;platform_win32.lib;vulkan_win32.lib;vulkan.lib;vulkan-1.lib;%(AdditionalDependencies)</AdditionalDependencies>
9797
<AdditionalLibraryDirectories>$(SolutionDir)$(Configuration);$(VULKAN_SDK)\Lib32</AdditionalLibraryDirectories>
9898
</Link>
99+
<PostBuildEvent>
100+
<Command>cd "$(ProjectDir)src\"
101+
call compile_and_resources.bat</Command>
102+
<Message>Compile shaders and copy assets</Message>
103+
</PostBuildEvent>
104+
<PreBuildEvent>
105+
<Command>
106+
</Command>
107+
</PreBuildEvent>
99108
</ItemDefinitionGroup>
100109
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
101110
<ClCompile>
@@ -110,6 +119,15 @@
110119
<AdditionalDependencies>core.lib;engine.lib;platform_win32.lib;vulkan_win32.lib;vulkan.lib;vulkan-1.lib;%(AdditionalDependencies)</AdditionalDependencies>
111120
<AdditionalLibraryDirectories>$(SolutionDir)$(Configuration);$(VULKAN_SDK)\Lib</AdditionalLibraryDirectories>
112121
</Link>
122+
<PostBuildEvent>
123+
<Command>cd "$(ProjectDir)src\"
124+
call compile_and_resources.bat</Command>
125+
<Message>Compile shaders and copy assets</Message>
126+
</PostBuildEvent>
127+
<PreBuildEvent>
128+
<Command>
129+
</Command>
130+
</PreBuildEvent>
113131
</ItemDefinitionGroup>
114132
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
115133
<ClCompile>
@@ -130,6 +148,15 @@
130148
<AdditionalDependencies>core.lib;engine.lib;platform_win32.lib;vulkan_win32.lib;vulkan.lib;vulkan-1.lib;%(AdditionalDependencies)</AdditionalDependencies>
131149
<AdditionalLibraryDirectories>$(SolutionDir)$(Configuration);$(VULKAN_SDK)\Lib32</AdditionalLibraryDirectories>
132150
</Link>
151+
<PostBuildEvent>
152+
<Command>cd "$(ProjectDir)src\"
153+
call compile_and_resources.bat</Command>
154+
<Message>Compile shaders and copy assets</Message>
155+
</PostBuildEvent>
156+
<PreBuildEvent>
157+
<Command>
158+
</Command>
159+
</PreBuildEvent>
133160
</ItemDefinitionGroup>
134161
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
135162
<ClCompile>
@@ -150,15 +177,42 @@
150177
<AdditionalDependencies>core.lib;engine.lib;platform_win32.lib;vulkan_win32.lib;vulkan.lib;vulkan-1.lib;%(AdditionalDependencies)</AdditionalDependencies>
151178
<AdditionalLibraryDirectories>$(SolutionDir)$(Configuration);$(VULKAN_SDK)\Lib</AdditionalLibraryDirectories>
152179
</Link>
180+
<PostBuildEvent>
181+
<Command>cd "$(ProjectDir)src\"
182+
call compile_and_resources.bat</Command>
183+
<Message>Compile shaders and copy assets</Message>
184+
</PostBuildEvent>
185+
<PreBuildEvent>
186+
<Command>
187+
</Command>
188+
</PreBuildEvent>
153189
</ItemDefinitionGroup>
154190
<ItemGroup>
155191
<ClCompile Include="src\Sandbox.cpp" />
156192
</ItemGroup>
157193
<ItemGroup>
158-
<None Include="src\frag.spv" />
159-
<None Include="src\shader.vert" />
160-
<None Include="src\shader.frag" />
161-
<None Include="src\vert.spv" />
194+
<_EmbedManagedResourceFile Include="src\shader.vert">
195+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
196+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
197+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
198+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
199+
<FileType>Document</FileType>
200+
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</DeploymentContent>
201+
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</DeploymentContent>
202+
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
203+
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
204+
</_EmbedManagedResourceFile>
205+
<_EmbedManagedResourceFile Include="src\shader.frag">
206+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
207+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
208+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
209+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
210+
<FileType>Document</FileType>
211+
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</DeploymentContent>
212+
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</DeploymentContent>
213+
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
214+
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
215+
</_EmbedManagedResourceFile>
162216
</ItemGroup>
163217
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
164218
<ImportGroup Label="ExtensionTargets">

Source/Sandbox/Sandbox.vcxproj.filters

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
1515
</Filter>
1616
</ItemGroup>
17-
<ItemGroup>
18-
<None Include="src\shader.vert" />
19-
<None Include="src\shader.frag" />
20-
<None Include="src\vert.spv" />
21-
<None Include="src\frag.spv" />
22-
</ItemGroup>
2317
<ItemGroup>
2418
<ClCompile Include="src\Sandbox.cpp">
2519
<Filter>Source Files</Filter>
2620
</ClCompile>
2721
</ItemGroup>
22+
<ItemGroup>
23+
<_EmbedManagedResourceFile Include="src\shader.vert" />
24+
<_EmbedManagedResourceFile Include="src\shader.frag" />
25+
</ItemGroup>
2826
</Project>

0 commit comments

Comments
 (0)