public Jeu() { Device = IrrlichtDevice.CreateDevice( DriverType.Direct3D9, new Dimension2Di(800, 600), 32, false, false, true); Device.SetWindowCaption("Canardstein 3D"); Device.OnEvent += Evenement; SceneNode cube = Device.SceneManager.AddCubeSceneNode(1, null, 0, new Vector3Df(2, 0, 0), new Vector3Df(0, 45, 0)); cube.SetMaterialFlag(MaterialFlag.Lighting, false); CameraSceneNode camera = Device.SceneManager.AddCameraSceneNode(null, new Vector3Df(0, 0, 0), new Vector3Df(2, 0, 0)); Device.CursorControl.Position = new Vector2Di(400, 300); Device.CursorControl.Visible = false; while (Device.Run()) { float tempsEcoule = (Device.Timer.Time - DerniereFrame) / 1000f; DerniereFrame = Device.Timer.Time; if (Device.CursorControl.Position.X != 400) { Rotation += (Device.CursorControl.Position.X - 400) * 0.0025; Device.CursorControl.Position = new Vector2Di(400, 300); VecteurAvant = new Vector3Df( x: (float)Math.Cos(Rotation), y: 0, z: -(float)Math.Sin(Rotation)); VecteurDroite = VecteurAvant; VecteurDroite.RotateXZby(-90); } Vector3Df vitesse = new Vector3Df(); if (K_Avant) { vitesse += VecteurAvant; } else if (K_Arriere) { vitesse -= VecteurAvant; } if (K_Gauche) { vitesse -= VecteurDroite; } else if (K_Droite) { vitesse += VecteurDroite; } vitesse = vitesse.Normalize() * tempsEcoule * 2; camera.Position += vitesse; camera.Target = camera.Position + VecteurAvant; Device.VideoDriver.BeginScene(ClearBufferFlag.Color | ClearBufferFlag.Depth, Color.OpaqueMagenta); Device.SceneManager.DrawAll(); Device.VideoDriver.EndScene(); } }
void buildShadowVolume(List <Vector3Df> shadowVertices, MeshBuffer meshbuffer, Matrix matrix, Vector3Df light) { ushort[] indices = meshbuffer.Indices as ushort[]; if (indices == null) { throw new ArgumentException(); } Triangle3Df t123 = new Triangle3Df(); for (int i = 0; i < indices.Length; i += 3) { Vector3Df v1 = meshbuffer.GetPosition(indices[i]); Vector3Df v2 = meshbuffer.GetPosition(indices[i + 1]); Vector3Df v3 = meshbuffer.GetPosition(indices[i + 2]); matrix.TransformVector(ref v1); matrix.TransformVector(ref v2); matrix.TransformVector(ref v3); t123.Set(v1, v2, v3); Vector3Df v1Dir = v1 - light; if (!t123.IsFrontFacing(v1Dir)) { continue; } Vector3Df v2Dir = v2 - light; Vector3Df v3Dir = v3 - light; // calc near points Vector3Df v1near = v1 + v1Dir * shadowNearMultiplier; Vector3Df v2near = v2 + v2Dir * shadowNearMultiplier; Vector3Df v3near = v3 + v3Dir * shadowNearMultiplier; // calc infinity points Vector3Df v1inf = v1 + v1Dir.Normalize() * shadowInfinityRange; Vector3Df v2inf = v2 + v2Dir.Normalize() * shadowInfinityRange; Vector3Df v3inf = v3 + v3Dir.Normalize() * shadowInfinityRange; // top shadowVertices.Add(v1near); shadowVertices.Add(v2near); shadowVertices.Add(v3near); // bottom shadowVertices.Add(v3inf); shadowVertices.Add(v2inf); shadowVertices.Add(v1inf); // side1 shadowVertices.Add(v1inf); shadowVertices.Add(v2near); shadowVertices.Add(v1near); shadowVertices.Add(v1inf); shadowVertices.Add(v2inf); shadowVertices.Add(v2near); // side2 shadowVertices.Add(v2inf); shadowVertices.Add(v3near); shadowVertices.Add(v2near); shadowVertices.Add(v2inf); shadowVertices.Add(v3inf); shadowVertices.Add(v3near); // side3 shadowVertices.Add(v1near); shadowVertices.Add(v3near); shadowVertices.Add(v1inf); shadowVertices.Add(v3near); shadowVertices.Add(v3inf); shadowVertices.Add(v1inf); } }
static void Main(string[] args) { // Initialize device. DriverType driverType; if (!AskUserForDriver(out driverType)) { return; } IrrlichtDevice device = IrrlichtDevice.CreateDevice(driverType, new Dimension2Di(640, 480)); if (device == null) { return; } // Add event handling. device.OnEvent += new IrrlichtDevice.EventHandler(device_OnEvent); // Save important pointers. VideoDriver driver = device.VideoDriver; SceneManager smgr = device.SceneManager; Logger logger = device.Logger; // Initialize joysticks and print info about them. List <JoystickInfo> joystickList = device.ActivateJoysticks(); if (joystickList != null) { logger.Log("Joystick support is enabled and " + joystickList.Count.ToString() + " joystick(s) are present."); foreach (JoystickInfo j in joystickList) { logger.Log("Joystick " + j.Joystick.ToString() + ":"); logger.Log("\tName: \"" + j.Name + "\""); logger.Log("\tAxisCount: " + j.AxisCount.ToString()); logger.Log("\tButtonCount: " + j.ButtonCount.ToString()); logger.Log("\tPovHat: " + j.PovHat.ToString()); } } else { logger.Log("Joystick support is not enabled."); } device.SetWindowCaption("Mouse and joystick - Irrlicht Lime - " + joystickList.Count.ToString() + " joystick(s)"); // Create an arrow mesh and move it around either with the joystick axis/hat, // or make it follow the mouse pointer (when no joystick movement). SceneNode node = smgr.AddMeshSceneNode( smgr.AddArrowMesh( "Arrow", new Color(255, 0, 0), new Color(0, 255, 0), 16, 16, 2.0f, 1.3f, 0.1f, 0.6f ) ); node.SetMaterialFlag(MaterialFlag.Lighting, false); CameraSceneNode camera = smgr.AddCameraSceneNode(); camera.Position = new Vector3Df(0, 0, -10); // As in example #4, we'll use framerate independent movement. uint then = device.Timer.Time; const float MovementSpeed = 5.0f; // Run main cycle. while (device.Run()) { // Work out a frame delta time. uint now = device.Timer.Time; float frameDeltaTime = (float)(now - then) / 1000.0f; // in seconds then = now; bool movedWithJoystick = false; Vector3Df nodePosition = node.Position; if (joystickList.Count > 0) { float moveHorizontal = 0.0f; // range is -1.0 for full left to +1.0 for full right float moveVertical = 0.0f; // range is -1.0 for full down to +1.0 for full up // We receive the full analog range of the axes, and so have to implement our own dead zone. // This is an empirical value, since some joysticks have more jitter or creep around the center // point than others. We'll use 5% of the range as the dead zone, but generally you would want // to give the user the option to change this. float DeadZone = 0.05f; moveHorizontal = joystickState.Axis[0] / 32767.0f; // "0" for X axis if (Math.Abs(moveHorizontal) < DeadZone) { moveHorizontal = 0.0f; } moveVertical = joystickState.Axis[1] / -32767.0f; // "1" for Y axis if (Math.Abs(moveVertical) < DeadZone) { moveVertical = 0.0f; } // POV will contain 65535 if POV hat info no0t supported, so we can check its range. ushort povDegrees = (ushort)(joystickState.POV / 100); if (povDegrees < 360) { if (povDegrees > 0 && povDegrees < 180) { moveHorizontal = +1.0f; } else if (povDegrees > 180) { moveHorizontal = -1.0f; } if (povDegrees > 90 && povDegrees < 270) { moveVertical = -1.0f; } else if (povDegrees > 270 || povDegrees < 90) { moveVertical = +1.0f; } } // If we have any movement, apply it. if (Math.Abs(moveHorizontal) > 0.0001f || Math.Abs(moveVertical) > 0.0001f) { float m = frameDeltaTime * MovementSpeed; nodePosition = new Vector3Df(moveHorizontal * m, moveVertical * m, nodePosition.Z); movedWithJoystick = true; } } // If the arrow node isn't being moved with the joystick, then have it follow the mouse cursor. if (!movedWithJoystick) { // Create a ray through the mouse cursor. Line3Df ray = smgr.SceneCollisionManager.GetRayFromScreenCoordinates(mouseState.Position, camera); // And intersect the ray with a plane around the node facing towards the camera. Plane3Df plane = new Plane3Df(nodePosition, new Vector3Df(0, 0, -1)); Vector3Df mousePosition; if (plane.GetIntersectionWithLine(ray.Start, ray.Vector, out mousePosition)) { // We now have a mouse position in 3d space; move towards it. Vector3Df toMousePosition = mousePosition - nodePosition; float availableMovement = frameDeltaTime * MovementSpeed; if (toMousePosition.Length <= availableMovement) { nodePosition = mousePosition; // jump to the final position } else { nodePosition += toMousePosition.Normalize() * availableMovement; // move towards it } } } node.Position = nodePosition; // Turn lighting on and off depending on whether the left mouse button is down. node.SetMaterialFlag(MaterialFlag.Lighting, mouseState.IsLeftButtonDown); // Draw all. driver.BeginScene(true, true, new Color(113, 113, 133)); smgr.DrawAll(); driver.EndScene(); } // Drop the device. device.Drop(); }
public Jeu() { Device = IrrlichtDevice.CreateDevice( DriverType.Direct3D9, new Dimension2Di(800, 600), 32, false, false, true); Device.SetWindowCaption("Canardstein 3D"); Device.OnEvent += Evenement; Audio = new ISoundEngine(); for (int i = 0; i < 3; i++) { TexturePistolet[i] = Device.VideoDriver.GetTexture(@"Textures\pistolet" + i.ToString() + ".png"); } for (int i = 0; i < 7; i++) { TextureGarde[i] = Device.VideoDriver.GetTexture(@"Textures\nazi" + i.ToString("00") + ".png"); } TextureMur = Device.VideoDriver.GetTexture(@"Textures\mur.png"); TextureMurDeco = Device.VideoDriver.GetTexture(@"Textures\mur_deco.png"); TextureSol = Device.VideoDriver.GetTexture(@"Textures\sol.png"); TexturePlafond = Device.VideoDriver.GetTexture(@"Textures\plafond.png"); TexturePolice = Device.VideoDriver.GetTexture(@"Textures\police.png"); using (Bitmap carte = (Bitmap)System.Drawing.Image.FromFile(@"Textures\carte.png")) { for (int x = 0; x < 32; x++) { for (int y = 0; y < 32; y++) { System.Drawing.Color col = carte.GetPixel(x, y); Murs[x, y] = false; if ((col.R == 255) && (col.G == 255) && (col.B == 255)) { SceneNode cube = Device.SceneManager.AddCubeSceneNode(1, null, 0, new Vector3Df(x, 0, y)); cube.SetMaterialFlag(MaterialFlag.Lighting, false); cube.SetMaterialFlag(MaterialFlag.Fog, true); cube.SetMaterialTexture(0, TextureMur); Murs[x, y] = true; } else if ((col.R == 0) && (col.G == 0) && (col.B == 255)) { SceneNode cube = Device.SceneManager.AddCubeSceneNode(1, null, 0, new Vector3Df(x, 0, y)); cube.SetMaterialFlag(MaterialFlag.Lighting, false); cube.SetMaterialFlag(MaterialFlag.Fog, true); cube.SetMaterialTexture(0, TextureMurDeco); Murs[x, y] = true; } } } } //On va maintenant créer un plancher et un plafond, histoire de mettre un peu d'ordre. On commence par générer meshSol, //le modèle 3D d'un plan que nous allons nommer « plan » (parce qu'il est obligatoire de fournir un nom), divisé en 32 × 32 cases de taille 1 × 1. //On ne va pas lui assigner de texture pour le moment (null). Ce plan sera parfaitement horizontal, avec 0 × 0 variation de hauteur 0. //Enfin, la texture sera répétée 32 fois horizontalement et verticalement. Mesh meshSol = Device.SceneManager.AddHillPlaneMesh("plan", new Dimension2Df(1, 1), new Dimension2Di(32, 32), null, 0, new Dimension2Df(0, 0), new Dimension2Df(32, 32)); //On désactive l'éclairage dynamique sur notre nouvel objet, comme on l'avait déjà fait pour le cube, et on lui assigne TextureSol. //Puis on le positionne à une distance d'une demi-unité sous le zéro (ou, plus précisément, sous les pieds de notre héros, la position Y de la caméra étant de 0), //et à −15.5 sur les axes X et Z (notre plan mesurant 32 par 32 cases de largeur 1, le déplacer de 15,5 unités permet de s'assurer que l'un de ses coins sera à la coordonnée 0,0, //ce sera utile dans la prochaine leçon). MeshSceneNode sol = Device.SceneManager.AddMeshSceneNode(meshSol); sol.SetMaterialFlag(MaterialFlag.Lighting, false); sol.SetMaterialFlag(MaterialFlag.Fog, true); sol.SetMaterialTexture(0, TextureSol); sol.Position = new Vector3Df(15.5f, -0.5f, 15.5f); //Pareil pour le plafond, sauf qu'on le place à une hauteur de 0,5 (et non de −0,5), et qu'on le pivote de 180 sur l'axe X pour le tourner vers le bas. MeshSceneNode plafond = Device.SceneManager.AddMeshSceneNode(meshSol); plafond.SetMaterialFlag(MaterialFlag.Lighting, false); plafond.SetMaterialFlag(MaterialFlag.Fog, true); plafond.SetMaterialTexture(0, TexturePlafond); plafond.Position = new Vector3Df(15.5f, 0.5f, 15.5f); plafond.Rotation = new Vector3Df(180, 0, 0); CameraSceneNode camera = Device.SceneManager.AddCameraSceneNode(null, new Vector3Df(1, 0, 1), new Vector3Df(2, 0, 1)); //Abaisse la distance minimum d'affichage de la caméra. La valeur par défaut est de 1, ce qui ne nous convient pas : //plafond et sol se trouvant à 0,5 unité de la caméra, ils seraient trop près pour être dessinés. camera.NearValue = 0.1f; AjouterChose <Ennemi>(3, 3); AjouterChose <Ennemi>(6, 12); AjouterChose <Ennemi>(12, 12); AjouterChose <Ennemi>(20, 6); AjouterChose <Ennemi>(28, 16); AjouterChose <Ennemi>(11, 27); Device.VideoDriver.Fog = new Fog(new IrrlichtLime.Video.Color(138, 125, 81, 0), FogType.Linear, 0, 10); ParticleSystemSceneNode part = Device.SceneManager.AddParticleSystemSceneNode(false, null, 0, new Vector3Df(29, -.5f, 30)); part.SetMaterialFlag(MaterialFlag.Lighting, false); ParticleEmitter em = part.CreateBoxEmitter( new AABBox(-.5f, 0, -.5f, .5f, 0, .5f), new Vector3Df(0.0f, 0.0015f, 0.0f), 20, 24, new IrrlichtLime.Video.Color(0, 128, 255, 0), new IrrlichtLime.Video.Color(0, 255, 255, 0), 800, 800, 0, new Dimension2Df(0.005f, 0.04f), new Dimension2Df(0.01f, 0.08f)); part.Emitter = em; while (Device.Run()) { float tempsEcoule = (Device.Timer.Time - DerniereFrame) / 1000f; DerniereFrame = Device.Timer.Time; if ((AlphaSang > 0) && (Vies > 0)) { AlphaSang = Math.Max(0, AlphaSang - tempsEcoule * 500); } if (((int)camera.Position.X == 29) && ((int)camera.Position.Z == 30)) { Device.Close(); } if (Device.CursorControl.Position.X != 400) { Rotation += (Device.CursorControl.Position.X - 400) * 0.0025; Device.CursorControl.Position = new Vector2Di(400, 300); VecteurAvant = new Vector3Df((float)Math.Cos(Rotation), 0, -(float)Math.Sin(Rotation)); VecteurDroite = VecteurAvant; VecteurDroite.RotateXZby(-90); } for (int i = 0; i < Choses.Count; i++) { Choses[i].MiseAJour(tempsEcoule, camera); } Vector3Df vitesse = new Vector3Df(); if (K_Avant) { vitesse += VecteurAvant; } else if (K_Arriere) { vitesse -= VecteurAvant; } if (K_Gauche) { vitesse -= VecteurDroite; } else if (K_Droite) { vitesse += VecteurDroite; } vitesse = vitesse.Normalize() * tempsEcoule * 2; if (TenterMouvement(camera, vitesse) || TenterMouvement(camera, new Vector3Df(vitesse.X, 0, 0)) || TenterMouvement(camera, new Vector3Df(0, 0, vitesse.Z))) { camera.Target = camera.Position + VecteurAvant; } Device.VideoDriver.BeginScene(ClearBufferFlag.Color | ClearBufferFlag.Depth, IrrlichtLime.Video.Color.OpaqueMagenta); Device.SceneManager.DrawAll(); Device.VideoDriver.Draw2DImage(TexturePistolet[FramePistolet], new Recti(new Vector2Di(250, 300), new Dimension2Di(300, 300)), new Recti(0, 0, 512, 512), null, COULEUR_BLANC, true); DessinerHUD(); Device.VideoDriver.EndScene(); if (FramePistolet > 0) { ProchaineFramePistolet -= tempsEcoule; if (ProchaineFramePistolet <= 0f) { FramePistolet++; if (FramePistolet > 2) { FramePistolet = 0; } ProchaineFramePistolet = 0.1f; } } } }