示例#1
0
        /// <summary>
        /// createGrassMesh
        /// This demonstrates how to create a manual mesh
        ///  only use static members of MeshBuilderHelper to help set vertex and index data.
        /// </summary>
        void createGrassMesh()
        {
            OgreDotNet.MeshPtr mp = MeshManager.Instance.CreateManual(GRASS_MESH_NAME, "General");
            SubMesh            sm = mp.Get().CreateSubMesh();

            sm.useSharedVertices      = false;
            sm.vertexData             = new VertexData();
            sm.vertexData.vertexStart = 0;
            sm.vertexData.vertexCount = 12;
            //mIndexType = HardwareIndexBuffer::IT_16BIT;

            VertexDeclaration dcl    = sm.vertexData.vertexDeclaration;
            UInt32            offset = 0;

            UInt32 offPos = dcl.addElement(0, offset, VertexElementType.VET_FLOAT3, VertexElementSemantic.VES_POSITION).getOffset();

            offset = VertexElement.getTypeSize(VertexElementType.VET_FLOAT3);

            UInt32 offNorm = dcl.addElement(0, offset, VertexElementType.VET_FLOAT3, VertexElementSemantic.VES_NORMAL).getOffset();

            offset += VertexElement.getTypeSize(VertexElementType.VET_FLOAT3);

            UInt32 offUV = dcl.addElement(0, offset, VertexElementType.VET_FLOAT2, VertexElementSemantic.VES_TEXTURE_COORDINATES).getOffset();

            offset += VertexElement.getTypeSize(VertexElementType.VET_FLOAT2);

            UInt32 vertexsize = offset;

            mLog.LogMessage(string.Format("createGrassMesh vertexsize={0}", vertexsize));

            HardwareVertexBufferSharedPtr vbuf = HardwareBufferManager.getSingleton().createVertexBuffer(
                vertexsize, 12, HardwareBuffer.Usage.HBU_STATIC_WRITE_ONLY);

            IntPtr pV = vbuf.Get().Lock(HardwareBuffer.LockOptions.HBL_DISCARD);

            Vector3    vec = new Vector3(GRASS_WIDTH / 2.0f, 0.0f, 0.0f);
            Quaternion rot = Quaternion.FromAngleAxis(60, Vector3.UnitY);
            uint       i;

            for (i = 0; i < 3; ++i)
            {
                OgreDotNet.MeshBuilderHelper.SetVertexFloat(pV, vertexsize, (i * 4) + 0, offPos, -vec.x, GRASS_HEIGHT, -vec.z);                         //position
                OgreDotNet.MeshBuilderHelper.SetVertexFloat(pV, vertexsize, (i * 4) + 0, offNorm, 0.0f, 1.0f, 0.0f);                                    //normal
                OgreDotNet.MeshBuilderHelper.SetVertexFloat(pV, vertexsize, (i * 4) + 0, offUV, 0.0f, 0.0f);                                            //uv

                OgreDotNet.MeshBuilderHelper.SetVertexFloat(pV, vertexsize, (i * 4) + 1, offPos, vec.x, GRASS_HEIGHT, vec.z);                           //position
                OgreDotNet.MeshBuilderHelper.SetVertexFloat(pV, vertexsize, (i * 4) + 1, offNorm, 0.0f, 1.0f, 0.0f);                                    //normal
                OgreDotNet.MeshBuilderHelper.SetVertexFloat(pV, vertexsize, (i * 4) + 1, offUV, 1.0f, 0.0f);                                            //uv

                OgreDotNet.MeshBuilderHelper.SetVertexFloat(pV, vertexsize, (i * 4) + 2, offPos, -vec.x, 0.0f, -vec.z);                                 //position
                OgreDotNet.MeshBuilderHelper.SetVertexFloat(pV, vertexsize, (i * 4) + 2, offNorm, 0.0f, 1.0f, 0.0f);                                    //normal
                OgreDotNet.MeshBuilderHelper.SetVertexFloat(pV, vertexsize, (i * 4) + 2, offUV, 0.0f, 1.0f);                                            //uv

                OgreDotNet.MeshBuilderHelper.SetVertexFloat(pV, vertexsize, (i * 4) + 3, offPos, vec.x, 0.0f, vec.z);                                   //position
                OgreDotNet.MeshBuilderHelper.SetVertexFloat(pV, vertexsize, (i * 4) + 3, offNorm, 0.0f, 1.0f, 0.0f);                                    //normal
                OgreDotNet.MeshBuilderHelper.SetVertexFloat(pV, vertexsize, (i * 4) + 3, offUV, 1.0f, 1.0f);                                            //uv

                vec = rot * vec;
            }

            vbuf.Get().Unlock();
            sm.vertexData.vertexBufferBinding.setBinding(0, vbuf);
            sm.indexData.indexCount  = 6 * 3;
            sm.indexData.indexBuffer = HardwareBufferManager.getSingleton().createIndexBuffer(
                HardwareIndexBuffer.IndexType.IT_16BIT, 6 * 3, HardwareBuffer.Usage.HBU_STATIC_WRITE_ONLY);

            IntPtr pI = sm.indexData.indexBuffer.Get().Lock(HardwareBuffer.LockOptions.HBL_DISCARD);

            for (i = 0; i < 3; ++i)
            {
                UInt16 off = (UInt16)(i * 4);
                OgreDotNet.MeshBuilderHelper.SetIndex16bit(pI, (i * 2) + 0, (UInt16)(0 + off), (UInt16)(3 + off), (UInt16)(1 + off));
                OgreDotNet.MeshBuilderHelper.SetIndex16bit(pI, (i * 2) + 1, (UInt16)(0 + off), (UInt16)(2 + off), (UInt16)(3 + off));
            }

            sm.indexData.indexBuffer.Get().Unlock();
            sm.SetMaterialName(GRASS_MATERIAL);
            mp.Get().Load();
        }
