Beispiel #1
0
        void Application_Idle(object sender, EventArgs e)
        {
            if (m_device == null)
            {
                return;
            }

            m_CB_Simulation.m._deltaTime       = floatTrackbarControlDeltaTime.Value;
            m_CB_Simulation.m._springConstant  = floatTrackbarControlSpringConstant.Value;
            m_CB_Simulation.m._dampingConstant = floatTrackbarControlDampingConstant.Value;
            m_CB_Simulation.m._restDistance    = floatTrackbarControlRestDistance.Value;
            m_CB_Simulation.m._K.Set(floatTrackbarControlK0.Value, floatTrackbarControlK1.Value, floatTrackbarControlK2.Value, floatTrackbarControlK3.Value);
            m_CB_Simulation.UpdateData();


//          Point	clientPos = panelOutput.PointToClient( Control.MousePosition );
//          m_CB_Main.m.mousePosition.Set( GRAPH_SIZE * (float) clientPos.X / panelOutput.Width, GRAPH_SIZE * (float) clientPos.Y / panelOutput.Height );
//          m_CB_Main.m.mouseButtons = (uint) ((((Control.MouseButtons & MouseButtons.Left) != 0) ? 1 : 0)
// //											| (((Control.MouseButtons & MouseButtons.Middle) != 0) ? 2 : 0)
//                                          | (m_plotSource ? 2 : 0)
//                                          | (((Control.MouseButtons & MouseButtons.Right) != 0) ? 4 : 0)
//                                          | (Control.ModifierKeys == Keys.Shift ? 8 : 0));
//          m_CB_Main.m.diffusionCoefficient = floatTrackbarControlDiffusionCoefficient.Value;
//          m_CB_Main.m.flags = (uint) (
//                                    (checkBoxShowSearch.Checked ? 1 : 0)
//
//                                    // 2 bits to select 4 display modes
//                                  | (radioButtonShowNormalizedSpace.Checked ? 2 : 0)
//                                  | (radioButtonShowResultsSpace.Checked ? 4 : 0)
//
//                                  | (checkBoxShowLog.Checked ? 8 : 0)
//                              );
//          m_CB_Main.m.sourceIndex = (uint) integerTrackbarControlSimulationSourceIndex.Value;
//          m_CB_Main.m.sourcesCount = (uint) m_simulationHotSpots.Count;
//          m_CB_Main.m.resultsConfinementDistance = floatTrackbarControlResultsSpaceConfinement.Value;

            //////////////////////////////////////////////////////////////////////////
            // Perform simulation
            if (checkBoxRun.Checked && m_shader_ComputeForces.Use())
            {
                // Compute forces
                m_SB_Nodes.SetInput(1);
                m_SB_Links.SetInput(2);

                m_SB_NodeSims[0].SetInput(0);                           // Input positions & velocities
                m_SB_NodeSims[1].SetOutput(0);                          // Output positions & velocities

                m_SB_Forces.SetOutput(1);                               // Simulation forces

                uint groupsCount = (m_nodesCount + 255) >> 8;
                m_shader_ComputeForces.Dispatch(groupsCount, 1, 1);

                // Apply forces
                m_shader_Simulate.Use();
                m_shader_Simulate.Dispatch(groupsCount, 1, 1);

                m_SB_NodeSims[0].RemoveFromLastAssignedSlots();
                m_SB_NodeSims[1].RemoveFromLastAssignedSlotUAV();
                m_SB_Forces.RemoveFromLastAssignedSlotUAV();

                // Swap simulation buffers
                StructuredBuffer <SB_NodeSim> temp = m_SB_NodeSims[0];
                m_SB_NodeSims[0] = m_SB_NodeSims[1];
                m_SB_NodeSims[1] = temp;
            }


            //////////////////////////////////////////////////////////////////////////
            // Render
#if RENDER_GRAPH_PROPER
            if (m_shader_RenderGraphLink.Use())
            {
                m_device.SetRenderStates(RASTERIZER_STATE.CULL_NONE, DEPTHSTENCIL_STATE.READ_WRITE_DEPTH_LESS, BLEND_STATE.DISABLED);
                m_device.SetRenderTarget(m_device.DefaultTarget, m_device.DefaultDepthStencil);

                m_device.Clear(float4.Zero);
                m_device.ClearDepthStencil(m_device.DefaultDepthStencil, 1.0f, 0, true, false);

                m_SB_NodeSims[0].SetInput(0);
                m_SB_Nodes.SetInput(1);
                m_SB_Links.SetInput(2);
                m_SB_LinkSources.SetInput(3);

                m_tex_FalseColors.SetPS(4);

                m_device.ScreenQuad.RenderInstanced(m_shader_RenderGraphLink, m_totalLinksCount);
            }

            if (m_shader_RenderGraphNode.Use())
            {
                m_device.SetRenderStates(RASTERIZER_STATE.NOCHANGE, DEPTHSTENCIL_STATE.DISABLED, BLEND_STATE.NOCHANGE);
                m_device.SetRenderTarget(m_device.DefaultTarget, null);

                m_SB_NodeSims[0].SetInput(0);
                m_SB_Nodes.SetInput(1);
                m_SB_Links.SetInput(2);
                m_SB_LinkSources.SetInput(3);

                m_tex_FalseColors.SetPS(4);

                m_device.ScreenQuad.RenderInstanced(m_shader_RenderGraphNode, m_nodesCount);
            }
#else
            m_device.SetRenderStates(RASTERIZER_STATE.CULL_NONE, DEPTHSTENCIL_STATE.DISABLED, BLEND_STATE.DISABLED);

            if (m_shader_RenderGraph.Use())
            {
                m_device.SetRenderTarget(m_device.DefaultTarget, null);

                m_SB_NodeSims[0].SetInput(0);
                m_SB_Nodes.SetInput(1);

                m_tex_FalseColors.SetPS(4);

                m_device.RenderFullscreenQuad(m_shader_RenderGraph);
            }
#endif

            //////////////////////////////////////////////////////////////////////////
            // Draw some text
            if (m_displayText != null && m_displayText.Length > 0 && m_shader_RenderText.Use())
            {
                m_device.SetRenderStates(RASTERIZER_STATE.CULL_NONE, DEPTHSTENCIL_STATE.DISABLED, BLEND_STATE.DISABLED);
                m_device.SetRenderTarget(m_device.DefaultTarget, null);

                m_tex_FontAtlas.SetPS(0);
                m_tex_FontRectangle.SetVS(1);

                // Fill text letters
                int totalWidth  = 0;
                int totalHeight = (int)m_tex_FontAtlas.Height;
                for (int i = 0; i < m_displayText.Length; i++)
                {
                    int       letterIndex = m_char2Index[(int)m_displayText[i]];
                    Rectangle rect        = m_fontRectangles[letterIndex];
                    m_SB_Text.m[i].m_letterIndex = (uint)letterIndex;
                    m_SB_Text.m[i].m_offset      = totalWidth;
                    m_SB_Text.m[i].m_ratio       = rect.Width;
                    totalWidth += rect.Width;
                }
                // Normalize
                float norm = 1.0f / totalWidth;
                for (int i = 0; i < m_displayText.Length; i++)
                {
                    m_SB_Text.m[i].m_offset *= norm;
                    m_SB_Text.m[i].m_ratio  *= norm;
                }
                m_SB_Text.Write();
                m_SB_Text.SetInput(2);

                // Setup text rectangle
                const float textHeight_pixels = 20.0f;                          // What we want the text size to be on screen
                float       textWidth_pixels  = totalWidth * textHeight_pixels / totalHeight;

                float2 textSize_proj = new float2(2.0f * textWidth_pixels / panelOutput.Width, 2.0f * textHeight_pixels / panelOutput.Height);

                float2 selectedNodePosition_proj = new float2(2.0f * (m_selectedNodePosition.x - m_CB_Main.m._cameraCenter.x) / m_CB_Main.m._cameraSize.x,
                                                              -2.0f * (m_selectedNodePosition.y - m_CB_Main.m._cameraCenter.y) / m_CB_Main.m._cameraSize.y);
                m_CB_Text.m._position.Set(selectedNodePosition_proj.x - 0.5f * textSize_proj.x, selectedNodePosition_proj.y + 1.0f * textSize_proj.y);                          // Horizontal centering on node position, vertical offset so it's above
                m_CB_Text.m._right.Set(textSize_proj.x, 0.0f);
                m_CB_Text.m._up.Set(0.0f, textSize_proj.y);
                m_CB_Text.UpdateData();

                m_device.ScreenQuad.RenderInstanced(m_shader_RenderText, (uint)m_displayText.Length);
            }

            //////////////////////////////////////////////////////////////////////////
            // Auto-center camera
            if (checkBoxAutoCenter.Checked)
            {
                m_SB_NodeSims[0].Read();

                float2 minPos = new float2(float.MaxValue, float.MaxValue);
                float2 maxPos = new float2(-float.MaxValue, -float.MaxValue);
                for (int i = 0; i < m_nodesCount; i++)
                {
                    float2 P = m_SB_NodeSims[0].m[i].m_position;
                    if (float.IsNaN(P.x) || float.IsNaN(P.y))
                    {
                        continue;
                    }

                    minPos.Min(P);
                    maxPos.Max(P);
                }

                if (minPos.x > maxPos.x || minPos.y > maxPos.y)
                {
                    minPos.Set(-10, -10);
                    maxPos.Set(10, 10);
                }

                float2 size = maxPos - minPos;
                if (size.x * panelOutput.Height > size.y * panelOutput.Width)
                {
                    // Fit horizontally
                    size.y = size.x * panelOutput.Height / panelOutput.Width;
                }
                else
                {
                    // Fit vertically
                    size.x = size.y * panelOutput.Width / panelOutput.Height;
                }

                m_CB_Main.m._cameraSize   = 1.05f * size;
                m_CB_Main.m._cameraCenter = 0.5f * (minPos + maxPos);

                m_CB_Main.UpdateData();
            }

            m_device.Present(false);
        }
