void            BuildPrimitives()
        {
            VertexP3N3[] vertices = new VertexP3N3[2 * VERTICES_COUNT];
            uint[]       indices  = new uint[3 * VERTICES_COUNT];
            for (int i = 0; i < VERTICES_COUNT; i++)
            {
                float  a = Mathf.TWOPI * i / VERTICES_COUNT;
                float3 P = new float3(Mathf.Cos(a), Mathf.Sin(a), 0);

                vertices[2 * i + 0].P = P;
                vertices[2 * i + 0].N = new float3((float)i / VERTICES_COUNT, 0, 0);
                vertices[2 * i + 1].P = P;
                vertices[2 * i + 1].N = new float3((float)i / VERTICES_COUNT, 1, 0);

                indices[3 * i + 0] = (uint)(2 * i + 0);
                indices[3 * i + 1] = (uint)(2 * i + 1);
                indices[3 * i + 2] = (uint)(2 * ((i + 1) % VERTICES_COUNT) + 1);
            }

            m_prim_disk = new Primitive(m_device, 2 * VERTICES_COUNT, VertexP3N3.FromArray(vertices), indices, Primitive.TOPOLOGY.TRIANGLE_LIST, VERTEX_FORMAT.P3N3);
        }
Exemple #2
0
        private void buttonBuildCell_Click(object sender, EventArgs e)
        {
            if (m_Prim_CellFaces != null)
            {
                m_Prim_CellFaces.Dispose();
                m_Prim_CellEdges.Dispose();
                m_Prim_CellFaces = null;
                m_Prim_CellEdges = null;
            }

            float AreaThresholdLow  = m_AreaMin + 0.1f * (m_AreaAvg - m_AreaMin);
            float AreaThresholdHigh = m_AreaMax - 0.1f * (m_AreaMax - m_AreaAvg);
//          float		AreaThresholdLow = m_AreaAvg / 1.5f;
//          float		AreaThresholdHigh = 1.5f * m_AreaAvg;

            List <VertexP3N3> vertices      = new List <VertexP3N3>();
            List <uint>       indices_Faces = new List <uint>();
            List <uint>       Indices_Edges = new List <uint>();

            float AreaMin   = float.MaxValue;
            float AreaMax   = -float.MaxValue;
            float TotalArea = 0.0f;

            for (int FaceIndex = 0; FaceIndex < m_neighborPositions.Length; FaceIndex++)
            {
                float3 P = m_neighborPositions[FaceIndex];
                float3 N = -P.Normalized;                       // Pointing inward

                // Build the polygon by cutting it with all other neighbors
                CellPolygon Polygon = new CellPolygon(P, N);
                for (int NeighborIndex = 0; NeighborIndex < m_neighborPositions.Length; NeighborIndex++)
                {
                    if (NeighborIndex != FaceIndex)
                    {
                        float3 Pn = m_neighborPositions[NeighborIndex];
                        float3 Nn = -Pn.Normalized;                             // Pointing inward
                        Polygon.Cut(Pn, Nn);
                    }
                }

                // Append vertices & indices for both faces & edges
                uint VertexOffset  = (uint)vertices.Count;
                uint VerticesCount = (uint)Polygon.m_Vertices.Length;
                if (VerticesCount > 0)
                {
                    float PolygonArea = 0.0f;
                    for (uint FaceTriangleIndex = 0; FaceTriangleIndex < VerticesCount - 2; FaceTriangleIndex++)
                    {
                        indices_Faces.Add(VertexOffset + 0);
                        indices_Faces.Add(VertexOffset + 1 + FaceTriangleIndex);
                        indices_Faces.Add(VertexOffset + 2 + FaceTriangleIndex);

                        float Area = 0.5f * (Polygon.m_Vertices[2 + FaceTriangleIndex] - Polygon.m_Vertices[0]).Cross(Polygon.m_Vertices[1 + FaceTriangleIndex] - Polygon.m_Vertices[0]).Length;
                        PolygonArea += Area;
                    }
                    AreaMin    = Math.Min(AreaMin, PolygonArea);
                    AreaMax    = Math.Max(AreaMax, PolygonArea);
                    TotalArea += PolygonArea;

                    for (uint VertexIndex = 0; VertexIndex < VerticesCount; VertexIndex++)
                    {
                        Indices_Edges.Add(VertexOffset + VertexIndex);
                        Indices_Edges.Add(VertexOffset + (VertexIndex + 1) % VerticesCount);
                    }

                    float3 Color = PolygonArea <AreaThresholdLow ? new float3(1, 0, 0) : PolygonArea> AreaThresholdHigh ? new float3(0, 1, 0) : new float3(1, 1, 0);

                    foreach (float3 Vertex in Polygon.m_Vertices)
                    {
                        vertices.Add(new VertexP3N3()
                        {
                            P = Vertex, N = Color
                        });
                    }
                }
            }

            m_AreaMin       = AreaMin;
            m_AreaMax       = AreaMax;
            m_AreaAvg       = TotalArea / m_neighborPositions.Length;
            labelStats.Text = "Area min: " + m_AreaMin + "\r\n"
                              + "Area max: " + m_AreaMax + "\r\n"
                              + "Area average: " + m_AreaAvg + "\r\n"
                              + "Area total: " + TotalArea + "\r\n";

            m_Prim_CellFaces = new Primitive(m_Device, (uint)vertices.Count, VertexP3N3.FromArray(vertices.ToArray()), indices_Faces.ToArray(), Primitive.TOPOLOGY.TRIANGLE_LIST, VERTEX_FORMAT.P3N3);
            m_Prim_CellEdges = new Primitive(m_Device, (uint)vertices.Count, VertexP3N3.FromArray(vertices.ToArray()), Indices_Edges.ToArray(), Primitive.TOPOLOGY.LINE_LIST, VERTEX_FORMAT.P3N3);
        }
