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
|
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace Projet3A
{
/// <summary>
/// Description résumée de fenetre.
/// </summary>
public class Fenetre : Form
{
#region Donnée membres
private PresentParameters presentParams;
private Device dc;//"Lien" sur la carte graphique
private Matrix matProj;
private Matrix matVue;
private bool fullScreen = false;//plein ecran ou non
private bool dcPerdu = false;//dc perdu ou non
#endregion
#region Propriétés
public Device D3DDevice
{
get { if(dc != null) { return dc; }
else { return null; }
}
}
public Matrix MatVue { get { return matVue; } }
public Matrix MatProj { get { return matProj; } }
#endregion
#region Methodes
/// <summary>
/// Constructeur
/// </summary>
public Fenetre()
{
Width = 800;
Height = 600;
Text = "Projet de 3eme année";
//Message box pour savoir ce si on lance en plein ecran ou non
if(MessageBox.Show("Fullscreen?", "Fullscreen", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
fullScreen = true;
}
AdapterInformation adapterInfo = Manager.Adapters[0]; //Recuperation des infos sur la carte graphique
presentParams = new PresentParameters();
presentParams.BackBufferCount = 1; //Nombre de backbuffers à creer
presentParams.BackBufferFormat = adapterInfo.CurrentDisplayMode.Format; //Format courrant de la carte graphique
presentParams.BackBufferHeight = this.Height;
presentParams.BackBufferWidth = this.Width;
presentParams.SwapEffect = SwapEffect.Discard; //Le systeme fait ne garde pas le backbuffer apres utilisation
presentParams.AutoDepthStencilFormat = DepthFormat.D24S8; //24 bits pour la profondeur et 8 bits pour le pinceau
presentParams.EnableAutoDepthStencil = true; //Direct3D gere tout seul le tampon de profondeur
presentParams.Windowed = true;//fenetré
if(fullScreen == true)
{
Cursor.Hide();//on cache le cursor
presentParams.Windowed = false; //en plein ecran
presentParams.BackBufferWidth = this.Width;
presentParams.BackBufferHeight = this.Height;
presentParams.PresentationInterval = PresentInterval.Immediate;
presentParams.FullScreenRefreshRateInHz = Manager.Adapters[0].CurrentDisplayMode.RefreshRate;
}
dc = new Device(0, DeviceType.Hardware, this.Handle, CreateFlags.SoftwareVertexProcessing, presentParams);
// Matrice perspective
matProj = Matrix.PerspectiveFovLH((float)Math.PI/4, 1.0f, 1.0f, 1000.0f);
//Caméra (MAtrice Vue)
matVue = Matrix.LookAtLH(new Vector3(0.0f, 0.0f, -4.0f),
new Vector3(0.0f, 0.0f, 0.0f),
new Vector3(0.0f, 1.0f, 0.0f));
dc.Transform.Projection = matProj;
dc.Transform.View = matVue;
//Format des sommets en espace objet
dc.VertexFormat = CustomVertex.PositionNormalColored.Format;
// Utiliser une lumière directionnelle blanche provenant de derrière notre épaule droite
dc.Lights[0].Diffuse = Color.White;
dc.Lights[0].Type = LightType.Directional;
dc.Lights[0].Direction = new Vector3(-3, -1, 3);
dc.Lights[0].Update();
dc.Lights[0].Enabled = true;
dc.RenderState.CullMode = Cull.None;
dc.RenderState.Lighting = false;
// Rajoutons une petite lumière ambiente à la scène
dc.RenderState.Ambient = Color.FromArgb(0x40, 0x40, 0x40);
//Evenement pour le resizing
dc.DeviceResizing += new System.ComponentModel.CancelEventHandler(this.CancelResize);
}
/// <summary>
/// On "annule" l'evenement qui est genere lors d'un resize
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CancelResize(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
}
/// <summary>
/// Fonction permettant de restaure le device en cas de perte
/// </summary>
private void Recover()
{
try
{
dc.TestCooperativeLevel();
}
catch(DeviceLostException)
{
}
catch(DeviceNotResetException)
{
try
{
dc.Reset(presentParams);
dcPerdu = false;
}
catch(DeviceLostException)
{
}
}
}
/// <summary>
/// Initialisation du dessin
/// </summary>
/// <param name="bgColor"></param>
public void BeginRender(Color bgColor)
{
if(dcPerdu)
Recover();
if(dcPerdu)
return;
if(dc != null)
{
dc.Clear(ClearFlags.Target | ClearFlags.ZBuffer | ClearFlags.Stencil, bgColor, 1.0f, 0);
dc.BeginScene();
DessinerCube();
}
}
/// <summary>
/// Fin du dessin
/// </summary>
public void FinishRender()
{
if(dcPerdu)
return;
dc.EndScene();
try
{
dc.Present();
}
catch(DeviceLostException)
{
dcPerdu = true;
}
}
/// <summary>
/// DiposeDevice
/// </summary>
public void DisposeDevice()
{
if(dc != null)
{
dc.Dispose();
}
}
/// <summary>
/// Gestion des interruptions clavier
/// </summary>
/// <param name="e"></param>
protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
switch(e.KeyCode)
{
case Keys.F5:
presentParams.Windowed=!presentParams.Windowed;
dc.Reset(presentParams);
break;
case Keys.Escape:
this.Close();
break;
default:
base.OnKeyDown(e);
break;
}
}
/// <summary>
/// Dessin du cube
/// </summary>
public void DessinerCube()
{
Cube3D cub=new Cube3D();
dc.SetStreamSource(0, cub.CreerCube(dc), 0);
//Comment il doit le dessiner (ici en triangle)
dc.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
}
#endregion
}
} |
Partager