コード例 #1
0
        public void TestSphereMesh()
        {
            XNAGame game = new XNAGame();

            List <SphereMesh> meshes = new List <SphereMesh>();


            TangentVertex[] vertices;
            short[]         indices;
            SphereMesh.CreateUnitSphereVerticesAndIndices(4, out vertices, out indices);

            bool wireframe = false;

            game.InitializeEvent +=
                delegate
            {
                SphereMesh mesh;
                mesh = new SphereMesh(1, 4, Color.Green);
                mesh.Initialize(game);
                meshes.Add(mesh);

                mesh             = new SphereMesh(1, 5, Color.PowderBlue);
                mesh.WorldMatrix = Matrix.CreateTranslation(3, 0, 0);
                mesh.Initialize(game);
                meshes.Add(mesh);

                mesh             = new SphereMesh(1, 12, Color.Yellow);
                mesh.WorldMatrix = Matrix.CreateTranslation(6, 0, 0);
                mesh.Initialize(game);
                meshes.Add(mesh);

                mesh             = new SphereMesh(1, 30, Color.Turquoise);
                mesh.WorldMatrix = Matrix.CreateTranslation(9, 0, 0);
                mesh.Initialize(game);
                meshes.Add(mesh);

                mesh             = new SphereMesh(40, 100, Color.Red);
                mesh.WorldMatrix = Matrix.CreateTranslation(0, 0, -60);
                mesh.Initialize(game);
                meshes.Add(mesh);


                mesh             = new SphereMesh(40, 13, Color.Red);
                mesh.WorldMatrix = Matrix.CreateTranslation(100, 0, -60);
                mesh.Initialize(game);
                meshes.Add(mesh);
            };
            game.UpdateEvent +=
                delegate
            {
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.W))
                {
                    wireframe = !wireframe;
                }
            };

            game.DrawEvent +=
                delegate
            {
                game.LineManager3D.AddAABB(meshes[1].BoundingBox, Matrix.Identity, Color.Red);

                int i;
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.R))
                {
                    for (i = 0; i < meshes.Count; i++)
                    {
                        meshes[i].ReloadShader(game);
                    }
                }
                game.GraphicsDevice.RenderState.FillMode = FillMode.Solid;
                game.GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;

                if (!wireframe)
                {
                    for (i = 1; i < meshes.Count; i++)
                    {
                        meshes[i].Render(game);
                    }
                }


                i = 0;
                meshes[i].Color = Color.LightGreen;
                meshes[i].Render(game);

                game.GraphicsDevice.RenderState.CullMode = CullMode.CullClockwiseFace;
                meshes[i].Color = Color.Red;
                meshes[i].Render(game);

                game.GraphicsDevice.RenderState.FillMode = FillMode.WireFrame;
                game.GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;

                meshes[i].Color = Color.Black;
                meshes[i].Render(game);

                if (wireframe)
                {
                    for (i = 1; i < meshes.Count; i++)
                    {
                        meshes[i].Render(game);
                    }
                }
                for (i = 0; i < vertices.Length; i++)
                {
                    game.LineManager3D.AddCenteredBox(vertices[i].pos, 0.05f, Color.Red);
                }
            };

            game.Run();
        }
