protected override void OnLoad(EventArgs e) { base.OnLoad(e); // cache location etc for when we come out of fullscreen windowedLocation = Location; windowedSize = Size; // pick up view and projection for projector view = new SharpDX.Matrix(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { view[i, j] = (float)projector.pose[i, j]; } } view.Invert(); view.Transpose(); var cameraMatrix = projector.cameraMatrix; float fx = (float)cameraMatrix[0, 0]; float fy = (float)cameraMatrix[1, 1]; float cx = (float)cameraMatrix[0, 2]; float cy = (float)cameraMatrix[1, 2]; float near = 0.1f; float far = 100.0f; float w = projector.width; float h = projector.height; projection = GraphicsTransforms.ProjectionMatrixFromCameraMatrix(fx, fy, cx, cy, w, h, near, far); projection.Transpose(); }
/// <summary> /// Transforms the absolute position to a position relative to transport object the player is on /// </summary> /// <param name="loc">The location</param> /// <returns>The location relative to the transport the player is on</returns> public static Location GetRelativeToPlayerTransport(this Location loc) { var player = ObjectManager.Instance.Player; var transport = player?.CurrentTransport; if (transport == null) { return(null); } Matrix inverse; var transportMatrix = transport.TransportMatrix; Matrix.Invert(ref transportMatrix, out inverse); var x = inverse.M31 * loc.Z + inverse.M11 * loc.X + inverse.M21 * loc.Y + inverse.M41; var y = inverse.M32 * loc.Z + inverse.M12 * loc.X + inverse.M22 * loc.Y + inverse.M42; var z = inverse.M33 * loc.Z + inverse.M13 * loc.X + inverse.M23 * loc.Y + inverse.M43; return(new Location(x, y, z)); }
// could be method on Projector: void SetViewProjectionFromProjector(ProjectorCameraEnsemble.Projector projector) { // pick up view and projection for a given projector view = new SharpDX.Matrix(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { view[i, j] = (float)projector.pose[i, j]; } } view.Invert(); view.Transpose(); var cameraMatrix = projector.cameraMatrix; float fx = (float)cameraMatrix[0, 0]; float fy = (float)cameraMatrix[1, 1]; float cx = (float)cameraMatrix[0, 2]; float cy = (float)cameraMatrix[1, 2]; float near = 0.1f; float far = 100.0f; float w = projector.width; float h = projector.height; projection = ProjectionMatrixFromCameraMatrix(fx, fy, cx, cy, w, h, near, far); projection.Transpose(); }
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 SetSceneConstants() { FreeLook freelook = Demo.Freelook; Vector3 up = MathHelper.Convert(freelook.Up); sceneConstants.View = Matrix.LookAtLH(MathHelper.Convert(freelook.Eye), MathHelper.Convert(freelook.Target), up); sceneConstants.Projection = Matrix.PerspectiveFovLH(FieldOfView, AspectRatio, _nearPlane, FarPlane); sceneConstants.ViewInverse = Matrix.Invert(sceneConstants.View); Vector3 light = new Vector3(20, 30, 10); Texture2DDescription depthBuffer = lightDepthTexture.Description; Matrix lightView = Matrix.LookAtLH(light, Vector3.Zero, up); Matrix lightProjection = Matrix.OrthoLH(depthBuffer.Width / 8.0f, depthBuffer.Height / 8.0f, _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(); inverseProjectionVar.SetMatrix(Matrix.Invert(sceneConstants.Projection)); inverseViewVar.SetMatrix(sceneConstants.ViewInverse); lightInverseViewProjectionVar.SetMatrix(Matrix.Invert(sceneConstants.LightViewProjection)); lightPositionVar.Set(new Vector4(light, 1)); eyePositionVar.Set(new Vector4(MathHelper.Convert(freelook.Eye), 1)); eyeZAxisVar.Set(new Vector4(Vector3.Normalize(MathHelper.Convert(freelook.Target - freelook.Eye)), 1)); float tanHalfFOVY = (float)Math.Tan(FieldOfView * 0.5f); tanHalfFOVXVar.Set(tanHalfFOVY * AspectRatio); tanHalfFOVYVar.Set(tanHalfFOVY); float projectionA = FarPlane / (FarPlane - _nearPlane); float projectionB = -projectionA * _nearPlane; projectionAVar.Set(projectionA); projectionBVar.Set(projectionB); 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); }
void SetViewProjectionFromCamera(ProjectorCameraEnsemble.Camera camera) { view = new SharpDX.Matrix(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { view[i, j] = (float)camera.pose[i, j]; } } view.Invert(); view.Transpose(); float aspect = (float)videoPanel1.Width / (float)videoPanel1.Height; projection = PerspectiveFov(35.0f / 180.0f * (float)Math.PI, aspect, 0.1f, 100.0f); projection.Transpose(); }
void SetSceneConstants() { FreeLook freelook = Demo.Freelook; Vector3 up = MathHelper.Convert(freelook.Up); sceneConstants.View = Matrix.LookAtLH(MathHelper.Convert(freelook.Eye), MathHelper.Convert(freelook.Target), up); sceneConstants.Projection = Matrix.PerspectiveFovLH(FieldOfView, AspectRatio, _nearPlane, FarPlane); sceneConstants.ViewInverse = Matrix.Invert(sceneConstants.View); Vector3 light = new Vector3(20, 30, 10); Texture2DDescription depthBuffer = lightDepthTexture.Description; Matrix lightView = Matrix.LookAtLH(light, Vector3.Zero, up); Matrix lightProjection = Matrix.OrthoLH(depthBuffer.Width / 8.0f, depthBuffer.Height / 8.0f, _nearPlane, FarPlane); sceneConstants.LightViewProjection = lightView * lightProjection; using (var data = sceneConstantsBuffer.Map(MapMode.WriteDiscard)) { Marshal.StructureToPtr(sceneConstants, data.DataPointer, false); sceneConstantsBuffer.Unmap(); } effect2.GetVariableByName("InverseProjection").AsMatrix().SetMatrix(Matrix.Invert(sceneConstants.Projection)); effect2.GetVariableByName("InverseView").AsMatrix().SetMatrix(sceneConstants.ViewInverse); effect2.GetVariableByName("LightInverseViewProjection").AsMatrix().SetMatrix(Matrix.Invert(sceneConstants.LightViewProjection)); effect2.GetVariableByName("LightPosition").AsVector().Set(new Vector4(light, 1)); effect2.GetVariableByName("EyePosition").AsVector().Set(new Vector4(MathHelper.Convert(freelook.Eye), 1)); effect2.GetVariableByName("EyeZAxis").AsVector().Set(new Vector4(Vector3.Normalize(MathHelper.Convert(freelook.Target - freelook.Eye)), 1)); float tanHalfFOVY = (float)Math.Tan(FieldOfView * 0.5f); effect2.GetVariableByName("TanHalfFOVX").AsScalar().Set(tanHalfFOVY * AspectRatio); effect2.GetVariableByName("TanHalfFOVY").AsScalar().Set(tanHalfFOVY); float projectionA = FarPlane / (FarPlane - _nearPlane); float projectionB = -projectionA * _nearPlane; effect2.GetVariableByName("ProjectionA").AsScalar().Set(projectionA); effect2.GetVariableByName("ProjectionB").AsScalar().Set(projectionB); Matrix overlayMatrix = Matrix.Scaling(2 * info.Width / _width, 2 * info.Height / _height, 1.0f); overlayMatrix *= Matrix.Translation(-(_width - info.Width) / _width, (_height - info.Height) / _height, 0.0f); effect2.GetVariableByName("OverlayViewProjection").AsMatrix().SetMatrix(overlayMatrix); }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); // cache location etc for when we come out of fullscreen windowedLocation = Location; windowedSize = Size; // pick up view and projection for projector view = new SharpDX.Matrix(); for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) view[i, j] = (float)projector.pose[i, j]; view.Invert(); view.Transpose(); var cameraMatrix = projector.cameraMatrix; float fx = (float)cameraMatrix[0, 0]; float fy = (float)cameraMatrix[1, 1]; float cx = (float)cameraMatrix[0, 2]; float cy = (float)cameraMatrix[1, 2]; float near = 0.1f; float far = 100.0f; float w = projector.width; float h = projector.height; projection = GraphicsTransforms.ProjectionMatrixFromCameraMatrix(fx, fy, cx, cy, w, h, near, far); projection.Transpose(); }