void RenderLight(Light light) { lightWorldVar.SetMatrixTranspose(light.World); lightPositionRadiusVar.Set(new Vector4(light.Position, light.Radius)); lightColorVar.Set(light.Color); lightAccumulationPass.Apply(_immediateContext); _immediateContext.DrawIndexed(pointLightVolumeIndices.Length, 0, 0); }
void SetSceneConstants() { FreeLook freelook = Demo.FreeLook; Vector3 up = MathHelper.Convert(freelook.Up); Vector3 eye = MathHelper.Convert(freelook.Eye); Vector4 eyePosition = new Vector4(eye, 1); sceneConstants.View = Matrix.LookAtLH(eye, MathHelper.Convert(freelook.Target), up); Matrix.PerspectiveFovLH(FieldOfView, AspectRatio, _nearPlane, FarPlane, out sceneConstants.Projection); Matrix.Invert(ref sceneConstants.View, out sceneConstants.ViewInverse); Texture2DDescription depthBuffer = lightDepthTexture.Description; Vector3 lightPosition = sunLightDirection * -60; Matrix lightView = Matrix.LookAtLH(lightPosition, Vector3.Zero, up); //Matrix lightProjection = Matrix.OrthoLH(depthBuffer.Width / 8.0f, depthBuffer.Height / 8.0f, _nearPlane, FarPlane); Matrix lightProjection = Matrix.PerspectiveFovLH(FieldOfView, (float)depthBuffer.Width / (float)depthBuffer.Height, _nearPlane, FarPlane); sceneConstants.LightViewProjection = lightView * lightProjection; DataStream data; _immediateContext.MapSubresource(sceneConstantsBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out data); Marshal.StructureToPtr(sceneConstants, data.DataPointer, false); _immediateContext.UnmapSubresource(sceneConstantsBuffer, 0); data.Dispose(); sunLightDirectionVar.Set(new Vector4(sunLightDirection, 1)); Matrix overlayMatrix = Matrix.Scaling(info.Width / _width, info.Height / _height, 1.0f); overlayMatrix *= Matrix.Translation(-(_width - info.Width) / _width, (_height - info.Height) / _height, 0.0f); overlayViewProjectionVar.SetMatrix(overlayMatrix); lightProjectionVar.SetMatrixTranspose(ref sceneConstants.Projection); lightViewVar.SetMatrixTranspose(ref sceneConstants.View); lightViewInverseVar.SetMatrix(ref sceneConstants.ViewInverse); lightViewportWidthVar.Set(_width); lightViewportHeightVar.Set(_height); lightEyePositionVar.Set(ref eyePosition); float tanHalfFovY = (float)Math.Tan(FieldOfView * 0.5f); float tanHalfFovX = tanHalfFovY * AspectRatio; float projectionA = FarPlane / (FarPlane - _nearPlane); float projectionB = -projectionA * _nearPlane; Vector4 viewParameters = new Vector4(tanHalfFovX, tanHalfFovY, projectionA, projectionB); lightViewParametersVar.Set(ref viewParameters); viewportWidthVar.Set(_width); viewportHeightVar.Set(_height); viewParametersVar.Set(ref viewParameters); }
void IScene.Render() { Device device = _cachedDevice; if (device == null) { throw new InvalidOperationException("Rendering failed because device is null"); } if (device.IsDisposed) { throw new InvalidOperationException("Rendering failed because device is disposed"); } if (_altaxoCamera == null) { throw new InvalidOperationException("Rendering failed because camera is null"); } if (_altaxoDrawingGeometry == null) { throw new InvalidOperationException("Rendering failed because drawing is null"); } UseNextTriangleDeviceBuffers(); float time = _renderCounter / 100f; ++_renderCounter; Matrix worldViewProjTr; // world-view matrix, transposed if (null != _altaxoCamera) { var cam = _altaxoCamera; var eye = cam.EyePosition; var target = cam.TargetPosition; var up = cam.UpVector; //view = Matrix.LookAtRH(new Vector3((float)eye.X, (float)eye.Y, (float)eye.Z), new Vector3((float)target.X, (float)target.Y, (float)target.Z), new Vector3((float)up.X, (float)up.Y, (float)up.Z)); //var viewProjD3D = (cam as PerspectiveCamera).GetLookAtRHTimesPerspectiveRHMatrix(_hostSize.Y / _hostSize.X); var viewProjD3D = cam.GetViewProjectionMatrix(_hostSize.Y / _hostSize.X); worldViewProjTr = new Matrix( (float)viewProjD3D.M11, (float)viewProjD3D.M21, (float)viewProjD3D.M31, (float)viewProjD3D.M41, (float)viewProjD3D.M12, (float)viewProjD3D.M22, (float)viewProjD3D.M32, (float)viewProjD3D.M42, (float)viewProjD3D.M13, (float)viewProjD3D.M23, (float)viewProjD3D.M33, (float)viewProjD3D.M43, (float)viewProjD3D.M14, (float)viewProjD3D.M24, (float)viewProjD3D.M34, (float)viewProjD3D.M44 ); } else { var view = Matrix.LookAtRH(new Vector3(0, 0, -1500), new Vector3(0, 0, 0), Vector3.UnitY); var proj = Matrix.PerspectiveFovRH((float)Math.PI / 4.0f, (float)(_hostSize.X / _hostSize.Y), 0.1f, float.MaxValue); var viewProj = Matrix.Multiply(view, proj); // Update WorldViewProj Matrix worldViewProjTr = viewProj; worldViewProjTr.Transpose(); } // World projection and camera _evWorldViewProj.SetMatrixTranspose(ref worldViewProjTr); _evEyePosition.Set(ToVector3(_altaxoCamera.EyePosition)); // lighting _lighting.SetLighting(_altaxoLightSettings, _altaxoCamera); // Material is separate for each buffer, therefore it is set there foreach (var entry in _thisTriangleDeviceBuffers[1]) // Position-Color { DrawPositionColorIndexedTriangleBuffer(device, entry, worldViewProjTr); } foreach (var entry in _thisTriangleDeviceBuffers[3]) // Position-Normal { DrawPositionNormalIndexedTriangleBuffer(device, entry, worldViewProjTr); } foreach (var entry in _thisTriangleDeviceBuffers[4]) // Position-Normal-Color { DrawPositionNormalColorIndexedTriangleBuffer(device, entry, worldViewProjTr); } foreach (var entry in _thisTriangleDeviceBuffers[6]) // Position-Normal-U { DrawPositionNormalUIndexedTriangleBuffer(device, entry, worldViewProjTr); } // ------------------ end of document geometry drawing ---------------------------------- // ------------------ start of marker geometry drawing ---------------------------------- var markerTriangles = _markerGeometryTriangleDeviceBuffer; if (null != markerTriangles) { DrawPositionColorIndexedTriangleBufferNoMaterial(device, markerTriangles, worldViewProjTr); } var markerLines = _markerGeometryLineListBuffer; if (null != markerLines && markerLines.VertexCount > 0) { DrawPositionColorLineListBufferNoMaterial(device, markerLines, worldViewProjTr); } // ------------------ end of marker geometry drawing ---------------------------------- // ------------------ start of overlay geometry drawing ---------------------------------- var overlayTriangles = _overlayGeometryTriangleDeviceBuffer; if (null != overlayTriangles) { DrawPositionColorIndexedTriangleBufferNoMaterial(device, overlayTriangles, worldViewProjTr); } var overlayLines = _overlayGeometryLineListBuffer; if (null != overlayLines && overlayLines.VertexCount > 0) { DrawPositionColorLineListBufferNoMaterial(device, overlayLines, worldViewProjTr); } // ------------------ end of overlay geometry drawing ---------------------------------- }