Beispiel #1
0
        /// <summary>
        /// Converts a quick string of a Texture Coordinate set to an actual TextureCoordinates.
        /// </summary>
        /// <param name="str">The string.</param>
        /// <returns>The texture coordinates.</returns>
        public static TextureCoordinates FromString(string str)
        {
            TextureCoordinates tc = new TextureCoordinates();

            string[] data = str.SplitFast('/');
            tc.xscale = (float)Utilities.StringToFloat(data[0]);
            tc.yscale = (float)Utilities.StringToFloat(data[1]);
            tc.xshift = (float)Utilities.StringToFloat(data[2]);
            tc.yshift = (float)Utilities.StringToFloat(data[3]);
            tc.xflip  = data[4] == "t";
            tc.yflip  = data[5] == "t";
            return(tc);
        }
        /// <summary>
        /// Converts a quick string of a Texture Coordinate set to an actual TextureCoordinates.
        /// </summary>
        /// <param name="str">The string.</param>
        /// <returns>The texture coordinates.</returns>
        public static TextureCoordinates FromString(string str)
        {
            TextureCoordinates tc = new TextureCoordinates();

            string[] data = str.SplitFast('/');
            tc.XScale = StringConversionHelper.StringToFloat(data[0]);
            tc.YScale = StringConversionHelper.StringToFloat(data[1]);
            tc.XShift = StringConversionHelper.StringToFloat(data[2]);
            tc.YShift = StringConversionHelper.StringToFloat(data[3]);
            tc.XFlip  = data[4] == "t";
            tc.YFlip  = data[5] == "t";
            return(tc);
        }
        /// <summary>
        /// Generates a cube model.
        /// </summary>
        /// <returns>The cube model.</returns>
        public Model GenerateCube()
        {
            Model m = new Model("cube")
            {
                Engine   = this,
                Skinned  = true,
                ModelMin = new BEPUutilities.Vector3(-1, -1, -1),
                ModelMax = new BEPUutilities.Vector3(1, 1, 1)
            };
            ModelMesh mm = new ModelMesh("cube");

            mm.vbo.Prepare();
            TextureCoordinates tc = new TextureCoordinates();

            mm.vbo.AddSide(Location.UnitX, tc, offs: true);
            mm.vbo.AddSide(Location.UnitY, tc, offs: true);
            mm.vbo.AddSide(Location.UnitZ, tc, offs: true);
            mm.vbo.AddSide(-Location.UnitX, tc, offs: true);
            mm.vbo.AddSide(-Location.UnitY, tc, offs: true);
            mm.vbo.AddSide(-Location.UnitZ, tc, offs: true);
            m.Original = new Model3D();
            Model3DMesh m3m = new Model3DMesh()
            {
                Name = "cube"
            };

            m3m.Indices       = new List <int>(mm.vbo.Indices.ConvertAll((u) => (int)u));
            m3m.Vertices      = new List <BEPUutilities.Vector3>(mm.vbo.Vertices.ConvertAll((o) => o.ToLocation().ToBVector()));
            m3m.TexCoords     = new List <BEPUutilities.Vector2>(mm.vbo.TexCoords.ConvertAll((o) => new BEPUutilities.Vector2(o.X, o.Y)));
            m3m.Normals       = new List <BEPUutilities.Vector3>(mm.vbo.Normals.ConvertAll((o) => o.ToLocation().ToBVector()));
            m.Original.Meshes = new List <Model3DMesh>()
            {
                m3m
            };
            mm.vbo.GenerateVBO();
            mm.VBOGenned = true;
            m.Meshes.Add(mm);
            return(m);
        }
        /// <summary>
        /// Generates a cube model.
        /// </summary>
        /// <returns>The cube model.</returns>
        public Model GenerateCube()
        {
            Model m = new Model("cube")
            {
                Engine   = this,
                Skinned  = true,
                ModelMin = new BEPUutilities.Vector3(-1, -1, -1),
                ModelMax = new BEPUutilities.Vector3(1, 1, 1)
            };
            ModelMesh mm = new ModelMesh("cube");

            Renderable.ListBuilder builder = new Renderable.ListBuilder();
            builder.Prepare();
            TextureCoordinates tc = new TextureCoordinates();

            builder.AddSide(Location.UnitX, tc, offs: true);
            builder.AddSide(Location.UnitY, tc, offs: true);
            builder.AddSide(Location.UnitZ, tc, offs: true);
            builder.AddSide(-Location.UnitX, tc, offs: true);
            builder.AddSide(-Location.UnitY, tc, offs: true);
            builder.AddSide(-Location.UnitZ, tc, offs: true);
            m.Original = new Model3D();
            Model3DMesh m3m = new Model3DMesh()
            {
                Name = "cube"
            };

            m3m.Indices       = builder.Indices.ToArray();
            m3m.Vertices      = builder.Vertices.ConvertAll((o) => o.ToLocation().ToBVector()).ToArray();
            m3m.TexCoords     = builder.TexCoords.ConvertAll((o) => new BEPUutilities.Vector2(o.X, o.Y)).ToArray();
            m3m.Normals       = builder.Normals.ConvertAll((o) => o.ToLocation().ToBVector()).ToArray();
            m.Original.Meshes = new Model3DMesh[] { m3m };
            mm.BaseRenderable.GenerateVBO(builder);
            m.AddMesh(mm);
            return(m);
        }
