示例#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 = 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");

            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);
        }
示例#3
0
            /// <summary>
            /// Helper to add an axis-aligned side to this <see cref="Renderable"/> builder.
            /// </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)
            {
                if (Vertices.Capacity < 6)
                {
                    SetCapacity(12);
                }
                // 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);
                    AddEmptyBoneInfo();
                }
                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!");
                }
            }