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
| Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace DX
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Matrix view;
public Device device = null;
public Mesh MySphere = null;
public void InitializeGraphics()
{
// setting components size
DXpnl.Top = TopPnl.Height;
DXpnl.Left = 0;
DXpnl.Width = ClientSize.Width;
DXpnl.Height = ClientSize.Height - TopPnl.Height;
// paramètres de présentation
PresentParameters Params = new PresentParameters();
Params.Windowed = true;
Params.BackBufferFormat = Format.X8R8G8B8;
Params.AutoDepthStencilFormat = DepthFormat.D24S8;
Params.EnableAutoDepthStencil = true;
Params.BackBufferCount = 2;
Params.SwapEffect = SwapEffect.Discard;
// création du device;
device = new Device(0, DeviceType.Hardware, DXpnl.Handle,
CreateFlags.SoftwareVertexProcessing, Params);
// public static Mesh Sphere(Device , radius, slices, stacks);
MySphere = Mesh.Sphere(device, 3, 128, 128);
}
private void Render()
{
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer,
System.Drawing.Color.Black, 1.0f, 0);
device.BeginScene();
device.Transform.Projection = Matrix.PerspectiveFovLH(
(float)Math.PI/4 , 1.0f, 1.0f, 100.0f);
// Function LookAtLH ( cameraPosition, cameraTarget,cameraUpVector)
view = Matrix.LookAtLH(new Vector3(0, 0, -20),
new Vector3(0, 0, 0), new Vector3(0, 1, 0));
device.Transform.View = view;
device.RenderState.Ambient = System.Drawing.Color.White;
device.RenderState.Lighting = true;
// set Lights
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Position = new Vector3(0.0f, 0.0f, -10.0f);
device.Lights[0].Direction = new Vector3(1.0f, -0.5f, 1.0f);
device.Lights[0].Ambient = Color.White;
device.Lights[0].DiffuseColor = ColorValue.FromColor(Color.White);
device.Lights[0].SpecularColor = ColorValue.FromColor(Color.WhiteSmoke);
device.Lights[0].Enabled = true;
Material mat = new Material();
mat.Ambient = Color.Aqua;
device.Material = mat;
device.SetTexture(0, null);
MySphere.DrawSubset(0);
device.EndScene();
device.Present();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Render();
}
}
} |
Partager