private void loadShowcaseScene()
 {
     importer = new ObjImporter();
     importer.AddMaterialFileStream(Path.GetFileName(ShowcaseTest.ShowcaseMTL), File.OpenRead(ShowcaseTest.ShowcaseMTL));
     importer.ImportObjFile(ShowcaseTest.ShowcaseOBJ);
     interpreter = new OBJInterpreter(importer, new RAMTextureFactory());
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads a mesh from an obj file, and the similarly name mat file.
        /// Textures are loaded in the texturefactory
        /// </summary>
        public static IMesh LoadMeshFromObj(FileInfo objFile)
        {
            var importer = new ObjImporter(); //TODO: Garbage Collector fancyness

            var        materialFilePath   = objFile.FullName.Substring(0, objFile.FullName.Length - objFile.Extension.Length) + ".mtl";
            var        materialFileName   = objFile.Name.Substring(0, objFile.Name.Length - objFile.Extension.Length) + ".mtl";
            FileStream materialFileStream = null;

            try
            {
                if (File.Exists(materialFilePath))
                {
                    materialFileStream = File.Open(materialFilePath, FileMode.Open, FileAccess.Read);
                    importer.AddMaterialFileStream(materialFileName, materialFileStream);
                }
                importer.ImportObjFile(objFile.FullName);
                return(instance.converter.CreateMesh(importer));
            }
            finally
            {
                if (materialFileStream != null)
                {
                    materialFileStream.Close();
                }
                materialFileStream = null;
            }
        }
Ejemplo n.º 3
0
        public void TestOBJToRAMMeshConverter()
        {
            var c = new OBJToRAMMeshConverter(new RAMTextureFactory());


            var fsMat = new FileStream(TestFiles.CrateMtl, FileMode.Open);


            var importer = new ObjImporter();

            importer.AddMaterialFileStream("Crate01.mtl", fsMat);
            importer.ImportObjFile(TestFiles.CrateObj);


            fsMat.Close();



            var mesh = c.CreateMesh(importer);

            importer = new ObjImporter();
            importer.AddMaterialFileStream("CollisionModelBoxes001.mtl",
                                           EmbeddedFile.GetStream("MHGameWork.TheWizards.Tests.Physics.Files.CollisionModelBoxes001.mtl",
                                                                  "CollisionModelBoxes001.mtl"));
            importer.ImportObjFile(EmbeddedFile.GetStream("MHGameWork.TheWizards.Tests.Physics.Files.CollisionModelBoxes001.obj", "CollisionModelBoxes001.obj"));

            mesh = c.CreateMesh(importer);
        }
Ejemplo n.º 4
0
        public static RAMMesh CreateGuildHouseMesh(OBJToRAMMeshConverter c)
        {
            ObjImporter importer;

            importer = new ObjImporter();
            importer.AddMaterialFileStream("GuildHouse01.mtl", File.OpenRead(TestFiles.GuildHouseMtl));
            importer.ImportObjFile(TestFiles.GuildHouseObj);

            return(c.CreateMesh(importer));
        }
Ejemplo n.º 5
0
        public void TestImportGeometry()
        {
            XNAGame game = new XNAGame();

            ObjImporter        importer = new ObjImporter();
            bool               dirty    = true;
            Seeder             seeder   = new Seeder(489);
            LineManager3DLines lines    = null;


            var fsCrate  = new FileStream(TestFiles.CrateObj, FileMode.Open);
            var fsMHouse = new FileStream(TestFiles.MerchantsHouseObj, FileMode.Open);

            game.UpdateEvent += delegate
            {
                if (game.Keyboard.IsKeyPressed(Keys.C) || dirty)
                {
                    lines = new LineManager3DLines();
                    importer.ImportObjFile(fsCrate);

                    renderWireframe(importer, lines, Color.Black, Vector3.Zero);
                    renderObjects(importer, lines, seeder, Vector3.Right * 5);
                    renderSubObjects(importer, lines, seeder, Vector3.Right * 10);

                    importer.ImportObjFile(fsMHouse);

                    renderWireframe(importer, lines, Color.Black, Vector3.Forward * 30);
                    renderObjects(importer, lines, seeder, Vector3.Right * 30 + Vector3.Forward * 30);
                    renderSubObjects(importer, lines, seeder, Vector3.Right * 60 + Vector3.Forward * 30);
                    dirty = false;
                }
            };

            game.DrawEvent += delegate
            {
                game.LineManager3D.Render(lines);
            };

            game.Run();

            fsCrate.Close();
            fsMHouse.Close();
        }
Ejemplo n.º 6
0
        public static RAMMesh CreateMeshFromObj(OBJToRAMMeshConverter c, string obj, string mtl)
        {
            var         fi = new FileInfo(mtl);
            ObjImporter importer;

            importer = new ObjImporter();
            importer.AddMaterialFileStream(fi.Name, File.OpenRead(mtl));
            importer.ImportObjFile(obj);

            return(c.CreateMesh(importer));
        }
Ejemplo n.º 7
0
        public void TestMeshRendererRenderCity()
        {
            var c = new OBJToRAMMeshConverter(new RAMTextureFactory());


            var importer = new ObjImporter();

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

            var mesh = c.CreateMesh(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 el = renderer.AddMesh(mesh);

            el.WorldMatrix = Matrix.CreateTranslation(Vector3.Right * 0 * 2 + Vector3.UnitZ * 0 * 2);


            var game = new XNAGame();

            game.IsFixedTimeStep = false;
            game.DrawFps         = true;
            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();



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

            game.Run();
        }
Ejemplo n.º 8
0
        public void TestImportMaterials()
        {
            ObjImporter importer = new ObjImporter();


            var fsMat = new FileStream(TestFiles.CrateMtl, FileMode.Open);

            importer.AddMaterialFileStream("Crate01.mtl", fsMat);
            importer.ImportObjFile(TestFiles.CrateObj);

            var materials = importer.Materials;

            fsMat.Close();

            //TODO: check the values
        }
Ejemplo n.º 9
0
        public static RAMMesh CreateMerchantsHouseMeshOLD()
        {
            ObjImporter importer = new ObjImporter();


            importer.AddMaterialFileStream("MerchantsHouse.mtl", new FileStream(TestFiles.MerchantsHouseMtl, FileMode.Open));
            importer.ImportObjFile(TestFiles.MerchantsHouseObj);

            var textureFactory = new RAMTextureFactory();

            /*textureFactory.AddAssemblyResolvePath(typeof(ObjImporter).Assembly,
             *                                    "MHGameWork.TheWizards.OBJParser.Files.maps");*/
            var conv = new OBJToRAMMeshConverter(textureFactory);

            return(conv.CreateMesh(importer));
        }
        public static RAMMesh GetBarrelMesh(OBJToRAMMeshConverter c)
        {
            var fsMat = new FileStream(TestFiles.BarrelMtl, FileMode.Open);

            var importer = new ObjImporter();

            importer.AddMaterialFileStream("Barrel01.mtl", fsMat);

            importer.ImportObjFile(TestFiles.BarrelObj);

            var meshes = c.CreateMeshesFromObjects(importer);

            fsMat.Close();

            return(meshes[0]);
        }
Ejemplo n.º 11
0
        public void TestDeferredMeshRendererRenderCity()
        {
            var c = new OBJToRAMMeshConverter(new RAMTextureFactory());


            var importer = new ObjImporter();

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

            var mesh = c.CreateMesh(importer);

            var game = new DX11Game();

            game.InitDirectX();
            var context = game.Device.ImmediateContext;


            var texturePool = new MHGameWork.TheWizards.Graphics.SlimDX.Rendering.Deferred.TexturePool(game);

            var gBuffer = new GBuffer(game.Device, 800, 600);

            var renderer = new DeferredMeshesRenderer(game, gBuffer, texturePool);



            var el = renderer.AddMesh(mesh);

            el.WorldMatrix = global::SlimDX.Matrix.Translation(MathHelper.Right * 0 * 2 + global::SlimDX.Vector3.UnitZ * 0 * 2);


            game.GameLoopEvent += delegate
            {
                gBuffer.Clear();
                gBuffer.SetTargetsToOutputMerger();

                renderer.Draw();

                context.ClearState();
                game.SetBackbuffer();

                GBufferTest.DrawGBuffer(game, gBuffer);
            };
            global::SlimDX.Configuration.EnableObjectTracking = false;

            game.Run();
        }
        public IMesh loadMeshFromFile(string objFile, string matFile, string matName)
        {
            var fsMat = new FileStream(matFile, FileMode.Open);

            var importer = new ObjImporter();

            importer.AddMaterialFileStream(matName, fsMat);

            importer.ImportObjFile(objFile);


            var meshes = converter.CreateMesh(importer);

            fsMat.Close();

            return(optimizer.CreateOptimized(meshes)); // Optimize the mesh
        }
Ejemplo n.º 13
0
        public void TestMeshRendererAdvanced()
        {
            var texFactory = new RAMTextureFactory();
            var c          = new OBJToRAMMeshConverter(texFactory);

            var game = new XNAGame();

            game.IsFixedTimeStep = false;
            game.DrawFps         = true;


            var importer = new ObjImporter();

            importer.AddMaterialFileStream("Crate01.mtl", File.OpenRead(TestFiles.CrateMtl));
            importer.ImportObjFile(TestFiles.CrateObj);

            var mesh = c.CreateMesh(importer);

            RAMMesh mesh2 = DefaultMeshes.CreateMerchantsHouseMesh(c);


            RAMMesh mesh3 = DefaultMeshes.CreateGuildHouseMesh(c);

            SimpleMeshRenderer renderer = InitDefaultMeshRenderer(game);


            var el = renderer.AddMesh(mesh);

            el.WorldMatrix = Matrix.CreateTranslation(Vector3.Right * 0 * 2 + Vector3.UnitZ * 0 * 2);

            el             = renderer.AddMesh(mesh2);
            el.WorldMatrix = Matrix.CreateTranslation(new Vector3(0, 0, 80));

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    el             = renderer.AddMesh(mesh3);
                    el.WorldMatrix = Matrix.CreateTranslation(new Vector3(j * 30, 0, 70 + i * 30));
                }
            }



            game.Run();
        }