Beispiel #5
0
        /// <summary>
        /// Helper to add an axis-aligned side to this VBO.
        /// </summary>
        /// <param name="normal">The normal.</param>
        /// <param name="tc">The texture coordinates.</param>
        /// <param name="offs">Whether to offset.</param>
        /// <param name="texf">The texture ID.</param>
        public void AddSide(Location normal, TextureCoordinates tc, bool offs = false, float texf = 0)
        {
            // TODO: IMPROVE! Or discard?!
            for (int i = 0; i < 6; i++)
            {
                Normals.Add(normal.ToOpenTK());
                Colors.Add(new Vector4(1f, 1f, 1f, 1f));
                Indices.Add((uint)Indices.Count);
                BoneIDs.Add(new Vector4(0, 0, 0, 0));
                BoneWeights.Add(new Vector4(0f, 0f, 0f, 0f));
                BoneIDs2.Add(new Vector4(0, 0, 0, 0));
                BoneWeights2.Add(new Vector4(0f, 0f, 0f, 0f));
            }
            float aX   = (tc.xflip ? 1 : 0) + tc.xshift;
            float aY   = (tc.yflip ? 1 : 0) + tc.yshift;
            float bX   = (tc.xflip ? 0 : 1) * tc.xscale + tc.xshift;
            float bY   = (tc.yflip ? 1 : 0) + tc.yshift;
            float cX   = (tc.xflip ? 0 : 1) * tc.xscale + tc.xshift;
            float cY   = (tc.yflip ? 0 : 1) * tc.yscale + tc.yshift;
            float dX   = (tc.xflip ? 1 : 0) + tc.xshift;
            float dY   = (tc.yflip ? 0 : 1) * tc.yscale + tc.yshift;
            float zero = offs ? -0.5f : -1; // Sssh
            float one  = offs ? 0.5f : 1;

            if (normal.Z == 1)
            {
                // T1
                TexCoords.Add(new Vector3(bX, bY, texf));
                Vertices.Add(new Vector3(zero, zero, one));
                TexCoords.Add(new Vector3(cX, cY, texf));
                Vertices.Add(new Vector3(zero, one, one));
                TexCoords.Add(new Vector3(dX, dY, texf));
                Vertices.Add(new Vector3(one, one, one));
                // T2
                TexCoords.Add(new Vector3(bX, bY, texf));
                Vertices.Add(new Vector3(zero, zero, one));
                TexCoords.Add(new Vector3(dX, dY, texf));
                Vertices.Add(new Vector3(one, one, one));
                TexCoords.Add(new Vector3(aX, aY, texf));
                Vertices.Add(new Vector3(one, zero, one));
            }
            else if (normal.Z == -1)
            {
                // T1
                TexCoords.Add(new Vector3(cX, cY, texf));
                Vertices.Add(new Vector3(one, one, zero));
                TexCoords.Add(new Vector3(dX, dY, texf));
                Vertices.Add(new Vector3(zero, one, zero));
                TexCoords.Add(new Vector3(aX, aY, texf));
                Vertices.Add(new Vector3(zero, zero, zero));
                // T2
                TexCoords.Add(new Vector3(bX, bY, texf));
                Vertices.Add(new Vector3(one, zero, zero));
                TexCoords.Add(new Vector3(cX, cY, texf));
                Vertices.Add(new Vector3(one, one, zero));
                TexCoords.Add(new Vector3(aX, aY, texf));
                Vertices.Add(new Vector3(zero, zero, zero));
            }
            else if (normal.X == 1)
            {
                // T1
                TexCoords.Add(new Vector3(bX, bY, texf));
                Vertices.Add(new Vector3(one, one, one));
                TexCoords.Add(new Vector3(cX, cY, texf));
                Vertices.Add(new Vector3(one, one, zero));
                TexCoords.Add(new Vector3(dX, dY, texf));
                Vertices.Add(new Vector3(one, zero, zero));
                // T2
                TexCoords.Add(new Vector3(aX, aY, texf));
                Vertices.Add(new Vector3(one, zero, one));
                TexCoords.Add(new Vector3(bX, bY, texf));
                Vertices.Add(new Vector3(one, one, one));
                TexCoords.Add(new Vector3(dX, dY, texf));
                Vertices.Add(new Vector3(one, zero, zero));
            }
            else if (normal.X == -1)
            {
                // T1
                TexCoords.Add(new Vector3(cX, cY, texf));
                Vertices.Add(new Vector3(zero, zero, zero));
                TexCoords.Add(new Vector3(dX, dY, texf));
                Vertices.Add(new Vector3(zero, one, zero));
                TexCoords.Add(new Vector3(aX, aY, texf));
                Vertices.Add(new Vector3(zero, one, one));
                // T2
                TexCoords.Add(new Vector3(cX, cY, texf));
                Vertices.Add(new Vector3(zero, zero, zero));
                TexCoords.Add(new Vector3(aX, aY, texf));
                Vertices.Add(new Vector3(zero, one, one));
                TexCoords.Add(new Vector3(bX, bY, texf));
                Vertices.Add(new Vector3(zero, zero, one));
            }
            else if (normal.Y == 1)
            {
                // T1
                TexCoords.Add(new Vector3(aX, aY, texf));
                Vertices.Add(new Vector3(one, one, one));
                TexCoords.Add(new Vector3(bX, bY, texf));
                Vertices.Add(new Vector3(zero, one, one));
                TexCoords.Add(new Vector3(cX, cY, texf));
                Vertices.Add(new Vector3(zero, one, zero));
                // T2
                TexCoords.Add(new Vector3(dX, dY, texf));
                Vertices.Add(new Vector3(one, one, zero));
                TexCoords.Add(new Vector3(aX, aY, texf));
                Vertices.Add(new Vector3(one, one, one));
                TexCoords.Add(new Vector3(cX, cY, texf));
                Vertices.Add(new Vector3(zero, one, zero));
            }
            else if (normal.Y == -1)
            {
                // T1
                TexCoords.Add(new Vector3(dX, dY, texf));
                Vertices.Add(new Vector3(zero, zero, zero));
                TexCoords.Add(new Vector3(aX, aY, texf));
                Vertices.Add(new Vector3(zero, zero, one));
                TexCoords.Add(new Vector3(bX, bY, texf));
                Vertices.Add(new Vector3(one, zero, one));
                // T2
                TexCoords.Add(new Vector3(dX, dY, texf));
                Vertices.Add(new Vector3(zero, zero, zero));
                TexCoords.Add(new Vector3(bX, bY, texf));
                Vertices.Add(new Vector3(one, zero, one));
                TexCoords.Add(new Vector3(cX, cY, texf));
                Vertices.Add(new Vector3(one, zero, zero));
            }
            else
            {
                throw new Exception("Lazy code can't handle unique normals! Only axis-aligned, normalized normals!");
            }
        }