コード例 #2
0
        public void TestOBJToRAMMeshConverterPerObjectVisual()
        {
            var c = new OBJToRAMMeshConverter(new RAMTextureFactory());


            var importer = new ObjImporter();

            importer.AddMaterialFileStream("Town001.mtl", new FileStream(TestFiles.TownMtl, FileMode.Open));
            importer.ImportObjFile(TestFiles.TownObj);

            var meshes = c.CreateMeshesFromObjects(importer);

            var texturePool           = new TexturePool();
            var meshpartPool          = new MeshPartPool();
            var vertexDeclarationPool = new VertexDeclarationPool();

            var renderer = new SimpleMeshRenderer(texturePool, meshpartPool, vertexDeclarationPool);

            vertexDeclarationPool.SetVertexElements <TangentVertex>(TangentVertex.VertexElements);



            var spheres = new List <ClientPhysicsTestSphere>();
            var engine  = new PhysicsEngine();
            PhysicsDebugRendererXNA debugRenderer = null;

            var builder = new MeshPhysicsActorBuilder(new MeshPhysicsPool());

            TheWizards.Client.ClientPhysicsQuadTreeNode root;

            int numNodes = 20;

            root = new ClientPhysicsQuadTreeNode(
                new BoundingBox(
                    new Vector3(-numNodes * numNodes / 2f, -100, -numNodes * numNodes / 2f),
                    new Vector3(numNodes * numNodes / 2f, 100, numNodes * numNodes / 2f)));

            QuadTree.Split(root, 5);

            var physicsElementFactoryXNA = new MeshPhysicsFactoryXNA(engine, root);
            var physicsElementFactory    = physicsElementFactoryXNA.Factory;

            var physicsElements = new List <MeshStaticPhysicsElement>();

            for (int i = 0; i < 0 * 100 + 1 * meshes.Count; i++)
            {
                var mesh = meshes[i];
                var el   = renderer.AddMesh(mesh);
                el.WorldMatrix = Matrix.CreateTranslation(Vector3.Right * 0 * 2 + Vector3.UnitZ * 0 * 2);

                var pEl = physicsElementFactory.CreateStaticElement(mesh, Matrix.Identity);
                physicsElements.Add(pEl);
            }

            var game = new XNAGame();

            game.IsFixedTimeStep                    = false;
            game.DrawFps                            = true;
            game.SpectaterCamera.FarClip            = 5000;
            game.Graphics1.PreparingDeviceSettings += delegate(object sender, PreparingDeviceSettingsEventArgs e)
            {
                DisplayMode displayMode = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode;
                e.GraphicsDeviceInformation.PresentationParameters.BackBufferFormat = displayMode.Format;
                e.GraphicsDeviceInformation.PresentationParameters.BackBufferWidth  = displayMode.Width;
                e.GraphicsDeviceInformation.PresentationParameters.BackBufferHeight = displayMode.Height;
                game.SpectaterCamera.AspectRatio = displayMode.Width / (float)displayMode.Height;
            };
            game.Graphics1.ToggleFullScreen();

            var sphereMesh = new SphereMesh(0.3f, 20, Color.Green);
            var visualizer = new QuadTreeVisualizerXNA();

            game.AddXNAObject(physicsElementFactoryXNA);

            game.AddXNAObject(texturePool);
            game.AddXNAObject(meshpartPool);
            game.AddXNAObject(vertexDeclarationPool);
            game.AddXNAObject(renderer);


            game.InitializeEvent += delegate
            {
                engine.Initialize();
                debugRenderer = new PhysicsDebugRendererXNA(game, engine.Scene);
                debugRenderer.Initialize(game);
                sphereMesh.Initialize(game);
            };

            bool showPhysics = true;

            game.DrawEvent += delegate
            {
                if (game.Keyboard.IsKeyPressed(Keys.P))
                {
                    showPhysics = !showPhysics;
                }
                if (showPhysics)
                {
                    debugRenderer.Render(game);
                }
                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Green;

                    return(node.PhysicsObjects.Count == 0);
                });

                visualizer.RenderNodeGroundBoundig(game, root,
                                                   delegate(ClientPhysicsQuadTreeNode node, out Color col)
                {
                    col = Color.Orange;

                    return(node.PhysicsObjects.Count > 0);
                });

                for (int i = 0; i < physicsElements.Count; i++)
                {
                    var el = physicsElements[i];
                    //game.LineManager3D.AddBox(BoundingBox.CreateFromSphere( el.BoundingSphere), Color.Orange);
                }
                for (int i = 0; i < spheres.Count; i++)
                {
                    sphereMesh.WorldMatrix = Matrix.CreateTranslation(spheres[i].Center);
                    sphereMesh.Render(game);
                }
            };
            game.UpdateEvent += delegate
            {
                engine.Update(game.Elapsed);
                sphereMesh.Update(game);
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    var iSphere = new ClientPhysicsTestSphere(engine.Scene,
                                                              game.SpectaterCamera.CameraPosition + game.SpectaterCamera.CameraDirection
                                                              , 0.3f);

                    iSphere.InitDynamic();
                    iSphere.Actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 10;

                    spheres.Add(iSphere);
                }



                for (int i = 0; i < spheres.Count; i++)
                {
                    spheres[i].Update(root, game);
                }
            };

            game.Run();
        }
コード例 #3
0
 public void Initialize(IXNAGame _game)
 {
     sphereMesh.Initialize(game);
 }
