コード例 #1
0
ファイル: cGrass.cs プロジェクト: 0000duck/ogredotnet
        protected override void CreateScene()
        {
            mLog = LogManager.Singleton.createLog("DemoGrass.log", false, true);
            mLog.LogMessage(string.Format("DemoGrass log {0}", System.DateTime.Now));

            //Init waveGrass varibles
            xinc     = OgreMath.PI * 0.4f;
            zinc     = OgreMath.PI * 0.55f;
            xpos     = OgreMath.RangeRandom(-OgreDotNet.OgreMath.PI, OgreDotNet.OgreMath.PI);
            zpos     = OgreMath.RangeRandom(-OgreDotNet.OgreMath.PI, OgreDotNet.OgreMath.PI);
            xxoffset = new Math3D.Vector4(0.0f, 0.0f, 0.0f, 0.0f);


            mSceneManager.SetSkyBox(true, "Examples/SpaceSkyBox");

            setupLighting();
            mSceneManager.AmbientLightColor = Color.FromArgb(125, 125, 125, 125);


            //create a ground plain
            mLog.LogMessage("Point 1");
            Plane plane = new Plane();

            plane.Normal.x = 0;
            plane.Normal.y = 1;
            plane.Normal.z = 0;
            plane.D        = 0;
            MeshManager.GetSingleton().CreatePlane("Myplane", "General", plane,
                                                   14500, 14500, 10, 10, true, 1, 50, 50, Vector3.UnitZ);
            Entity pPlaneEnt = mSceneManager.CreateEntity("plane", "Myplane");

            pPlaneEnt.SetMaterialName("Examples/GrassFloor");
            pPlaneEnt.SetCastShadows(false);
            mSceneManager.GetRootSceneNode().CreateChildSceneNode().AttachObject(pPlaneEnt);


            //create the grass mesh
            mLog.LogMessage("Point 2");
            createGrassMesh2();
            Entity e = null;

            e = mSceneManager.CreateEntity("1", GRASS_MESH_NAME);


            //close the the grass mesh in a region
            StaticGeometry s = mSceneManager.CreateStaticGeometry("bing");

            s.setRegionDimensions(new Vector3(1000, 1000, 1000));
            // Set the region origin so the centre is at 0 world
            s.setOrigin(new Vector3(-500, 500, -500));
            mLog.LogMessage("Point 3");
            for (int x = -1950; x < 1950; x += 150)                     //1950
            {
                for (int z = -1950; z < 1950; z += 150)
                {
                    Vector3 pos;
                    pos.x = x + OgreMath.RangeRandom(-25, 25);
                    pos.y = 0;
                    pos.z = z + OgreMath.RangeRandom(-25, 25);
                    Quaternion orientation = Quaternion.FromAngleAxis(
                        new Degree(OgreMath.RangeRandom(0, 359)).ValueDegrees(), Vector3.UnitY);
                    Vector3 scale = new Vector3(1, OgreMath.RangeRandom(0.85f, 1.15f), 1);
                    s.addEntity(e, pos, orientation, scale);
                }
            }
            mLog.LogMessage("Point 4");
            s.build();
            mStaticGeom = s;
            //mStaticGeom.dump("Grass.SG.Dump.txt");



            //Put an Ogre head in the middle
            mLog.LogMessage("Point 5");
            OgreDotNet.MeshPtr m = MeshManager.GetSingleton().Load("ogrehead.mesh", "General");
            Mesh   mm = m.Get();
            UInt16 src = 0, dest = 0;

            if (!mm.SuggestTangentVectorBuildParams(ref src, ref dest))
            {
                mLog.LogMessage("got from SuggestTangentVectorBuildParams src=" + src + " dest=" + dest);
                mm.BuildTangentVectors(src, dest);
            }
            e = mSceneManager.CreateEntity("head", "ogrehead.mesh");
            e.SetMaterialName("Examples/OffsetMapping/Specular");
            SceneNode headNode = mSceneManager.GetRootSceneNode().CreateChildSceneNode();

            headNode.AttachObject(e);
            headNode.SetScale(7, 7, 7);
            headNode.SetPosition(0, 200, 0);
            e.SetNormaliseNormals(true);


            mLog.LogMessage("Point 6");
            //setup camera position
            mCamera.Move(new Vector3(0, 350, 800));
            mCamera.LookAt = new Vector3(0, 200, 0);
            mLog.LogMessage("Point end");
        }
コード例 #2
0
ファイル: cGrass.cs プロジェクト: 0000duck/ogredotnet
        /// <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();
        }