@@ -91,9 +91,10 @@ std::array<unsigned, 36> Box::CreateIndices()
9191}
9292
9393Box::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
154155void 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}
0 commit comments