示例#2
0
        private unsafe void createCube(string name, Mogre.Vector3 gpose, double d)
        {
            MeshPtr msh  = MeshManager.Singleton.CreateManual(name, "General");
            SubMesh sub1 = msh.CreateSubMesh("1");

            const float sqrt13    = 0.577350269f; /* sqrt(1/3) */
            const int   nVertices = 8;
            const int   vbufCount = 3 * 2 * nVertices;

            float[] vertices = new float[vbufCount] {
                (float)(gpose.x - d / 2), (float)(gpose.y + d / 2), (float)(gpose.z - d / 2), //0 position
                -sqrt13, sqrt13, -sqrt13,                                                     //0 normal A
                (float)(gpose.x + d / 2), (float)(gpose.y + d / 2), (float)(gpose.z - d / 2), //1 position
                sqrt13, sqrt13, -sqrt13,                                                      //1 normal B
                (float)(gpose.x + d / 2), (float)(gpose.y - d / 2), (float)(gpose.z - d / 2), //2 position
                sqrt13, -sqrt13, -sqrt13,                                                     //2 normal F
                (float)(gpose.x - d / 2), (float)(gpose.y - d / 2), (float)(gpose.z - d / 2), //3 position
                -sqrt13, -sqrt13, -sqrt13,                                                    //3 normal H
                (float)(gpose.x - d / 2), (float)(gpose.y + d / 2), (float)(gpose.z + d / 2),
                -sqrt13, sqrt13, sqrt13,                                                      //4 normal C
                (float)(gpose.x + d / 2), (float)(gpose.y + d / 2), (float)(gpose.z + d / 2),
                sqrt13, sqrt13, sqrt13,                                                       //5 normal D
                (float)(gpose.x + d / 2), (float)(gpose.y - d / 2), (float)(gpose.z + d / 2),
                sqrt13, -sqrt13, sqrt13,                                                      //6 normal E
                (float)(gpose.x - d / 2), (float)(gpose.y - d / 2), (float)(gpose.z + d / 2),
                -sqrt13, -sqrt13, sqrt13,                                                     //7 normal G
            };

            const int ibufCount = 36;

            ushort[] faces = new ushort[ibufCount] {
                //back
                0, 2, 3,
                0, 1, 2,
                //right
                1, 6, 2,
                1, 5, 6,
                //front
                4, 6, 5,
                4, 7, 6,
                //left
                0, 7, 4,
                0, 3, 7,
                //top
                0, 5, 1,
                0, 4, 5,
                //bottom
                2, 7, 3,
                2, 6, 7
            };

            sub1.vertexData             = new VertexData();
            sub1.vertexData.vertexCount = nVertices;

            VertexDeclaration decl = sub1.vertexData.vertexDeclaration;
            uint offset            = 0;

            //position
            decl.AddElement(0, offset, VertexElementType.VET_FLOAT3, VertexElementSemantic.VES_POSITION);
            offset += VertexElement.GetTypeSize(VertexElementType.VET_FLOAT3);
            //normal
            decl.AddElement(0, offset, VertexElementType.VET_FLOAT3, VertexElementSemantic.VES_NORMAL);
            offset += VertexElement.GetTypeSize(VertexElementType.VET_FLOAT3);

            HardwareVertexBufferSharedPtr vbuf = HardwareBufferManager.Singleton.CreateVertexBuffer(offset, sub1.vertexData.vertexCount, HardwareBuffer.Usage.HBU_STATIC_WRITE_ONLY);
            VertexBufferBinding           bind = sub1.vertexData.vertexBufferBinding;

            void *pVertices;

            fixed(float *pFVertice = vertices)
            {
                pVertices = (void *)pFVertice;
            }

            vbuf.WriteData(0, vbuf.SizeInBytes, pVertices, true);
            bind.SetBinding(0, vbuf);

            void *pFaces;

            fixed(ushort *pUFaces = faces)
            {
                pFaces = (void *)pUFaces;
            }

            HardwareIndexBufferSharedPtr ibuf = HardwareBufferManager.Singleton.CreateIndexBuffer(HardwareIndexBuffer.IndexType.IT_16BIT, ibufCount, HardwareBuffer.Usage.HBU_STATIC_WRITE_ONLY);

            ibuf.WriteData(0, ibuf.SizeInBytes, pFaces, true);

            sub1.useSharedVertices     = false;
            sub1.indexData.indexBuffer = ibuf;
            sub1.indexData.indexCount  = ibufCount;
            sub1.indexData.indexStart  = 0;

            sub1.SetMaterialName("Examples/10PointBlock");

            msh._setBounds(new AxisAlignedBox(-100, -100, -100, 100, 100, 100));
            msh._setBoundingSphereRadius(Mogre.Math.Sqrt(3 * 100 * 100));

            msh.Load();
        }
