Example #1
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;
                }
            }
        }
    }
Example #2
0
        public void run()
        {
            /* At first, we let the user select the driver type,
             * then start up the engine, set a caption, and get a
             * pointer to the video driver.
             */
            // ask user for driver
            DriverType driverType;
            // Ask user to select driver:
            StringBuilder sb = new StringBuilder();

            sb.Append("Please select the driver you want for this example:\n");
            sb.Append("\n(a) Direct3D 9.0c\n(b) Direct3D 8.1\n(c) OpenGL 1.5");
            sb.Append("\n(d) Software Renderer\n(e) Apfelbaum Software Renderer");
            sb.Append("\n(f) Null Device\n(otherKey) exit\n\n");
            // Get the user's input:
            TextReader tIn  = Console.In;
            TextWriter tOut = Console.Out;

            tOut.Write(sb.ToString());
            string input = tIn.ReadLine();

            // Select device based on user's input:
            switch (input)
            {
            case "a":
                driverType = DriverType.DIRECT3D9;
                break;

            case "b":
                driverType = DriverType.DIRECT3D8;
                break;

            case "c":
                driverType = DriverType.OPENGL;
                break;

            case "d":
                driverType = DriverType.SOFTWARE;
                break;

            case "e":
                driverType = DriverType.SOFTWARE2;
                break;

            case "f":
                driverType = DriverType.NULL_DRIVER;
                break;

            default:
                return;
            }
            // Create device and exit if creation fails:
            device = new IrrlichtDevice(driverType, new Dimension2D(1024, 768), 32, false, true, true);
            if (device == null)
            {
                tOut.Write("Device creation failed.");
                return;
            }

            /*
             * Get a pointer to the video driver and the SceneManager so that
             * we do not always have to write device.VideoDriver()
             */
            IVideoDriver driver = device.VideoDriver;

            /*All 2d graphics in this example are put together into one texture,
             * 2ddemo.bmp. Because we want to draw colorkey based sprites, we need
             * to load this texture and tell the engine, which part of it should be
             * transparent based on a colorkey. In this example, we don't tell it
             * the color directly, we just say "Hey Irrlicht Engine, you'll find the
             * color I want at position (0,0) on the texture.". Instead, it would be
             * also possible to call driver.MakeColorKeyTexture(images, Color)
             * to make e.g. all black pixels transparent. Please note, that
             * makeColorKeyTexture just creates an alpha channel based on the color.*/
            ITexture images = driver.GetTexture(path + "2ddemo.bmp");

            driver.MakeColorKeyTexture(images, new Position2D(0, 0));

            /*
             * To be able to draw some text with two different fonts,
             * we load them. Ok, we load just one, as first font we just
             * use the default font which is built into the engine.
             * Also, we define two rectangles, which specify the position
             * of the images of the red imps (little flying creatures) in
             * the texture.
             */
            IGUIFont font  = device.GUIEnvironment.BuiltInFont;
            IGUIFont font2 = device.GUIEnvironment.GetFont(path + "fonthaettenschweiler.bmp");
            Rect     imp1  = new Rect(349, 15, 385, 78);
            Rect     imp2  = new Rect(387, 15, 423, 78);

            /*
             * Everything is prepared, now we can draw everything in the draw loop,
             * between the begin scene and end scene calls. In this example, we are just
             * doing 2d graphics, but it would be no problem to mix them with 3d graphics.
             * Just try it out, and draw some 3d vertices or set up a scene with the scene
             * manager and draw it.
             */
            while (device.Run() && driver != null)
            {
                if (device.WindowActive)
                {
                    uint time = device.Timer.Time;
                    driver.BeginScene(true, true, new Color(0, 120, 102, 136));

                    /*
                     * First, we draw 3 sprites, using the alpha channel we created with
                     * makeColorKeyTexture. The last parameter specifiys that the drawing
                     * method should use thiw alpha channel. The parameter before the last
                     * one specifies a color, with which the sprite should be colored.
                     * (255,255,255,255) is full white, so the sprite will look like the
                     * original. The third sprite is drawed colored based on the time.*/
                    // draw fire & dragons background world
                    driver.Draw2DImage(images, new Position2D(50, 50),
                                       new Rect(0, 0, 342, 224),
                                       new Color(255, 255, 255, 255), true);
                    // draw flying imp
                    driver.Draw2DImage(images, new Position2D(164, 125),
                                       (time / 500 % 2) == 0 ? imp1 : imp2,
                                       new Color(255, 255, 255, 255), true);
                    // draw second flying imp with colorcylce
                    driver.Draw2DImage(images, new Position2D(270, 105),
                                       (time / 500 % 2) == 0 ? imp1 : imp2,
                                       new Color(255, ((int)(time) % 255), 255, 255), true);
                    // Drawing text is really simple. The code should be self explanatory.
                    if (font != null)
                    {
                        font.Draw("This is some text",
                                  new Rect(130, 10, 300, 50),
                                  new Color(255, 255, 255, 255), false, false);
                    }
                    if (font2 != null)
                    {
                        font2.Draw("This is some text",
                                   new Rect(130, 20, 300, 60),
                                   new Color(255, (int)time % 255, (int)time % 255, 255),
                                   false, false);
                    }

                    /*At last, we draw the Irrlicht Engine logo (without using
                     * a color or an alpha channel) and a transparent 2d Rectangle
                     * at the position of the mouse cursor.*/
                    // draw logo
                    driver.Draw2DImage(images, new Position2D(10, 10),
                                       new Rect(354, 87, 442, 118), new Color(255, 255, 255, 255), false);
                    // draw transparent rect under cursor
                    Position2D m = device.CursorControl.Position;
                    driver.Draw2DRectangle(new Color(100, 255, 255, 255),
                                           new Rect(m.X - 20, m.Y - 20, m.X + 20, m.Y + 20));
                    driver.EndScene();
                }
            }

            /*
             * In the end, delete the Irrlicht device.
             */
            // Instead of device->drop, we'll use:
            GC.Collect();
        }