Ejemplo n.º 14
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();
        }
Ejemplo n.º 15
0
        public void TestDeferredRendererCity()
        {
            var game = new DX11Game();

            game.InitDirectX();
            SlimDX.Configuration.EnableObjectTracking = false;

            var renderer = new DeferredRenderer(game);

            var c = new OBJToRAMMeshConverter(new RAMTextureFactory());


            var importer = new ObjImporter();

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

            var meshes = c.CreateMeshesFromObjects(importer);

            for (int index = 0; index < meshes.Count; index++)
            {
                var ramMesh = meshes[index];
                var el      = renderer.CreateMeshElement(ramMesh);
            }
            var directional = renderer.CreateDirectionalLight();

            directional.ShadowsEnabled = true;
            var point = renderer.CreatePointLight();

            point.LightRadius   *= 2;
            point.ShadowsEnabled = true;
            var spot = renderer.CreateSpotLight();

            spot.LightRadius   *= 2;
            spot.ShadowsEnabled = true;

            var visualizer = new QuadTreeVisualizer();

            var  otherCam = new SpectaterCamera(1, 10000);
            var  camState = false;
            int  state    = 0;
            bool rotate   = false;

            game.GameLoopEvent += delegate
            {
                if (game.Keyboard.IsKeyPressed(Key.D1))
                {
                    state = 0;
                }
                if (game.Keyboard.IsKeyPressed(Key.D2))
                {
                    state = 1;
                }
                if (game.Keyboard.IsKeyPressed(Key.D3))
                {
                    state = 2;
                }
                if (game.Keyboard.IsKeyPressed(Key.D4))
                {
                    state = 3;
                }

                if (game.Keyboard.IsKeyPressed(Key.C))
                {
                    camState = !camState;
                }

                if (game.Keyboard.IsKeyPressed(Key.NumberPad0))
                {
                    rotate = !rotate;
                }

                if (rotate)
                {
                    game.SpectaterCamera.AngleHorizontal += game.Elapsed * MathHelper.Pi * (1 / 8f);
                }

                switch (state)
                {
                case 0:
                    break;

                case 1:
                    directional.LightDirection = game.SpectaterCamera.CameraDirection;
                    break;

                case 2:
                    point.LightPosition = game.SpectaterCamera.CameraPosition;

                    break;

                case 3:
                    spot.LightPosition = game.SpectaterCamera.CameraPosition;
                    spot.SpotDirection = game.SpectaterCamera.CameraDirection;
                    break;
                }

                if (camState)
                {
                    game.Camera = game.SpectaterCamera;
                    renderer.DEBUG_SeperateCullCamera = null;
                }
                else
                {
                    game.Camera = otherCam;
                    renderer.DEBUG_SeperateCullCamera = game.SpectaterCamera;
                }
                game.SpectaterCamera.EnableUserInput = camState;
                otherCam.EnableUserInput             = !camState;
                otherCam.Update(game.Elapsed, game.Keyboard, game.Mouse);


                renderer.Draw();


                if (false)
                {
                    //renderer.DEBUG_FrustumCuller.CullCamera = game.Camera;
                    //renderer.DEBUG_FrustumCuller.UpdateVisibility();
                    //for (int i = 0; i < renderer.DebugMeshesRenderer.Elements.Count; i++)
                    //{
                    //    var el = renderer.DebugMeshesRenderer.Elements[i];

                    //    game.LineManager3D.AddBox(el.BoundingBox.dx(), new SlimDX.Color4(0, 1, 0));
                    //}

                    //visualizer.RenderNodeGroundBoundig(game, renderer.DEBUG_FrustumCuller.RootNode);
                    visualizer.RenderNodeGroundBoundig(game, renderer.DEBUG_FrustumCuller.RootNode,
                                                       delegate(FrustumCuller.CullNode quadTreeNode, out Color4 color4)
                    {
                        if (point.Views[2].IsNodeVisible(quadTreeNode))
                        {
                            color4 = new Color4(1, 0, 0);
                        }
                        else
                        {
                            color4 = new Color4(0, 1, 0);
                        }
                        return(true);
                    });
                }

                game.LineManager3D.AddViewFrustum(game.SpectaterCamera.ViewProjection, new SlimDX.Color4(1, 0, 0));
            };


            game.Run();
        }
        public void TestMeshRendererAdvanced()
        {
            var texFactory = new RAMTextureFactory();
            var c          = new OBJToRAMMeshConverter(texFactory);

            var game = new DX11Game();

            game.InitDirectX();
            var context = game.Device.ImmediateContext;

            var importer = new ObjImporter();

            importer.AddMaterialFileStream("Crate01.mtl", File.OpenRead(TestFiles.CrateMtl));
            importer.ImportObjFile(TestFiles.CrateObj);

            var mesh = c.CreateMesh(importer);

            RAMMesh mesh2 = RenderingTestsHelper.CreateMerchantsHouseMesh(c);


            RAMMesh mesh3 = RenderingTestsHelper.CreateGuildHouseMesh(c);

            var gBuffer = new GBuffer(game.Device, 800, 600);

            DeferredMeshesRenderer renderer = InitDefaultMeshRenderer(game, gBuffer);


            var el = renderer.AddMesh(mesh);

            el.WorldMatrix = Matrix.Translation(MathHelper.Right * 0 * 2 + Vector3.UnitZ * 0 * 2);

            el             = renderer.AddMesh(mesh2);
            el.WorldMatrix = Matrix.Translation(new Vector3(0, 0, 80));

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    el             = renderer.AddMesh(mesh3);
                    el.WorldMatrix = Matrix.Translation(new Vector3(j * 30, 0, 70 + i * 30));
                }
            }



            game.GameLoopEvent += delegate
            {
                gBuffer.Clear();
                gBuffer.SetTargetsToOutputMerger();

                context.Rasterizer.State = game.HelperStates.RasterizerShowAll;

                renderer.Draw();

                context.ClearState();
                game.SetBackbuffer();

                GBufferTest.DrawGBuffer(game, gBuffer);
            };
            game.Run();
        }
