示例#1
0
文件: Form1.cs 项目: vr3d/GodComplex
        private void    BuildQuad()
        {
            VertexPt4[] Vertices = new VertexPt4[] {
                new VertexPt4()
                {
                    Pt = new float4(-1, +1, 0, 1)
                },
                new VertexPt4()
                {
                    Pt = new float4(-1, -1, 0, 1)
                },
                new VertexPt4()
                {
                    Pt = new float4(+1, +1, 0, 1)
                },
                new VertexPt4()
                {
                    Pt = new float4(+1, -1, 0, 1)
                },
            };

            ByteBuffer VerticesBuffer = VertexPt4.FromArray(Vertices);

            Reg(m_Prim_Quad = new Primitive(m_Device, Vertices.Length, VerticesBuffer, null, Primitive.TOPOLOGY.TRIANGLE_STRIP, VERTEX_FORMAT.Pt4));
        }
示例#2
0
        private void    BuildQuad()
        {
            VertexPt4[] Vertices = new VertexPt4[4];
            Vertices[0] = new VertexPt4()
            {
                Pt = new float4(-1, +1, 0, 1)
            };                                                                                  // Top-Left
            Vertices[1] = new VertexPt4()
            {
                Pt = new float4(-1, -1, 0, 1)
            };                                                                                  // Bottom-Left
            Vertices[2] = new VertexPt4()
            {
                Pt = new float4(+1, +1, 0, 1)
            };                                                                                  // Top-Right
            Vertices[3] = new VertexPt4()
            {
                Pt = new float4(+1, -1, 0, 1)
            };                                                                                  // Bottom-Right

            ByteBuffer VerticesBuffer = VertexPt4.FromArray(Vertices);

            m_Prim_Quad = new Primitive(m_Device, Vertices.Length, VerticesBuffer, null, Primitive.TOPOLOGY.TRIANGLE_STRIP, VERTEX_FORMAT.Pt4);
        }
