1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace bla
{
public partial class Form1 : Form
{
// Attributs DirectX
private Device dev = null;
private VertexBuffer vbuf = null;
// Attributs de la vue
private float x;
private float y;
private float z;
private float angleX;
private float angleY;
// Attributs des periphériques
private float indiceDeZoom = 0.5f;
Microsoft.DirectX.DirectInput.Device mouseInputDevice = null;
Microsoft.DirectX.DirectInput.Device keyboardInputDevice = null;
// CONSTRUCTEUR de la CLASSE
public Form1()
{
InitializeComponent();
this.InitCoordonnees();
this.InitD3D();
this.InitPeripherique();
this.CreerElement3D();
}
// PROPRIETIES de la CLASSE
public float X
{
get { return x; }
set { x = value; }
}
public float Y
{
get { return y; }
set { y = value; }
}
public float Z
{
get { return z; }
set { z = value; }
}
public float AngleX
{
get { return angleX; }
set { angleX = value; }
}
public float AngleY
{
get { return angleY; }
set { angleY = value; }
}
public float IndiceDeZoom
{
get { return indiceDeZoom; }
set { indiceDeZoom = value; }
}
public Environnement Env
{
get { return env; }
set { env = value; }
}
public void InitCoordonnees()
{
this.X = -1f;
this.Y = -1f;
this.Z = -1f;
this.AngleX = 0f;
this.AngleY = 0f;
}
// GESTION de la partie DIRECTX de la CLASSE
public void InitD3D()
{
// On commence par définir le mode graphique que l'on veut utiliser.
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true; // on active le mode fenêtré.
presentParams.SwapEffect = SwapEffect.Discard; // Bien pour le debug (rempli les buffer par du bruit)
this.dev = new Device(0, // Numéro de votre carte graphique
DeviceType.Hardware, // Gestion matériel (par la carté graphique) ou logiciel du device.
this, // Ou afficher
CreateFlags.SoftwareVertexProcessing, // Mode d'affichage des Vertexes
presentParams); // Paramêtre du device.
// Ensuite on peut modifier les propriétées du rendu du device :
this.dev.RenderState.Lighting = false; // On désactive le système de lumière pour simplifier le rendu 3D.
// Nous avons vu dans le tutoriel précédent que les surfaces
// n'étaient visibles que d'un côté, selon l'ordre dans lequel
// on donne les coordonnées des vertices.
// On peut obliger l'affichage des 2 côtés grâce à ce paramêtre :
this.dev.RenderState.CullMode = Cull.None;
}
public void CreerElement3D()
{
// Initialisation du biffer de vertices :
this.vbuf = new VertexBuffer(typeof(CustomVertex.PositionOnly), // Type des vertices, on ne précisera que leur position. (d'autres formats seront vus dans les autres tutoriels).
6, // Nombre de vertices dans le buffer.
this.dev, // Device utilisé.
Usage.WriteOnly, // On utilise le buffer en mode écriture seulement.
CustomVertex.PositionOnly.Format, // On donne le format d'un vertice.
Pool.Managed); // Comment est géré la mémoire : code managé pour plus de sécurité.
// Récupère le tableau de vertices du buffer.
Array tmpArray = vbuf.Lock(0, LockFlags.Discard);
// On créé un tableau de vertices pour les mettre das le buffer :
CustomVertex.PositionOnly[] verts = (CustomVertex.PositionOnly[])tmpArray;
//ajout repere X
verts[0].Position = new Vector3(0f, 0f, 0f);
verts[1].Position = new Vector3(30f, 0f, 0f);
//ajout repere Y
verts[2].Position = new Vector3(0f, 0f, 0f);
verts[3].Position = new Vector3(0f, 30f, 0f);
//ajout repere Z
verts[4].Position = new Vector3(0f, 0f, 0f);
verts[5].Position = new Vector3(0f, 0f, 30f);
this.vbuf.Unlock(); // On libère le buffer de vertices.
}
public void DessinerMesh3DGenerique(float x, float y, float z, Color couleur)
{
this.dev.Transform.World = Matrix.Multiply(Matrix.Translation(x * this.IndiceDeZoom + this.X, y + this.Y, z * this.IndiceDeZoom + this.Z), Matrix.RotationY(this.AngleX));
Mesh meme = Mesh.Box(this.dev, 0.2f * this.IndiceDeZoom, 0.2f * this.IndiceDeZoom, 0.2f * this.IndiceDeZoom);// on crée un mesh qui correspondra a une box
Material mat = new Material();// on crée un matérial qui correspondra à ce qui va recouvrir la box
mat.Ambient = couleur; //on affecte au material qui va recouvrir la box la couleur de la boules concernée
mat.Diffuse = couleur;// pareil pour la couleur diffuse
this.dev.Material = mat;
Console.WriteLine("{0},{1},{2}",x * this.IndiceDeZoom + this.X, y + this.Y, z * this.IndiceDeZoom + this.Z);
meme.DrawSubset(0);
}
public void Render()
{
// On commence par tout vider et mettre du bleu en fond.
this.dev.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
this.dev.BeginScene();
this.dev.SetStreamSource(0, this.vbuf, 0);
this.dev.VertexFormat = CustomVertex.PositionColored.Format;
//lumiere();
this.dev.DrawPrimitives(PrimitiveType.LineList, 0, 3);
// On applique la matrice à notre monde.
//this.dev.Transform.World = Matrix.Multiply(matrixTranslation, matrixRotation);
//DessinerMesh3DGenerique(0f, 0f, 0f, Color.White);
DessinerMesh3DGenerique(10f, 0f, 10f, Color.Brown);
AjustementVue();
this.dev.EndScene();
this.dev.Present(); // Lancer l'affichage
}
private void lumiere()
{
this.dev.RenderState.Lighting = true;// onactive les lumières
this.dev.Lights[0].Type = LightType.Directional;// on cré la première lumière de type directionnel
this.dev.Lights[0].Diffuse = System.Drawing.Color.White;// on definit la couleur de la lumière
this.dev.Lights[0].Direction = new Vector3(1, 0, 0);
//device.Lights[0].Commit();// on fat savoir au device l'éxistence des lumières
this.dev.Lights[0].Enabled = true;// on active la lumière 0'
}
public void AjustementVue()
{
// On précise comment est-ce que la caméra (la matrice View) regarde la scène :
this.dev.Transform.View = Matrix.LookAtLH(
new Vector3(0f, 0f, -4f), // position de la caméra.
new Vector3(0f, 0f, 0f), // la direction du regard (l'origine où est centré le rectangle)
new Vector3(0f, 1f, 0f)); // le vecteur directeur du regard (en face de soi)
// Enfin, on applique une perspective à tout ca pour avoir notre rendu 2D :
this.dev.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0F, 1.0F, 1000.0F);
}
// GESTION de la SOURIS et du CLAVIER
public void InitPeripherique()
{
// On prend le contrôle de la souris.
mouseInputDevice = new Microsoft.DirectX.DirectInput.Device(Microsoft.DirectX.DirectInput.SystemGuid.Mouse);
mouseInputDevice.Acquire();
Microsoft.DirectX.DirectInput.CooperativeLevelFlags coopFlags;
coopFlags = Microsoft.DirectX.DirectInput.CooperativeLevelFlags.Exclusive | Microsoft.DirectX.DirectInput.CooperativeLevelFlags.Foreground;
// On prend le contrôle du clavier.
keyboardInputDevice = new Microsoft.DirectX.DirectInput.Device(Microsoft.DirectX.DirectInput.SystemGuid.Keyboard);
// On ajuste le device de contrôle pour qu'il s'occupe de la fenêtre courante
// et on lui envoie nos paramètres.
keyboardInputDevice.SetCooperativeLevel(this, coopFlags);
}
public void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.AngleY = (float)(e.Y * (2 * Math.PI));
this.AngleX = (float)((e.X * (2 * Math.PI) / 1280) +Math.PI);
}
public void processMouse()
{
Microsoft.DirectX.DirectInput.MouseState mouseStateData = mouseInputDevice.CurrentMouseState;
// Ajuste la variable du pourcentage de redimensionnement si la roulette
// de la souris est roulée vers le haut ou vert le bas.
if ((IndiceDeZoom < 5.0f) && (mouseStateData.Z > 0))
IndiceDeZoom += 0.2f;
else if ((IndiceDeZoom > 0.2f) && (mouseStateData.Z < 0))
IndiceDeZoom -= 0.2f;
}
public void processKeyBoard()
{
try
{
keyboardInputDevice.Acquire();
Microsoft.DirectX.DirectInput.KeyboardState state = keyboardInputDevice.GetCurrentKeyboardState();
// Traitement des touches.
if (state[Microsoft.DirectX.DirectInput.Key.Escape])
// On quitte si la touche Escape est enfoncée.
Close();
else if (state[Microsoft.DirectX.DirectInput.Key.Up])
{
this.X += (float)(Math.Sin(this.AngleX) * 0.5f);
this.Z += (float)(Math.Cos(this.AngleX) * -0.5f);
}
else if (state[Microsoft.DirectX.DirectInput.Key.Down])
{
this.X -= (float)(Math.Sin(this.AngleX) * 0.5f);
this.Z -= (float)(Math.Cos(this.AngleX) * -0.5f);
}
else if (state[Microsoft.DirectX.DirectInput.Key.Left])
{
this.X += (float)(Math.Cos(this.AngleX) * 0.1f);
this.Z -= (float)(Math.Sin(this.AngleX) * -0.1f);
}
else if (state[Microsoft.DirectX.DirectInput.Key.Right])
{
this.X -= (float)(Math.Cos(this.AngleX) * 0.1f);
this.Z += (float)(Math.Sin(this.AngleX) * -0.1f);
}
}
catch { }
}
}
} |
Partager