コード例 #1
0
        public static VertexPositionNormal[] GenerateCubeData(Vector3 position, float length, float width, float height)
        {
            #region Vertices

            Vector3 p0 = new Vector3(-length * .5f, -width * .5f, height * .5f);
            Vector3 p1 = new Vector3(length * .5f, -width * .5f, height * .5f);
            Vector3 p2 = new Vector3(length * .5f, -width * .5f, -height * .5f);
            Vector3 p3 = new Vector3(-length * .5f, -width * .5f, -height * .5f);

            Vector3 p4 = new Vector3(-length * .5f, width * .5f, height * .5f);
            Vector3 p5 = new Vector3(length * .5f, width * .5f, height * .5f);
            Vector3 p6 = new Vector3(length * .5f, width * .5f, -height * .5f);
            Vector3 p7 = new Vector3(-length * .5f, width * .5f, -height * .5f);

            Vector3[] vertices = new Vector3[]
            {
                // Bottom
                p0, p1, p2, p3,

                // Left
                p7, p4, p0, p3,

                // Front
                p4, p5, p1, p0,

                // Back
                p6, p7, p3, p2,

                // Right
                p5, p6, p2, p1,

                // Top
                p7, p6, p5, p4
            };

            #endregion

            #region Normals

            Vector3 up    = Vector3Helper.Up;
            Vector3 down  = Vector3Helper.Down;
            Vector3 front = Vector3Helper.Forward;
            Vector3 back  = Vector3Helper.Backward;
            Vector3 left  = Vector3Helper.Left;
            Vector3 right = Vector3Helper.Right;

            Vector3[] normals = new Vector3[]
            {
                // Bottom
                down, down, down, down,

                // Left
                left, left, left, left,

                // Front
                front, front, front, front,

                // Back
                back, back, back, back,

                // Right
                right, right, right, right,

                // Top
                up, up, up, up
            };
            #endregion

            #region UVs
            Vector2 _00 = new Vector2(0f, 0f);
            Vector2 _10 = new Vector2(1f, 0f);
            Vector2 _01 = new Vector2(0f, 1f);
            Vector2 _11 = new Vector2(1f, 1f);

            Vector2[] uvs = new Vector2[]
            {
                // Bottom
                _11, _01, _00, _10,

                // Left
                _11, _01, _00, _10,

                // Front
                _11, _01, _00, _10,

                // Back
                _11, _01, _00, _10,

                // Right
                _11, _01, _00, _10,

                // Top
                _11, _01, _00, _10,
            };
            #endregion

            #region Indices
            int[] triangles = new int[]
            {
                // Bottom
                3, 1, 0,
                3, 2, 1,

                // Left
                3 + 4 * 1, 1 + 4 * 1, 0 + 4 * 1,
                3 + 4 * 1, 2 + 4 * 1, 1 + 4 * 1,

                // Front
                3 + 4 * 2, 1 + 4 * 2, 0 + 4 * 2,
                3 + 4 * 2, 2 + 4 * 2, 1 + 4 * 2,

                // Back
                3 + 4 * 3, 1 + 4 * 3, 0 + 4 * 3,
                3 + 4 * 3, 2 + 4 * 3, 1 + 4 * 3,

                // Right
                3 + 4 * 4, 1 + 4 * 4, 0 + 4 * 4,
                3 + 4 * 4, 2 + 4 * 4, 1 + 4 * 4,

                // Top
                3 + 4 * 5, 1 + 4 * 5, 0 + 4 * 5,
                3 + 4 * 5, 2 + 4 * 5, 1 + 4 * 5,
            };
            #endregion

            VertexPositionNormalTexture[] output = new VertexPositionNormalTexture[triangles.Length];

            for (int i = 0; i < triangles.Length; i++)
            {
                output[i] = new VertexPositionNormalTexture(vertices[triangles[i]],
                                                            normals[triangles[i]],
                                                            uvs[triangles[i]]);
            }

            return(output.Select(x => (VertexPositionNormal)x)
                   .ToArray());
        }
コード例 #2
0
        public WaveFrontRenderer(GraphicsDevice device, string file)
        {
            _effect = new BasicEffect(device);
            _effect.EnableDefaultLighting();
            _effect.PreferPerPixelLighting = false;

            var result = FileFormatObj.Load(device, file, true);

            foreach (var msg in result.Messages)
            {
                if (msg.Exception == null)
                {
                    Console.WriteLine($"File: {msg.FileName} - Details: {msg.Details}");
                }
                else
                {
                    Console.WriteLine($"File: {msg.FileName} - Exception: {msg.Exception.ToString()}");
                }
            }
            var vertices = result.Model.Vertices;
            var normals  = result.Model.Normals;
            var uvs      = result.Model.Uvs;

            /*Dictionary<string, BasicEffect> materials = new Dictionary<string, BasicEffect>();
             * foreach (var mat in result.Model.Materials)
             * {
             *      if (materials.ContainsKey(mat.Name)) continue;
             *
             *      var effect = new BasicEffect(device);
             *      {
             *              SetBasicEffect(ref effect, mat);
             *      }
             *
             *      materials.Add(mat.Name, effect);
             * }*/

            List <Group> groups = new List <Group>();

            foreach (var g in result.Model.Groups)
            {
                int textureCount = 0;
                List <VertexPositionNormalTexture> textures = new List <VertexPositionNormalTexture>();
                Group gr = new Group();
                gr.Name  = "";
                gr.Areas = new Areas[g.Faces.Count];

                for (var i = 0; i < g.Faces.Count; i++)
                {
                    var face = g.Faces[i];

                    VertexPositionNormalTexture[] faceVertices = new VertexPositionNormalTexture[face.Indices.Count];
                    for (var index = 0; index < face.Indices.Count; index++)
                    {
                        var indice = face.Indices[index];

                        var vert = vertices[indice.Vertex];

                        var v = new VertexPositionNormalTexture()
                        {
                            Position = vert
                        };

                        if (indice.Normal.HasValue)
                        {
                            var normal = normals[indice.Normal.Value];
                            v.Normal = normal;
                        }

                        if (indice.Uv.HasValue)
                        {
                            var uv = uvs[indice.Uv.Value];
                            v.TextureCoordinate = uv;
                        }

                        faceVertices[index] = v;
                    }

                    int startIndex = textureCount;
                    textures.AddRange(faceVertices);
                    textureCount += faceVertices.Length;

                    //	BasicEffect effect;
                    //if (!materials.TryGetValue(face.Material.Name, out effect))
                    //{
                    //	effect = new BasicEffect(device);
                    //	SetBasicEffect(ref effect, face.Material);
                    //}

                    Areas a = new Areas();
                    a.Material = face.Material;
                    //a.Effect = effect;
                    a.Length = faceVertices.Length;
                    a.Start  = startIndex;
                    a.Box    = BoundingSphere.CreateFromPoints(faceVertices.Select(x => x.Position));

                    if (face.Material.Transparency.HasValue)
                    {
                        if (face.Material.Transparency.Value < 1f)
                        {
                            a.HasTransparency = true;
                        }
                    }

                    gr.Areas[i] = a;
                }

                gr.Buffer = new VertexBuffer(device, VertexPositionNormalTexture.VertexDeclaration, textures.Count, BufferUsage.WriteOnly);
                gr.Buffer.SetData(textures.ToArray());

                groups.Add(gr);
            }

            Groups = groups.ToArray();
            //	Buffer = new VertexBuffer(device, VertexPositionNormalTexture.VertexDeclaration, textures.Count, BufferUsage.WriteOnly);
            //Buffer.SetData(textures.ToArray());
        }