public MainGameComponent() { var vLightDir = new XMFloat3(-1, 1, -1); this.LightDirection = XMVector3.Normalize(vLightDir); XMVector eye = new XMVector(0.0f, 0.0f, -100.0f, 0.0f); XMVector at = new XMVector(0.0f, 0.0f, -0.0f, 0.0f); XMVector up = new XMVector(0.0f, 1.0f, 0.0f, 0.0f); float fObjectRadius = 378.15607f; XMVector radius = XMVector3.Normalize(at - eye).Scale(fObjectRadius * 3.0f); this.ViewMatrix = XMMatrix.LookAtLH(eye, at, up) * XMMatrix.TranslationFromVector(radius); this.WorldMatrix = XMMatrix.Identity; }
public void Render() { var context = this.deviceResources.D3DContext; context.OutputMergerSetRenderTargets(new[] { this.deviceResources.D3DRenderTargetView }, this.deviceResources.D3DDepthStencilView); context.ClearRenderTargetView(this.deviceResources.D3DRenderTargetView, new float[] { 0.0f, 0.125f, 0.3f, 1.0f }); context.ClearDepthStencilView(this.deviceResources.D3DDepthStencilView, D3D11ClearOptions.Depth, 1.0f, 0); context.InputAssemblerSetInputLayout(this.inputLayout); context.InputAssemblerSetVertexBuffers( 0, new[] { this.vertexBuffer }, new uint[] { SimpleVertex.Size }, new uint[] { 0 }); context.InputAssemblerSetIndexBuffer(this.indexBuffer, DxgiFormat.R16UInt, 0); context.InputAssemblerSetPrimitiveTopology(D3D11PrimitiveTopology.TriangleList); // Update matrix variables and lighting variables ConstantBufferData cb; cb.World = this.worldMatrix.Transpose(); cb.View = this.viewMatrix.Transpose(); cb.Projection = this.projectionMatrix.Transpose(); cb.LightDir = new XMFloat4[2]; cb.LightColor = new XMFloat4[2]; for (int i = 0; i < 2; i++) { cb.LightDir[i] = this.lightDirs[i]; cb.LightColor[i] = this.lightColors[i]; } //cb.LightDir0 = this.lightDirs[0]; //cb.LightDir1 = this.lightDirs[1]; //cb.LightColor0 = this.lightColors[0]; //cb.LightColor1 = this.lightColors[1]; cb.OutputColor = new XMFloat4(0, 0, 0, 0); context.UpdateSubresource(this.constantBuffer, 0, null, cb, 0, 0); // Render the cube context.VertexShaderSetShader(this.vertexShader, null); context.VertexShaderSetConstantBuffers(0, new[] { this.constantBuffer }); context.PixelShaderSetShader(this.pixelShader, null); context.PixelShaderSetConstantBuffers(0, new[] { this.constantBuffer }); context.DrawIndexed((uint)MainGameComponent.Indices.Length, 0, 0); // Render each light context.PixelShaderSetShader(this.pixelShaderSolid, null); for (int m = 0; m < 2; m++) { XMMatrix light = XMMatrix.TranslationFromVector(5.0f * this.lightDirs[m].ToVector()); XMMatrix lightScale = XMMatrix.Scaling(0.2f, 0.2f, 0.2f); light = lightScale * light; // Update the world variable to reflect the current light cb.World = light.Transpose(); cb.OutputColor = this.lightColors[m]; context.UpdateSubresource(this.constantBuffer, 0, null, cb, 0, 0); context.DrawIndexed((uint)MainGameComponent.Indices.Length, 0, 0); } }