public void SetProject(ProjectMV project)
 {
     _translation = new Vector2(-project.Width / 2.0f, -project.Height / 2.0f);
     _zoom        = 1.0f;
     OnResize();
 }
Beispiel #2
0
        public void Render(DeviceContext deviceContext, ProjectMV project)
        {
            var   camera = project.Camera;
            float bottom = camera.ProjectY(0.0f);
            float top    = camera.ProjectY(project.Height);
            float left   = camera.ProjectX(0.0f);
            float right  = camera.ProjectX(project.Width);

            Matrix     linesWorld;
            Matrix     projection = camera.Projection;
            Matrix     worldViewProjection;
            DataStream dataStream;

            #region Boundary Lines

            deviceContext.InputAssembler.InputLayout = _inputLayoutLines;
            deviceContext.VertexShader.Set(_vertexShaderLines);
            deviceContext.HullShader.Set(null);
            deviceContext.DomainShader.Set(null);
            deviceContext.PixelShader.Set(_pixelShaderLines);

            deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineStrip;
            deviceContext.InputAssembler.SetVertexBuffers(0, _bufferBindingBoundaryLines);

            deviceContext.VertexShader.SetConstantBuffers(0, _globalBufferLines);
            deviceContext.PixelShader.SetConstantBuffers(0, _globalBufferLines);

            linesWorld     = Matrix.Identity;
            linesWorld.M11 = right - left;
            linesWorld.M22 = -top + bottom;
            linesWorld.M41 = left - camera.Width / 2.0f;
            linesWorld.M42 = -bottom + camera.Height / 2.0f;

            Matrix.Multiply(ref linesWorld, ref projection, out worldViewProjection);
            // Transpose local Matrices
            Matrix.Transpose(ref worldViewProjection, out worldViewProjection);

            dataStream = null;
            deviceContext.MapSubresource(_globalBufferLines, 0, MapMode.WriteDiscard, MapFlags.None, out dataStream);
            dataStream.Write(worldViewProjection);
            dataStream.Write(_colorBoundaryLines);
            deviceContext.UnmapSubresource(_globalBufferLines, 0);

            deviceContext.Draw(5, 0);

            #endregion
            #region  Render Background
            Vector4 workingRect = new Vector4(bottom, top, left, right);
            Vector4 metrics     = new Vector4(borderWidth, -borderWidth, camera.Width, -camera.Height);


            Matrix backgroundWorld = Matrix.Identity;
            backgroundWorld.M41 = -camera.Width / 2.0f;
            backgroundWorld.M42 = camera.Height / 2.0f;


            Matrix.Multiply(ref backgroundWorld, ref projection, out worldViewProjection);
            // Transpose local Matrices
            Matrix.Transpose(ref worldViewProjection, out worldViewProjection);

            deviceContext.VertexShader.SetConstantBuffers(0, _globalBufferBackground);
            deviceContext.PixelShader.SetConstantBuffers(0, _globalBufferBackground);

            deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            deviceContext.InputAssembler.InputLayout       = _inputLayoutBackground;
            deviceContext.VertexShader.Set(_vertexShaderBackground);
            deviceContext.HullShader.Set(null);
            deviceContext.DomainShader.Set(null);
            deviceContext.PixelShader.Set(_pixelShaderBackground);

            deviceContext.InputAssembler.SetIndexBuffer(_indexBufferBackground, Format.R32_UInt, 0);
            deviceContext.InputAssembler.SetVertexBuffers(0, _bufferBindingBackground);

            dataStream = null;
            deviceContext.MapSubresource(_globalBufferBackground, 0, MapMode.WriteDiscard, MapFlags.None, out dataStream);
            dataStream.Write(worldViewProjection);
            dataStream.Write(_color1Background);
            dataStream.Write(_color2Background);
            dataStream.Write(workingRect);
            dataStream.Write(metrics);
            deviceContext.UnmapSubresource(_globalBufferBackground, 0);

            deviceContext.DrawIndexed(18, 0, 0);

            #endregion
            #region Scala Lines
            deviceContext.VertexShader.SetConstantBuffers(0, _globalBufferLines);
            deviceContext.PixelShader.SetConstantBuffers(0, _globalBufferLines);

            deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineList;
            deviceContext.InputAssembler.InputLayout       = _inputLayoutLines;
            deviceContext.VertexShader.Set(_vertexShaderLines);
            deviceContext.HullShader.Set(null);
            deviceContext.DomainShader.Set(null);
            deviceContext.PixelShader.Set(_pixelShaderLines);

            deviceContext.InputAssembler.SetVertexBuffers(0, _bufferBindingScalaLines);

            // Top Lines
            linesWorld     = Matrix.Identity;
            linesWorld.M42 = camera.Height / 2.0f - 15.0f;
            linesWorld.M22 = 10.0f;
            // Compute Zoom

            float level;
            float scala;
            computeScala(camera.Zoom, out scala, out level);

            float shiftStepsX = (float)Math.Floor(left / scala);
            float shiftX      = shiftStepsX * scala;
            int   endIndex    = Math.Min((int)(camera.Width / scala), _countScalaElements) + 2;
            //int beginIndex = 20 - (int)((left- shift) / 10.0f);
            //int beginIndex = 20 - (int)((left- shift) * scala / 1000.0f);
            int beginIndex = 0;

            linesWorld.M11 = scala;
            linesWorld.M41 = -camera.Width / 2.0f + left - shiftX - scala;

            Matrix.Multiply(ref linesWorld, ref projection, out worldViewProjection);
            // Transpose local Matrices
            Matrix.Transpose(ref worldViewProjection, out worldViewProjection);

            dataStream = null;
            deviceContext.MapSubresource(_globalBufferLines, 0, MapMode.WriteDiscard, MapFlags.None, out dataStream);
            dataStream.Write(worldViewProjection);
            dataStream.Write(_colorScalaLines);
            deviceContext.UnmapSubresource(_globalBufferLines, 0);

            deviceContext.Draw(20 * endIndex, beginIndex * 2);


            // Left Lines
            float shiftStepsY = (float)Math.Floor((camera.Height - bottom) / scala + 1.0f);
            float shiftY      = shiftStepsY * scala;
            endIndex = Math.Min((int)(camera.Height / scala), _countScalaElements) + 2;

            linesWorld     = Matrix.Identity;
            linesWorld.M42 = +camera.Height / 2.0f - bottom - shiftY;
            linesWorld.M12 = scala;


            linesWorld.M41 = -camera.Width / 2.0f + 15.0f;
            linesWorld.M11 = 0.0f;
            linesWorld.M22 = 0.0f;
            linesWorld.M21 = -10.0f;

            Matrix.Multiply(ref linesWorld, ref projection, out worldViewProjection);
            // Transpose local Matrices
            Matrix.Transpose(ref worldViewProjection, out worldViewProjection);

            dataStream = null;
            deviceContext.MapSubresource(_globalBufferLines, 0, MapMode.WriteDiscard, MapFlags.None, out dataStream);
            dataStream.Write(worldViewProjection);
            dataStream.Write(_colorScalaLines);
            deviceContext.UnmapSubresource(_globalBufferLines, 0);

            deviceContext.Draw(20 * endIndex, 0);
            #endregion
            #region Curser Lines
            if (_drawCursorLines)
            {
                //Vector2 curserpos = camera.UnProject(project.CurserPos);

                metrics = new Vector4(borderWidth, -borderWidth, (float)project.CurserPos.X, -(float)project.CurserPos.Y);
                Matrix cursorWorld = Matrix.Identity;
                cursorWorld.M41 = -camera.Width / 2.0f;
                cursorWorld.M42 = camera.Height / 2.0f;


                Matrix.Multiply(ref cursorWorld, ref projection, out worldViewProjection);
                // Transpose local Matrices
                Matrix.Transpose(ref worldViewProjection, out worldViewProjection);

                deviceContext.VertexShader.SetConstantBuffers(0, _globalBufferCursorLines);
                deviceContext.PixelShader.SetConstantBuffers(0, _globalBufferCursorLines);

                deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineList;
                deviceContext.InputAssembler.InputLayout       = _inputLayoutCursorLines;
                deviceContext.VertexShader.Set(_vertexShaderCursorLines);
                deviceContext.HullShader.Set(null);
                deviceContext.DomainShader.Set(null);
                deviceContext.PixelShader.Set(_pixelShaderCursorLines);

                deviceContext.InputAssembler.SetVertexBuffers(0, _bufferBindingCursorLines);

                dataStream = null;
                deviceContext.MapSubresource(_globalBufferCursorLines, 0, MapMode.WriteDiscard, MapFlags.None, out dataStream);
                dataStream.Write(worldViewProjection);
                dataStream.Write(_colorCursorLines);
                dataStream.Write(metrics);
                deviceContext.UnmapSubresource(_globalBufferCursorLines, 0);

                deviceContext.Draw(4, 0);
            }
            #endregion
            #region Top Numbers
            int   digits = Math.Max(-(int)Math.Floor(Math.Log10(level)) + 0, 0);
            float valueX = -shiftStepsX * level;

            float firstX = left - shiftX;
            int   countX = (int)((camera.Width + 30.0f) / scala);

            for (int i = 0; i < countX; i++)
            {
                float posX = firstX + 5.0f + scala * i;
                _numbersRenderer.Draw(valueX, posX, camera.Height - 10.0f, deviceContext, camera, digits);
                valueX += level;
            }
            // Left Numbers
            float valueY = -shiftStepsY * level;

            float firstY = (bottom - shiftY);
            int   countY = (int)((camera.Height + 30.0f) / scala);

            //for (int i = 0; i < countY; i++)
            //{
            //	float posY = firstY - scala * i;
            //	//_numbersRenderer.Draw(valueY, 12.0f, -posY, deviceContext, camera, true);
            //	_numbersRenderer.Draw(valueY, 12.0f, camera.Height/2.0f - posY - 10.0f, deviceContext, camera, true);

            //	valueY += level;
            //}
            for (int i = 1; i < countY + 2; i++)
            {
                _numbersRenderer.Draw(level * (-shiftStepsY + i), 10.0f, camera.Height - (bottom + shiftY) + 5.0f + scala * i, deviceContext, camera, digits, true);
            }
            #endregion
        }