private static float _playerVerticalSpeed = 0.0f; // used to calculate vertical speed for gravity and jump
        static void Main(string[] args)
        {
            IrrlichtDevice device = IrrlichtDevice.CreateDevice(DriverType.OpenGL, new Dimension2Di(ResolutionX, ResolutionY), 24, false, false, false);

            device.OnEvent += Device_OnEvent;
            device.SetWindowCaption("Hello World! - Irrlicht Engine Demo");

            VideoDriver  driver = device.VideoDriver;
            SceneManager smgr   = device.SceneManager;

            GUIEnvironment gui = device.GUIEnvironment;

            gui.AddStaticText("Hello World! This is the Irrlicht Software renderer!",
                              new Recti(10, 10, 260, 22), true);

            AnimatedMesh          mesh       = smgr.GetMesh("../../media/sydney.md2");
            AnimatedMeshSceneNode sydneyNode = smgr.AddAnimatedMeshSceneNode(mesh);

            var cubeSceneNode = smgr.AddCubeSceneNode(2.0f);

            cubeSceneNode.Scale    = new Vector3Df(6.0f, 2.0f, 100.0f);
            cubeSceneNode.Position = new Vector3Df(cubeSceneNode.Position.X, GroundAltitude + 0.5f, cubeSceneNode.Position.Z);

            if (sydneyNode != null)
            {
                sydneyNode.SetMaterialFlag(MaterialFlag.Lighting, true);
                sydneyNode.SetMD2Animation(AnimationTypeMD2.Stand);
                sydneyNode.SetMaterialTexture(0, driver.GetTexture("../../media/sydney.bmp"));
            }

            var lightSceneNode = smgr.AddLightSceneNode();
            var light          = lightSceneNode.LightData;

            light.DiffuseColor = new Colorf(0.8f, 1.0f, 1.0f);
            light.Type         = LightType.Directional;
            light.Position     = new Vector3Df(0.0f, 5.0f, 5.0f);


            var cam = smgr.AddCameraSceneNode(null, new Vector3Df(20, 30, -40), new Vector3Df(20, 5, 0));

            device.Timer.Start();

            uint then = device.Timer.RealTime;

            while (device.Run())
            {
                // As the game is simple we will handle our simple physics ourselves
                uint  now            = device.Timer.RealTime;
                uint  elapsed        = now - then;
                float elapsedTimeSec = (float)elapsed / 1000.0f;
                then = now;

                // we target 58.8 FPS
                if (elapsed < 17)
                {
                    device.Sleep(17 - (int)elapsed);
                }
                else
                {
                    // uh-oh, it took more than .125 sec to do render loop!
                    // what do we do now?
                }

                _playerVerticalSpeed += elapsedTimeSec * -(VerticalGravity * 10.0f);

                var calculatedNewPos = sydneyNode.Position - new Vector3Df(0.0f, _playerVerticalSpeed * elapsedTimeSec, 0.0f);

                float offsetSydney = sydneyNode.BoundingBox.Extent.Y / 2.0f;
                _isGrounded = calculatedNewPos.Y <= (GroundAltitude + offsetSydney);
                if (_isGrounded)
                {
                    _playerVerticalSpeed = 0.0f;
                    calculatedNewPos.Y   = GroundAltitude + offsetSydney;
                }
                else
                {
                    calculatedNewPos.Y = calculatedNewPos.Y;
                }

                sydneyNode.Position = calculatedNewPos;

                driver.BeginScene(true, true, new Color(100, 101, 140));
                smgr.DrawAll();
                gui.DrawAll();

                driver.EndScene();
            }

            device.Drop();
        }
Example #2
0
        static void Main()
        {
            DriverType?driverType = AskForDriver();

            if (!driverType.HasValue)
            {
                return;
            }

            IrrlichtDevice device = IrrlichtDevice.CreateDevice(driverType.Value, new Dimension2Di(800, 600));

            if (device == null)
            {
                return;
            }

            device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent);
            device.SetWindowCaption("Mesh handling - Irrlicht Engine - press [1], [2], [3] to regenerate mesh");
            VideoDriver  driver = device.VideoDriver;
            SceneManager scene  = device.SceneManager;

            // Generate starting height map and mesh

            HeightMap map = new HeightMap(255, 255);

            map.Generate(HeightMap.HeightFunc.EggBox);

            HeightMesh mesh = new HeightMesh();

            mesh.Init(driver, map, 50.0f, HeightMesh.ColorFunc.GreyscaleBasedOnTheHeight);

            // Add the mesh to the scene graph

            MeshSceneNode meshnode = scene.AddMeshSceneNode(mesh.Mesh);

            meshnode.SetMaterialFlag(MaterialFlag.BackFaceCulling, false);

            // Add light (just for nice effects)

            LightSceneNode    lightnode = scene.AddLightSceneNode(null, new Vector3Df(0, 100, 0), new Colorf(1, 1, 1), 500.0f);
            SceneNodeAnimator anim      = scene.CreateFlyCircleAnimator(new Vector3Df(0, 150, 0), 250.0f);

            lightnode.AddAnimator(anim);
            anim.Drop();

            // Add camera

            CameraSceneNode camera = scene.AddCameraSceneNodeFPS();

            camera.Position = new Vector3Df(-20.0f, 100.0f, -20.0f);
            camera.Target   = new Vector3Df(200.0f, -100.0f, 200.0f);
            camera.FarValue = 20000.0f;

            // Main loop

            while (device.Run())
            {
                if (!device.WindowActive)
                {
                    device.Sleep(100);
                    continue;
                }

                if (IsKeyDown(KeyCode.KeyW))
                {
                    meshnode.SetMaterialFlag(MaterialFlag.Wireframe, !meshnode.GetMaterial(0).Wireframe);
                }
                else if (IsKeyDown(KeyCode.Key1))
                {
                    map.Generate(HeightMap.HeightFunc.EggBox);
                    mesh.Init(driver, map, 50.0f, HeightMesh.ColorFunc.GreyscaleBasedOnTheHeight);
                }
                else if (IsKeyDown(KeyCode.Key2))
                {
                    map.Generate(HeightMap.HeightFunc.MoreSine);
                    mesh.Init(driver, map, 50.0f, HeightMesh.ColorFunc.CoordinateInterpolation);
                }
                else if (IsKeyDown(KeyCode.Key3))
                {
                    map.Generate(HeightMap.HeightFunc.JustExp);
                    mesh.Init(driver, map, 50.0f, HeightMesh.ColorFunc.CoordinateInterpolation);
                }

                driver.BeginScene();
                scene.DrawAll();
                driver.EndScene();
            }

            // Clean up

            mesh.Drop();
            device.Drop();
        }