示例#3
0
        private void CreateGrassMesh()
        {
            // Each grass section is 3 planes at 60 degrees to each other
            // Normals point straight up to simulate correct lighting
            Mesh msh = MeshManager.Singleton.CreateManual(GRASS_MESH_NAME, ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, null);

            SubMesh sm = msh.CreateSubMesh();

            sm.useSharedVertices      = false;
            sm.vertexData             = new VertexData();
            sm.vertexData.vertexStart = 0;
            sm.vertexData.vertexCount = 12;

            VertexDeclaration dcl = sm.vertexData.vertexDeclaration;
            uint offset           = 0;

            dcl.AddElement(0, offset, VertexElementType.VET_FLOAT3, VertexElementSemantic.VES_POSITION);
            offset += VertexElement.GetTypeSize(VertexElementType.VET_FLOAT3);
            dcl.AddElement(0, offset, VertexElementType.VET_FLOAT3, VertexElementSemantic.VES_NORMAL);
            offset += VertexElement.GetTypeSize(VertexElementType.VET_FLOAT3);
            dcl.AddElement(0, offset, VertexElementType.VET_FLOAT2, VertexElementSemantic.VES_TEXTURE_COORDINATES);
            offset += VertexElement.GetTypeSize(VertexElementType.VET_FLOAT2);

            HardwareVertexBufferSharedPtr vbuf = HardwareBufferManager.Singleton.CreateVertexBuffer(offset, 12, HardwareBuffer.Usage.HBU_STATIC_WRITE_ONLY);

            int i;

            unsafe {
                float *pData = (float *)(vbuf.Lock(HardwareBuffer.LockOptions.HBL_DISCARD));

                Vector3    baseVec = new Vector3(GRASS_WIDTH / 2, 0, 0);
                Vector3    vec     = baseVec;
                Quaternion rot     = new Quaternion();
                rot.FromAngleAxis(Math.DegreesToRadians(60), Vector3.UNIT_Y);

                for (i = 0; i < 3; ++i)
                {
                    //position
                    *pData++ = -vec.x;
                    *pData++ = GRASS_HEIGHT;
                    *pData++ = -vec.z;
                    // normal
                    *pData++ = 0;
                    *pData++ = 1;
                    *pData++ = 0;
                    // uv
                    *pData++ = 0;
                    *pData++ = 0;

                    // position
                    *pData++ = vec.x;
                    *pData++ = GRASS_HEIGHT;
                    *pData++ = vec.z;
                    // normal
                    *pData++ = 0;
                    *pData++ = 1;
                    *pData++ = 0;
                    // uv
                    *pData++ = 1;
                    *pData++ = 0;

                    // position
                    *pData++ = -vec.x;
                    *pData++ = 0;
                    *pData++ = -vec.z;
                    // normal
                    *pData++ = 0;
                    *pData++ = 1;
                    *pData++ = 0;
                    // uv
                    *pData++ = 0;
                    *pData++ = 1;

                    // position
                    *pData++ = vec.x;
                    *pData++ = 0;
                    *pData++ = vec.z;
                    // normal
                    *pData++ = 0;
                    *pData++ = 1;
                    *pData++ = 0;
                    // uv
                    *pData++ = 1;
                    *pData++ = 1;

                    vec = rot * vec;
                } //for
            }     //unsafe

            vbuf.Unlock();

            sm.vertexData.vertexBufferBinding.SetBinding(0, vbuf);
            sm.indexData.indexCount  = 6 * 3;
            sm.indexData.indexBuffer = HardwareBufferManager.Singleton.CreateIndexBuffer(HardwareIndexBuffer.IndexType.IT_16BIT, 6 * 3, HardwareBuffer.Usage.HBU_STATIC_WRITE_ONLY);

            unsafe {
                ushort *pI = (ushort *)(sm.indexData.indexBuffer.Lock(HardwareBuffer.LockOptions.HBL_DISCARD));

                for (i = 0; i < 3; ++i)
                {
                    int off  = i * 4;
                    *   pI++ = (ushort)(off);
                    *   pI++ = (ushort)(off + 3);
                    *   pI++ = (ushort)(off + 1);

                    *pI++ = (ushort)(off + 0);
                    *pI++ = (ushort)(off + 2);
                    *pI++ = (ushort)(off + 3);
                }
            }

            sm.indexData.indexBuffer.Unlock();
            sm.SetMaterialName(GRASS_MATERIAL);

            msh.Load();
        }