private void ComputeDisplayParametersForThumbnail() { if (currentConstellation == "Error") { return; } IPlace target = Constellations.ConstellationCentroids[currentConstellation]; Lineset boundries = Constellations.boundries[currentConstellation]; Vector3[] points = new Vector3[boundries.Points.Count]; Vector3[] pointsOut = new Vector3[boundries.Points.Count]; for (int i = 0; i < points.Length; i++) { points[i] = Coordinates.GeoTo3d(boundries.Points[i].Dec, boundries.Points[i].RA); } Matrix mat = Matrix.RotationY((float)(-(24 - (target.RA + 6)) / 12 * Math.PI)); mat = Matrix.Multiply(mat, Matrix.RotationX((float)(target.Lat / 180f * Math.PI))); mat = Matrix.Multiply(mat, Matrix.Scaling(50, -50, 50)); mat = Matrix.Multiply(mat, Matrix.Translation(48, 22, 0)); // mat.Translate(58, 33,0); Vector3.TransformCoordinate(points, ref mat, pointsOut); lines = new PointF[points.Length + 1]; int index = 0; foreach (Vector3 point in pointsOut) { lines[index++] = new PointF((float)point.X, (float)point.Y); } lines[index] = lines[0]; }
public void SetViewRect(Coordinates[] corners) { if (corners.Length != 4) { return; } if (string.IsNullOrEmpty(currentConstellation)) { return; } bool change = false; if (this.corners != null) { for (int i = 0; i < 4; i++) { if (this.corners[i] != corners[i]) { change = true; break; } } } else { change = true; } if (!change) { return; } this.corners = corners; IPlace target = Constellations.ConstellationCentroids[currentConstellation]; Vector3[] points = new Vector3[4]; Vector3[] pointsOut = new Vector3[4]; for (int i = 0; i < 4; i++) { points[i] = Coordinates.GeoTo3d(this.corners[i].Dec, ((this.corners[i].RA / 24.0 * 360) - 180)); } Matrix mat = Matrix.RotationY((float)(-(24 - (target.RA + 6)) / 12 * Math.PI)); mat = Matrix.Multiply(mat, Matrix.RotationX((float)(target.Lat / 180f * Math.PI))); mat = Matrix.Multiply(mat, Matrix.Scaling(60, -60, 60)); mat = Matrix.Multiply(mat, Matrix.Translation(58, 33, 0)); Vector3.TransformCoordinate(points, ref mat, pointsOut); fov = new PointF[5]; int index = 0; foreach (Vector3 point in pointsOut) { fov[index++] = new PointF((float)point.X, (float)point.Y); } fov[4] = fov[0]; //Calculate view length double a = fov[0].X - fov[1].X; double b = fov[0].Y - fov[1].Y; double c = Math.Sqrt(a * a + b * b); RefreshHint(); }
public void InitializeD3D11(IntPtr wndHandle, int width, int height) { CloseD3D11(); _dxgiFactory = new Factory1(); _dxiAdapter = _dxgiFactory.Adapters[0]; _d3d11Device = new Device(_dxiAdapter, DeviceCreationFlags.BgraSupport, FeatureLevel.Level_11_0); _dxgiDevice = _d3d11Device.QueryInterface <DXGIDevice>(); _dxgiDevice.MaximumFrameLatency = 1; // Compile Vertex and Pixel shaders var vertexShaderByteCode = ShaderBytecode.CompileFromFile("VSShader.fx", "main", "vs_4_0", ShaderFlags.None, EffectFlags.None); _vertexShader = new VertexShader(_d3d11Device, vertexShaderByteCode); var pixelShaderByteCode = ShaderBytecode.CompileFromFile("PSShader.fx", "main", "ps_4_0", ShaderFlags.None, EffectFlags.None); _pixelShader = new PixelShader(_d3d11Device, pixelShaderByteCode); InputElement[] inputElements = new InputElement[3]; inputElements[0] = new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, InputClassification.PerVertexData, 0); inputElements[1] = new InputElement("TEXCOORD", 0, Format.R32G32_Float, 12, 0, InputClassification.PerVertexData, 0); inputElements[2] = new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 20, 0, InputClassification.PerVertexData, 0); _inputLayout = new InputLayout(_d3d11Device, vertexShaderByteCode, inputElements); BufferDescription vertexShaderDesc = new BufferDescription(Matrix.SizeInBytes * 2, BindFlags.ConstantBuffer, ResourceUsage.Default); _vertexShaderConstans = new Buffer(_d3d11Device, vertexShaderDesc); SamplerStateDescription samplerStateDescription = new SamplerStateDescription(); samplerStateDescription.Filter = Filter.MinMagMipLinear; samplerStateDescription.AddressU = TextureAddressMode.Clamp; samplerStateDescription.AddressV = TextureAddressMode.Clamp; samplerStateDescription.AddressW = TextureAddressMode.Clamp; samplerStateDescription.MipLodBias = 0.0f; samplerStateDescription.MaximumAnisotropy = 1; samplerStateDescription.ComparisonFunction = Comparison.Always; samplerStateDescription.MinimumLod = 0.0f; samplerStateDescription.MaximumLod = float.MaxValue; _samplerState = new SamplerState(_d3d11Device, samplerStateDescription); RasterizerStateDescription rasterizerStateDescription = new RasterizerStateDescription(); rasterizerStateDescription.IsAntialiasedLineEnabled = false; rasterizerStateDescription.CullMode = CullMode.None; rasterizerStateDescription.DepthBias = 0; rasterizerStateDescription.DepthBiasClamp = 0.0f; rasterizerStateDescription.IsDepthClipEnabled = true; rasterizerStateDescription.FillMode = FillMode.Solid; rasterizerStateDescription.IsFrontCounterClockwise = false; rasterizerStateDescription.IsMultisampleEnabled = false; rasterizerStateDescription.IsScissorEnabled = false; rasterizerStateDescription.SlopeScaledDepthBias = 0.0f; _rasterizerState = new RasterizerState(_d3d11Device, rasterizerStateDescription); _d3d11Device.ImmediateContext.InputAssembler.InputLayout = _inputLayout; _d3d11Device.ImmediateContext.VertexShader.SetShader(_vertexShader, null, 0); _d3d11Device.ImmediateContext.VertexShader.SetConstantBuffers(0, 1, _vertexShaderConstans); SwapChainDescription swapChainDescription = new SwapChainDescription(); swapChainDescription.ModeDescription.Width = width; swapChainDescription.ModeDescription.Height = height; swapChainDescription.ModeDescription.Format = Format.B8G8R8A8_UNorm; swapChainDescription.ModeDescription.RefreshRate.Numerator = 1; //pretty ugly //its better to autodetect screen refresh rate swapChainDescription.ModeDescription.RefreshRate.Denominator = 60; swapChainDescription.SampleDescription.Count = 1; swapChainDescription.SampleDescription.Quality = 0; swapChainDescription.Usage = Usage.RenderTargetOutput; swapChainDescription.BufferCount = 2; swapChainDescription.ModeDescription.Scaling = DisplayModeScaling.Unspecified; swapChainDescription.SwapEffect = SwapEffect.FlipSequential; swapChainDescription.Flags = 0; swapChainDescription.IsWindowed = true; swapChainDescription.OutputHandle = wndHandle; _swapChain = new SwapChain(_dxgiFactory, _d3d11Device, swapChainDescription); _dxgiFactory.MakeWindowAssociation(wndHandle, WindowAssociationFlags.IgnoreAll); Texture2D backBuffer = _swapChain.GetBackBuffer <Texture2D>(0); _mainRenderTargerView = new RenderTargetView(_d3d11Device, backBuffer); backBuffer.Dispose(); backBuffer = null; Matrix projection = Matrix.Identity; Matrix view = new Matrix(); /* Update the view matrix */ view[0, 0] = 2.0f / (float)width; view[0, 1] = 0.0f; view[0, 2] = 0.0f; view[0, 3] = 0.0f; view[1, 0] = 0.0f; view[1, 1] = -2.0f / (float)height; view[1, 2] = 0.0f; view[1, 3] = 0.0f; view[2, 0] = 0.0f; view[2, 1] = 0.0f; view[2, 2] = 1.0f; view[2, 3] = 0.0f; view[3, 0] = -1.0f; view[3, 1] = 1.0f; view[3, 2] = 0.0f; view[3, 3] = 1.0f; VertexShaderConstants vertexShaderConstansData = new VertexShaderConstants(); vertexShaderConstansData.projectionAndView = Matrix.Multiply(view, projection); vertexShaderConstansData.model = Matrix.Identity; _vertexShaderConstansData = Marshal.AllocHGlobal(Marshal.SizeOf(vertexShaderConstansData)); Marshal.StructureToPtr(vertexShaderConstansData, _vertexShaderConstansData, false); _d3d11Device.ImmediateContext.UpdateSubresource(ref vertexShaderConstansData, _vertexShaderConstans); ViewPort viewPort = new ViewPort(); viewPort.X = 0; viewPort.Y = 0; viewPort.Width = width; viewPort.Height = height; viewPort.MinDepth = 0.0f; viewPort.MaxDepth = 1.0f; _d3d11Device.ImmediateContext.Rasterizer.SetViewport(viewPort); float minu, maxu, minv, maxv; minu = 0.0f; maxu = 1.0f; minv = 0.0f; maxv = 1.0f; // Instantiate Vertex buiffer from vertex data var vertices = Buffer.Create(_d3d11Device, BindFlags.VertexBuffer, new[] { //ul 0.0f, 0.0f, 0.0f, minu, minv, 1.0f, 1.0f, 1.0f, 1.0f, //dl 0.0f, (float)height, 0.0f, minu, maxv, 1.0f, 1.0f, 1.0f, 1.0f, //ur (float)width, 0.0f, 0.0f, maxu, minv, 1.0f, 1.0f, 1.0f, 1.0f, //dr (float)width, (float)height, 0.0f, maxu, maxv, 1.0f, 1.0f, 1.0f, 1.0f }); _d3d11Device.ImmediateContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, 36, 0)); _d3d11Device.ImmediateContext.Rasterizer.State = _rasterizerState; _d3d11Device.ImmediateContext.PixelShader.SetShader(_pixelShader, null, 0); _d3d11Device.ImmediateContext.PixelShader.SetSamplers(0, 1, _samplerState); _d3d11Device.ImmediateContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip; }