Beispiel #2
0
        public void             SampleCubeMap(float3 _View, CubeMapSampler _Sampler, out byte _R, out byte _G, out byte _B)
        {
            AbsView.Set(Math.Abs(_View.x), Math.Abs(_View.y), Math.Abs(_View.z));
            float MaxComponent = Math.Max(Math.Max(AbsView.x, AbsView.y), AbsView.z);

            fXYZ.Set(_View.x / MaxComponent, _View.y / MaxComponent, _View.z / MaxComponent);
            int FaceIndex = 0;

            if (Math.Abs(fXYZ.x) > 1.0 - 1e-6)
            {                   // +X or -X
                if (_View.x > 0.0)
                {
                    FaceIndex = 0;
                    fXY.Set(-fXYZ.z, fXYZ.y);
                }
                else
                {
                    FaceIndex = 1;
                    fXY.Set(fXYZ.z, fXYZ.y);
                }
            }
            else if (Math.Abs(fXYZ.y) > 1.0 - 1e-6)
            {                   // +Y or -Y
                if (_View.y > 0.0)
                {
                    FaceIndex = 2;
                    fXY.Set(fXYZ.x, -fXYZ.z);
                }
                else
                {
                    FaceIndex = 3;
                    fXY.Set(fXYZ.x, fXYZ.z);
                }
            }
            else             // if ( Math.Abs( fXYZ.z ) > 1.0-1e-6 )
            {                // +Z or -Z
                if (_View.z > 0.0)
                {
                    FaceIndex = 4;
                    fXY.Set(fXYZ.x, fXYZ.y);
                }
                else
                {
                    FaceIndex = 5;
                    fXY.Set(-fXYZ.x, fXYZ.y);
                }
            }

            fXY.y = -fXY.y;

            int X = (int)(Probe.CUBE_MAP_SIZE * 0.5 * (1.0 + fXY.x));
            int Y = (int)(Probe.CUBE_MAP_SIZE * 0.5 * (1.0 + fXY.y));

//          if ( X < 0 || X > Probe.CUBE_MAP_SIZE-1 )
//              throw new Exception();
//          if ( Y < 0 || Y > Probe.CUBE_MAP_SIZE-1 )
//              throw new Exception();

            X = Math.Min(Probe.CUBE_MAP_SIZE - 1, X);
            Y = Math.Min(Probe.CUBE_MAP_SIZE - 1, Y);

            Probe.Pixel[,]  CubeMapFace = m_Probe.m_CubeMap[FaceIndex];

            _Sampler(CubeMapFace[X, Y], out _R, out _G, out _B);
        }