static void initUI() { irr.GUI.Skin.SetFont(irr.GUI.GetFont("../../media/fontlucida.png")); irr.GUI.AddButton(new Recti(20, 10, 160, 40), null, ChooseFolderButtonId, "Choose folder..."); GUIStaticText t = irr.GUI.AddStaticText("???", new Recti(180, 10, irr.Device.VideoDriver.ScreenSize.Width - 20, 40), false, false, null, CurrentFolderStaticTextId); t.SetTextAlignment(GUIAlignment.UpperLeft, GUIAlignment.Center); t.OverrideColor = Color.OpaqueWhite; }
static void Main() { device = IrrlichtDevice.CreateDevice(DriverType.OpenGL, new Dimension2Di(1024, 768), 32, false, true); device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent); device.SetWindowCaption("Minesweeper - Irrlicht Engine"); VideoDriver driver = device.VideoDriver; SceneManager scene = device.SceneManager; GUIEnvironment gui = device.GUIEnvironment; device.FileSystem.AddFileArchive("../../media/minesweeper.media.files"); game = new Game(device); // set up camera camera = scene.AddCameraSceneNode(); // Maya(null, -500, 50, 200); setupCameraPositionAndTarget(); // set up background Mesh m = scene.GetMesh("back.obj"); scene.MeshManipulator.Scale(m, new Vector3Df(80)); scene.MeshManipulator.FlipSurfaces(m); scene.MeshManipulator.RecalculateNormals(m); scene.MeshManipulator.SetVertexColors(m, new Color(80, 80, 80)); Material t = new Material(); t.Type = MaterialType.Reflection2Layer; t.Lighting = false; t.SetTexture(0, device.VideoDriver.GetTexture("TEXTURE-ref.jpg")); t.SetTexture(1, device.VideoDriver.GetTexture("TEXTURE-ref.jpg")); MeshSceneNode n = scene.AddMeshSceneNode(m, null, 7777); n.SetMaterial(0, t); n.Position = game.CenterOfTheBoard; SceneNodeAnimator a = scene.CreateRotationAnimator(new Vector3Df(0.07f, 0.01f, 0.05f)); n.AddAnimator(a); a.Drop(); // set up light light = scene.AddLightSceneNode(null, game.CenterOfTheBoard, new Colorf(1, 1, 1), 200); m = scene.AddVolumeLightMesh("lvol1", 32, 32, new Color(5, 5, 5)); scene.MeshManipulator.Scale(m, new Vector3Df(15, 30, 15)); n = scene.AddMeshSceneNode(m, light); n.Position = new Vector3Df(0, -10, 0); n.Rotation = new Vector3Df(180, 0, 0); n.SetMaterialType(MaterialType.TransparentAddColor); m = scene.AddVolumeLightMesh("lvol2", 32, 32, new Color(5, 5, 5)); scene.MeshManipulator.Scale(m, new Vector3Df(15, 30, 15)); n = scene.AddMeshSceneNode(m, light); n.Position = new Vector3Df(0, -10, 0); n.SetMaterialType(MaterialType.TransparentAddColor); // add gui hint text = gui.AddStaticText(TextStart, new Recti(10, 10, 200, 40)); text.OverrideColor = Color.SolidYellow; // main loop while (device.Run()) { driver.BeginScene(); scene.DrawAll(); gui.DrawAll(); if (optionFPS) { gui.BuiltInFont.Draw(driver.FPS + " FPS", driver.ScreenSize.Width - 50, 10, Color.SolidWhite); } driver.EndScene(); } device.Drop(); }
static void Main() { DriverType?driverType = AskForDriver(); if (!driverType.HasValue) { return; } IrrlichtDevice device = IrrlichtDevice.CreateDevice(driverType.Value, new Dimension2Di(640, 480)); if (device == null) { return; } VideoDriver driver = device.VideoDriver; SceneManager smgr = device.SceneManager; GUIEnvironment env = device.GUIEnvironment; // load and display animated fairy mesh AnimatedMeshSceneNode fairy = smgr.AddAnimatedMeshSceneNode(smgr.GetMesh("../../media/faerie.md2")); if (fairy != null) { fairy.SetMaterialTexture(0, driver.GetTexture("../../media/faerie2.bmp")); // set diffuse texture fairy.SetMaterialFlag(MaterialFlag.Lighting, true); // enable dynamic lighting fairy.GetMaterial(0).Shininess = 20.0f; // set size of specular highlights fairy.Position = new Vector3Df(-10, 0, -100); fairy.SetMD2Animation(AnimationTypeMD2.Stand); } // add white light smgr.AddLightSceneNode(null, new Vector3Df(-15, 5, -105), new Colorf(1, 1, 1)); // set ambient light smgr.AmbientLight = new Colorf(0.25f, 0.25f, 0.25f); // add fps camera CameraSceneNode fpsCamera = smgr.AddCameraSceneNodeFPS(); fpsCamera.Position = new Vector3Df(-50, 50, -150); // disable mouse cursor device.CursorControl.Visible = false; // create test cube SceneNode test = smgr.AddCubeSceneNode(60); // let the cube rotate and set some light settings SceneNodeAnimator anim = smgr.CreateRotationAnimator(new Vector3Df(0.3f, 0.3f, 0)); test.Position = new Vector3Df(-100, 0, -100); test.SetMaterialFlag(MaterialFlag.Lighting, false); // disable dynamic lighting test.AddAnimator(anim); anim.Drop(); // create render target Texture rt = null; CameraSceneNode fixedCam = null; if (driver.QueryFeature(VideoDriverFeature.RenderToTarget)) { rt = driver.AddRenderTargetTexture(new Dimension2Di(256), "RTT1"); test.SetMaterialTexture(0, rt); // set material of cube to render target // add fixed camera fixedCam = smgr.AddCameraSceneNode(null, new Vector3Df(10, 10, -80), new Vector3Df(-10, 10, -100)); } else { // create problem text GUIFont font = env.GetFont("../../media/fonthaettenschweiler.bmp"); if (font != null) { env.Skin.SetFont(font); } GUIStaticText text = env.AddStaticText( "Your hardware or this renderer is not able to use the " + "render to texture feature. RTT Disabled.", new Recti(150, 20, 470, 60)); text.OverrideColor = new Color(255, 255, 255, 100); } int lastFPS = -1; while (device.Run()) { if (device.WindowActive) { driver.BeginScene(ClearBufferFlag.All, new Color(0)); if (rt != null) { // draw scene into render target // set render target texture driver.SetRenderTarget(rt, ClearBufferFlag.All, new Color(0, 0, 255)); // make cube invisible and set fixed camera as active camera test.Visible = false; smgr.ActiveCamera = fixedCam; // draw whole scene into render buffer smgr.DrawAll(); // set back old render target // The buffer might have been distorted, so clear it driver.SetRenderTarget(null, ClearBufferFlag.All, new Color(0)); // make the cube visible and set the user controlled camera as active one test.Visible = true; smgr.ActiveCamera = fpsCamera; } // draw scene normally smgr.DrawAll(); env.DrawAll(); driver.EndScene(); // display frames per second in window title int fps = driver.FPS; if (lastFPS != fps) { device.SetWindowCaption(String.Format( "Render to Texture and Specular Highlights example - Irrlicht Engine [{0}] fps: {1}", driver.Name, fps)); lastFPS = fps; } } } device.Drop(); }
static void Main(string[] args) { DriverType?driverType = AskForDriver(); if (!driverType.HasValue) { return; } device = IrrlichtDevice.CreateDevice(driverType.Value, new Dimension2Di(800, 600), 16); if (device == null) { return; } device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent); device.SetWindowResizable(true); device.SetWindowCaption("Irrlicht Engine - Loading..."); VideoDriver driver = device.VideoDriver; GUIEnvironment env = device.GUIEnvironment; SceneManager smgr = device.SceneManager; smgr.Attributes.SetValue(SceneParameters.COLLADA_CreateSceneInstances, true); driver.SetTextureCreationFlag(TextureCreationFlag.Always32Bit, true); smgr.AddLightSceneNode(null, new Vector3Df(200), new Colorf(1.0f, 1.0f, 1.0f), 2000); smgr.AmbientLight = new Colorf(0.3f, 0.3f, 0.3f); // add our media directory as "search path" device.FileSystem.AddFileArchive("../../media/"); // read configuration from xml file // (we use .NET way to do this, since Lime doesn't support native Irrlicht' xml reader) XmlDocument xml = new XmlDocument(); xml.Load("../../media/config.xml"); startUpModelFile = xml.DocumentElement["startUpModel"].Attributes["file"].Value; caption = xml.DocumentElement["messageText"].Attributes["caption"].Value; messageText = xml.DocumentElement["messageText"].InnerText; if (args.Length > 0) { startUpModelFile = args[0]; } // set a nicer font GUIFont font = env.GetFont("fonthaettenschweiler.bmp"); if (font != null) { env.Skin.SetFont(font); } // load the irrlicht engine logo GUIImage img = env.AddImage( driver.GetTexture("irrlichtlogoalpha2.tga"), new Vector2Di(10, driver.ScreenSize.Height - 128)); img.ID = (int)guiID.Logo; // lock the logo's edges to the bottom left corner of the screen img.SetAlignment(GUIAlignment.UpperLeft, GUIAlignment.UpperLeft, GUIAlignment.LowerRight, GUIAlignment.LowerRight); // create menu GUIContextMenu menu = env.AddMenu(); menu.AddItem("File", -1, true, true); menu.AddItem("View", -1, true, true); menu.AddItem("Camera", -1, true, true); menu.AddItem("Help", -1, true, true); GUIContextMenu submenu; submenu = menu.GetSubMenu(0); submenu.AddItem("Open Model File & Texture...", (int)guiID.OpenModel); submenu.AddItem("Set Model Archive...", (int)guiID.SetModelArchive); submenu.AddItem("Load as Octree", (int)guiID.LoadAsOctree); submenu.AddSeparator(); submenu.AddItem("Quit", (int)guiID.Quit); submenu = menu.GetSubMenu(1); submenu.AddItem("sky box visible", (int)guiID.SkyBoxVisible, true, false, true); submenu.AddItem("toggle model debug information", (int)guiID.ToggleDebugInfo, true, true); submenu.AddItem("model material", -1, true, true); submenu = submenu.GetSubMenu(1); submenu.AddItem("Off", (int)guiID.DebugOff); submenu.AddItem("Bounding Box", (int)guiID.DebugBoundingBox); submenu.AddItem("Normals", (int)guiID.DebugNormals); submenu.AddItem("Skeleton", (int)guiID.DebugSkeleton); submenu.AddItem("Wire overlay", (int)guiID.DebugWireOverlay); submenu.AddItem("Half-Transparent", (int)guiID.DebugHalfTransparent); submenu.AddItem("Buffers bounding boxes", (int)guiID.DebugBuffersBoundingBoxes); submenu.AddItem("All", (int)guiID.DebugAll); submenu = menu.GetSubMenu(1).GetSubMenu(2); submenu.AddItem("Solid", (int)guiID.ModelMaterialSolid); submenu.AddItem("Transparent", (int)guiID.ModelMaterialTransparent); submenu.AddItem("Reflection", (int)guiID.ModelMaterialReflection); submenu = menu.GetSubMenu(2); submenu.AddItem("Maya Style", (int)guiID.CameraMaya); submenu.AddItem("First Person", (int)guiID.CameraFirstPerson); submenu = menu.GetSubMenu(3); submenu.AddItem("About", (int)guiID.About); // create toolbar GUIToolBar bar = env.AddToolBar(); Texture image = driver.GetTexture("open.png"); bar.AddButton((int)guiID.ButtonOpenModel, null, "Open a model", image, null, false, true); image = driver.GetTexture("tools.png"); bar.AddButton((int)guiID.ButtonShowToolbox, null, "Open Toolset", image, null, false, true); image = driver.GetTexture("zip.png"); bar.AddButton((int)guiID.ButtonSelectArchive, null, "Set Model Archive", image, null, false, true); image = driver.GetTexture("help.png"); bar.AddButton((int)guiID.ButtonShowAbout, null, "Open Help", image, null, false, true); // create a combobox with some senseless texts GUIComboBox box = env.AddComboBox(new Recti(250, 4, 350, 23), bar, (int)guiID.TextureFilter); box.AddItem("No filtering"); box.AddItem("Bilinear"); box.AddItem("Trilinear"); box.AddItem("Anisotropic"); box.AddItem("Isotropic"); // disable alpha setSkinTransparency(255, env.Skin); // add a tabcontrol createToolBox(); // create fps text GUIStaticText fpstext = env.AddStaticText("", new Recti(400, 4, 570, 23), true, false, bar); GUIStaticText postext = env.AddStaticText("", new Recti(10, 50, 470, 80), false, false, null, (int)guiID.PositionText); postext.Visible = false; // show about message box and load default model if (args.Length == 0) { showAboutText(); } loadModel(startUpModelFile); // add skybox skybox = smgr.AddSkyBoxSceneNode( "irrlicht2_up.jpg", "irrlicht2_dn.jpg", "irrlicht2_lf.jpg", "irrlicht2_rt.jpg", "irrlicht2_ft.jpg", "irrlicht2_bk.jpg"); // add a camera scene node camera[0] = smgr.AddCameraSceneNodeMaya(); camera[0].FarValue = 20000; // Maya cameras reposition themselves relative to their target, // so target the location where the mesh scene node is placed. camera[0].Target = new Vector3Df(0, 30, 0); camera[1] = smgr.AddCameraSceneNodeFPS(); camera[1].FarValue = 20000; camera[1].Position = new Vector3Df(0, 0, -70); camera[1].Target = new Vector3Df(0, 30, 0); setActiveCamera(camera[0]); // set window caption caption = string.Format("{0} - [{1}]", caption, driver.Name); device.SetWindowCaption(caption); // remember state so we notice when the window does lose the focus bool hasFocus = device.WindowFocused; // draw everything while (device.Run() && driver != null) { // Catch focus changes (workaround until Irrlicht has events for this) bool focused = device.WindowFocused; if (hasFocus && !focused) { onKillFocus(); } hasFocus = focused; if (device.WindowActive) { driver.BeginScene(ClearBufferFlag.All, new Color(50, 50, 50)); smgr.DrawAll(); env.DrawAll(); driver.EndScene(); string str = string.Format("FPS: {0} Tris: {1}", driver.FPS, driver.PrimitiveCountDrawn); fpstext.Text = str; CameraSceneNode cam = device.SceneManager.ActiveCamera; str = string.Format("Pos: {0} Tgt: {1}", cam.Position, cam.Target); postext.Text = str; } else { device.Yield(); } } device.Drop(); }
static void Main(string[] args) { device = IrrlichtDevice.CreateDevice(DriverType.OpenGL, new Dimension2Di(1024, 768), 32, false, true); device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent); device.SetWindowCaption("Minesweeper - Irrlicht Engine"); VideoDriver driver = device.VideoDriver; SceneManager scene = device.SceneManager; GUIEnvironment gui = device.GUIEnvironment; device.FileSystem.AddFileArchive("../../media/minesweeper.media.files"); game = new Game(device); // set up camera camera = scene.AddCameraSceneNode(); // Maya(null, -500, 50, 200); setupCameraPositionAndTarget(); // set up background Mesh m = scene.GetMesh("back.obj"); scene.MeshManipulator.Scale(m, new Vector3Df(80)); scene.MeshManipulator.FlipSurfaces(m); scene.MeshManipulator.RecalculateNormals(m); scene.MeshManipulator.SetVertexColors(m, new Color(80, 80, 80)); Material t = new Material(); t.Type = MaterialType.Reflection2Layer; t.Lighting = false; t.SetTexture(0, device.VideoDriver.GetTexture("TEXTURE-ref.jpg")); t.SetTexture(1, device.VideoDriver.GetTexture("TEXTURE-ref.jpg")); MeshSceneNode n = scene.AddMeshSceneNode(m, null, 7777); n.SetMaterial(0, t); n.Position = game.CenterOfTheBoard; SceneNodeAnimator a = scene.CreateRotationAnimator(new Vector3Df(0.07f, 0.01f, 0.05f)); n.AddAnimator(a); a.Drop(); // set up light light = scene.AddLightSceneNode(null, game.CenterOfTheBoard, new Colorf(1, 1, 1), 200); m = scene.AddVolumeLightMesh("lvol1", 32, 32, new Color(5, 5, 5)); scene.MeshManipulator.Scale(m, new Vector3Df(15, 30, 15)); n = scene.AddMeshSceneNode(m, light); n.Position = new Vector3Df(0, -10, 0); n.Rotation = new Vector3Df(180, 0, 0); n.SetMaterialType(MaterialType.TransparentAddColor); m = scene.AddVolumeLightMesh("lvol2", 32, 32, new Color(5, 5, 5)); scene.MeshManipulator.Scale(m, new Vector3Df(15, 30, 15)); n = scene.AddMeshSceneNode(m, light); n.Position = new Vector3Df(0, -10, 0); n.SetMaterialType(MaterialType.TransparentAddColor); // add gui hint text = gui.AddStaticText(TextStart, new Recti(10, 10, 200, 40)); text.OverrideColor = Color.OpaqueYellow; // main loop while (device.Run()) { driver.BeginScene(); scene.DrawAll(); gui.DrawAll(); if (optionFPS) gui.BuiltInFont.Draw(driver.FPS + " FPS", driver.ScreenSize.Width - 50, 10, Color.OpaqueWhite); driver.EndScene(); } device.Drop(); }
/// <summary> /// The irrlicht thread for rendering. /// </summary> private void StartIrr() { #if DEBUG try #endif { float DEGREES_TO_RADIANS = (float)(Math.PI / 180.0); //Setup IrrlichtCreationParameters irrparam = new IrrlichtCreationParameters(); if (irrlichtPanel.IsDisposed) { throw new Exception("Form closed!"); } if (irrlichtPanel.InvokeRequired) { irrlichtPanel.Invoke(new MethodInvoker(delegate { irrparam.WindowID = irrlichtPanel.Handle; })); } irrparam.DriverType = DriverType.Direct3D9; irrparam.BitsPerPixel = 32; irrparam.AntiAliasing = 1; device = IrrlichtDevice.CreateDevice(irrparam); if (device == null) { throw new NullReferenceException("Could not create device for engine!"); } driver = device.VideoDriver; smgr = SceneManagerWolvenKit.Create(device); gui = device.GUIEnvironment; smgr.Attributes.SetValue("TW_TW3_TEX_PATH", depot); driver.SetTextureCreationFlag(TextureCreationFlag.Always32Bit, true); lightNode = smgr.AddLightSceneNode(null, new Vector3Df(0, 0, 0), new Colorf(1.0f, 1.0f, 1.0f), 200000.0f); smgr.AmbientLight = new Colorf(1.0f, 1.0f, 1.0f); worldNode = smgr.AddEmptySceneNode(); //NOTE: Witcher assets use Z up but Irrlicht uses Y up so rotate the model worldNode.Rotation = new Vector3Df(-90, 0, 0); //NOTE: We also need to flip the x-coordinate with this rotation worldNode.Scale = new Vector3Df(-1.0f, 1.0f, 1.0f); worldNode.Visible = true; var dome = smgr.AddSkyDomeSceneNode(driver.GetTexture("Terrain\\skydome.jpg"), 16, 8, 0.95f, 2.0f); dome.Visible = true; fpsText = gui.AddStaticText("FPS: 0", new Recti(2, 10, 200, 30), false, false, null, 1, false); fpsText.OverrideColor = IrrlichtLime.Video.Color.SolidRed; fpsText.OverrideFont = gui.GetFont("#DefaultWKFont"); vertexCountText = gui.AddStaticText("Vertices: " + totalVertexCount.ToString(), new Recti(2, 32, 300, 52), false, false, null, 1, false); vertexCountText.OverrideColor = IrrlichtLime.Video.Color.SolidRed; vertexCountText.OverrideFont = gui.GetFont("#DefaultWKFont"); meshCountText = gui.AddStaticText("Meshes: " + totalMeshCount.ToString(), new Recti(2, 54, 300, 74), false, false, null, 1, false); meshCountText.OverrideColor = IrrlichtLime.Video.Color.SolidRed; meshCountText.OverrideFont = gui.GetFont("#DefaultWKFont"); var camera = smgr.AddCameraSceneNodeWolvenKit(); camera.FarValue = 10000.0f; //distanceBar.Invoke((MethodInvoker)delegate //{ // camera.FarValue = (float)Math.Pow(10.0, (double)distanceBar.Value); //}); viewPort = driver.ViewPort; var lineMat = new IrrlichtLime.Video.Material { Lighting = false }; var WMatrix = new Matrix(new Vector3Df(0, 0, 0), smgr.ActiveCamera.ModelRotation); var PMatrix = new Matrix(); PMatrix = PMatrix.BuildProjectionMatrixOrthoLH(100, 80, 0.001f, 10000.0f); var VMatrix = new Matrix(); VMatrix = VMatrix.BuildCameraLookAtMatrixLH(new Vector3Df(50, 0, 0), new Vector3Df(0, 0, 0), new Vector3Df(0, 1f, 0)); int gizmoX = (int)(irrlichtPanel.Width * 0.92f); int gizmoY = (int)(irrlichtPanel.Height * 0.92f); var gizmoViewPort = new Recti(gizmoX, gizmoY, irrlichtPanel.Width, irrlichtPanel.Height); while (device.Run()) { if (this.Visible) { ProcessCommand(); driver.BeginScene(ClearBufferFlag.All); int val = driver.FPS; fpsText.Text = "FPS: " + val.ToString(); smgr.DrawAll(); gui.DrawAll(); // draw xyz axis right bottom driver.ViewPort = gizmoViewPort; driver.SetMaterial(lineMat); WMatrix.SetRotationRadians(smgr.ActiveCamera.ModelRotation * DEGREES_TO_RADIANS); driver.SetTransform(TransformationState.World, WMatrix); driver.SetTransform(TransformationState.Projection, PMatrix); driver.SetTransform(TransformationState.View, VMatrix); driver.Draw3DLine(0, 0, 0, 30f, 0, 0, IrrlichtLime.Video.Color.SolidGreen); driver.Draw3DLine(0, 0, 0, 0, 30f, 0, IrrlichtLime.Video.Color.SolidBlue); driver.Draw3DLine(0, 0, 0, 0, 0, 30f, IrrlichtLime.Video.Color.SolidRed); driver.ViewPort = viewPort; driver.EndScene(); } else { device.Yield(); } } } #if DEBUG catch (ThreadAbortException) { } catch (NullReferenceException) { } catch (Exception ex) { if (!this.IsDisposed) { MessageBox.Show(ex.Message); //this.Invoke(new MethodInvoker(delegate { this.Close(); })); } } #endif }
static void Main(string[] args) { DriverType driverType; if (!AskUserForDriver(out driverType)) { return; } device = IrrlichtDevice.CreateDevice(driverType, new Dimension2Di(640, 480)); if (device == null) { return; } device.SetWindowCaption("Irrlicht Engine - User Interface Demo"); device.SetWindowResizable(true); VideoDriver driver = device.VideoDriver; GUIEnvironment env = device.GUIEnvironment; GUISkin skin = env.Skin; GUIFont font = env.GetFont("../../media/fonthaettenschweiler.bmp"); if (font != null) { skin.SetFont(font); } skin.SetFont(env.BuiltInFont, GUIDefaultFont.Tooltip); env.AddButton(new Recti(10, 240, 110, 240 + 32), null, GUI_ID_ButtonQuit, "Quit", "Exits Program"); env.AddButton(new Recti(10, 280, 110, 280 + 32), null, GUI_ID_ButtonWindowNew, "New Window", "Launches a new Window"); env.AddButton(new Recti(10, 320, 110, 320 + 32), null, GUI_ID_ButtonFileOpen, "File Open", "Opens a file"); env.AddStaticText("Transparent Control:", new Recti(150, 20, 350, 40), true); GUIScrollBar scrollbar = env.AddScrollBar(true, new Recti(150, 45, 350, 60), null, GUI_ID_ScrollbarTransparency); scrollbar.MaxValue = 255; scrollbar.Position = (int)env.Skin.GetColor(GUIDefaultColor.WindowBackground).Alpha; GUIStaticText trq = env.AddStaticText("Logging ListBox:", new Recti(50, 110, 250, 130), true); listbox = env.AddListBox(new Recti(50, 140, 250, 210)); env.AddEditBox("Editable Text", new Recti(350, 80, 550, 100)); device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent); env.AddImage(driver.GetTexture("../../media/irrlichtlogoalpha2.tga"), new Vector2Di(10, 10)); while (device.Run()) { if (device.WindowActive) { driver.BeginScene(true, true, new Color(200, 200, 200)); env.DrawAll(); driver.EndScene(); } } device.Drop(); }
/// <summary> /// The irrlicht thread for rendering. /// </summary> private void StartIrr() { try { //Setup IrrlichtCreationParameters irrparam = new IrrlichtCreationParameters(); if (irrlichtPanel.IsDisposed) { throw new Exception("Form closed!"); } if (irrlichtPanel.InvokeRequired) { irrlichtPanel.Invoke(new MethodInvoker(delegate { irrparam.WindowID = irrlichtPanel.Handle; })); } irrparam.DriverType = DriverType.Direct3D9; irrparam.DriverType = DriverType.OpenGL; irrparam.BitsPerPixel = 16; device = IrrlichtDevice.CreateDevice(irrparam); if (device == null) { throw new NullReferenceException("Could not create device for engine!"); } driver = device.VideoDriver; smgr = device.SceneManager; gui = device.GUIEnvironment; var cam = smgr.AddCameraSceneNode(null); cam.TargetAndRotationBinding = true; cam.Position = new Vector3Df(-100.0f, 500.0f, 100.0f); cam.Target = new Vector3Df(0.0f); cam.FarValue = 42000.0f; device.CursorControl.Visible = false; //Terrain heightmap = driver.CreateImage("Terrain\\basemap.bmp"); terrain = smgr.AddTerrainSceneNode( "Terrain\\basemap.bmp", null, -1, new Vector3Df(0.0f) ); terrain.Scale = new Vector3Df(32, 5, 32); terrain.SetMaterialFlag(MaterialFlag.Lighting, false); terrain.SetMaterialTexture(0, driver.GetTexture("Terrain\\rockwall.jpg")); selector = smgr.CreateTerrainTriangleSelector(terrain, 0); var arrow = smgr.AddAnimatedMeshSceneNode(smgr.AddArrowMesh("arrow", new IrrlichtLime.Video.Color(255, 255, 0, 0), new IrrlichtLime.Video.Color(255, 0, 255, 0)), null); arrow.SetMaterialFlag(MaterialFlag.Lighting, false); arrow.Scale = new Vector3Df(100.0f); arrow.Rotation = new Vector3Df(0.0f, 0.0f, 180.0f); //Skybox and skydome driver.SetTextureCreationFlag(TextureCreationFlag.CreateMipMaps, false); /*var box = smgr.AddSkyBoxSceneNode( * ("Terrain\\irrlicht2_up.jpg"), * ("Terrain\\irrlicht2_dn.jpg"), * ("Terrain\\irrlicht2_lf.jpg"), * ("Terrain\\irrlicht2_rt.jpg"), * ("Terrain\\irrlicht2_ft.jpg"), * ("Terrain\\irrlicht2_bk.jpg")); * box.Visible = true;*/ var dome = smgr.AddSkyDomeSceneNode(driver.GetTexture("Terrain\\skydome.jpg"), 16, 8, 0.95f, 2.0f); dome.Visible = true; driver.SetTextureCreationFlag(TextureCreationFlag.CreateMipMaps, true); var helpq = gui.AddStaticText("Press Q to disable focus!", new Recti(0, this.ClientSize.Height - 40, 100, this.ClientSize.Height), true, true, null, 1, true); var helpesc = gui.AddStaticText("Press ESC to quit", new Recti(0, this.ClientSize.Height - 20, 100, this.ClientSize.Height), true, true, null, 1, true); middletext = gui.AddStaticText("Click to enable mouselook and move with WASD", new Recti(ClientSize.Width / 2 - 100, this.ClientSize.Height / 2, ClientSize.Width / 2 + 100, this.ClientSize.Height / 2 + 30), true, true, null, 1, true); middletext.OverrideColor = IrrlichtLime.Video.Color.SolidWhite; middletext.BackgroundColor = IrrlichtLime.Video.Color.SolidBlack; var irrTimer = device.Timer; var then = 0; var then30 = 0; device.CursorControl.Visible = false; Vector2Df lastcurr = new Vector2Df(0f); uint dt = 0; while (device.Run()) { driver.BeginScene(ClearBufferFlag.All); if (catchmouse) { // move the arrow to the nearest vertex ... IrrlichtLime.Core.Vector2Di center = new IrrlichtLime.Core.Vector2Di(irrlichtPanel.Width / 2, irrlichtPanel.Height / 2); Line3Df ray = smgr.SceneCollisionManager.GetRayFromScreenCoordinates(center, cam); Vector3Df pos; Triangle3Df Tri; var curr = device.CursorControl.RelativePosition; // Threshold and periodical check so we don't spin around due to float conversions if (device.Timer.Time > dt && curr.GetDistanceFrom(lastcurr) > 0.01f) { Line3Df cursor_ray = smgr.SceneCollisionManager .GetRayFromScreenCoordinates(new Vector2Di((int)(curr.X * irrlichtPanel.Width), (int)(curr.Y * irrlichtPanel.Height)), cam); smgr.ActiveCamera.Target = cursor_ray.Middle; dt = device.Timer.Time + 30; lastcurr = curr; device.CursorControl.Position = center; } if (smgr.SceneCollisionManager.GetCollisionPoint(ray, selector, out pos, out Tri, out outnode)) { var scale = 32; // terrain is scaled 32X var size = 512; // heightmap is 512x512 pixels var x = (pos.X / scale); var z = (pos.Z / scale); var index = x * size + z; x *= scale; z *= scale; arrow.Position = new Vector3Df(x, terrain.GetHeight(x, z) + 100, z); } } smgr.DrawAll(); gui.DrawAll(); driver.EndScene(); //device.CursorControl.Position = new Vector2Di(irrlichtPanel. } device.Drop(); } catch (ThreadAbortException) { } catch (NullReferenceException) { } catch (Exception ex) { if (!this.IsDisposed) { MessageBox.Show(ex.Message); //this.Invoke(new MethodInvoker(delegate { this.Close(); })); } } }
private void frmTerrain_KeyDown(object sender, KeyEventArgs e) { var cam = smgr.ActiveCamera; float cameraspeed = 100f; switch (e.KeyCode) { case Keys.Escape: { device.Close(); irrThread.Abort(); this.Close(); break; } case Keys.W: { var vec = (cam.Target - cam.Position).Normalize() * cameraspeed; cam.Position += vec; cam.Target += vec; break; } case Keys.S: { var vec = (cam.Target - cam.Position).Normalize() * cameraspeed; cam.Position -= vec; cam.Target -= vec; break; } case Keys.A: { var vec = (cam.Target - cam.Position).Normalize().DotProduct(cam.Target.Normalize()) * cameraspeed; cam.Position -= vec; cam.Target -= vec; break; } case Keys.D: { var vec = (cam.Target - cam.Position).Normalize().DotProduct(cam.Target.Normalize()) * cameraspeed; cam.Position += vec; cam.Target += vec; break; } case Keys.P: { Console.WriteLine("--------- DEBUG ---------"); Console.WriteLine($"[DEBUG] Camera position - (X - {cam.Position.X}, Y - {cam.Position.Y}, Z - {cam.Position.Z})"); Console.WriteLine("--------- DEBUG ---------"); break; } case Keys.Q: { Console.WriteLine("[INFO] Mouse released!"); device.CursorControl.Visible = true; Cursor.Show(); catchmouse = false; middletext = gui.AddStaticText("Click to enable mouselook and move with WASD", new Recti(ClientSize.Width / 2 - 100, this.ClientSize.Height / 2, ClientSize.Width / 2 + 100, this.ClientSize.Height / 2 + 30), true, true, null, 1, true); middletext.OverrideColor = IrrlichtLime.Video.Color.SolidWhite; middletext.BackgroundColor = IrrlichtLime.Video.Color.SolidBlack; break; } } }