Пример #1
0
        static void Main(string[] args)
        {
            // start up the engine

            IrrlichtDevice device = new IrrlichtDevice(DriverType.OPENGL);

            device.ResizeAble    = true;
            device.WindowCaption = "Irrlicht.NET C# example 01 - Hello World";

            // load some textures

            ITexture texSydney = device.VideoDriver.GetTexture(@"..\..\media\sydney.bmp");
            ITexture texWall   = device.VideoDriver.GetTexture(@"..\..\media\wall.bmp");
            ITexture texLogo   = device.VideoDriver.GetTexture(@"..\..\media\irrlichtlogoaligned.jpg");

            // load the animated mesh of sydney

            Irrlicht.Scene.IAnimatedMesh mesh =
                device.SceneManager.GetMesh(@"..\..\media\sydney.md2");

            if (mesh == null)
            {
                System.Windows.Forms.MessageBox.Show(
                    @"Could not load mesh ..\..\media\sydney.md2, exiting.",
                    "Problem starting program");
                return;
            }

            // add a camera, a test scene node and the animated mesh to the scene

            ICameraSceneNode cam =
                device.SceneManager.AddCameraSceneNodeFPS(null, 100, 100, -1);

            cam.Position = new Vector3D(20, 0, -50);


            ISceneNode node = device.SceneManager.AddCubeSceneNode(15,
                                                                   null, -1, new Vector3D(30, -15, 0));

            node.SetMaterialTexture(0, texWall);

            node = device.SceneManager.AddAnimatedMeshSceneNode(mesh, null, -1);
            node.SetMaterialTexture(0, texSydney);
            node.SetMaterialFlag(MaterialFlag.LIGHTING, false);

            // make cursor invisible
            device.CursorControl.Visible = false;

            // start drawing loop

            int fps = 0;

            while (device.Run())
            {
                if (device.WindowActive)
                {
                    device.VideoDriver.BeginScene(true, true, new Color(0, 100, 100, 100));

                    device.SceneManager.DrawAll();

                    // draw the logo
                    device.VideoDriver.Draw2DImage(
                        texLogo, new Position2D(10, 10),
                        new Rect(0, 0, 88, 31),
                        new Rect(new Position2D(0, 0), device.VideoDriver.ScreenSize),
                        new Color(0xffffff), false);

                    device.VideoDriver.EndScene();

                    if (fps != device.VideoDriver.FPS)
                    {
                        fps = device.VideoDriver.FPS;
                        device.WindowCaption = "Irrlicht.NET C# example 01 - Hello World [" +
                                               device.VideoDriver.Name + "] fps:" + fps;
                    }
                }
            } // end drawing-loop
        }     // end main()
Пример #2
0
        /// <summary>
        /// This method loads a model and displays it using an
        /// addAnimatedMeshSceneNode and the scene manager. Nothing difficult. It also
        /// displays a short message box, if the model could not be loaded.
        /// </summary>
        /// <param name="fileName">File name of the model to be loaded.</param>
        private void LoadModel(string fileName)
        {
            // Check if we have a valid device to use.
            if (device == null)
            {
                MessageBox.Show("Can't load model because the device is not created.", caption);
                return;
            }

            bool foundBSP = false;

            // modify the name if it a .pk3 file
            if (fileName.EndsWith(".pk3"))
            {
                device.FileSystem.AddZipFileArchive(fileName);
                foundBSP = true;
            }

            // Clear the model if there was something loaded before.
            if (model != null)
            {
                model.Remove();
            }

            // Load a mesh from file.
            scene.IAnimatedMesh m = device.SceneManager.GetMesh(fileName);

            if (m == null)
            {
                // Model could not be loaded
                if (startupModelFile != fileName)
                {
                    string error = "The model could not be loaded.\nMaybe it is not a supported file format.";
                    MessageBox.Show(error, caption);
                }
                return;
            }

            // load a model into the engine from the mesh
            model = device.SceneManager.AddAnimatedMeshSceneNode(m, null, 0);

            // set default material properties
            if (!foundBSP)
            {
                if (mnuViewMatSolid.Checked)
                {
                    model.SetMaterialType(video.MaterialType.SOLID);
                }
                else if (mnuViewMatTransp.Checked)
                {
                    model.SetMaterialType(video.MaterialType.TRANSPARENT_ADD_COLOR);
                }
                else if (mnuViewMatReflect.Checked)
                {
                    model.SetMaterialType(video.MaterialType.SPHERE_MAP);
                }
            }

            model.SetMaterialFlag(video.MaterialFlag.LIGHTING, false);
            model.DebugDataVisible = true;
            model.AnimationSpeed   = 1000;
        }
