public void Update(ITimer timer) { float t = timer == null ? 0.0f : (float)timer.TotalSeconds; this.worldMatrix1 = XMMatrix.RotationY(t); XMMatrix spin = XMMatrix.RotationZ(-t); XMMatrix orbit = XMMatrix.RotationY(-t * 2.0f); XMMatrix translate = XMMatrix.Translation(-4.0f, 0.0f, 0.0f); XMMatrix scale = XMMatrix.Scaling(0.3f, 0.3f, 0.3f); this.worldMatrix2 = scale * spin * translate * orbit; }
static void Transform(XMMatrix target, Viewport viewport, float angle, float scale, float w, float h) { using var r = new XMMatrix(); using var b = new XMMatrix(); using var s = new XMMatrix(); using var t = new XMMatrix(); r.RotationZ((float)(angle / 180 * Math.PI)); s.Scaling(scale, scale, 1.0f); t.Translation(viewport.Width / 2 - w * scale / 2, viewport.Height / 2 - h * scale / 2, 0.0f); b.Translation(-viewport.Width / 2, -viewport.Height / 2, 0.0f); b.Multiply(r); r.Set(b); b.Identity(); b.Translation(viewport.Width / 2, viewport.Height / 2, 0.0f); r.Multiply(b); s.Multiply(t); s.Multiply(r); target.Set(s); }
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); } }