/// <summary> /// Renders the cubemap in one pass using a geometry shader. /// </summary> /// <param name="context">The render context.</param> /// <param name="entity">The entity the cubemap is attached to.</param> /// <param name="component">The CubemapSource component.</param> private void RenderInSinglePass(RenderContext context, Entity entity, CubemapSourceComponent component) { var cameraPos = component.Entity.Transformation.Translation; for (var i = 0; i < 6; ++i) { Matrix worldToCamera; Matrix projection; ComputeViewProjectionMatrices(cameraPos, TargetPositions[i], CameraUps[i], component, out worldToCamera, out projection); cameraViewProjMatrices[i] = worldToCamera * projection; } // TODO: set parameters on another collection? GraphicsDevice.Parameters.Set(CameraCubeKeys.CameraViewProjectionMatrices, cameraViewProjMatrices); GraphicsDevice.Parameters.Set(CameraCubeKeys.CameraWorldPosition, cameraPos); if (component.RenderTarget == null) { component.CreateFullViewRenderTarget(); } GraphicsDevice.Clear(component.DepthStencil, DepthStencilClearOptions.DepthBuffer); GraphicsDevice.Clear(component.RenderTarget, Color.Black); GraphicsDevice.SetRenderTargets(component.DepthStencil, component.RenderTarget); base.OnRendering(context); GraphicsDevice.SetRenderTarget(GraphicsDevice.DepthStencilBuffer, GraphicsDevice.BackBuffer); GraphicsDevice.Parameters.Remove(CameraCubeKeys.CameraViewProjectionMatrices); GraphicsDevice.Parameters.Remove(CameraCubeKeys.CameraWorldPosition); }
/// <summary> /// Renders the cubemap in 6 passes, one for each face. /// </summary> /// <param name="context">The render context.</param> /// <param name="entity">The entity the cubemap is attached to.</param> /// <param name="component">The CubemapSource component.</param> private void RenderInSixPasses(RenderContext context, Entity entity, CubemapSourceComponent component) { var cameraPos = entity.Transformation.Translation; for (var i = 0; i < 6; ++i) { Matrix worldToCamera; Matrix projection; ComputeViewProjectionMatrices(cameraPos, TargetPositions[i], CameraUps[i], component, out worldToCamera, out projection); // TODO: set parameters on another collection? GraphicsDevice.Parameters.Set(TransformationKeys.View, worldToCamera); GraphicsDevice.Parameters.Set(TransformationKeys.Projection, projection); GraphicsDevice.Parameters.Set(CameraKeys.NearClipPlane, component.NearPlane); GraphicsDevice.Parameters.Set(CameraKeys.FarClipPlane, component.FarPlane); GraphicsDevice.Parameters.Set(CameraKeys.FieldOfView, MathUtil.PiOverTwo); GraphicsDevice.Parameters.Set(CameraKeys.Aspect, 1); if (component.RenderTargets == null) { component.CreateSingleViewRenderTargets(); } var renderTarget = component.RenderTargets[i]; GraphicsDevice.Clear(component.DepthStencil, DepthStencilClearOptions.DepthBuffer); GraphicsDevice.Clear(renderTarget, Color.Black); GraphicsDevice.SetRenderTargets(component.DepthStencil, renderTarget); // TODO: flip face culling base.OnRendering(context); } GraphicsDevice.SetRenderTarget(GraphicsDevice.DepthStencilBuffer, GraphicsDevice.BackBuffer); GraphicsDevice.Parameters.Remove(TransformationKeys.View); GraphicsDevice.Parameters.Remove(TransformationKeys.Projection); GraphicsDevice.Parameters.Remove(CameraKeys.NearClipPlane); GraphicsDevice.Parameters.Remove(CameraKeys.FarClipPlane); GraphicsDevice.Parameters.Remove(CameraKeys.FieldOfView); GraphicsDevice.Parameters.Remove(CameraKeys.Aspect); GraphicsDevice.Parameters.Remove(CameraKeys.FocusDistance); }
private static void ComputeViewProjectionMatrices(Vector3 position, Vector3 faceOffset, Vector3 up, CubemapSourceComponent source, out Matrix viewMatrix, out Matrix projection) { viewMatrix = Matrix.LookAtRH(position, position + faceOffset, up); Matrix.PerspectiveFovRH(MathUtil.PiOverTwo, 1, source.NearPlane, source.FarPlane, out projection); }