コード例 #4
0
        public void TestOBJToRAMMeshConverterPerObjectVisualCool()
        {
            var textureFactory = new RAMTextureFactory();
            var c = new OBJToRAMMeshConverter(textureFactory);


            var importer = new ObjImporter();

            importer.AddMaterialFileStream("Town001.mtl", new FileStream("../GameData/Town/OBJ03/Town001.mtl", FileMode.Open));
            importer.ImportObjFile("../GameData/Town/OBJ03/Town001.obj");

            var meshes = c.CreateMeshesFromObjects(importer);

            var texturePool           = new TexturePool();
            var meshpartPool          = new MeshPartPool();
            var vertexDeclarationPool = new VertexDeclarationPool();

            var renderer = new SimpleMeshRenderer(texturePool, meshpartPool, vertexDeclarationPool);

            vertexDeclarationPool.SetVertexElements <TangentVertex>(TangentVertex.VertexElements);



            var spheres = new List <ClientPhysicsTestSphere>();
            var engine  = new PhysicsEngine();
            PhysicsDebugRendererXNA debugRenderer = null;

            var root = CreatePhysicsQuadtree(20, 5);

            var physicsElementFactoryXNA = new MeshPhysicsFactoryXNA(engine, root);
            var physicsElementFactory    = physicsElementFactoryXNA.Factory;

            var physicsElements = new List <MeshStaticPhysicsElement>();

            for (int i = 0; i < 0 * 100 + 1 * meshes.Count; i++)
            {
                var mesh = meshes[i];
                var el   = renderer.AddMesh(mesh);
                el.WorldMatrix = Matrix.CreateTranslation(Vector3.Right * 0 * 2 + Vector3.UnitZ * 0 * 2);

                var pEl = physicsElementFactory.CreateStaticElement(mesh, Matrix.Identity);
                physicsElements.Add(pEl);
            }
            var gameMeshes = new List <OBJParserTest.TestGameMesh>();

            var game = new XNAGame();

            game.IsFixedTimeStep                    = false;
            game.DrawFps                            = true;
            game.SpectaterCamera.FarClip            = 5000;
            game.Graphics1.PreparingDeviceSettings += delegate(object sender, PreparingDeviceSettingsEventArgs e)
            {
                DisplayMode displayMode = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode;
                e.GraphicsDeviceInformation.PresentationParameters.BackBufferFormat = displayMode.Format;
                e.GraphicsDeviceInformation.PresentationParameters.BackBufferWidth  = displayMode.Width;
                e.GraphicsDeviceInformation.PresentationParameters.BackBufferHeight = displayMode.Height;
                game.SpectaterCamera.AspectRatio = displayMode.Width / (float)displayMode.Height;
            };
            game.Graphics1.ToggleFullScreen();

            var barrelMesh = OBJParserTest.GetBarrelMesh(c);
            var crateMesh  = OBJParserTest.GetCrateMesh(c);

            var sphereMesh = new SphereMesh(0.3f, 20, Color.Green);
            var visualizer = new QuadTreeVisualizerXNA();

            game.AddXNAObject(physicsElementFactoryXNA);

            game.AddXNAObject(texturePool);
            game.AddXNAObject(meshpartPool);
            game.AddXNAObject(vertexDeclarationPool);
            game.AddXNAObject(renderer);


            game.InitializeEvent += delegate
            {
                engine.Initialize();
                debugRenderer = new PhysicsDebugRendererXNA(game, engine.Scene);
                debugRenderer.Initialize(game);
                sphereMesh.Initialize(game);

                for (int i = 0; i < meshes.Count; i++)
                {
                    var mesh = meshes[i];
                    var data = mesh.GetCollisionData();

                    /*if (data.TriangleMesh != null)
                     *  physicsElementFactory.MeshPhysicsPool.PreloadTriangleMesh(engine.Scene, data.TriangleMesh);*/
                }
            };

            bool showPhysics = true;

            game.DrawEvent += delegate
            {
                if (game.Keyboard.IsKeyPressed(Keys.P))
                {
                    showPhysics = !showPhysics;
                }
                if (showPhysics)
                {
                    debugRenderer.Render(game);
                }

                /*visualizer.RenderNodeGroundBoundig(game, root,
                 *  delegate(ClientPhysicsQuadTreeNode node, out Color col)
                 *  {
                 *      col = Color.Green;
                 *
                 *      return node.PhysicsObjects.Count == 0;
                 *  });
                 *
                 * visualizer.RenderNodeGroundBoundig(game, root,
                 * delegate(ClientPhysicsQuadTreeNode node, out Color col)
                 * {
                 *     col = Color.Orange;
                 *
                 *     return node.PhysicsObjects.Count > 0;
                 * });*/

                for (int i = 0; i < physicsElements.Count; i++)
                {
                    var el = physicsElements[i];
                    //game.LineManager3D.AddBox(BoundingBox.CreateFromSphere( el.BoundingSphere), Color.Orange);
                }
                for (int i = 0; i < spheres.Count; i++)
                {
                    sphereMesh.WorldMatrix = Matrix.CreateTranslation(spheres[i].Center);
                    sphereMesh.Render(game);
                }
            };
            game.UpdateEvent += delegate
            {
                engine.Update(game.Elapsed);
                sphereMesh.Update(game);
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    var pEl = physicsElementFactory.CreateDynamicElement(crateMesh,
                                                                         Matrix.CreateTranslation(
                                                                             game.SpectaterCamera.CameraPosition +
                                                                             game.SpectaterCamera.CameraDirection));
                    pEl.Actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 30;

                    var rEl = renderer.AddMesh(crateMesh);


                    gameMeshes.Add(new OBJParserTest.TestGameMesh(rEl, pEl));
                }
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.E))
                {
                    var pEl = physicsElementFactory.CreateDynamicElement(barrelMesh,
                                                                         Matrix.CreateTranslation(
                                                                             game.SpectaterCamera.CameraPosition +
                                                                             game.SpectaterCamera.CameraDirection));
                    pEl.Actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 30;

                    var rEl = renderer.AddMesh(barrelMesh);


                    gameMeshes.Add(new OBJParserTest.TestGameMesh(rEl, pEl));
                }


                for (int i = 0; i < gameMeshes.Count; i++)
                {
                    var m = gameMeshes[i];
                    m.RenderElement.WorldMatrix = m.PhysicsElement.World;
                }
            };

            game.Run();
        }