Exemple #3
0
        private void    BuildCube()
        {
            VertexP3N3[] Vertices = new VertexP3N3[4 * 6];
            Vertices[4 * 0 + 0] = new VertexP3N3()
            {
                P = new float3(+1, +1, +1), N = new float3(0, 0, 0)
            };                                                                                                          // +X
            Vertices[4 * 0 + 1] = new VertexP3N3()
            {
                P = new float3(+1, -1, +1), N = new float3(0, 1, 0)
            };
            Vertices[4 * 0 + 2] = new VertexP3N3()
            {
                P = new float3(+1, -1, -1), N = new float3(1, 1, 0)
            };
            Vertices[4 * 0 + 3] = new VertexP3N3()
            {
                P = new float3(+1, +1, -1), N = new float3(1, 0, 0)
            };

            Vertices[4 * 1 + 0] = new VertexP3N3()
            {
                P = new float3(-1, +1, -1), N = new float3(0, 0, 1)
            };                                                                                                          // -X
            Vertices[4 * 1 + 1] = new VertexP3N3()
            {
                P = new float3(-1, -1, -1), N = new float3(0, 1, 1)
            };
            Vertices[4 * 1 + 2] = new VertexP3N3()
            {
                P = new float3(-1, -1, +1), N = new float3(1, 1, 1)
            };
            Vertices[4 * 1 + 3] = new VertexP3N3()
            {
                P = new float3(-1, +1, +1), N = new float3(1, 0, 1)
            };

            Vertices[4 * 2 + 0] = new VertexP3N3()
            {
                P = new float3(-1, +1, -1), N = new float3(0, 0, 2)
            };                                                                                                          // +Y
            Vertices[4 * 2 + 1] = new VertexP3N3()
            {
                P = new float3(-1, +1, +1), N = new float3(0, 1, 2)
            };
            Vertices[4 * 2 + 2] = new VertexP3N3()
            {
                P = new float3(+1, +1, +1), N = new float3(1, 1, 2)
            };
            Vertices[4 * 2 + 3] = new VertexP3N3()
            {
                P = new float3(+1, +1, -1), N = new float3(1, 0, 2)
            };

            Vertices[4 * 3 + 0] = new VertexP3N3()
            {
                P = new float3(-1, -1, +1), N = new float3(0, 0, 3)
            };                                                                                                          // -Y
            Vertices[4 * 3 + 1] = new VertexP3N3()
            {
                P = new float3(-1, -1, -1), N = new float3(0, 1, 3)
            };
            Vertices[4 * 3 + 2] = new VertexP3N3()
            {
                P = new float3(+1, -1, -1), N = new float3(1, 1, 3)
            };
            Vertices[4 * 3 + 3] = new VertexP3N3()
            {
                P = new float3(+1, -1, +1), N = new float3(1, 0, 3)
            };

            Vertices[4 * 4 + 0] = new VertexP3N3()
            {
                P = new float3(-1, +1, +1), N = new float3(0, 0, 4)
            };                                                                                                          // +Z
            Vertices[4 * 4 + 1] = new VertexP3N3()
            {
                P = new float3(-1, -1, +1), N = new float3(0, 1, 4)
            };
            Vertices[4 * 4 + 2] = new VertexP3N3()
            {
                P = new float3(+1, -1, +1), N = new float3(1, 1, 4)
            };
            Vertices[4 * 4 + 3] = new VertexP3N3()
            {
                P = new float3(+1, +1, +1), N = new float3(1, 0, 4)
            };

            Vertices[4 * 5 + 0] = new VertexP3N3()
            {
                P = new float3(+1, +1, -1), N = new float3(0, 0, 5)
            };                                                                                                          // -Z
            Vertices[4 * 5 + 1] = new VertexP3N3()
            {
                P = new float3(+1, -1, -1), N = new float3(0, 1, 5)
            };
            Vertices[4 * 5 + 2] = new VertexP3N3()
            {
                P = new float3(-1, -1, -1), N = new float3(1, 1, 5)
            };
            Vertices[4 * 5 + 3] = new VertexP3N3()
            {
                P = new float3(-1, +1, -1), N = new float3(1, 0, 5)
            };

            UInt32[] Indices = new UInt32[6 * 6];
            for (int FaceIndex = 0; FaceIndex < 6; FaceIndex++)
            {
                Indices[6 * FaceIndex + 0] = (uint)(4 * FaceIndex + 0);
                Indices[6 * FaceIndex + 1] = (uint)(4 * FaceIndex + 1);
                Indices[6 * FaceIndex + 2] = (uint)(4 * FaceIndex + 2);

                Indices[6 * FaceIndex + 3] = (uint)(4 * FaceIndex + 0);
                Indices[6 * FaceIndex + 4] = (uint)(4 * FaceIndex + 2);
                Indices[6 * FaceIndex + 5] = (uint)(4 * FaceIndex + 3);
            }

            ByteBuffer VerticesBuffer = VertexP3N3.FromArray(Vertices);

            Reg(m_Prim_Cube = new Primitive(m_Device, Vertices.Length, VerticesBuffer, Indices, Primitive.TOPOLOGY.TRIANGLE_LIST, VERTEX_FORMAT.P3N3));
        }
