示例#1
0
        public static LODSector Create(AABBox dimension)
        {
            LODSector s = new LODSector();

            s.dimension = dimension;
            s.d1        = dimension.MinEdge;
            s.d2        = new Vector3Df(dimension.MinEdge.X, dimension.MinEdge.Y, dimension.MaxEdge.Z);
            s.d3        = new Vector3Df(dimension.MinEdge.X, dimension.MaxEdge.Y, dimension.MinEdge.Z);
            s.d4        = new Vector3Df(dimension.MinEdge.X, dimension.MaxEdge.Y, dimension.MaxEdge.Z);
            s.d5        = dimension.MaxEdge;
            s.d6        = new Vector3Df(dimension.MaxEdge.X, dimension.MinEdge.Y, dimension.MinEdge.Z);
            s.d7        = new Vector3Df(dimension.MaxEdge.X, dimension.MinEdge.Y, dimension.MaxEdge.Z);
            s.d8        = new Vector3Df(dimension.MaxEdge.X, dimension.MaxEdge.Y, dimension.MinEdge.Z);

            s.lodItems = new List <LODItem>();

            return(s);
        }
示例#2
0
		public static LODSector Create(AABBox dimension)
		{
			LODSector s = new LODSector();
			s.dimension = dimension;
			s.d1 = dimension.MinEdge;
			s.d2 = new Vector3Df(dimension.MinEdge.X, dimension.MinEdge.Y, dimension.MaxEdge.Z);
			s.d3 = new Vector3Df(dimension.MinEdge.X, dimension.MaxEdge.Y, dimension.MinEdge.Z);
			s.d4 = new Vector3Df(dimension.MinEdge.X, dimension.MaxEdge.Y, dimension.MaxEdge.Z);
			s.d5 = dimension.MaxEdge;
			s.d6 = new Vector3Df(dimension.MaxEdge.X, dimension.MinEdge.Y, dimension.MinEdge.Z);
			s.d7 = new Vector3Df(dimension.MaxEdge.X, dimension.MinEdge.Y, dimension.MaxEdge.Z);
			s.d8 = new Vector3Df(dimension.MaxEdge.X, dimension.MaxEdge.Y, dimension.MinEdge.Z);

			s.lodItems = new List<LODItem>();

			return s;
		}
