-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from LeeVangraefschepe/terrain-component
Added terrain component
- Loading branch information
Showing
32 changed files
with
806 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
float4x4 gWorld : WORLD; | ||
float4x4 gWorldViewProj : WORLDVIEWPROJECTION; | ||
float4x4 gLightViewProj : LIGHTVIEWPROJECTION; | ||
float3 gLightDirection = float3(-0.577f, -0.577f, 0.577f); | ||
float4 gColor = float4(1.0f, 1.0f, 1.0f, 1.0f); | ||
|
||
Texture2D gShadowMap; | ||
Texture2D gHeightMap; | ||
|
||
float gMaxHeight = 100; | ||
|
||
struct VS_INPUT { | ||
float3 pos : POSITION; | ||
float2 uv : TEXCOORD0; | ||
}; | ||
struct VS_OUTPUT { | ||
float4 pos : SV_POSITION; | ||
float3 normal : NORMAL; | ||
float4 lPos : TEXCOORD1; | ||
}; | ||
|
||
SamplerState samLinear | ||
{ | ||
Filter = MIN_MAG_MIP_LINEAR; | ||
AddressU = Wrap; // or Mirror or Clamp or Border | ||
AddressV = Wrap; // or Mirror or Clamp or Border | ||
}; | ||
|
||
DepthStencilState EnableDepth | ||
{ | ||
DepthEnable = TRUE; | ||
DepthWriteMask = ALL; | ||
}; | ||
|
||
RasterizerState BackCulling | ||
{ | ||
CullMode = BACK; | ||
}; | ||
|
||
float gShadowMapBias = 0.0005f; | ||
|
||
float2 texOffset(int u, int v) | ||
{ | ||
return float2(u / 1280.0f, v / 720.0f); | ||
} | ||
|
||
SamplerComparisonState cmpSampler | ||
{ | ||
// sampler state | ||
Filter = COMPARISON_MIN_MAG_MIP_LINEAR; | ||
AddressU = MIRROR; | ||
AddressV = MIRROR; | ||
|
||
// sampler comparison state | ||
ComparisonFunc = LESS_EQUAL; | ||
}; | ||
|
||
float EvaluateShadowMap(float4 lpos) | ||
{ | ||
// Re-homogenize position after interpolation | ||
lpos.xyz /= lpos.w; | ||
|
||
// If position is not visible to the light - dont illuminate it | ||
// Results in hard light frustum | ||
if (lpos.x < -1.0f || lpos.x > 1.0f || | ||
lpos.y < -1.0f || lpos.y > 1.0f || | ||
lpos.z < 0.0f || lpos.z > 1.0f) | ||
return 1.0f; | ||
|
||
// Transform clip space coords to texture space coords (-1:1 to 0:1) | ||
lpos.x = lpos.x / 2.0f + 0.5f; | ||
lpos.y = lpos.y / -2.0f + 0.5f; | ||
|
||
// Apply shadow map bias | ||
lpos.z -= gShadowMapBias; | ||
|
||
// PCF sampling for shadow map | ||
float sum = 0; | ||
|
||
// Perform PCF filtering on a 4 x 4 texel neighborhood | ||
for (float y = -1.5; y <= 1.5; y += 1.0) | ||
{ | ||
for (float x = -1.5; x <= 1.5; x += 1.0) | ||
{ | ||
sum += gShadowMap.SampleCmpLevelZero(cmpSampler, lpos.xy + texOffset(x, y), lpos.z); | ||
} | ||
} | ||
|
||
float shadowMapDepth = sum / 16.0; | ||
|
||
return shadowMapDepth * 0.5f + 0.5f; | ||
} | ||
|
||
//-------------------------------------------------------------------------------------- | ||
// Vertex Shader | ||
//-------------------------------------------------------------------------------------- | ||
VS_OUTPUT VS(VS_INPUT input) | ||
{ | ||
VS_OUTPUT output; | ||
float2 heightMapPos = input.uv; | ||
// Step 1: convert position into float4 and multiply with matWorldViewProj | ||
float height = gHeightMap[heightMapPos]; | ||
|
||
float L = gHeightMap[heightMapPos + float2(-1.0f, 0.0f)] * gMaxHeight; | ||
float R = gHeightMap[heightMapPos + float2(1.0f, 0.0f)] * gMaxHeight; | ||
float T = gHeightMap[heightMapPos + float2(0.0f, 1.0f)] * gMaxHeight; | ||
float B = gHeightMap[heightMapPos + float2(0.0f, -1.0f)] * gMaxHeight; | ||
|
||
output.pos = mul(float4(input.pos + float3(0, height * gMaxHeight, 0), 1.0f), gWorldViewProj); | ||
// Step 2: rotate the normal: NO TRANSLATION | ||
// this is achieved by clipping the 4x4 to a 3x3 matrix, | ||
// thus removing the postion row of the matrix | ||
float3 normal = normalize(float3(-R + L, 4, -T + B)); | ||
output.normal = /*normalize(mul(normal, (float3x3) gWorld))*/normal; | ||
output.lPos = mul(float4(input.pos, 1.0f), mul(gWorld, gLightViewProj)); | ||
|
||
return output; | ||
} | ||
|
||
//-------------------------------------------------------------------------------------- | ||
// Pixel Shader | ||
//-------------------------------------------------------------------------------------- | ||
float4 PS(VS_OUTPUT input) : SV_TARGET | ||
{ | ||
float shadowValue = EvaluateShadowMap(input.lPos); | ||
|
||
float3 color_rgb = gColor.rgb; | ||
float color_a = gColor.a; | ||
|
||
//HalfLambert Diffuse :) | ||
float diffuseStrength = dot(input.normal, -gLightDirection); | ||
diffuseStrength = diffuseStrength * 0.5 + 0.5; | ||
diffuseStrength = saturate(diffuseStrength); | ||
color_rgb = color_rgb * diffuseStrength; | ||
|
||
return float4(color_rgb * shadowValue, color_a); | ||
} | ||
|
||
//-------------------------------------------------------------------------------------- | ||
// Technique | ||
//-------------------------------------------------------------------------------------- | ||
technique11 DefaultTechnique | ||
{ | ||
pass P0 | ||
{ | ||
SetRasterizerState(BackCulling); | ||
SetDepthStencilState(EnableDepth, 0); | ||
|
||
SetVertexShader(CompileShader(vs_4_0, VS())); | ||
SetGeometryShader(NULL); | ||
SetPixelShader(CompileShader(ps_4_0, PS())); | ||
} | ||
} | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,45 @@ | ||
#pragma once | ||
|
||
#include "Vertex.h" | ||
#include <Debug.h> | ||
|
||
#include <vector> | ||
|
||
namespace leap::graphics | ||
{ | ||
struct CustomMesh final | ||
class CustomMesh final | ||
{ | ||
std::vector<Vertex> vertices{}; | ||
std::vector<unsigned int> indices{}; | ||
public: | ||
template<typename T> | ||
void AddVertex(const T& vertex) | ||
{ | ||
constexpr unsigned int vertexSize{ sizeof(T) }; | ||
|
||
if (m_PrevSize != 0 && vertexSize != m_PrevSize) Debug::LogError("LeapEngine Graphics Error : Custom Mesh received two different kinds of vertex types"); | ||
|
||
const size_t vertexStart{ m_Vertices.size() }; | ||
|
||
m_Vertices.resize(m_Vertices.size() + vertexSize); | ||
memcpy_s(&m_Vertices[vertexStart], vertexSize, &vertex, vertexSize); | ||
|
||
m_PrevSize = vertexSize; | ||
} | ||
void AddIndex(unsigned int index) | ||
{ | ||
m_Indices.emplace_back(index); | ||
} | ||
void SetIndices(std::vector<unsigned int>&& indices) | ||
{ | ||
m_Indices = indices; | ||
} | ||
|
||
const std::vector<unsigned char>& GetVertexBuffer() const { return m_Vertices; } | ||
unsigned int GetVertexSize() const { return m_PrevSize; } | ||
const std::vector<unsigned int>& GetIndexBuffer() const { return m_Indices; } | ||
|
||
private: | ||
unsigned int m_PrevSize{}; | ||
|
||
std::vector<unsigned char> m_Vertices{}; | ||
std::vector<unsigned int> m_Indices{}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.