Пример #3
0
    public void runIndoorTest()
    {
        device = new IrrlichtDevice(SelectedDriverType,
                                    new Dimension2D(800, 600), 16, false, true, false);

        device.EventReceiver = this;
        device.ResizeAble    = true;
        device.WindowCaption = "Irrlicht.NET indoor test";

        // load some textures and meshes

        ITexture texSydney = device.VideoDriver.GetTexture(@"..\..\media\sydney.bmp");
        ITexture texWall   = device.VideoDriver.GetTexture(@"..\..\media\wall.jpg");
        ITexture texLogo   = device.VideoDriver.GetTexture(@"..\..\media\irrlichtlogoaligned.jpg");

        Irrlicht.Scene.IAnimatedMesh mesh =
            device.SceneManager.GetMesh(@"..\..\media\sydney.md2");

        if (mesh == null)
        {
            System.Windows.Forms.MessageBox.Show(
                @"Could not load mesh ..\..\media\sydney.md2, exiting.",
                "Problem starting program");
            return;
        }

        // add a cube to the scene

        ISceneNode node = device.SceneManager.AddCubeSceneNode(15,
                                                               null, -1, new Vector3D(30, -15, 0));

        node.SetMaterialTexture(0, texWall);
        node.SetMaterialFlag(Irrlicht.Video.MaterialFlag.LIGHTING, false);

        // add an animator to the cube to make it rotate

        ISceneNodeAnimator anim = device.SceneManager.CreateRotationAnimator(new Vector3D(0.2f, 0.2f, 0));

        node.AddAnimator(anim);

        // add animated mesh

        IAnimatedMeshSceneNode anode = device.SceneManager.AddAnimatedMeshSceneNode(mesh, null, -1);

        anode.SetMaterialTexture(0, texSydney);
        anode.SetMaterialFlag(MaterialFlag.LIGHTING, false);

        anode.Scale    = new Vector3D(2, 2, 2);
        anode.Position = new Vector3D(0, -20, 0);

        // add a shadow

        Shadow = anode.AddShadowVolumeSceneNode();
        if (Shadow != null)
        {
            Shadow.Visible = false;
        }

        // where no light there no shadow
        device.SceneManager.AddLightSceneNode(null, new Vector3D(20, 100, -50),
                                              new Colorf(255, 0, 0), 200, -1);

        // add quake 3 level

        device.FileSystem.AddZipFileArchive("../../media/map-20kdm2.pk3");

        IAnimatedMesh q3levelmesh = device.SceneManager.GetMesh("20kdm2.bsp");
        ISceneNode    q3node      = device.SceneManager.AddOctTreeSceneNode(q3levelmesh, null, -1);

        q3node.Position = new Vector3D(-1370, -130, -1400);

        // create octtree triangle selector for q3 mesh

        ITriangleSelector selector = device.SceneManager.CreateOctTreeTriangleSelector(
            q3levelmesh.GetMesh(0), q3node, 128);

        // add billboard

        IBillboardSceneNode bill = device.SceneManager.AddBillboardSceneNode(null,
                                                                             new Dimension2Df(20, 20), new Vector3D(0, 0, 0), -1);

        bill.SetMaterialType(MaterialType.TRANSPARENT_ADD_COLOR);
        bill.SetMaterialTexture(0, device.VideoDriver.GetTexture("../../media/particle.bmp"));
        bill.SetMaterialFlag(MaterialFlag.LIGHTING, false);
        bill.SetMaterialFlag(MaterialFlag.ZBUFFER, false);

        // create camera

        ICameraSceneNode cam =
            device.SceneManager.AddCameraSceneNodeFPS(null, 100, 300, -1);

        cam.Position = new Vector3D(20, 300, -50);

        // make cursor invisible
        device.CursorControl.Visible = false;

        // create collision animator and add it to the camera

        ISceneNodeAnimator collAnim = device.SceneManager.CreateCollisionResponseAnimator(
            selector, cam,
            new Vector3D(30, 50, 30),            // size of ellipsoid around camera
            new Vector3D(0, -3, 0),              // gravity
            new Vector3D(0, 50, 0),              // translation
            0.0005f);                            // sliding value

        cam.AddAnimator(collAnim);

        // load some font and set it into the skin

        IGUIFont font = device.GUIEnvironment.GetFont("../../media/fonthaettenschweiler.bmp");

        device.GUIEnvironment.Skin.Font = font;

        // add some gui stuff

        device.GUIEnvironment.AddMessageBox("Hello World",
                                            "I'm a Irrlicht.NET MessageBox. Please press SPACE to close me.", true,
                                            MessageBoxFlag.OK | MessageBoxFlag.CANCEL, null, -1);

        // start drawing loop

        int fps = 0;

        while (device.Run())
        {
            if (device.WindowActive)
            {
                device.VideoDriver.BeginScene(true, true, new Color(255, 0, 0, 50));

                // draw scene

                device.SceneManager.DrawAll();
                device.GUIEnvironment.DrawAll();

                // do some collision testing

                Line3D line = new Line3D();
                line.start = cam.Position;
                line.end   = ((cam.Target - line.start).Normalize() * 1000.0f) + line.start;

                Vector3D   intersection = new Vector3D();
                Triangle3D tri          = new Triangle3D();

                if (device.SceneManager.SceneCollisionManager.GetCollisionPoint(
                        line, selector, out intersection, out tri))
                {
                    bill.Position = intersection;

                    Material mat = new Material();
                    mat.Lighting = false;

                    device.VideoDriver.SetTransform(TransformationState.WORLD, new Matrix4());
                    device.VideoDriver.SetMaterial(mat);
                    device.VideoDriver.Draw3DTriangle(tri, new Color(0, 255, 0, 0));
                }

                // draw 2d logo

                device.VideoDriver.Draw2DImage(
                    texLogo, new Position2D(10, 10),
                    new Rect(0, 0, 88, 31),
                    new Rect(new Position2D(0, 0), device.VideoDriver.ScreenSize),
                    new Color(0xffffff), false);

                // draw some text
                font.Draw("Press 'S' to toggle the visibility of the realtime shadow.",
                          new Position2D(120, 20), new Color(100, 150, 200, 200));

                device.VideoDriver.EndScene();

                if (fps != device.VideoDriver.FPS)
                {
                    fps = device.VideoDriver.FPS;
                    device.WindowCaption = "Irrlicht.NET test (primitives:" +
                                           device.VideoDriver.PrimitiveCountDrawn + ") fps:" + fps;
                }
            }
        }
    }