示例#3
0
		static void Main(string[] args)
		{
			int lodItemCount = AskUserForLODItemCount();

			DriverType driverType;
			if (!AskUserForDriver(out driverType))
				return;

			device = IrrlichtDevice.CreateDevice(driverType, new Dimension2Di(800, 600));
			if (device == null)
				return;

			device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent);
			device.SetWindowCaption("Simple LOD - Irrlicht Lime");
			device.CursorControl.Visible = false;

			VideoDriver driver = device.VideoDriver;
			SceneManager scene = device.SceneManager;

			// generate all LODs of mesh

			List<Mesh> lodMesh = new List<Mesh>();
			Material meshMaterial = null;
			List<int> lodTriangleCount = new List<int>();

			int[] p = new int[] { 100, 50, 32, 20, 12, 6, 3 };
			for (int i = 0; i < p.Length; i++)
			{
				Mesh m = scene.GeometryCreator.CreateSphereMesh(50, p[i], p[i]);

				MeshBuffer mb = m.GetMeshBuffer(0);
				mb.Material.Type = MaterialType.Solid;
				mb.Material.SetTexture(0, driver.GetTexture("../../media/earth.jpg"));

				m.SetMaterialFlag(MaterialFlag.Lighting, false);
				lodMesh.Add(m);

				if (meshMaterial == null)
					meshMaterial = m.GetMeshBuffer(0).Material;

				lodTriangleCount.Add(mb.IndexCount / 3);
			}

			// generate world,
			// we generate a lot of objects with random positions in huge virtual cube

			int virtualCubeSide = 20000;
			LODItem[] lodItems = new LODItem[lodItemCount];
			Random r = new Random(12345000);
			for (int i = 0; i < lodItemCount; i++)
			{
				Matrix tmat = new Matrix(
					new Vector3Df( // translation
						r.Next(virtualCubeSide) - virtualCubeSide / 2,
						r.Next(virtualCubeSide) - virtualCubeSide / 2,
						r.Next(virtualCubeSide) - virtualCubeSide / 2));

				Vector3Df rvect = new Vector3Df(
					(float)r.NextDouble() / 200.0f,
					(float)r.NextDouble() / 200.0f,
					(float)r.NextDouble() / 200.0f);

				lodItems[i] = LODItem.Create(device, lodMesh, tmat, rvect);
			}

			// split world on virtual sectors (cubes) for faster visibility check

			int lodSectorSide = 6; // total number of sectors will be lodSectorSide^3, so for 6 it is 216
			int lodSectorSize = virtualCubeSide / lodSectorSide;
			LODSector[,,] lodSectors = new LODSector[lodSectorSide, lodSectorSide, lodSectorSide];

			for (int i = 0; i < lodSectorSide; i++)
			{
				for (int j = 0; j < lodSectorSide; j++)
				{
					for (int k = 0; k < lodSectorSide; k++)
					{
						AABBox dimension = new AABBox(
							new Vector3Df(i * lodSectorSize, j * lodSectorSize, k * lodSectorSize),
							new Vector3Df((i + 1) * lodSectorSize, (j + 1) * lodSectorSize, (k + 1) * lodSectorSize));

						dimension.MinEdge -= virtualCubeSide / 2;
						dimension.MaxEdge -= virtualCubeSide / 2;

						LODSector s = LODSector.Create(dimension);
						lodSectors[i, j, k] = s;
					}
				}
			}

			for (int i = 0; i < lodItems.Length; i++)
			{
				Vector3Df pos = lodItems[i].Position;
				pos += virtualCubeSide / 2;
				pos /= lodSectorSize;

				int ix = (int)pos.X;
				int iy = (int)pos.Y;
				int iz = (int)pos.Z;

				if (ix < 0) ix = 0;
				if (ix > lodSectorSide - 1)
					ix = lodSectorSide - 1;

				if (iy < 0) ix = 0;
				if (iy > lodSectorSide - 1)
					iy = lodSectorSide - 1;

				if (iz < 0) iz = 0;
				if (iz > lodSectorSide - 1)
					iz = lodSectorSide - 1;

				lodSectors[ix, iy, iz].AddLODItem(lodItems[i]);
			}

			// camera

			CameraSceneNode camera = scene.AddCameraSceneNodeFPS();
			camera.FarValue = 30000;

			// font, which we are going to use to show any text we need

			IrrlichtLime.GUI.GUIFont font = device.GUIEnvironment.GetFont("../../media/fontlucida.png");

			// render loop

			while (device.Run())
			{
				driver.BeginScene();
				scene.DrawAll();

				if (isLabelMode)
				{
					LODItem.LabelPositions = new List<Vector2Di>();
					LODItem.LabelTexts = new List<string>();
				}
				else
				{
					LODItem.LabelPositions = null;
					LODItem.LabelTexts = null;
				}

				meshMaterial.Wireframe = isWireframeMode;
				device.VideoDriver.SetMaterial(meshMaterial);

				uint timer = device.Timer.Time;
				Vector3Df cameraPosition = camera.AbsolutePosition;
				AABBox cameraViewBox = camera.ViewFrustum.BoundingBox;

				for (int i = 0; i < lodSectorSide; i++)
				{
					for (int j = 0; j < lodSectorSide; j++)
					{
						for (int k = 0; k < lodSectorSide; k++)
						{
							lodSectors[i, j, k].Draw(timer, cameraPosition, cameraViewBox);
						}
					}
				}

				if (isLabelMode)
				{
					for (int i = 0; i < LODItem.LabelPositions.Count; i++)
					{
						driver.Draw2DLine(
							LODItem.LabelPositions[i] - new Vector2Di(10, 0),
							LODItem.LabelPositions[i] + new Vector2Di(50, 0),
							Color.OpaqueGreen);

						driver.Draw2DLine(
							LODItem.LabelPositions[i] - new Vector2Di(0, 10),
							LODItem.LabelPositions[i] + new Vector2Di(0, 50),
							Color.OpaqueGreen);

						font.Draw(LODItem.LabelTexts[i], LODItem.LabelPositions[i], Color.OpaqueGreen);
					}
				}

				if (isStatsMode)
				{
					// show LOD stats

					int[] lodCount = new int[7] { 0, 0, 0, 0, 0, 0, 0 };
					for (int i = 0; i < lodItems.Length; i++)
						lodCount[lodItems[i].CurrentLOD]++;

					string f = "";
					for (int i = 0; i < lodCount.Length; i++)
					{
						int n = lodCount[i];
						f += "LOD" + i.ToString() + ": " + n.ToString() + " [" + ((n * 100) / lodItemCount).ToString() + "%] objects\n";
					}

					string l = "------------------------";

					font.Draw(
						string.Format("Stats\n{0}\n{1}{2}\nTotal: {3} [100%] objects", l, f, l, lodItemCount),
						new Vector2Di(10, 140),
						Color.OpaqueMagenta);
				}

				// show general stats

				font.Draw(string.Format(
					"Camera position: {0}\nTotal LOD 0 triangles: {1}\nTriangles currently drawn: {2}\nDriver: {3}\nFPS: {4}",
					camera.AbsolutePosition,
					lodTriangleCount[0] * lodItemCount,
					driver.PrimitiveCountDrawn,
					driver.Name,
					driver.FPS),
					10, 10, Color.OpaqueYellow);

				// show active keys

				font.Draw(
				    "[S] Toggle stats\n[W] Toggle wireframe\n[L] Toggle labels (only for LODs from 0 to 4)\n[Esc] Exit application",
				    10, driver.ScreenSize.Height - 80, Color.OpaqueCyan);

				driver.EndScene();
			}

			// drop

			device.Drop();
		}