Exemple #4
0
		private void	BuildCube()
		{
			VertexP3N3[]	Vertices = new VertexP3N3[4*6];
			Vertices[4*0+0] = new VertexP3N3() { P = new float3( +1, +1, +1 ), N = new float3( 0, 0, 0 ) };	// +X
			Vertices[4*0+1] = new VertexP3N3() { P = new float3( +1, -1, +1 ), N = new float3( 0, 1, 0 ) };
			Vertices[4*0+2] = new VertexP3N3() { P = new float3( +1, -1, -1 ), N = new float3( 1, 1, 0 ) };
			Vertices[4*0+3] = new VertexP3N3() { P = new float3( +1, +1, -1 ), N = new float3( 1, 0, 0 ) };

			Vertices[4*1+0] = new VertexP3N3() { P = new float3( -1, +1, -1 ), N = new float3( 0, 0, 1 ) };	// -X
			Vertices[4*1+1] = new VertexP3N3() { P = new float3( -1, -1, -1 ), N = new float3( 0, 1, 1 ) };
			Vertices[4*1+2] = new VertexP3N3() { P = new float3( -1, -1, +1 ), N = new float3( 1, 1, 1 ) };
			Vertices[4*1+3] = new VertexP3N3() { P = new float3( -1, +1, +1 ), N = new float3( 1, 0, 1 ) };

			Vertices[4*2+0] = new VertexP3N3() { P = new float3( -1, +1, -1 ), N = new float3( 0, 0, 2 ) };	// +Y
			Vertices[4*2+1] = new VertexP3N3() { P = new float3( -1, +1, +1 ), N = new float3( 0, 1, 2 ) };
			Vertices[4*2+2] = new VertexP3N3() { P = new float3( +1, +1, +1 ), N = new float3( 1, 1, 2 ) };
			Vertices[4*2+3] = new VertexP3N3() { P = new float3( +1, +1, -1 ), N = new float3( 1, 0, 2 ) };

			Vertices[4*3+0] = new VertexP3N3() { P = new float3( -1, -1, +1 ), N = new float3( 0, 0, 3 ) };	// -Y
			Vertices[4*3+1] = new VertexP3N3() { P = new float3( -1, -1, -1 ), N = new float3( 0, 1, 3 ) };
			Vertices[4*3+2] = new VertexP3N3() { P = new float3( +1, -1, -1 ), N = new float3( 1, 1, 3 ) };
			Vertices[4*3+3] = new VertexP3N3() { P = new float3( +1, -1, +1 ), N = new float3( 1, 0, 3 ) };

			Vertices[4*4+0] = new VertexP3N3() { P = new float3( -1, +1, +1 ), N = new float3( 0, 0, 4 ) };	// +Z
			Vertices[4*4+1] = new VertexP3N3() { P = new float3( -1, -1, +1 ), N = new float3( 0, 1, 4 ) };
			Vertices[4*4+2] = new VertexP3N3() { P = new float3( +1, -1, +1 ), N = new float3( 1, 1, 4 ) };
			Vertices[4*4+3] = new VertexP3N3() { P = new float3( +1, +1, +1 ), N = new float3( 1, 0, 4 ) };

			Vertices[4*5+0] = new VertexP3N3() { P = new float3( +1, +1, -1 ), N = new float3( 0, 0, 5 ) };	// -Z
			Vertices[4*5+1] = new VertexP3N3() { P = new float3( +1, -1, -1 ), N = new float3( 0, 1, 5 ) };
			Vertices[4*5+2] = new VertexP3N3() { P = new float3( -1, -1, -1 ), N = new float3( 1, 1, 5 ) };
			Vertices[4*5+3] = new VertexP3N3() { P = new float3( -1, +1, -1 ), N = new float3( 1, 0, 5 ) };

			UInt32[]	Indices = new UInt32[6*6];
			for ( int FaceIndex=0; FaceIndex < 6; FaceIndex++ )
			{
				Indices[6*FaceIndex+0] = (uint) (4*FaceIndex+0);
				Indices[6*FaceIndex+1] = (uint) (4*FaceIndex+1);
				Indices[6*FaceIndex+2] = (uint) (4*FaceIndex+2);

				Indices[6*FaceIndex+3] = (uint) (4*FaceIndex+0);
				Indices[6*FaceIndex+4] = (uint) (4*FaceIndex+2);
				Indices[6*FaceIndex+5] = (uint) (4*FaceIndex+3);
			}

			ByteBuffer	VerticesBuffer = VertexP3N3.FromArray( Vertices );

			Reg( m_Prim_Cube = new Primitive( m_Device, Vertices.Length, VerticesBuffer, Indices, Primitive.TOPOLOGY.TRIANGLE_LIST, VERTEX_FORMAT.P3N3 ) );
		}