示例#3
0
        void    BuildPrimitives()
        {
            {                   // Post-process quad
                List <VertexPt4> Vertices = new List <VertexPt4>();
                Vertices.Add(new VertexPt4()
                {
                    Pt = new float4(-1, +1, 0, 1)
                });
                Vertices.Add(new VertexPt4()
                {
                    Pt = new float4(-1, -1, 0, 1)
                });
                Vertices.Add(new VertexPt4()
                {
                    Pt = new float4(+1, +1, 0, 1)
                });
                Vertices.Add(new VertexPt4()
                {
                    Pt = new float4(+1, -1, 0, 1)
                });
                m_Prim_Quad = new Primitive(m_Device, 4, VertexPt4.FromArray(Vertices.ToArray()), null, Primitive.TOPOLOGY.TRIANGLE_STRIP, VERTEX_FORMAT.Pt4);
            }

            {                   // Sphere Primitive
                List <VertexP3N3G3T2> Vertices = new List <VertexP3N3G3T2>();
                List <uint>           Indices  = new List <uint>();

                const int SUBDIVS_THETA = 80;
                const int SUBDIVS_PHI   = 160;
                for (int Y = 0; Y <= SUBDIVS_THETA; Y++)
                {
                    double Theta    = Y * Math.PI / SUBDIVS_THETA;
                    float  CosTheta = (float)Math.Cos(Theta);
                    float  SinTheta = (float)Math.Sin(Theta);
                    for (int X = 0; X <= SUBDIVS_PHI; X++)
                    {
                        double Phi    = X * 2.0 * Math.PI / SUBDIVS_PHI;
                        float  CosPhi = (float)Math.Cos(Phi);
                        float  SinPhi = (float)Math.Sin(Phi);

                        float3 N  = new float3(SinTheta * SinPhi, CosTheta, SinTheta * CosPhi);
                        float3 T  = new float3(CosPhi, 0.0f, -SinPhi);
                        float2 UV = new float2((float)X / SUBDIVS_PHI, (float)Y / SUBDIVS_THETA);
                        Vertices.Add(new VertexP3N3G3T2()
                        {
                            P = N, N = N, T = T, UV = UV
                        });
                    }
                }

                for (int Y = 0; Y < SUBDIVS_THETA; Y++)
                {
                    int CurrentLineOffset = Y * (SUBDIVS_PHI + 1);
                    int NextLineOffset    = (Y + 1) * (SUBDIVS_PHI + 1);
                    for (int X = 0; X <= SUBDIVS_PHI; X++)
                    {
                        Indices.Add((uint)(CurrentLineOffset + X));
                        Indices.Add((uint)(NextLineOffset + X));
                    }
                    if (Y < SUBDIVS_THETA - 1)
                    {
                        Indices.Add((uint)(NextLineOffset - 1));                                // Degenerate triangle to end the line
                        Indices.Add((uint)NextLineOffset);                                      // Degenerate triangle to start the next line
                    }
                }

                m_Prim_Sphere = new Primitive(m_Device, Vertices.Count, VertexP3N3G3T2.FromArray(Vertices.ToArray()), Indices.ToArray(), Primitive.TOPOLOGY.TRIANGLE_STRIP, VERTEX_FORMAT.P3N3G3T2);
            }

            {                   // Build the cube
                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,
                };

                VertexP3N3G3T2[] Vertices = new VertexP3N3G3T2[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 VertexP3N3G3T2()
                    {
                        P = N - T + B,
                        N = N,
                        T = T,
//						B = B,
                        UV = new float2(0, 0)
                    };
                    Vertices[4 * FaceIndex + 1] = new VertexP3N3G3T2()
                    {
                        P = N - T - B,
                        N = N,
                        T = T,
//						B = B,
                        UV = new float2(0, 1)
                    };
                    Vertices[4 * FaceIndex + 2] = new VertexP3N3G3T2()
                    {
                        P = N + T - B,
                        N = N,
                        T = T,
//						B = B,
                        UV = new float2(1, 1)
                    };
                    Vertices[4 * FaceIndex + 3] = new VertexP3N3G3T2()
                    {
                        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);
                }

                ByteBuffer VerticesBuffer = VertexP3N3G3T2.FromArray(Vertices);

                m_Prim_Cube = new Primitive(m_Device, Vertices.Length, VerticesBuffer, Indices, Primitive.TOPOLOGY.TRIANGLE_LIST, VERTEX_FORMAT.P3N3G3T2);
            }
        }
        private void    BuildPrimitives()
        {
            {
                VertexPt4[] Vertices = new VertexPt4[4];
                Vertices[0] = new VertexPt4()
                {
                    Pt = new float4(-1, +1, 0, 1)
                };                                                                                      // Top-Left
                Vertices[1] = new VertexPt4()
                {
                    Pt = new float4(-1, -1, 0, 1)
                };                                                                                      // Bottom-Left
                Vertices[2] = new VertexPt4()
                {
                    Pt = new float4(+1, +1, 0, 1)
                };                                                                                      // Top-Right
                Vertices[3] = new VertexPt4()
                {
                    Pt = new float4(+1, -1, 0, 1)
                };                                                                                      // Bottom-Right

                ByteBuffer VerticesBuffer = VertexPt4.FromArray(Vertices);

                m_Prim_Quad = new Primitive(m_Device, Vertices.Length, VerticesBuffer, null, Primitive.TOPOLOGY.TRIANGLE_STRIP, VERTEX_FORMAT.Pt4);
            }

            {
                VertexP3N3G3B3T2[] Vertices = new VertexP3N3G3B3T2[4];
                Vertices[0] = new VertexP3N3G3B3T2()
                {
                    P = new float3(-1, +1, 0), N = new float3(0, 0, 1), T = new float3(1, 0, 0), B = new float3(0, 1, 0), UV = new float2(0, 0)
                };                                                                                                                                                                                              // Top-Left
                Vertices[1] = new VertexP3N3G3B3T2()
                {
                    P = new float3(-1, -1, 0), N = new float3(0, 0, 1), T = new float3(1, 0, 0), B = new float3(0, 1, 0), UV = new float2(0, 1)
                };                                                                                                                                                                                              // Bottom-Left
                Vertices[2] = new VertexP3N3G3B3T2()
                {
                    P = new float3(+1, +1, 0), N = new float3(0, 0, 1), T = new float3(1, 0, 0), B = new float3(0, 1, 0), UV = new float2(1, 0)
                };                                                                                                                                                                                              // Top-Right
                Vertices[3] = new VertexP3N3G3B3T2()
                {
                    P = new float3(+1, -1, 0), N = new float3(0, 0, 1), T = new float3(1, 0, 0), B = new float3(0, 1, 0), UV = new float2(1, 1)
                };                                                                                                                                                                                              // Bottom-Right

                ByteBuffer VerticesBuffer = VertexP3N3G3B3T2.FromArray(Vertices);

                m_Prim_Rectangle = new Primitive(m_Device, Vertices.Length, VerticesBuffer, null, Primitive.TOPOLOGY.TRIANGLE_STRIP, VERTEX_FORMAT.P3N3G3B3T2);
            }

            {                   // Build the sphere
                const int          W        = 41;
                const int          H        = 22;
                VertexP3N3G3B3T2[] Vertices = new VertexP3N3G3B3T2[W * H];
                for (int Y = 0; Y < H; Y++)
                {
                    double Theta    = Math.PI * Y / (H - 1);
                    float  CosTheta = (float)Math.Cos(Theta);
                    float  SinTheta = (float)Math.Sin(Theta);
                    for (int X = 0; X < W; X++)
                    {
                        double Phi    = 2.0 * Math.PI * X / (W - 1);
                        float  CosPhi = (float)Math.Cos(Phi);
                        float  SinPhi = (float)Math.Sin(Phi);

                        float3 N = new float3(SinTheta * SinPhi, CosTheta, SinTheta * CosPhi);
                        float3 T = new float3(CosPhi, 0.0f, -SinPhi);
                        float3 B = N.Cross(T);

                        Vertices[W * Y + X] = new VertexP3N3G3B3T2()
                        {
                            P  = N,
                            N  = N,
                            T  = T,
                            B  = B,
                            UV = new float2(2.0f * X / W, 1.0f * Y / H)
                        };
                    }
                }

                ByteBuffer VerticesBuffer = VertexP3N3G3B3T2.FromArray(Vertices);

                uint[] Indices    = new uint[(H - 1) * (2 * W + 2) - 2];
                int    IndexCount = 0;
                for (int Y = 0; Y < H - 1; Y++)
                {
                    for (int X = 0; X < W; X++)
                    {
                        Indices[IndexCount++] = (uint)((Y + 0) * W + X);
                        Indices[IndexCount++] = (uint)((Y + 1) * W + X);
                    }
                    if (Y < H - 2)
                    {
                        Indices[IndexCount++] = (uint)((Y + 1) * W - 1);
                        Indices[IndexCount++] = (uint)((Y + 1) * W + 0);
                    }
                }

                m_Prim_Sphere = new Primitive(m_Device, Vertices.Length, VerticesBuffer, Indices, Primitive.TOPOLOGY.TRIANGLE_STRIP, VERTEX_FORMAT.P3N3G3B3T2);
            }

            {                   // Build the cube
                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,
                };

                VertexP3N3G3B3T2[] Vertices = new VertexP3N3G3B3T2[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 VertexP3N3G3B3T2()
                    {
                        P  = N - T + B,
                        N  = N,
                        T  = T,
                        B  = B,
                        UV = new float2(0, 0)
                    };
                    Vertices[4 * FaceIndex + 1] = new VertexP3N3G3B3T2()
                    {
                        P  = N - T - B,
                        N  = N,
                        T  = T,
                        B  = B,
                        UV = new float2(0, 1)
                    };
                    Vertices[4 * FaceIndex + 2] = new VertexP3N3G3B3T2()
                    {
                        P  = N + T - B,
                        N  = N,
                        T  = T,
                        B  = B,
                        UV = new float2(1, 1)
                    };
                    Vertices[4 * FaceIndex + 3] = new VertexP3N3G3B3T2()
                    {
                        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);
                }

                ByteBuffer VerticesBuffer = VertexP3N3G3B3T2.FromArray(Vertices);

                m_Prim_Cube = new Primitive(m_Device, Vertices.Length, VerticesBuffer, Indices, Primitive.TOPOLOGY.TRIANGLE_LIST, VERTEX_FORMAT.P3N3G3B3T2);
            }
        }