Ejemplo n.º 17
0
        public void TestObjImportPhysics()
        {
            XNAGame game = new XNAGame();

            game.SpectaterCamera.CameraPosition = new Vector3(0, 0, -40);
            game.SpectaterCamera.FarClip        = 10000;
            PhysicsEngine engine = new PhysicsEngine();

            //game.AddXNAObject(engine);



            game.InitializeEvent += delegate
            {
                engine.Initialize();

                PhysicsDebugRendererXNA debugRenderer = new PhysicsDebugRendererXNA(game, engine.Scene);

                game.AddXNAObject(debugRenderer);


                ObjImporter importer = new ObjImporter();
                //importer.ImportObjFile(EmbeddedFile.GetStream(typeof(ObjImporter).Assembly, "MHGameWork.TheWizards.OBJParser.Files.Crate.obj", "Crate.obj"));
                importer.ImportObjFile(TestFiles.MerchantsHouseObj);
                List <Vector3> positions            = importer.Vertices;
                Vector3[]      transformedPositions = new Vector3[positions.Count];
                Matrix         objectMatrix         = Matrix.CreateScale(5);
                Matrix         transform            = objectMatrix;
                Vector3.Transform(positions.ToArray(), ref transform, transformedPositions);
                var indices = new List <int>();

                for (int i = 0; i < importer.Groups.Count; i++)
                {
                    for (int j = 0; j < importer.Groups[i].SubObjects.Count; j++)
                    {
                        for (int k = 0; k < importer.Groups[i].SubObjects[j].Faces.Count; k++)
                        {
                            indices.Add(importer.Groups[i].SubObjects[j].Faces[k].V1.Position);
                            indices.Add(importer.Groups[i].SubObjects[j].Faces[k].V2.Position);
                            indices.Add(importer.Groups[i].SubObjects[j].Faces[k].V3.Position);
                        }
                    }
                }



                var triangleMesh = CreateTriangleMesh(transformedPositions, indices.ToArray(), engine.Scene);

                var triangleMeshShapeDesc = new TriangleMeshShapeDescription();
                triangleMeshShapeDesc.TriangleMesh = triangleMesh;

                var actorDesc = new ActorDescription(triangleMeshShapeDesc);

                var actor = engine.Scene.CreateActor(actorDesc);
            };

            game.UpdateEvent += delegate
            {
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    Actor actor = PhysicsHelper.CreateDynamicSphereActor(engine.Scene, 1, 1);
                    actor.GlobalPosition = game.SpectaterCamera.CameraPosition +
                                           game.SpectaterCamera.CameraDirection * 5;
                    actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 5;
                }
                engine.Update(game.Elapsed);
            };

            game.Run();

            engine.Dispose();
        }