Exemple #5
0
        /// <summary>
        /// Build the cube primitive
        /// </summary>
        void    BuildCube()
        {
            float3[] Normals = new float3[6] {
                -float3.UnitX,
                float3.UnitX,
                -float3.UnitY,
                float3.UnitY,
                -float3.UnitZ,
                float3.UnitZ,
            };

            float3[] Tangents = new float3[6] {
                float3.UnitZ,
                -float3.UnitZ,
                float3.UnitX,
                -float3.UnitX,
                -float3.UnitX,
                float3.UnitX,
            };

            VertexP3N3[] vertices = new VertexP3N3[6 * 4];
            uint[]       indices  = new uint[2 * 6 * 3];

            for (int FaceIndex = 0; FaceIndex < 6; FaceIndex++)
            {
                float3 N = Normals[FaceIndex];
                float3 T = Tangents[FaceIndex];
                float3 B = N.Cross(T);

                vertices[4 * FaceIndex + 0] = new VertexP3N3()
                {
                    P = N - T + B,
                    N = N,
//					T = T,
//					B = B,
//					UV = new float2( 0, 0 )
                };
                vertices[4 * FaceIndex + 1] = new VertexP3N3()
                {
                    P = N - T - B,
                    N = N,
//                  T = T,
//                  B = B,
//                  UV = new float2( 0, 1 )
                };
                vertices[4 * FaceIndex + 2] = new VertexP3N3()
                {
                    P = N + T - B,
                    N = N,
//                  T = T,
//                  B = B,
//                  UV = new float2( 1, 1 )
                };
                vertices[4 * FaceIndex + 3] = new VertexP3N3()
                {
                    P = N + T + B,
                    N = N,
//                  T = T,
//                  B = B,
//                  UV = new float2( 1, 0 )
                };

                indices[2 * 3 * FaceIndex + 0] = (uint)(4 * FaceIndex + 0);
                indices[2 * 3 * FaceIndex + 1] = (uint)(4 * FaceIndex + 1);
                indices[2 * 3 * FaceIndex + 2] = (uint)(4 * FaceIndex + 2);
                indices[2 * 3 * FaceIndex + 3] = (uint)(4 * FaceIndex + 0);
                indices[2 * 3 * FaceIndex + 4] = (uint)(4 * FaceIndex + 2);
                indices[2 * 3 * FaceIndex + 5] = (uint)(4 * FaceIndex + 3);
            }

            m_primitiveCube = new Primitive(m_device, (uint)vertices.Length, VertexP3N3.FromArray(vertices), indices, Primitive.TOPOLOGY.TRIANGLE_LIST, VERTEX_FORMAT.P3N3);

//          VertexP3N3[]	vertices = new VertexP3N3[4*6];
//          uint[]			indices = new uint[6*2*3];
//          for ( uint face=0; face < 6; face++ ) {
//              float3	N = (1 - 2 * (face & 1)) * new float3( (face >> 1) == 0 ? 1 : 0, (face >> 1) == 1 ? 1 : 0, (face >> 1) == 2 ? 1 : 0 );
//              float3	T = (1 - 2 * (face & 1)) * new float3( (face >> 1) == 1 ? 1 : 0, (face >> 1) == 2 ? 1 : 0, (face >> 1) == 0 ? 1 : 0 );
//              float3	B = T.Cross( N );
//
//              for ( uint v=0; v < 4; v++ ) {
//                  vertices[4*face+v].N = N;
//                  vertices[4*face+v].P = N + (2.0f*(v & 1)-1) * T + (2.0f*((v >> 1) & 1)-1) * B;
//              }
//
//              indices[3*(2*face+0)+0] = 4*face + 0;
//              indices[3*(2*face+0)+1] = 4*face + 1;
//              indices[3*(2*face+0)+2] = 4*face + 2;
//              indices[3*(2*face+1)+0] = 4*face + 2;
//              indices[3*(2*face+1)+1] = 4*face + 1;
//              indices[3*(2*face+1)+2] = 4*face + 3;
//          }
//
//          m_primitiveCube = new Primitive( m_device, 6*4, VertexP3N3.FromArray( vertices ), indices, Primitive.TOPOLOGY.TRIANGLE_LIST, VERTEX_FORMAT.P3N3 );
        }