using System; using System.Drawing; using System.Windows.Forms; using System.Reflection; using Microsoft.WindowsMobile.DirectX; using Microsoft.WindowsMobile.DirectX.Direct3D; using System.Runtime.InteropServices; namespace ft.fr.cube { // The main application class public class Cube : Form { // Our global variables for this project Device device = null; VertexBuffer vertexBuffer = null; PresentParameters presentParams = new PresentParameters(); Texture texture1 = null; Texture texture = null; // Class constructor public Cube() { // Set the caption // InitializeComponent(); this.InitializeGraphics(); this.creerTriangle(); this.OnResetDevice(device, null); } // Prepares the rendering device public bool InitializeGraphics() { try { // We don't want to run fullscreen presentParams.Windowed = true; // Discard the frames presentParams.SwapEffect = SwapEffect.Discard; // Turn on a Depth stencil // presentParams.EnableAutoDepthStencil = true; // And the stencil format presentParams.AutoDepthStencilFormat = DepthFormat.D16; //Create a device this.device = new Device(0, DeviceType.Default, this, CreateFlags.None, presentParams); this.device.RenderState.Lighting = false; } catch (DirectXException e) { // Catch any errors and return a failure MessageBox.Show("Erreur divce construction"+e.StackTrace); return false; } return true; } // Called whenever the rendering device is created void creerTriangle() { try { // Now Create the VB // Get the device capabilities Pool vertexBufferPool; Caps caps; caps = this.device.DeviceCaps; if (caps.SurfaceCaps.SupportsVidVertexBuffer) vertexBufferPool = Pool.VideoMemory; else vertexBufferPool = Pool.SystemMemory; this.vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured),36, this.device, Usage.DoNotClip, CustomVertex.PositionNormalTextured.Format, vertexBufferPool); // Récupère le tableau de vertices du buffer. Array tmpArray = this.vertexBuffer.Lock(0, LockFlags.None); // On créé un tableau de vertices pour les mettre das le buffer : CustomVertex.PositionNormalTextured[] verts = (CustomVertex.PositionNormalTextured[])tmpArray; verts[0].X = -0.5f; verts[0].Y = 0.5f; verts[0].Z = 0.5f;//1-++ verts[0].Tu = 0; verts[0].Tv = 0; verts[1].X = -0.5f; verts[1].Y = -0.5f; verts[1].Z = 0.5f;//0--+ verts[1].Tu = 1; verts[1].Tv = 0; verts[2].X = 0.5f; verts[2].Y = 0.5f; verts[2].Z = 0.5f;//2+++ verts[2].Tu = 0; verts[2].Tv = 1; verts[3].X = 0.5f; verts[3].Y = 0.5f; verts[3].Z = 0.5f;//2+++ verts[3].Tu = 0; verts[3].Tv = 1; verts[4].X = -0.5f; verts[4].Y = -0.5f; verts[4].Z = 0.5f;//0--+ verts[4].Tu = 1; verts[4].Tv = 0; verts[5].X = 0.5f; verts[5].Y = -0.5f; verts[5].Z = 0.5f;//3+-+ verts[5].Tu = 1; verts[5].Tv = 1; //face2 verts[6].X = -0.5f; verts[6].Y = -0.5f; verts[6].Z = 0.5f;//0--+ verts[6].Tu = 1; verts[6].Tv = 1; verts[7].X = 0.5f; verts[7].Y = -0.5f; verts[7].Z = -0.5f;//4+-- verts[7].Tu = 0; verts[7].Tv = 0; verts[8].X = 0.5f; verts[8].Y = -0.5f; verts[8].Z = 0.5f;//3+-+ verts[8].Tu = 0; verts[8].Tv = 1; verts[9].X = -0.5f; verts[9].Y = -0.5f; verts[9].Z = 0.5f;//0--+ verts[9].Tu = 1 ; verts[9].Tv = 1; verts[10].X = -0.5f; verts[10].Y = -0.5f; verts[10].Z = -0.5f;//7--- verts[10].Tu = 1; verts[10].Tv = 0; verts[11].X = 0.5f; verts[11].Y = -0.5f; verts[11].Z = -0.5f;//4+-- verts[11].Tu = 0; verts[11].Tv = 0; //ma face 3 verts[12].X = -0.5f; verts[12].Y = -0.5f; verts[12].Z = 0.5f;//0--+ verts[12].Tu = 0; verts[12].Tv = 0; verts[13].X = -0.5f; verts[13].Y = 0.5f; verts[13].Z = 0.5f;//1-++ verts[13].Tu = 1; verts[13].Tv = 0; verts[14].X = -0.5f; verts[14].Y = 0.5f; verts[14].Z = -0.5f;//6-+- verts[14].Tu = 1; verts[14].Tv = 1; verts[15].X = -0.5f; verts[15].Y = -0.5f; verts[15].Z = 0.5f;//0--+ verts[15].Tu = 0; verts[15].Tv = 0; verts[16].X = -0.5f; verts[16].Y = 0.5f; verts[16].Z = -0.5f;//6-+- verts[16].Tu = 1; verts[16].Tv = 1; verts[17].X = -0.5f; verts[17].Y = -0.5f; verts[17].Z = -0.5f;//7--- verts[17].Tu = 0; verts[17].Tv = 1; //Face 4 verts[18].X = -0.5f; verts[18].Y = 0.5f; verts[18].Z = 0.5f;//1-++ verts[18].Tu = 1; verts[18].Tv = 1; verts[19].X = 0.5f; verts[19].Y = 0.5f; verts[19].Z = -0.5f;//5++- verts[19].Tu = 0; verts[19].Tv = 0; verts[20].X = -0.5f; verts[20].Y = 0.5f; verts[20].Z = -0.5f;//6-+- verts[20].Tu = 0; verts[20].Tv = 1; verts[21].X = 0.5f; verts[21].Y = 0.5f; verts[21].Z = 0.5f;//2+++ verts[21].Tu = 1; verts[21].Tv = 0; verts[22].X = 0.5f; verts[22].Y = 0.5f; verts[22].Z = -0.5f;//5++- verts[22].Tu = 0; verts[22].Tv = 0; verts[23].X = -0.5f; verts[23].Y = 0.5f; verts[23].Z = 0.5f;////-++ verts[23].Tu = 1; verts[23].Tv = 1; //f5 verts[24].X = 0.5f; verts[24].Y = 0.5f; verts[24].Z = 0.5f;//2+++ verts[24].Tu = 0; verts[24].Tv = 0; verts[25].X = 0.5f; verts[25].Y = -0.5f; verts[25].Z = 0.5f;//3+-+ verts[25].Tu = 1; verts[25].Tv = 0; verts[26].X = 0.5f; verts[26].Y = -0.5f; verts[26].Z = -0.5f;//4+-- verts[26].Tu = 1; verts[26].Tv = 1; verts[27].X = 0.5f; verts[27].Y = 0.5f; verts[27].Z = 0.5f;//2+++ verts[27].Tu = 0; verts[27].Tv = 0; verts[28].X = 0.5f; verts[28].Y = -0.5f; verts[28].Z = -0.5f;//4+-- verts[28].Tu = 1; verts[28].Tv =1; verts[29].X = 0.5f; verts[29].Y = 0.5f; verts[29].Z = -0.5f;//5++- verts[29].Tu = 0; verts[29].Tv = 1; //f6 verts[30].X = 0.5f; verts[30].Y = -0.5f; verts[30].Z = -0.5f;//4+-- verts[30].Tu = 1; verts[30].Tv = 1; verts[31].X = -0.5f; verts[31].Y = 0.5f; verts[31].Z = -0.5f;//6-+- verts[31].Tu = 0; verts[31].Tv = 0; verts[32].X = 0.5f; verts[32].Y = 0.5f; verts[32].Z = -0.5f;//5++- verts[32].Tu = 1; verts[32].Tv = 0; verts[33].X = 0.5f; verts[33].Y = -0.5f; verts[33].Z = -0.5f;//4+-- verts[33].Tu = 1; verts[33].Tv = 1; verts[34].X = -0.5f; verts[34].Y = -0.5f; verts[34].Z = -0.5f;//7--- verts[34].Tu = 0; verts[34].Tv = 1; verts[35].X = -0.5f; verts[35].Y = 0.5f; verts[35].Z = -0.5f;//6-+- verts[35].Tu = 0; verts[35].Tv = 0; this.vertexBuffer.Unlock(); // On libère le buffer de vertices. } catch (DriverInvalidCallException e) { MessageBox.Show("Erreur "+e.StackTrace); } // this.SetupMatrices(); } // Called whenever the rendering device is reset void OnResetDevice(object sender, EventArgs e) { Device dev = (Device)sender; // Turn off culling, so we see the front and back of the triangle dev.RenderState.CullMode = Cull.None; // Turn off D3D lighting dev.RenderState.Lighting = false; // Turn on the ZBuffer dev.RenderState.ZBufferEnable = true; // Turn on perspective correction for textures // This provides a more accurate visual at the cost // of a small performance overhead dev.RenderState.TexturePerspective = true; // Now create our texture /* texture1 = TextureLoader.FromStream(dev, Assembly.GetExecutingAssembly().GetManifestResourceStream( "Texture.Content.pichy.bmp"));*/ texture = TextureLoader.FromStream(dev, Assembly.GetExecutingAssembly().GetManifestResourceStream( "Texture.Content.pichy.bmp")); } // Sets up the world, view, and projection matrices each frame // Sets up the world, view, and projection matrices each frame private void SetupMatrices() { float angle = Environment.TickCount / 1000.0F; /*device.Transform.World = Matrix.RotationYawPitchRoll(angle, angle / 1.0F, 2);*/ device.Transform.World = Matrix.RotationX(angle); device.Transform.World = Matrix.RotationY(angle); /* device.Transform.World = Matrix.RotationAxis( new Vector3(1.0f, 1, 1.0f), Environment.TickCount / 1000.0f);*/ // "up" to be in the y-direction. device.Transform.View = Matrix.LookAtLH( new Vector3(0.0f, 0.0f, -5.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f)); device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 8.0f, 1.0f, 1.0f, 100.0f); } protected void SetupMat() { float angle = Environment.TickCount / 800.0F; device.Transform.World = Matrix.RotationYawPitchRoll(angle, angle / 1.0F,1); device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0.5F, -1000), new Vector3(0, 0.5F, 0), new Vector3(0, 1, 0)); device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 1200.0F, 1.0F, 900.0F, 1100.0F); } // All rendering for each frame occurs here private void Render() { // On commence par tout vider et mettre du bleu en fond. this.device.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0); // Commencer à définir la scène this.device.BeginScene(); // SetupMatrices(); SetupMat(); // Sélection de la source (numéro de la source, ou sont les vertices à afficher, 1er vertex à afficher) device.SetTexture(0, texture); device.TextureState[0].ColorOperation = TextureOperation.SelectArg1; device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor; device.TextureState[0].AlphaOperation = TextureOperation.SelectArg1; device.TextureState[0].AlphaArgument1 = TextureArgument.TextureColor; device.TextureState[0].ResultArgument = TextureArgument.Current; /* device.SetTexture(1, texture1); device.TextureState[1].ColorOperation = TextureOperation.Modulate; device.TextureState[1].ColorArgument1 = TextureArgument.TextureColor; device.TextureState[1].ColorArgument2 = TextureArgument.Current; device.TextureState[1].AlphaOperation = TextureOperation.Disable; device.TextureState[1].ResultArgument = TextureArgument.Current;*/ /* device.SetTexture(1, texture); device.TextureState[1].ColorOperation = TextureOperation.Modulate; device.TextureState[1].ColorArgument1 = TextureArgument.TextureColor; device.TextureState[1].ColorArgument2 = TextureArgument.Diffuse; device.TextureState[1].AlphaOperation = TextureOperation.Disable;*/ this.device.SetStreamSource(0, this.vertexBuffer, 0); // On redonne à nouveau le format de vertex // this.device.VertexFormat = CustomVertex.PositionOnly.Format; // Affichage des vertices en triangle : //- TriangleList : dessine un triangle tout les couples de 3 points //- TriangleStrip : dessine le 1er triangle avec le 1er couple, puis continu en gardant le 3ème point du couple précédent et forme un couple avec les 2 suivants // Enfin on indique le Vertexe de départ, ici 0, et le nombre de primitives a cré (= nombre de triangle), ici 1 puisque l'on a 3 vertexes. this.device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12); this.device.EndScene(); // Fin de la définition this.device.Present(); // Lancer l'affichage } // Called when the window needs to be repainted protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { // Render on painting this.Render(); // Render again this.Invalidate(); } // Called when the window background needs to be repainted protected override void OnPaintBackground( System.Windows.Forms.PaintEventArgs e) { // Don't paint the background } // Handles any keyboard keys which have been pressed protected override void OnKeyPress( System.Windows.Forms.KeyPressEventArgs e) { // Esc was pressed if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape) this.Close(); } /// /// The main entry point for the application. /// static void Main() { // Initialize Direct3D Cube f = new Cube(); f.Show(); Application.Run(f); } } }