Ejemplo n.º 18
0
        public void TestOBJToRAMMeshConverterVisual()
        {
            PhysicsEngine           engine        = new PhysicsEngine();
            PhysicsDebugRendererXNA debugRenderer = null;



            var texFactory = new RAMTextureFactory();
            var c          = new OBJToRAMMeshConverter(texFactory);


            var importer = new ObjImporter();

            importer.AddMaterialFileStream("CollisionModelBoxes001.mtl",
                                           EmbeddedFile.GetStream("MHGameWork.TheWizards.Tests.Physics.Files.CollisionModelBoxes001.mtl",
                                                                  "CollisionModelBoxes001.mtl"));
            importer.ImportObjFile(EmbeddedFile.GetStream("MHGameWork.TheWizards.Tests.Physics.Files.CollisionModelBoxes001.obj", "CollisionModelBoxes001.obj"));


            var mesh = c.CreateMesh(importer);

            importer = new ObjImporter();
            importer.AddMaterialFileStream("HouseTest.mtl", new FileStream("../GameData/Core/001-House01_BoxTest-OBJ/HouseTest.mtl", FileMode.Open));
            importer.ImportObjFile("../GameData/Core/001-House01_BoxTest-OBJ/HouseTest.obj");

            var mesh2 = c.CreateMesh(importer);

            importer = new ObjImporter();
            var fsMHouseMat = new FileStream(TestFiles.MerchantsHouseMtl, FileMode.Open);

            importer.AddMaterialFileStream("MerchantsHouse.mtl", fsMHouseMat);
            importer.ImportObjFile(TestFiles.MerchantsHouseObj);

            fsMHouseMat.Close();

            var mesh3 = c.CreateMesh(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 el = renderer.AddMesh(mesh);

            el.WorldMatrix = Matrix.CreateTranslation(Vector3.Right * 0 * 2 + Vector3.UnitZ * 0 * 2);

            el             = renderer.AddMesh(mesh2);
            el.WorldMatrix = Matrix.CreateTranslation(new Vector3(0, 0, 80));

            el             = renderer.AddMesh(mesh3);
            el.WorldMatrix = Matrix.CreateTranslation(new Vector3(0, 0, -80));



            var game = new XNAGame();

            game.IsFixedTimeStep = false;
            game.DrawFps         = true;

            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);


                var builder = new MeshPhysicsActorBuilder(new MeshPhysicsPool());
                builder.CreateActorStatic(engine.Scene, mesh.GetCollisionData(), Matrix.Identity);
                builder.CreateActorStatic(engine.Scene, mesh2.GetCollisionData(), Matrix.CreateTranslation(new Vector3(0, 0, 80)));
                builder.CreateActorStatic(engine.Scene, mesh3.GetCollisionData(), Matrix.CreateTranslation(new Vector3(0, 0, -80)));
            };

            game.DrawEvent += delegate
            {
                debugRenderer.Render(game);
            };
            game.UpdateEvent += delegate
            {
                engine.Update(game.Elapsed);
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F))
                {
                    Actor actor = PhysicsHelper.CreateDynamicSphereActor(engine.Scene, 0.3f, 1);
                    actor.GlobalPosition = game.SpectaterCamera.CameraPosition +
                                           game.SpectaterCamera.CameraDirection * 5;
                    actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 5;
                }
            };



            game.Run();
        }
Ejemplo n.º 19
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();
        }