Beispiel #1
0
        public MarsLanderForm(string _initialLines)
        {
            InitializeComponent();

            m_writer.m_owner = this;
            Console.SetIn(m_reader);
            Console.SetOut(m_writer);

            string[] initialLines = _initialLines.Split('\n');
            for (int i = 0; i < initialLines.Length; i++)
            {
                m_reader.m_lines.Add(initialLines[i]);
            }

            int landscapePointsCount = int.Parse(initialLines[0]);

            for (int i = 0; i < landscapePointsCount; i++)
            {
                string[] coords = initialLines[1 + i].Split(' ');
                float2   P      = new float2(float.Parse(coords[0]), float.Parse(coords[1]));
                m_landscape.Add(P);
                m_landscapeMin.Min(P);
                m_landscapeMax.Max(P);
            }

            string[] initialValues = initialLines[1 + landscapePointsCount].Split(' ');
            Pos      = new float2(int.Parse(initialValues[0]), int.Parse(initialValues[1]));
            Vel      = new float2(int.Parse(initialValues[2]), int.Parse(initialValues[3]));
            Fuel     = int.Parse(initialValues[4]);
            Rotation = int.Parse(initialValues[5]);
            Thrust   = int.Parse(initialValues[6]);

            m_pastPositions.Add(Pos);
        }
Beispiel #2
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);
        }
        Primitive       BuildTorus()
        {
            List <VertexP3N3G3B3T2> vertices = new List <VertexP3N3G3B3T2>();
            List <uint>             indices  = new List <uint>();

            // Build vertices
            VertexP3N3G3B3T2 V = new VertexP3N3G3B3T2();

            for (uint i = 0; i < SUBDIVS0; i++)
            {
                float  a0 = 2.0f * (float)Math.PI * i / SUBDIVS0;
                float3 X  = new float3((float)Math.Cos(a0), (float)Math.Sin(a0), 0.0f);
                float3 Y  = new float3(-X.y, X.x, 0.0f);
                float3 Z  = X.Cross(Y);
                float3 C  = RADIUS0 * X;                        // Center of little ring, around large ring

                for (uint j = 0; j < SUBDIVS1; j++)
                {
                    float  a1  = 2.0f * (float)Math.PI * j / SUBDIVS1;
                    float3 lsN = new float3((float)Math.Cos(a1), (float)Math.Sin(a1), 0.0f);
//					float3	lsN2 = new float3( -lsN.y, lsN.x, 0.0f );
                    float3 N = lsN.x * X + lsN.y * Z;
//					float3	N2 = lsN2.x * X + lsN2.y * Z;

                    V.P  = C + RADIUS1 * N;
                    V.N  = N;
                    V.T  = Y;
                    V.UV = new float2(4.0f * i / SUBDIVS0, 1.0f * j / SUBDIVS1);

                    vertices.Add(V);
                }
            }

            // Build indices and curvature
            uint[,]                 neighborIndices = new uint[3, 3];
            float3[]      neighbors    = new float3[8];
            List <float2> curvatures   = new List <float2>();
            float2        minCurvature = new float2(float.MaxValue, float.MaxValue);
            float2        maxCurvature = new float2(-float.MaxValue, -float.MaxValue);
            float2        avgCurvature = float2.Zero;
            int           count        = 0;

            for (uint i = 0; i < SUBDIVS0; i++)
            {
                uint Pi = (i + SUBDIVS0 - 1) % SUBDIVS0;
                uint Ni = (i + 1) % SUBDIVS0;

                uint ringCurrent  = SUBDIVS1 * i;
                uint ringPrevious = SUBDIVS1 * Pi;
                uint ringNext     = SUBDIVS1 * Ni;

                for (uint j = 0; j < SUBDIVS1; j++)
                {
                    uint Pj = (j + SUBDIVS1 - 1) % SUBDIVS1;
                    uint Nj = (j + 1) % SUBDIVS1;

                    neighborIndices[0, 0] = ringPrevious + Pj;
                    neighborIndices[0, 1] = ringPrevious + j;
                    neighborIndices[0, 2] = ringPrevious + Nj;
                    neighborIndices[1, 0] = ringCurrent + Pj;
                    neighborIndices[1, 1] = ringCurrent + j;
                    neighborIndices[1, 2] = ringCurrent + Nj;
                    neighborIndices[2, 0] = ringNext + Pj;
                    neighborIndices[2, 1] = ringNext + j;
                    neighborIndices[2, 2] = ringNext + Nj;

                    // Build 2 triangles
                    indices.Add(neighborIndices[1, 2]);
                    indices.Add(neighborIndices[1, 1]);
                    indices.Add(neighborIndices[2, 1]);
                    indices.Add(neighborIndices[1, 2]);
                    indices.Add(neighborIndices[2, 1]);
                    indices.Add(neighborIndices[2, 2]);

                    // Compute central vertex's curvature
                    VertexP3N3G3B3T2 centerVertex = vertices[(int)neighborIndices[1, 1]];
                    neighbors[0] = vertices[(int)neighborIndices[0, 0]].P;
                    neighbors[1] = vertices[(int)neighborIndices[1, 0]].P;
                    neighbors[2] = vertices[(int)neighborIndices[2, 0]].P;
                    neighbors[3] = vertices[(int)neighborIndices[0, 1]].P;
//					neighbors[] = vertices[(int) neighborIndices[1,1]].P;
                    neighbors[4] = vertices[(int)neighborIndices[2, 1]].P;
                    neighbors[5] = vertices[(int)neighborIndices[0, 2]].P;
                    neighbors[6] = vertices[(int)neighborIndices[1, 2]].P;
                    neighbors[7] = vertices[(int)neighborIndices[2, 2]].P;

                    // Single curvature
//                  float	curvature = ComputeTangentSphereRadius( centerVertex.P, centerVertex.N, neighbors, false );
//                  centerVertex.B.x = curvature;

                    // Double curvature
                    float2 curvature = ComputeTangentCurvatureRadii(centerVertex.P, centerVertex.N, centerVertex.T, neighbors, false);
                    centerVertex.B.x = curvature.x;
                    centerVertex.B.y = curvature.y;

                    vertices[(int)neighborIndices[1, 1]] = centerVertex;

                    curvatures.Add(curvature);
                    minCurvature.Min(curvature);
                    maxCurvature.Max(curvature);
                    avgCurvature += curvature;
                    count++;
                }
            }
            avgCurvature /= count;

            labelMeshInfo.Text = "Curvature Avg. = " + avgCurvature + " - Min = " + minCurvature + " - Max = " + maxCurvature;

            return(new Primitive(m_device, (uint)vertices.Count, VertexP3N3G3B3T2.FromArray(vertices.ToArray()), indices.ToArray(), Primitive.TOPOLOGY.TRIANGLE_LIST, VERTEX_FORMAT.P3N3G3B3T2));
        }