Skip to content

Commit 1c16785

Browse files
committed
Box object lambertian lighting
1 parent 384c98f commit 1c16785

4 files changed

Lines changed: 67 additions & 18 deletions

File tree

3DRadSpace/3DRadSpace_Editor_WindowsDX11/Frontend/AssetRenderer.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,12 @@ template<> bool AssetRenderer(IGraphicsDevice *device, const std::string &imageP
2727

2828
if(device && model)
2929
{
30-
//std::shared_ptr<BasicTextured> shader = std::make_shared<BasicTextured>(device);
31-
3230
auto boundingSphere = model->GetBoundingSphere();
3331
auto cmd = device->ImmediateContext();
3432

3533
for (auto&& n : iota(0, 3))
3634
{
3735
cmd->Clear(Colors::Gray);
38-
//model->SetShader(shader);
3936
model->Draw(
4037
Matrix4x4() *
4138
Matrix4x4::CreateLookAtView(
@@ -82,8 +79,31 @@ bool AssetRenderer(IGraphicsDevice* device, const std::string& imagePath, FontAs
8279
}
8380

8481
template<>
85-
bool AssetRenderer(IGraphicsDevice* device, const std::string& imagePath, Engine3DRadSpace::Content::Assets::SkyboxAsset* path)
82+
bool AssetRenderer(IGraphicsDevice* device, const std::string& imagePath, SkyboxAsset* skyboxAsset)
8683
{
87-
//TODO.
88-
return true;
84+
auto skybox = skyboxAsset->Get();
85+
86+
skybox->View = Matrix4x4::CreateLookAtView(
87+
Vector3(-1, 0, -1),
88+
Vector3::Zero(),
89+
Vector3::UnitY()
90+
);
91+
92+
skybox->Projection = Matrix4x4::CreatePerspectiveProjection(4.f / 3.f, 65, 0.01f, 500.0f);
93+
94+
if (device && skybox)
95+
{
96+
auto cmd = device->ImmediateContext();
97+
98+
for (auto&& n : std::ranges::views::iota(0, 3))
99+
{
100+
cmd->Clear();
101+
skybox->Draw3D();
102+
cmd->Present();
103+
}
104+
105+
cmd->SaveBackBufferToFile(imagePath);
106+
return true;
107+
}
108+
return false;
89109
}

3DRadSpace/Data/Shaders/Lambert_PositionNormal.hlsl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
cbuffer Data : register(b0)
1+
cbuffer AllData : register(b0)
22
{
3-
row_major matrix matWorldViewProj; // MVP transformation
3+
row_major matrix matWorldViewProj; // MVP transformation
44
row_major matrix matWorldInverseTranspose;
5-
}
6-
7-
cbuffer DirectionalLight : register(b1)
8-
{
95
float4 LightColor;
6+
float4 AmbientColor;
107
float3 LightDirection;
118
float Intensity;
129
}
@@ -36,5 +33,6 @@ VertexOut VS_Main(VertexIn v)
3633

3734
float4 PS_Main(VertexOut v) : SV_TARGET
3835
{
39-
return v.Color * LightColor * Intensity * max(0, dot(normalize(v.Normal), -LightDirection));
36+
float diffuse = max(0, dot(normalize(v.Normal), -LightDirection));
37+
return v.Color * (AmbientColor + LightColor * Intensity * diffuse);
4038
}

3DRadSpace/Engine3DRadSpace/Graphics/Primitives/Box.cpp

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@ std::array<unsigned, 36> Box::CreateIndices()
9191
}
9292

9393
Box::Box(IGraphicsDevice *device, const BoundingBox&b, Color color) :
94-
IPrimitive(device, nullptr),
95-
_box(b),
96-
_color(color)
94+
IPrimitive(device, nullptr),
95+
_box(b),
96+
_color(color),
97+
Light{ Colors::White, Color(color.R * 0.15f, color.G * 0.15f, color.B * 0.15f, 1.0f), Vector3(0,-1,0), 1.0f }
9798
{
9899
auto box_vertices = CreateVertices(b, color);
99100
_vertices = device->CreateVertexBuffer<VertexPositionNormalColor>(box_vertices, BufferUsage::ReadOnlyGPU);
@@ -153,6 +154,35 @@ void Box::SetColor(const Color&color)
153154

154155
void Box::Draw3D()
155156
{
156-
_shader->operator[](1)->SetData(0, &Light, sizeof(Light));
157-
IPrimitive::Draw3D();
157+
struct alignas(16) AllDataBuffer
158+
{
159+
Matrix4x4 MatWorldViewProj;
160+
Matrix4x4 MatWorldInverseTranspose;
161+
Vector4 LightColor;
162+
Vector4 AmbientColor;
163+
Vector3 LightDirection;
164+
float Intensity;
165+
};
166+
167+
Matrix4x4 mvp = _mvp();
168+
Matrix4x4 worldInverseTranspose = Matrix4x4::Transpose(Matrix4x4::Invert(Transform));
169+
170+
AllDataBuffer data =
171+
{
172+
mvp,
173+
worldInverseTranspose,
174+
Vector4(Light.LightColor.R, Light.LightColor.G, Light.LightColor.B, Light.LightColor.A),
175+
Vector4(Light.AmbientColor.R, Light.AmbientColor.G, Light.AmbientColor.B, Light.AmbientColor.A),
176+
Light.LightDirection,
177+
Light.Intensity
178+
};
179+
180+
// Upload to cbuffer slot 0 on every shader stage before binding
181+
_shader->SetData(&data, 0);
182+
183+
_shader->SetAll();
184+
185+
auto cmd = _device->ImmediateContext();
186+
cmd->SetTopology(VertexTopology::TriangleList);
187+
cmd->DrawVertexBufferWithindices(_vertices.get(), _indices.get());
158188
}

3DRadSpace/Engine3DRadSpace/Graphics/Rendering/DirectionalLight.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Engine3DRadSpace::Graphics::Rendering
77
struct DirectionalLight
88
{
99
Math::Color LightColor;
10+
Math::Color AmbientColor;
1011
Math::Vector3 LightDirection;
1112
float Intensity;
1213
};

0 commit comments

Comments
 (0)