示例#4
0
        static void Main()
        {
            int lodItemCount = AskForLODItemCount();

            DriverType?driverType = AskForDriver();

            if (!driverType.HasValue)
            {
                return;
            }

            device = IrrlichtDevice.CreateDevice(driverType.Value, new Dimension2Di(800, 600));
            if (device == null)
            {
                return;
            }

            device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent);
            device.SetWindowCaption("Simple LOD - Irrlicht Lime");
            device.CursorControl.Visible = false;

            VideoDriver  driver = device.VideoDriver;
            SceneManager scene  = device.SceneManager;

            // generate all LODs of mesh

            List <Mesh> lodMesh          = new List <Mesh>();
            Material    meshMaterial     = null;
            List <int>  lodTriangleCount = new List <int>();

            int[] p = new int[] { 100, 50, 32, 20, 12, 6, 3 };
            for (int i = 0; i < p.Length; i++)
            {
                Mesh m = scene.GeometryCreator.CreateSphereMesh(50, p[i], p[i]);

                MeshBuffer mb = m.GetMeshBuffer(0);
                mb.Material.Type = MaterialType.Solid;
                mb.Material.SetTexture(0, driver.GetTexture("../../media/earth.jpg"));

                m.SetMaterialFlag(MaterialFlag.Lighting, false);
                lodMesh.Add(m);

                if (meshMaterial == null)
                {
                    meshMaterial = m.GetMeshBuffer(0).Material;
                }

                lodTriangleCount.Add(mb.IndexCount / 3);
            }

            // generate world,
            // we generate a lot of objects with random positions in huge virtual cube

            int virtualCubeSide = 20000;

            LODItem[] lodItems = new LODItem[lodItemCount];
            Random    r        = new Random(12345000);

            for (int i = 0; i < lodItemCount; i++)
            {
                Matrix tmat = new Matrix(
                    new Vector3Df(                     // translation
                        r.Next(virtualCubeSide) - virtualCubeSide / 2,
                        r.Next(virtualCubeSide) - virtualCubeSide / 2,
                        r.Next(virtualCubeSide) - virtualCubeSide / 2));

                Vector3Df rvect = new Vector3Df(
                    (float)r.NextDouble() / 200.0f,
                    (float)r.NextDouble() / 200.0f,
                    (float)r.NextDouble() / 200.0f);

                lodItems[i] = LODItem.Create(device, lodMesh, tmat, rvect);
            }

            // split world on virtual sectors (cubes) for faster visibility check

            int lodSectorSide = 6;             // total number of sectors will be lodSectorSide^3, so for 6 it is 216
            int lodSectorSize = virtualCubeSide / lodSectorSide;

            LODSector[,,] lodSectors = new LODSector[lodSectorSide, lodSectorSide, lodSectorSide];

            for (int i = 0; i < lodSectorSide; i++)
            {
                for (int j = 0; j < lodSectorSide; j++)
                {
                    for (int k = 0; k < lodSectorSide; k++)
                    {
                        AABBox dimension = new AABBox(
                            new Vector3Df(i * lodSectorSize, j * lodSectorSize, k * lodSectorSize),
                            new Vector3Df((i + 1) * lodSectorSize, (j + 1) * lodSectorSize, (k + 1) * lodSectorSize));

                        dimension.MinEdge -= virtualCubeSide / 2;
                        dimension.MaxEdge -= virtualCubeSide / 2;

                        LODSector s = LODSector.Create(dimension);
                        lodSectors[i, j, k] = s;
                    }
                }
            }

            for (int i = 0; i < lodItems.Length; i++)
            {
                Vector3Df pos = lodItems[i].Position;
                pos += virtualCubeSide / 2;
                pos /= lodSectorSize;

                int ix = (int)pos.X;
                int iy = (int)pos.Y;
                int iz = (int)pos.Z;

                if (ix < 0)
                {
                    ix = 0;
                }
                if (ix > lodSectorSide - 1)
                {
                    ix = lodSectorSide - 1;
                }

                if (iy < 0)
                {
                    ix = 0;
                }
                if (iy > lodSectorSide - 1)
                {
                    iy = lodSectorSide - 1;
                }

                if (iz < 0)
                {
                    iz = 0;
                }
                if (iz > lodSectorSide - 1)
                {
                    iz = lodSectorSide - 1;
                }

                lodSectors[ix, iy, iz].AddLODItem(lodItems[i]);
            }

            // camera

            CameraSceneNode camera = scene.AddCameraSceneNodeFPS();

            camera.FarValue = 30000;

            // font, which we are going to use to show any text we need

            IrrlichtLime.GUI.GUIFont font = device.GUIEnvironment.GetFont("../../media/fontlucida.png");

            // render loop

            while (device.Run())
            {
                driver.BeginScene();
                scene.DrawAll();

                if (isLabelMode)
                {
                    LODItem.LabelPositions = new List <Vector2Di>();
                    LODItem.LabelTexts     = new List <string>();
                }
                else
                {
                    LODItem.LabelPositions = null;
                    LODItem.LabelTexts     = null;
                }

                meshMaterial.Wireframe = isWireframeMode;
                device.VideoDriver.SetMaterial(meshMaterial);

                uint      timer          = device.Timer.Time;
                Vector3Df cameraPosition = camera.AbsolutePosition;
                AABBox    cameraViewBox  = camera.ViewFrustum.BoundingBox;

                for (int i = 0; i < lodSectorSide; i++)
                {
                    for (int j = 0; j < lodSectorSide; j++)
                    {
                        for (int k = 0; k < lodSectorSide; k++)
                        {
                            lodSectors[i, j, k].Draw(timer, cameraPosition, cameraViewBox);
                        }
                    }
                }

                if (isLabelMode)
                {
                    for (int i = 0; i < LODItem.LabelPositions.Count; i++)
                    {
                        driver.Draw2DLine(
                            LODItem.LabelPositions[i] - new Vector2Di(10, 0),
                            LODItem.LabelPositions[i] + new Vector2Di(50, 0),
                            Color.SolidGreen);

                        driver.Draw2DLine(
                            LODItem.LabelPositions[i] - new Vector2Di(0, 10),
                            LODItem.LabelPositions[i] + new Vector2Di(0, 50),
                            Color.SolidGreen);

                        font.Draw(LODItem.LabelTexts[i], LODItem.LabelPositions[i], Color.SolidGreen);
                    }
                }

                if (isStatsMode)
                {
                    // show LOD stats

                    int[] lodCount = new int[7] {
                        0, 0, 0, 0, 0, 0, 0
                    };
                    for (int i = 0; i < lodItems.Length; i++)
                    {
                        lodCount[lodItems[i].CurrentLOD]++;
                    }

                    string f = "";
                    for (int i = 0; i < lodCount.Length; i++)
                    {
                        int n = lodCount[i];
                        f += "LOD" + i.ToString() + ": " + n.ToString() + " [" + ((n * 100) / lodItemCount).ToString() + "%] objects\n";
                    }

                    string l = "------------------------";

                    font.Draw(
                        string.Format("Stats\n{0}\n{1}{2}\nTotal: {3} [100%] objects", l, f, l, lodItemCount),
                        new Vector2Di(10, 140),
                        Color.SolidMagenta);
                }

                // show general stats

                font.Draw(string.Format(
                              "Camera position: {0}\nTotal LOD 0 triangles: {1}\nTriangles currently drawn: {2}\nDriver: {3}\nFPS: {4}",
                              camera.AbsolutePosition,
                              lodTriangleCount[0] * lodItemCount,
                              driver.PrimitiveCountDrawn,
                              driver.Name,
                              driver.FPS),
                          10, 10, Color.SolidYellow);

                // show active keys

                font.Draw(
                    "[S] Toggle stats\n[W] Toggle wireframe\n[L] Toggle labels (only for LODs from 0 to 4)\n[Esc] Exit application",
                    10, driver.ScreenSize.Height - 80, Color.SolidCyan);

                driver.EndScene();
            }

            // drop

            device.Drop();
        }