public bool PeutVoir(SceneNode objet1, SceneNode objet2) { Vector2Df pos = new Vector2Df(objet1.Position.X + .5f, objet1.Position.Z + .5f); Vector2Df pos2 = new Vector2Df(objet2.Position.X + .5f, objet2.Position.Z + .5f); float dist = pos.GetDistanceFrom(pos2); Vector2Df v = (pos2 - pos).Normalize() * 0.1f; for (float f = 0; f < dist; f += 0.1f) { Vector2Di posI = new Vector2Di((int)pos.X, (int)pos.Y); if ((posI.X < 0) || (posI.Y < 0) || (posI.X >= 32) || (posI.Y >= 32)) { return(false); } if (Murs[posI.X, posI.Y]) { return(false); } pos += v; } return(true); }
public void MouseClick(int x, int y, bool isRight) { if (m_state != State.Playing) { return; } Vector2Di m = new Vector2Di(x, y); Line3Df l = m_device.SceneManager.SceneCollisionManager.GetRayFromScreenCoordinates(m); SceneNode n = m_device.SceneManager.SceneCollisionManager.GetSceneNodeFromRayBB(l, 0x10000, m_root); if (n != null && n.ID >= 0x10000) { int i = n.ID - 0x10000; if (isRight) { flagCell(m_board[i]); } else { revealCell(m_board[i]); } } }
static void Main() { device = IrrlichtDevice.CreateDevice(DriverType.Direct3D9, new Dimension2Di(1280, 768)); // minimum: 1024 (which is 16x64) x 768 (which is 16x48) if (device == null) { return; } device.SetWindowCaption("Pathfinding - Irrlicht Engine"); device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent); VideoDriver driver = device.VideoDriver; GUIFont font = device.GUIEnvironment.GetFont("../../media/fontlucida.png"); Color fontNormalColor = Color.SolidWhite; Color fontActionColor = Color.SolidYellow; Texture pathfindingTexture = driver.GetTexture("../../media/pathfinding.png"); int cellSize = pathfindingTexture.Size.Height; pathfinding = new Pathfinding(64, 48, cellSize, 0, 0); pathfinding.SetCell(4, 4, Pathfinding.CellType.Start); pathfinding.SetCell(pathfinding.Width - 5, pathfinding.Height - 5, Pathfinding.CellType.Finish); while (device.Run()) { driver.BeginScene(ClearBufferFlag.Color); pathfinding.FindPath(); pathfinding.Draw(driver, pathfindingTexture); // draw info panel Vector2Di v = new Vector2Di(pathfinding.Width * pathfinding.CellSize + 20, 20); font.Draw("FPS: " + driver.FPS, v, fontNormalColor); v.Y += 32; font.Draw("Map size: " + pathfinding.Width + " x " + pathfinding.Height, v, fontNormalColor); v.Y += 16; font.Draw("Shortest path: " + (pathfinding.PathLength == -1 ? "N/A" : pathfinding.PathLength.ToString()), v, fontNormalColor); v.Y += 16; font.Draw("Calculation time: " + pathfinding.PathCalcTimeMs + " ms", v, fontNormalColor); v.Y += 32; font.Draw(workMode ? "[LMB] Set cell impassable" : "[LMB] Set Start cell", v, fontActionColor); v.Y += 16; font.Draw(workMode ? "[RMB] Set cell passable" : "[RMB] Set Finish cell", v, fontActionColor); v.Y += 16; font.Draw("[Space] Change mode", v, fontActionColor); v.Y += 32; font.Draw("[F1] Clean up the map", v, fontActionColor); v.Y += 16; font.Draw("[F2] Add random blocks", v, fontActionColor); driver.EndScene(); } device.Drop(); }
private void irrThreadDrawText(Vector2Di p, string s) { Dimension2Di d = irrDevice.GUIEnvironment.BuiltInFont.GetDimension(s); d.Width += 8; d.Height += 6; irrDevice.VideoDriver.Draw2DRectangle(new Recti(p, d), new Color(0x7F000000)); irrDevice.GUIEnvironment.BuiltInFont.Draw(s, p + new Vector2Di(4, 3), new Color(250, 250, 250)); }
static void Main(string[] args) { device = IrrlichtDevice.CreateDevice(DriverType.Direct3D9, new Dimension2Di(1280, 768)); if (device == null) return; device.SetWindowCaption("Pathfinding - Irrlicht Engine"); device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent); VideoDriver driver = device.VideoDriver; GUIFont font = device.GUIEnvironment.GetFont("../../media/fontlucida.png"); Color fontNormalColor = Color.OpaqueWhite; Color fontActionColor = Color.OpaqueYellow; Texture pathfindingTexture = driver.GetTexture("../../media/pathfinding.png"); int cellSize = pathfindingTexture.Size.Height; pathfinding = new Pathfinding(64, 48, cellSize, 0, 0); pathfinding.SetCell(4, 4, Pathfinding.CellType.Start); pathfinding.SetCell(pathfinding.Width - 5, pathfinding.Height - 5, Pathfinding.CellType.Finish); while (device.Run()) { driver.BeginScene(true, false); pathfinding.FindPath(); pathfinding.Draw(driver, pathfindingTexture); // draw info panel Vector2Di v = new Vector2Di(pathfinding.Width * pathfinding.CellSize + 20, 20); font.Draw("FPS: " + driver.FPS, v, fontNormalColor); v.Y += 32; font.Draw("Map size: " + pathfinding.Width + " x " + pathfinding.Height, v, fontNormalColor); v.Y += 16; font.Draw("Shortest path: " + (pathfinding.PathLength == -1 ? "N/A" : pathfinding.PathLength.ToString()), v, fontNormalColor); v.Y += 16; font.Draw("Calculation time: " + pathfinding.PathCalcTimeMs + " ms", v, fontNormalColor); v.Y += 32; font.Draw(workMode ? "[LMB] Set cell impassable" : "[LMB] Set Start cell", v, fontActionColor); v.Y += 16; font.Draw(workMode ? "[RMB] Set cell passable" : "[RMB] Set Finish cell", v, fontActionColor); v.Y += 16; font.Draw("[Space] Change mode", v, fontActionColor); v.Y += 32; font.Draw("[F1] Clean up the map", v, fontActionColor); v.Y += 16; font.Draw("[F2] Add random blocks", v, fontActionColor); driver.EndScene(); } device.Drop(); }
static void drawPreviewPlateTooltip() { if (hoveredNode == null || !hoveredNode.Visible) { return; } int k = hoveredNode.ID; Texture t = hoveredNode.GetMaterial(0).GetTexture(0); if (t != null && t.Name.Path != "NoPreviewTexture") { k = hoveredNode.ID & (0xFFFFFFF ^ SelectableNodeIdFlag); } string s = previewPlateInfo.ContainsKey(k) ? previewPlateInfo[k] : "???"; if (s != null) { Vector2Di p = irr.Device.CursorControl.Position + new Vector2Di(16); GUIFont f = irr.GUI.Skin.GetFont(GUIDefaultFont.Default); Dimension2Di d = f.GetDimension(s); d.Inflate(16, 12); Recti r = new Recti(p, d); VideoDriver v = irr.Driver; int ax = r.LowerRightCorner.X - v.ScreenSize.Width; int ay = r.LowerRightCorner.Y - v.ScreenSize.Height; if (ax > 0 || ay > 0) { if (ax < 0) { ax = 0; } if (ay < 0) { ay = 0; } r.Offset(-ax, -ay); } v.Draw2DRectangle(r, new Color(0xbb223355)); v.Draw2DRectangleOutline(r, new Color(0xbb445577)); f.Draw(s, r.UpperLeftCorner + new Vector2Di(8, 6), Color.SolidYellow); } }
public float DrawAll(Vector2Di screenOffset = null) { Vector2Di zero = new Vector2Di(0); int n = 0; foreach (Tile tile in tiles) { if (tile.TextureIsReady) { driver.Draw2DImage(tile.Texture, tile.ScreenPos + (screenOffset ?? zero)); n++; } } return((float)n / tiles.Count); }
public Triangle3Df interpolateFrom2D(Vector2Di input) { //We can assume two things: //That the hand will be considered in front of the object //And that the hand will always be orbiting around the object //So we calculate based off of sin and cos and relative positions SceneCollisionManager collisionManager = device.SceneManager.SceneCollisionManager; Line3Df ray = device.SceneManager.SceneCollisionManager.GetRayFromScreenCoordinates(input); //calcLine.End = calcLine.End.Normalize(); //calcLine.End *= new Vector3Df(20); // Tracks the current intersection point with the level or a mesh Vector3Df intersection; // Used to show with triangle has been hit Triangle3Df hitTriangle; SceneNode selectedSceneNode = device.SceneManager.SceneCollisionManager.GetSceneNodeAndCollisionPointFromRay( ray, out intersection, // This will be the position of the collision out hitTriangle); // This ensures that only nodes that we have set up to be pickable are considered SceneNode highlightedSceneNode = null; // If the ray hit anything, move the billboard to the collision position // and draw the triangle that was hit. if (selectedSceneNode != null) { //bill.Position = new Vector3Df(intersection); // We need to reset the transform before doing our own rendering. device.VideoDriver.SetTransform(TransformationState.World, new Matrix()); //device.VideoDriver.SetMaterial(material); device.VideoDriver.Draw3DTriangle(hitTriangle, new Color(255, 255, 0, 0)); // We can check the flags for the scene node that was hit to see if it should be // highlighted. The animated nodes can be highlighted, but not the Quake level mesh highlightedSceneNode = selectedSceneNode; // Highlighting in this case means turning lighting OFF for this node, // which means that it will be drawn with full brightness. //highlightedSceneNode.SetMaterialFlag(MaterialFlag.Lighting, false); } return(hitTriangle); }
public void Draw(uint time, Vector3Df cameraPosition) { if (time > nextUpdateAt) { // animation transformation.Rotation = rotationVector * time; // recalculate current LOD currentLOD = meshLODs.Count - 1; float distanceSQ = (transformation.Translation - cameraPosition).LengthSQ; for (int i = 0; i < lodDistanceSQ.Length - 1; i++) { if (distanceSQ < lodDistanceSQ[i]) { currentLOD = i; break; } } // next line assigns new time for LOD to be recalculated in future, // we do not use same value for all LODs here, because we don't want all the LODItems // to be recalculated in the same time (same frame). So we assign a value // which higher when current LOD is higher - which also means that for now we are // a distant object and it is less possible that we will need to change LOD at all; // but close objects (with small LOD value, like 0, 1 or 2) we need to pick quite short time. // This is OK if it will be really short, because these objects are too close and indeed may // change their LOD value very soon, however, we also understand, that in general all the objects // takes very large area, so in general we will have something like less than 2% with LOD level 0, 1 or 2, // all other will get higher LOD, and about more than 50% will have maximum LOD value -- they take more time to recalc // their LOD than to draw them, so we need to calc their LOD less frequent. // p.s.: we also use the fact, that we do not give user ability to reach oposite side of our world in 1 frame, // the speed at which user moves is slow in general. nextUpdateAt = time + updateIntervals[currentLOD]; } // drawing // we do no set material here, because we draw all LODItems with the same material, we set material in main rendering loop driver.SetTransform(TransformationState.World, transformation); // this is also very time consuming operation; we can optimize it // to make something like 100 calls (instead of 5000 - the number of LODItems) - we need to group LODItems all this we increase FPS up on // 10%, BUT it that case we will not be able to move independent LODItems, becase they will not need (and will not have) own transformation // matrix (only LODGroup will has it). So grouping is really greate for some completly static objects like trees, shrubs, stones, etc. // we draw single 16-bit meshbuffer driver.DrawMeshBuffer(meshLODs[currentLOD].GetMeshBuffer(0)); if (LabelPositions != null && currentLOD <= 4) { Vector2Di p = device.SceneManager.SceneCollisionManager.GetScreenCoordinatesFrom3DPosition(transformation.Translation); // now we filter here results which will not be visible; we know that: // - GetScreenCoordinatesFrom3DPosition() returns {-10000,-10000} for behind camera 3d positions // - we do not need to draw out small text if its out of the screen // p.s.: without this filtering we will have about 200-300 labels to draw (instead of about 10-20 which are trully visible) if (p.X > -200 && p.X < screenSize.Width + 200 && p.Y > -100 && p.Y < screenSize.Height + 100) { int t = meshLODs[currentLOD].GetMeshBuffer(0).IndexCount / 3; int d = (int)(transformation.Translation - cameraPosition).Length; LabelPositions.Add(p); LabelTexts.Add( "LOD: " + currentLOD.ToString() + "\nTrinagles: " + t.ToString() + "\nDistance: " + d.ToString()); } } }
bool OnEvent(Event e) { if (e.Type == EventType.Mouse) { int x = e.Mouse.X; int y = e.Mouse.Y; bool l = e.Mouse.IsLeftPressed(); if (l && guiImage.AbsolutePosition.IsPointInside(new Vector2Di(x, y))) { Vector2Di p = new Vector2Di(x, y) - guiImage.AbsolutePosition.UpperLeftCorner; if (e.Mouse.Type == MouseEventType.Move) { TexturePainter t = texture.Painter; if (p.X < texture.Size.Width && p.Y < texture.Size.Height && t.Lock(TextureLockMode.WriteOnly)) { t.SetLine(oldMouseX, oldMouseY, p.X, p.Y, new Color(255, 0, 0)); t.Unlock(true); } } oldMouseX = p.X; oldMouseY = p.Y; return(true); } } if (e.Type == EventType.GUI) { if (e.GUI.Type == GUIEventType.ElementClosed && e.GUI.Caller is GUIWindow) { device.Close(); return(true); } if (e.GUI.Type == GUIEventType.ButtonClicked) { if (e.GUI.Caller == guiSize128) { initGUI(128); createTexture(128); return(true); } if (e.GUI.Caller == guiSize256) { initGUI(256); createTexture(256); return(true); } if (e.GUI.Caller == guiSize512) { initGUI(512); createTexture(512); return(true); } } } return(false); }
public Tile(int screenX, int screenY, Dimension2Di screenDimension, VideoDriver driver) { ScreenPos = new Vector2Di(screenX, screenY); Texture = driver.AddTexture(screenDimension, string.Format("TileTexture({0},{1})", screenX, screenY)); TexturePainter = Texture.Painter; }
static bool device_OnEvent(Event evnt) { if (evnt.Type == EventType.Mouse) { Dimension2Di s = device.VideoDriver.ScreenSize; if (evnt.Mouse.Type == MouseEventType.Wheel) { Rectd r = new Rectd(); if (evnt.Mouse.Wheel > 0) { // zoom in int x1 = evnt.Mouse.X - s.Width / 2 + (int)evnt.Mouse.Wheel * s.Width / 10; int y1 = evnt.Mouse.Y - s.Height / 2 + (int)evnt.Mouse.Wheel * s.Height / 10; r.UpperLeftCorner = fGen.GetWindowCoord(x1, y1); r.LowerRightCorner = fGen.GetWindowCoord(2 * evnt.Mouse.X - x1, 2 * evnt.Mouse.Y - y1); device.CursorControl.Position = new Vector2Di(s.Width / 2, s.Height / 2); } else { // zoom out int x1 = s.Width / 10; int y1 = s.Height / 10; r.UpperLeftCorner = fGen.GetWindowCoord(-x1, -y1); r.LowerRightCorner = fGen.GetWindowCoord(s.Width + x1, s.Height + y1); } fGen.Generate(r); return true; } if (evnt.Mouse.Type == MouseEventType.LeftDown) { mouseMoveStart = new Vector2Di(evnt.Mouse.X, evnt.Mouse.Y); return true; } if (evnt.Mouse.Type == MouseEventType.LeftUp) { Vector2Dd p1 = fGen.GetWindowCoord(evnt.Mouse.X, evnt.Mouse.Y); Vector2Dd p2 = fGen.GetWindowCoord(mouseMoveStart.X, mouseMoveStart.Y); Rectd r = fGen.GetWindow() + p2 - p1; fGen.Generate(r); mouseMoveStart = null; return true; } } if (evnt.Type == EventType.Key) { if (evnt.Key.PressedDown) { if (evnt.Key.Key == KeyCode.Esc) { device.Close(); return true; } if (evnt.Key.Key == KeyCode.F1) { showHelp = !showHelp; return true; } switch (evnt.Key.Char) { case '+': fGen.Generate(fGen.GetMaxIterations() + 1); return true; case '-': fGen.Generate(fGen.GetMaxIterations() - 1); return true; case '*': fGen.Generate(fGen.GetMaxIterations() + 10); return true; case '/': fGen.Generate(fGen.GetMaxIterations() - 10); return true; } } if (evnt.Key.Key == KeyCode.PrintScreen) // PrintScreen never comes with "evnt.Key.PressedDown == true" so we process it without checking that { string n = "Screenshot-" + DateTime.Now.Ticks + ".png"; Image i = device.VideoDriver.CreateScreenShot(); device.VideoDriver.WriteImage(i, n); i.Drop(); device.Logger.Log("Screenshot saved as " + n); return true; } } return false; }
public void MouseClick(int x, int y, bool isRight) { if (m_state != State.Playing) return; Vector2Di m = new Vector2Di(x, y); Line3Df l = m_device.SceneManager.SceneCollisionManager.GetRayFromScreenCoordinates(m); SceneNode n = m_device.SceneManager.SceneCollisionManager.GetSceneNodeFromRayBB(l, 0x10000, m_root); if (n != null && n.ID >= 0x10000) { int i = n.ID - 0x10000; if (isRight) flagCell(m_board[i]); else revealCell(m_board[i]); } }
static void Main(string[] args) { // setup Irrlicht device = IrrlichtDevice.CreateDevice(DriverType.Direct3D9, new Dimension2Di(1024, 768), 32, false, true); if (device == null) return; device.SetWindowCaption("Stencil Shadows - Irrlicht Engine"); device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent); VideoDriver driver = device.VideoDriver; SceneManager scene = device.SceneManager; GUIFont statsFont = device.GUIEnvironment.GetFont("../../media/fontlucida.png"); Material statsMaterial = Material.IdentityNoLighting; cameraNode = scene.AddCameraSceneNodeFPS(); cameraNode.FarValue = 20000; device.CursorControl.Visible = false; // setup shadows shadows = new Shadows(new Color(0xa0000000), 4000); // load quake level device.FileSystem.AddFileArchive("../../media/map-20kdm2.pk3"); Mesh m = scene.GetMesh("20kdm2.bsp").GetMesh(0); MeshSceneNode n = scene.AddOctreeSceneNode(m, null, -1, 1024); n.Position = new Vector3Df(-1300, -144, -1249); quakeLevelNode = n; // add faerie faerieNode = scene.AddAnimatedMeshSceneNode( scene.GetMesh("../../media/faerie.md2"), null, -1, new Vector3Df(100, -40, 80), new Vector3Df(0, 30, 0), new Vector3Df(1.6f)); faerieNode.SetMD2Animation(AnimationTypeMD2.Wave); faerieNode.AnimationSpeed = 20; faerieNode.GetMaterial(0).SetTexture(0, driver.GetTexture("../../media/faerie2.bmp")); faerieNode.GetMaterial(0).Lighting = false; faerieNode.GetMaterial(0).NormalizeNormals = true; shadows.AddObject(faerieNode); // add light lightMovementHelperNode = scene.AddEmptySceneNode(); n = scene.AddSphereSceneNode(2, 6, lightMovementHelperNode, -1, new Vector3Df(15, -10, 15)); n.SetMaterialFlag(MaterialFlag.Lighting, false); lightNode = n; shadows.AddLight(lightNode); // add flashlight m = scene.GetMesh("../../media/flashlight.obj"); n = scene.AddMeshSceneNode(m, lightNode, -1, new Vector3Df(0), new Vector3Df(0), new Vector3Df(5)); n.SetMaterialFlag(MaterialFlag.Lighting, false); flashlightNode = n; flashlightNode.Visible = false; // render uint shdFrameTime = 0; uint shdFrames = 0; uint shdFps = 0; while (device.Run()) { if (useShadowsRebuilding && shadows.BuildShadowVolume()) shdFrames++; uint t = device.Timer.Time; if (t - shdFrameTime > 1000) { shdFrameTime = t; shdFps = shdFrames; shdFrames = 0; } if (useLightBinding) { lightMovementHelperNode.Position = cameraNode.AbsolutePosition.GetInterpolated(lightMovementHelperNode.Position, 0.1); lightMovementHelperNode.Rotation = cameraNode.AbsoluteTransformation.Rotation; } driver.BeginScene(true, true, new Color(0xff112244)); scene.DrawAll(); if (useShadowsRendering) shadows.DrawShadowVolume(driver); // display stats device.VideoDriver.SetMaterial(statsMaterial); driver.Draw2DRectangle(new Recti(10, 10, 150, 220), new Color(0x7f000000)); Vector2Di v = new Vector2Di(20, 20); statsFont.Draw("Rendering", v, Color.OpaqueYellow); v.Y += 16; statsFont.Draw(driver.FPS + " fps", v, Color.OpaqueWhite); v.Y += 16; statsFont.Draw("[S]hadows " + (useShadowsRendering ? "ON" : "OFF"), v, Color.OpaqueGreen); v.Y += 16; statsFont.Draw("[L]ight binding " + (useLightBinding ? "ON" : "OFF"), v, Color.OpaqueGreen); v.Y += 16; statsFont.Draw("[F]lashlight " + (useFlashlight ? "ON" : "OFF"), v, Color.OpaqueGreen); v.Y += 32; statsFont.Draw("Shadows", v, Color.OpaqueYellow); v.Y += 16; statsFont.Draw(shdFps + " fps", v, Color.OpaqueWhite); v.Y += 16; statsFont.Draw(shadows.VerticesBuilt + " vertices", v, Color.OpaqueWhite); v.Y += 16; statsFont.Draw("[R]ebuilding " + (useShadowsRebuilding ? "ON" : "OFF"), v, Color.OpaqueGreen); v.Y += 16; statsFont.Draw("[Q]uake level " + (useShadowsQuakeLevel ? "ON" : "OFF"), v, Color.OpaqueGreen); driver.EndScene(); } shadows.Drop(); device.Drop(); }
static void Main() { // setup Irrlicht device = IrrlichtDevice.CreateDevice(DriverType.OpenGL, new Dimension2Di(1024, 768), 32, false, true); if (device == null) { return; } device.SetWindowCaption("Stencil Shadows - Irrlicht Engine"); device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent); VideoDriver driver = device.VideoDriver; SceneManager scene = device.SceneManager; GUIFont statsFont = device.GUIEnvironment.GetFont("../../media/fontlucida.png"); cameraNode = scene.AddCameraSceneNodeFPS(); cameraNode.FarValue = 20000; device.CursorControl.Visible = false; // setup shadows shadows = new Shadows(new Color(0xa0000000), 4000); // load quake level device.FileSystem.AddFileArchive("../../media/map-20kdm2.pk3"); Mesh m = scene.GetMesh("20kdm2.bsp").GetMesh(0); MeshSceneNode n = scene.AddOctreeSceneNode(m, null, -1, 1024); n.Position = new Vector3Df(-1300, -144, -1249); quakeLevelNode = n; // add faerie faerieNode = scene.AddAnimatedMeshSceneNode( scene.GetMesh("../../media/faerie.md2"), null, -1, new Vector3Df(100, -40, 80), new Vector3Df(0, 30, 0), new Vector3Df(1.6f)); faerieNode.SetMD2Animation(AnimationTypeMD2.Wave); faerieNode.AnimationSpeed = 20; faerieNode.GetMaterial(0).SetTexture(0, driver.GetTexture("../../media/faerie2.bmp")); faerieNode.GetMaterial(0).Lighting = false; faerieNode.GetMaterial(0).NormalizeNormals = true; shadows.AddObject(faerieNode); // add light lightMovementHelperNode = scene.AddEmptySceneNode(); n = scene.AddSphereSceneNode(2, 6, lightMovementHelperNode, -1, new Vector3Df(15, -10, 15)); n.SetMaterialFlag(MaterialFlag.Lighting, false); lightNode = n; shadows.AddLight(lightNode); // add flashlight m = scene.GetMesh("../../media/flashlight.obj"); n = scene.AddMeshSceneNode(m, lightNode, -1, new Vector3Df(0), new Vector3Df(0), new Vector3Df(5)); n.SetMaterialFlag(MaterialFlag.Lighting, false); flashlightNode = n; flashlightNode.Visible = false; // render uint shdFrameTime = 0; uint shdFrames = 0; uint shdFps = 0; while (device.Run()) { if (useShadowsRebuilding && shadows.BuildShadowVolume()) { shdFrames++; } uint t = device.Timer.Time; if (t - shdFrameTime > 1000) { shdFrameTime = t; shdFps = shdFrames; shdFrames = 0; } if (useLightBinding) { lightMovementHelperNode.Position = cameraNode.AbsolutePosition.GetInterpolated(lightMovementHelperNode.Position, 0.1); lightMovementHelperNode.Rotation = cameraNode.AbsoluteTransformation.Rotation; } driver.BeginScene(ClearBufferFlag.All, new Color(0xff112244)); scene.DrawAll(); if (useShadowsRendering) { shadows.DrawShadowVolume(driver); } // display stats driver.Draw2DRectangle(new Recti(10, 10, 150, 220), new Color(0x7f000000)); Vector2Di v = new Vector2Di(20, 20); statsFont.Draw("Rendering", v, Color.SolidYellow); v.Y += 16; statsFont.Draw(driver.FPS + " fps", v, Color.SolidWhite); v.Y += 16; statsFont.Draw("[S]hadows " + (useShadowsRendering ? "ON" : "OFF"), v, Color.SolidGreen); v.Y += 16; statsFont.Draw("[L]ight binding " + (useLightBinding ? "ON" : "OFF"), v, Color.SolidGreen); v.Y += 16; statsFont.Draw("[F]lashlight " + (useFlashlight ? "ON" : "OFF"), v, Color.SolidGreen); v.Y += 32; statsFont.Draw("Shadows", v, Color.SolidYellow); v.Y += 16; statsFont.Draw(shdFps + " fps", v, Color.SolidWhite); v.Y += 16; statsFont.Draw(shadows.VerticesBuilt + " vertices", v, Color.SolidWhite); v.Y += 16; statsFont.Draw("[R]ebuilding " + (useShadowsRebuilding ? "ON" : "OFF"), v, Color.SolidGreen); v.Y += 16; statsFont.Draw("[Q]uake level " + (useShadowsQuakeLevel ? "ON" : "OFF"), v, Color.SolidGreen); driver.EndScene(); } shadows.Drop(); device.Drop(); }
bool OnEvent(Event e) { if (e.Type == EventType.Mouse) { int x = e.Mouse.X; int y = e.Mouse.Y; bool l = e.Mouse.IsLeftPressed(); if (l && guiImage.AbsolutePosition.IsPointInside(new Vector2Di(x, y))) { Vector2Di p = new Vector2Di(x, y) - guiImage.AbsolutePosition.UpperLeftCorner; if (e.Mouse.Type == MouseEventType.Move) { TexturePainter t = texture.Painter; if (p.X < texture.Size.Width && p.Y < texture.Size.Height && t.Lock(TextureLockMode.WriteOnly)) { t.SetLine(oldMouseX, oldMouseY, p.X, p.Y, new Color(255, 0, 0)); t.Unlock(true); } } oldMouseX = p.X; oldMouseY = p.Y; return true; } } if (e.Type == EventType.GUI) { if (e.GUI.Type == GUIEventType.ElementClosed && e.GUI.Caller is GUIWindow) { device.Close(); return true; } if (e.GUI.Type == GUIEventType.ButtonClicked) { if (e.GUI.Caller == guiSize128) { initGUI(128); createTexture(128); return true; } if (e.GUI.Caller == guiSize256) { initGUI(256); createTexture(256); return true; } if (e.GUI.Caller == guiSize512) { initGUI(512); createTexture(512); return true; } } } return false; }
static void Main(string[] args) { checkBulletSharpDllPresence(); // setup Irrlicht device = IrrlichtDevice.CreateDevice(DriverType.Direct3D9, new Dimension2Di(1024, 768)); if (device == null) return; device.SetWindowCaption("BulletSharp Test - Irrlicht Engine"); device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent); VideoDriver driver = device.VideoDriver; SceneManager scene = device.SceneManager; GUIFont font = device.GUIEnvironment.GetFont("../../media/fontlucida.png"); CameraSceneNode camera = scene.AddCameraSceneNodeFPS(); camera.Position = new Vector3Df(100, 800, -1000); camera.Target = new Vector3Df(0, 100, 0); camera.FarValue = 30000; camera.AutomaticCulling = CullingType.FrustumBox; device.CursorControl.Visible = false; // setup physics physics = new Physics(); physics.Setup(new Vector3Df(0, -worldGravity, 0)); // setup particles particles = new Particles(device); // load quake level device.FileSystem.AddFileArchive("../../media/map-20kdm2.pk3"); Mesh mesh = scene.GetMesh("20kdm2.bsp").GetMesh(0); SceneNode quakeLevel = scene.AddOctreeSceneNode(mesh, null, -1, 1024); quakeLevel.Position = new Vector3Df(-1300, -144, -1249); physics.AddShape(Physics.Shape.Mesh, quakeLevel); // generate dynamic objects for (int i = 0; i < 3; i++) { for (int j = 0; j < 30; j++) { for (int k = 0; k < 3; k++) { MeshSceneNode n = scene.AddCubeSceneNode(cubeSize); n.SetMaterialTexture(0, driver.GetTexture("../../media/wall.bmp")); n.SetMaterialFlag(MaterialFlag.Lighting, false); n.Position = new Vector3Df(70 + i * cubeSize, 520 + j * cubeSize, -650 + k * cubeSize); physics.AddShape(Physics.Shape.Box, n, cubeMass); } } } // main loop uint curTime = 0; uint lastTime = 0; int simFps = 0; int simFrames = 0; uint simFramesTime = 0; while (device.Run()) { if (device.WindowActive) { // simulate physics lastTime = curTime; curTime = device.Timer.Time; if (!simPaused) { float deltaTime = (curTime - lastTime) / 1000.0f; bool b = physics.StepSimulation(deltaTime); if (b) simFrames++; } if (curTime - simFramesTime > 1000) { simFramesTime = curTime; simFps = simFrames; simFrames = 0; } // winnow particles particles.Winnow(curTime, simPaused); // render scene driver.BeginScene(true, true, new Color(40, 80, 160)); scene.DrawAll(); Material material = new Material(); material.Lighting = false; device.VideoDriver.SetMaterial(material); // display stats driver.Draw2DRectangle(new Recti(10, 10, 140, 180), new Color(0x7f000000)); Vector2Di v = new Vector2Di(20, 20); font.Draw("Rendering", v, Color.OpaqueYellow); v.Y += 16; font.Draw(scene.Attributes.GetValue("calls") + " nodes", v, Color.OpaqueWhite); v.Y += 16; font.Draw(driver.FPS + " fps", v, Color.OpaqueWhite); v.Y += 16; font.Draw("[T]rails " + (useTrails ? "ON" : "OFF"), v, Color.OpaqueGreen); v.Y += 32; font.Draw("Physics" + (simPaused ? " (paused)" : ""), v, Color.OpaqueYellow); v.Y += 16; font.Draw(physics.NumCollisionObjects + " shapes", v, Color.OpaqueWhite); v.Y += 16; font.Draw(simFps + " fps", v, Color.OpaqueWhite); v.Y += 16; font.Draw("[Space] to pause", v, Color.OpaqueGreen); driver.EndScene(); } device.Yield(); } // drop physics.Drop(); device.Drop(); }
static void Main(string[] args) { device = IrrlichtDevice.CreateDevice(DriverType.Direct3D8, new Dimension2Di(1024, 768)); if (device == null) return; device.SetWindowCaption("Fractal Generator - Irrlicht Engine"); device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent); VideoDriver driver = device.VideoDriver; GUIFont font = device.GUIEnvironment.GetFont("../../media/fontlucida.png"); Color fontBackgroundColor = new Color(0x7f000000); Color fontNormalColor = Color.OpaqueWhite; Color fontActionColor = Color.OpaqueYellow; fGen = new FractalGenerator(device); fGen.Generate(new Rectd( -driver.ScreenSize.Width / 250.0, -driver.ScreenSize.Height / 250.0, driver.ScreenSize.Width / 250.0, driver.ScreenSize.Height / 250.0)); while (device.Run()) { driver.BeginScene(false); Vector2Di o = null; if (mouseMoveStart != null) o = device.CursorControl.Position - mouseMoveStart; float w = fGen.DrawAll(o); // draw stats driver.Draw2DRectangle(new Recti(10, 10, 160, 56 + (w < 1 ? 16 : 0)), fontBackgroundColor); Vector2Di v = new Vector2Di(20, 16); font.Draw("Max iterations: " + fGen.GetMaxIterations(), v, fontNormalColor); v.Y += 16; font.Draw("Zoom: " + (long)fGen.GetZoomFactor().X + "x", v, fontNormalColor); if (w < 1) { v.Y += 16; font.Draw("Computing: " + (int)(w * 100) + "%...", v, fontActionColor); } // draw help int h = driver.ScreenSize.Height; driver.Draw2DRectangle(new Recti(10, showHelp ? h - 130 : h - 40, showHelp ? 220 : 160, h - 10), fontBackgroundColor); v.Y = h - 34; font.Draw("[F1] " + (showHelp ? "Hide" : "Show") + " help", v, fontNormalColor); if (showHelp) { v.Y = h - 124; font.Draw("[Mouse Left Button] Navigate", v, fontNormalColor); v.Y += 16; font.Draw("[Mouse Wheel] Zoom in/out", v, fontNormalColor); v.Y += 16; font.Draw("[+][-][*][/] Max iterations", v, fontNormalColor); v.Y += 16; font.Draw("[PrintScreen] Save screenshot", v, fontNormalColor); v.Y += 16; font.Draw("[Esc] Exit application", v, fontNormalColor); } driver.EndScene(); device.Yield(); } fGen.Drop(); device.Drop(); }
static bool device_OnEvent(Event evnt) { if (evnt.Type == EventType.Mouse) { Dimension2Di s = device.VideoDriver.ScreenSize; if (evnt.Mouse.Type == MouseEventType.Wheel) { Rectd r = new Rectd(); if (evnt.Mouse.Wheel > 0) { // zoom in int x1 = evnt.Mouse.X - s.Width / 2 + (int)evnt.Mouse.Wheel * s.Width / 10; int y1 = evnt.Mouse.Y - s.Height / 2 + (int)evnt.Mouse.Wheel * s.Height / 10; r.UpperLeftCorner = fGen.GetWindowCoord(x1, y1); r.LowerRightCorner = fGen.GetWindowCoord(2 * evnt.Mouse.X - x1, 2 * evnt.Mouse.Y - y1); device.CursorControl.Position = new Vector2Di(s.Width / 2, s.Height / 2); } else { // zoom out int x1 = s.Width / 10; int y1 = s.Height / 10; r.UpperLeftCorner = fGen.GetWindowCoord(-x1, -y1); r.LowerRightCorner = fGen.GetWindowCoord(s.Width + x1, s.Height + y1); } fGen.Generate(r); return(true); } if (evnt.Mouse.Type == MouseEventType.LeftDown) { mouseMoveStart = new Vector2Di(evnt.Mouse.X, evnt.Mouse.Y); return(true); } if (evnt.Mouse.Type == MouseEventType.LeftUp) { Vector2Dd p1 = fGen.GetWindowCoord(evnt.Mouse.X, evnt.Mouse.Y); Vector2Dd p2 = fGen.GetWindowCoord(mouseMoveStart.X, mouseMoveStart.Y); Rectd r = fGen.GetWindow() + p2 - p1; fGen.Generate(r); mouseMoveStart = null; return(true); } } if (evnt.Type == EventType.Key) { if (evnt.Key.PressedDown) { if (evnt.Key.Key == KeyCode.Esc) { device.Close(); return(true); } if (evnt.Key.Key == KeyCode.F1) { showHelp = !showHelp; return(true); } switch (evnt.Key.Char) { case '+': fGen.Generate(fGen.GetMaxIterations() + 1); return(true); case '-': fGen.Generate(fGen.GetMaxIterations() - 1); return(true); case '*': fGen.Generate(fGen.GetMaxIterations() + 10); return(true); case '/': fGen.Generate(fGen.GetMaxIterations() - 10); return(true); } } if (evnt.Key.Key == KeyCode.PrintScreen) // PrintScreen never comes with "evnt.Key.PressedDown == true" so we process it without checking that { string n = "Screenshot-" + DateTime.Now.Ticks + ".png"; Image i = device.VideoDriver.CreateScreenShot(); device.VideoDriver.WriteImage(i, n); i.Drop(); device.Logger.Log("Screenshot saved as " + n); return(true); } } return(false); }
static void Main(string[] args) { DriverType driverType; if (!AskUserForDriver(out driverType)) { return; } IrrlichtDevice device = IrrlichtDevice.CreateDevice(driverType, new Dimension2Di(512, 384)); if (device == null) { return; } device.SetWindowCaption("Irrlicht Engine - 2D Graphics Demo"); VideoDriver driver = device.VideoDriver; Texture images = driver.GetTexture("../../media/2ddemo.png"); driver.MakeColorKeyTexture(images, new Vector2Di(0, 0)); GUIFont font = device.GUIEnvironment.BuiltInFont; GUIFont font2 = device.GUIEnvironment.GetFont("../../media/fonthaettenschweiler.bmp"); Recti imp1 = new Recti(349, 15, 385, 78); Recti imp2 = new Recti(387, 15, 423, 78); driver.Material2D.Layer[0].BilinearFilter = true; driver.Material2D.AntiAliasing = AntiAliasingMode.FullBasic; while (device.Run()) { if (device.WindowActive) { int time = (int)device.Timer.Time; driver.BeginScene(true, true, new Color(120, 102, 136)); // draw fire & dragons background world driver.Draw2DImage(images, new Vector2Di(50, 50), new Recti(0, 0, 342, 224), null, new Color(255, 255, 255), true); // draw flying imp driver.Draw2DImage(images, new Vector2Di(164, 125), (time / 500 % 2) == 1 ? imp1 : imp2, null, new Color(255, 255, 255), true); // draw second flying imp with colorcylce driver.Draw2DImage(images, new Vector2Di(270, 105), (time / 500 % 2) == 1 ? imp1 : imp2, null, new Color(time % 255, 255, 255), true); // draw some text if (font != null) { font.Draw("This demo shows that Irrlicht is also capable of drawing 2D graphics.", 130, 10, new Color(255, 255, 255)); } // draw some other text if (font2 != null) { font2.Draw("Also mixing with 3d graphics is possible.", 130, 20, new Color(time % 255, time % 255, 255)); } driver.EnableMaterial2D(); driver.Draw2DImage(images, new Recti(10, 10, 108, 48), new Recti(354, 87, 442, 118)); driver.EnableMaterial2D(false); Vector2Di m = device.CursorControl.Position; driver.Draw2DRectangle(new Recti(m.X - 20, m.Y - 20, m.X + 20, m.Y + 20), new Color(255, 255, 255, 100)); driver.EndScene(); } } device.Drop(); }
static bool device_OnEvent(Event evnt) { if (evnt.Type == EventType.Mouse && evnt.Mouse.Type == MouseEventType.Move) { Vector2Di m = new Vector2Di(evnt.Mouse.X, evnt.Mouse.Y); Line3Df l = device.SceneManager.SceneCollisionManager.GetRayFromScreenCoordinates(m); Plane3Df p = new Plane3Df(new Vector3Df(0, 0, 0), new Vector3Df(100, 0, 0), new Vector3Df(0, 0, 100)); Vector3Df i; if (p.GetIntersectionWithLimitedLine(l.Start, l.End, out i)) { camera.Target = game.CenterOfTheBoard + new Vector3Df( (m.Y - device.VideoDriver.ScreenSize.Height / 2) / 100.0f, 0, (m.X - device.VideoDriver.ScreenSize.Width / 2) / 100.0f); i.Y += 25; // we want light to be a little bit above light.Position = i; } } if (window == null && evnt.Type == EventType.Mouse && (evnt.Mouse.Type == MouseEventType.LeftDown || evnt.Mouse.Type == MouseEventType.RightDown)) { text.Visible = false; // if user started to play - remove the gui text game.MouseClick(evnt.Mouse.X, evnt.Mouse.Y, evnt.Mouse.Type == MouseEventType.RightDown); if (game.StateOfTheGame != Game.State.Playing) { text.Visible = true; text.Text = game.StateOfTheGame == Game.State.Won ? TextWon : TextLost; } return true; } if (evnt.Type == EventType.Key && evnt.Key.PressedDown && evnt.Key.Key == KeyCode.Esc) { if (window != null) { window.Remove(); window = null; return true; } GUIEnvironment gui = device.GUIEnvironment; window = gui.AddWindow(new Recti(100, 100, 400, 400), true, "GAME MENU"); gui.AddButton(new Recti(20, 40, window.ClientRect.Width - 20, 60), window, 1510, "NEW GAME 5x5"); gui.AddButton(new Recti(20, 60, window.ClientRect.Width - 20, 80), window, 1520, "NEW GAME 10x10"); gui.AddButton(new Recti(20, 80, window.ClientRect.Width - 20, 100), window, 1530, "NEW GAME 15x15"); gui.AddButton(new Recti(20, 100, window.ClientRect.Width - 20, 120), window, 1540, "NEW GAME 20x20"); gui.AddCheckBox(optionShadows, new Recti(20, 140, window.ClientRect.Width - 20, 160), "SHOW REALTIME SHADOWS", window, 1710); gui.AddCheckBox(optionBackground, new Recti(20, 160, window.ClientRect.Width - 20, 180), "SHOW BACKGROUND", window, 1720); gui.AddCheckBox(optionFPS, new Recti(20, 180, window.ClientRect.Width - 20, 200), "SHOW FPS", window, 1730); gui.AddButton(new Recti(20, 260, window.ClientRect.Width - 20, 280), window, 1590, "EXIT GAME"); return true; } if (window != null && evnt.Type == EventType.GUI) { if (evnt.GUI.Caller == window && evnt.GUI.Type == GUIEventType.ElementClosed) { window.Remove(); window = null; return true; } if (evnt.GUI.Caller.ID == 1510 && evnt.GUI.Type == GUIEventType.ButtonClicked) { window.Remove(); window = null; game.NewGame(5, 5); setupCameraPositionAndTarget(); return true; } if (evnt.GUI.Caller.ID == 1520 && evnt.GUI.Type == GUIEventType.ButtonClicked) { window.Remove(); window = null; game.NewGame(10, 10); setupCameraPositionAndTarget(); return true; } if (evnt.GUI.Caller.ID == 1530 && evnt.GUI.Type == GUIEventType.ButtonClicked) { window.Remove(); window = null; game.NewGame(15, 15); setupCameraPositionAndTarget(); return true; } if (evnt.GUI.Caller.ID == 1540 && evnt.GUI.Type == GUIEventType.ButtonClicked) { window.Remove(); window = null; game.NewGame(20, 20); setupCameraPositionAndTarget(); return true; } if (evnt.GUI.Caller.ID == 1590 && evnt.GUI.Type == GUIEventType.ButtonClicked) { device.Close(); return true; } if (evnt.GUI.Caller.ID == 1710 && evnt.GUI.Type == GUIEventType.CheckBoxChanged) { optionShadows = (evnt.GUI.Caller as GUICheckBox).Checked; light.CastShadows = optionShadows; return true; } if (evnt.GUI.Caller.ID == 1720 && evnt.GUI.Type == GUIEventType.CheckBoxChanged) { optionBackground = (evnt.GUI.Caller as GUICheckBox).Checked; device.SceneManager.GetSceneNodeFromID(7777).Visible = optionBackground; return true; } if (evnt.GUI.Caller.ID == 1730 && evnt.GUI.Type == GUIEventType.CheckBoxChanged) { optionFPS = (evnt.GUI.Caller as GUICheckBox).Checked; return true; } } return false; }
static bool device_OnEvent(Event evnt) { if (evnt.Type == EventType.Mouse && evnt.Mouse.Type == MouseEventType.Move) { Vector2Di m = new Vector2Di(evnt.Mouse.X, evnt.Mouse.Y); Line3Df l = device.SceneManager.SceneCollisionManager.GetRayFromScreenCoordinates(m); Plane3Df p = new Plane3Df(new Vector3Df(0, 0, 0), new Vector3Df(100, 0, 0), new Vector3Df(0, 0, 100)); Vector3Df i = p.GetIntersectionWithLimitedLine(l.Start, l.End); if (i != null) { camera.Target = game.CenterOfTheBoard + new Vector3Df( (m.Y - device.VideoDriver.ScreenSize.Height / 2) / 100.0f, 0, (m.X - device.VideoDriver.ScreenSize.Width / 2) / 100.0f); i.Y += 25; // we want light to be a little bit above light.Position = i; } } if (window == null && evnt.Type == EventType.Mouse && (evnt.Mouse.Type == MouseEventType.LeftDown || evnt.Mouse.Type == MouseEventType.RightDown)) { text.Visible = false; // if user started to play - remove the gui text game.MouseClick(evnt.Mouse.X, evnt.Mouse.Y, evnt.Mouse.Type == MouseEventType.RightDown); if (game.StateOfTheGame != Game.State.Playing) { text.Visible = true; text.Text = game.StateOfTheGame == Game.State.Won ? TextWon : TextLost; } return(true); } if (evnt.Type == EventType.Key && evnt.Key.PressedDown && evnt.Key.Key == KeyCode.Esc) { if (window != null) { window.Remove(); window = null; return(true); } GUIEnvironment gui = device.GUIEnvironment; window = gui.AddWindow(new Recti(100, 100, 400, 400), true, "GAME MENU"); gui.AddButton(new Recti(20, 40, window.ClientRect.Width - 20, 60), window, 1510, "NEW GAME 5x5"); gui.AddButton(new Recti(20, 60, window.ClientRect.Width - 20, 80), window, 1520, "NEW GAME 10x10"); gui.AddButton(new Recti(20, 80, window.ClientRect.Width - 20, 100), window, 1530, "NEW GAME 15x15"); gui.AddButton(new Recti(20, 100, window.ClientRect.Width - 20, 120), window, 1540, "NEW GAME 20x20"); gui.AddCheckBox(optionShadows, new Recti(20, 140, window.ClientRect.Width - 20, 160), "SHOW REALTIME SHADOWS", window, 1710); gui.AddCheckBox(optionBackground, new Recti(20, 160, window.ClientRect.Width - 20, 180), "SHOW BACKGROUND", window, 1720); gui.AddCheckBox(optionFPS, new Recti(20, 180, window.ClientRect.Width - 20, 200), "SHOW FPS", window, 1730); gui.AddButton(new Recti(20, 260, window.ClientRect.Width - 20, 280), window, 1590, "EXIT GAME"); return(true); } if (window != null && evnt.Type == EventType.GUI) { if (evnt.GUI.Caller == window && evnt.GUI.Type == GUIEventType.ElementClosed) { window.Remove(); window = null; return(true); } if (evnt.GUI.Caller.ID == 1510 && evnt.GUI.Type == GUIEventType.ButtonClicked) { window.Remove(); window = null; game.NewGame(5, 5); setupCameraPositionAndTarget(); return(true); } if (evnt.GUI.Caller.ID == 1520 && evnt.GUI.Type == GUIEventType.ButtonClicked) { window.Remove(); window = null; game.NewGame(10, 10); setupCameraPositionAndTarget(); return(true); } if (evnt.GUI.Caller.ID == 1530 && evnt.GUI.Type == GUIEventType.ButtonClicked) { window.Remove(); window = null; game.NewGame(15, 15); setupCameraPositionAndTarget(); return(true); } if (evnt.GUI.Caller.ID == 1540 && evnt.GUI.Type == GUIEventType.ButtonClicked) { window.Remove(); window = null; game.NewGame(20, 20); setupCameraPositionAndTarget(); return(true); } if (evnt.GUI.Caller.ID == 1590 && evnt.GUI.Type == GUIEventType.ButtonClicked) { device.Close(); return(true); } if (evnt.GUI.Caller.ID == 1710 && evnt.GUI.Type == GUIEventType.CheckBoxChanged) { optionShadows = (evnt.GUI.Caller as GUICheckBox).Checked; light.CastShadows = optionShadows; return(true); } if (evnt.GUI.Caller.ID == 1720 && evnt.GUI.Type == GUIEventType.CheckBoxChanged) { optionBackground = (evnt.GUI.Caller as GUICheckBox).Checked; device.SceneManager.GetSceneNodeFromID(7777).Visible = optionBackground; return(true); } if (evnt.GUI.Caller.ID == 1730 && evnt.GUI.Type == GUIEventType.CheckBoxChanged) { optionFPS = (evnt.GUI.Caller as GUICheckBox).Checked; return(true); } } return(false); }
static void Main(string[] args) { checkBulletSharpDllPresence(); // setup Irrlicht device = IrrlichtDevice.CreateDevice(DriverType.Direct3D9, new Dimension2Di(1024, 768)); if (device == null) { return; } device.SetWindowCaption("BulletSharp Test - Irrlicht Engine"); device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent); VideoDriver driver = device.VideoDriver; SceneManager scene = device.SceneManager; GUIFont font = device.GUIEnvironment.GetFont("../../media/fontlucida.png"); CameraSceneNode camera = scene.AddCameraSceneNodeFPS(); camera.Position = new Vector3Df(100, 800, -1000); camera.Target = new Vector3Df(0, 100, 0); camera.FarValue = 30000; camera.AutomaticCulling = CullingType.FrustumBox; device.CursorControl.Visible = false; // setup physics physics = new Physics(); physics.Setup(new Vector3Df(0, -worldGravity, 0)); // setup particles particles = new Particles(device); // load quake level device.FileSystem.AddFileArchive("../../media/map-20kdm2.pk3"); Mesh mesh = scene.GetMesh("20kdm2.bsp").GetMesh(0); SceneNode quakeLevel = scene.AddOctreeSceneNode(mesh, null, -1, 1024); quakeLevel.Position = new Vector3Df(-1300, -144, -1249); physics.AddShape(Physics.Shape.Mesh, quakeLevel); // generate dynamic objects for (int i = 0; i < 3; i++) { for (int j = 0; j < 30; j++) { for (int k = 0; k < 3; k++) { MeshSceneNode n = scene.AddCubeSceneNode(cubeSize); n.SetMaterialTexture(0, driver.GetTexture("../../media/wall.bmp")); n.SetMaterialFlag(MaterialFlag.Lighting, false); n.Position = new Vector3Df(70 + i * cubeSize, 520 + j * cubeSize, -650 + k * cubeSize); physics.AddShape(Physics.Shape.Box, n, cubeMass); } } } // main loop uint curTime = 0; uint lastTime = 0; int simFps = 0; int simFrames = 0; uint simFramesTime = 0; while (device.Run()) { if (device.WindowActive) { // simulate physics lastTime = curTime; curTime = device.Timer.Time; if (!simPaused) { float deltaTime = (curTime - lastTime) / 1000.0f; bool b = physics.StepSimulation(deltaTime); if (b) { simFrames++; } } if (curTime - simFramesTime > 1000) { simFramesTime = curTime; simFps = simFrames; simFrames = 0; } // winnow particles particles.Winnow(curTime, simPaused); // render scene driver.BeginScene(true, true, new Color(40, 80, 160)); scene.DrawAll(); Material material = new Material(); material.Lighting = false; device.VideoDriver.SetMaterial(material); // display stats driver.Draw2DRectangle(new Recti(10, 10, 140, 180), new Color(0x7f000000)); Vector2Di v = new Vector2Di(20, 20); font.Draw("Rendering", v, Color.OpaqueYellow); v.Y += 16; font.Draw(scene.Attributes.GetValue("calls") + " nodes", v, Color.OpaqueWhite); v.Y += 16; font.Draw(driver.FPS + " fps", v, Color.OpaqueWhite); v.Y += 16; font.Draw("[T]rails " + (useTrails ? "ON" : "OFF"), v, Color.OpaqueGreen); v.Y += 32; font.Draw("Physics" + (simPaused ? " (paused)" : ""), v, Color.OpaqueYellow); v.Y += 16; font.Draw(physics.NumCollisionObjects + " shapes", v, Color.OpaqueWhite); v.Y += 16; font.Draw(simFps + " fps", v, Color.OpaqueWhite); v.Y += 16; font.Draw("[Space] to pause", v, Color.OpaqueGreen); driver.EndScene(); } device.Yield(); } // drop physics.Drop(); device.Drop(); }
static void Main(string[] args) { device = IrrlichtDevice.CreateDevice(DriverType.Direct3D8, new Dimension2Di(1024, 768)); if (device == null) { return; } device.SetWindowCaption("Fractal Generator - Irrlicht Engine"); device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent); VideoDriver driver = device.VideoDriver; GUIFont font = device.GUIEnvironment.GetFont("../../media/fontlucida.png"); Color fontBackgroundColor = new Color(0x7f000000); Color fontNormalColor = Color.OpaqueWhite; Color fontActionColor = Color.OpaqueYellow; fGen = new FractalGenerator(device); fGen.Generate(new Rectd( -driver.ScreenSize.Width / 250.0, -driver.ScreenSize.Height / 250.0, driver.ScreenSize.Width / 250.0, driver.ScreenSize.Height / 250.0)); while (device.Run()) { driver.BeginScene(false); Vector2Di o = null; if (mouseMoveStart != null) { o = device.CursorControl.Position - mouseMoveStart; } float w = fGen.DrawAll(o); // draw stats driver.Draw2DRectangle(new Recti(10, 10, 160, 56 + (w < 1 ? 16 : 0)), fontBackgroundColor); Vector2Di v = new Vector2Di(20, 16); font.Draw("Max iterations: " + fGen.GetMaxIterations(), v, fontNormalColor); v.Y += 16; font.Draw("Zoom: " + (long)fGen.GetZoomFactor().X + "x", v, fontNormalColor); if (w < 1) { v.Y += 16; font.Draw("Computing: " + (int)(w * 100) + "%...", v, fontActionColor); } // draw help int h = driver.ScreenSize.Height; driver.Draw2DRectangle(new Recti(10, showHelp ? h - 130 : h - 40, showHelp ? 220 : 160, h - 10), fontBackgroundColor); v.Y = h - 34; font.Draw("[F1] " + (showHelp ? "Hide" : "Show") + " help", v, fontNormalColor); if (showHelp) { v.Y = h - 124; font.Draw("[Mouse Left Button] Navigate", v, fontNormalColor); v.Y += 16; font.Draw("[Mouse Wheel] Zoom in/out", v, fontNormalColor); v.Y += 16; font.Draw("[+][-][*][/] Max iterations", v, fontNormalColor); v.Y += 16; font.Draw("[PrintScreen] Save screenshot", v, fontNormalColor); v.Y += 16; font.Draw("[Esc] Exit application", v, fontNormalColor); } driver.EndScene(); device.Yield(); } fGen.Drop(); device.Drop(); }
public float DrawAll(Vector2Di screenOffset = null) { Vector2Di zero = new Vector2Di(0); int n = 0; foreach (Tile tile in tiles) { if (tile.TextureIsReady) { driver.Draw2DImage(tile.Texture, tile.ScreenPos + (screenOffset ?? zero)); n++; } } return (float)n / tiles.Count; }