| Poster | : Anonymous | | Posts | : | | Country | : | | City | : |
| | | | Posted by Anonymous on 07/03/2011 at 05:14:57
| | Here's the codes:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace _DGame
{
public partial class DirectXForm : Form
{
private int WIDTH = 4;
private int HEIGHT = 3;
private Device device;
private System.ComponentModel.Container components = null;
private float angle = 0f;
private CustomVertex.PositionColored[] vertices;
private int[,] heightData;
private int[] indices;
private IndexBuffer ib;
private VertexBuffer vb;
public DirectXForm()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
}
public void InitializeDevice()
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
device.RenderState.FillMode = FillMode.WireFrame;
device.RenderState.CullMode = Cull.None;
}
public void CameraPositioning()
{
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 50f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 15), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
device.RenderState.Lighting = false;
device.RenderState.CullMode = Cull.None;
}
public void VertexDeclaration()
{
vb = new VertexBuffer(typeof(CustomVertex.PositionColored), WIDTH * HEIGHT, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
vertices = new CustomVertex.PositionColored[WIDTH * HEIGHT];
for (int x = 0; x < WIDTH; x++)
{
for (int y = 0; y < HEIGHT; y++)
{
vertices[x + y * WIDTH].Position = new Vector3(x, y, heightData[x, y]);
vertices[x + y * WIDTH].Color = Color.White.ToArgb();
}
}
vb.SetData(vertices, 0, LockFlags.None);
}
public void IndicesDeclaration()
{
ib = new IndexBuffer(typeof(int), (WIDTH - 1) * (HEIGHT - 1) * 6, device, Usage.WriteOnly, Pool.Default);
indices = new int[(WIDTH - 1) * (HEIGHT - 1) * 6];
for (int x = 0; x < WIDTH - 1; x++)
{
for (int y = 0; y < HEIGHT - 1; y++)
{
indices[(x + y * (WIDTH - 1)) * 6] = (x + 1) + (y + 1) * WIDTH;
indices[(x + y * (WIDTH - 1)) * 6 + 1] = (x + 1) + y * WIDTH;
indices[(x + y * (WIDTH - 1)) * 6 + 2] = x + y * WIDTH;
indices[(x + y * (WIDTH - 1)) * 6 + 3] = (x + 1) + (y + 1) * WIDTH;
indices[(x + y * (WIDTH - 1)) * 6 + 4] = x + y * WIDTH;
indices[(x + y * (WIDTH - 1)) * 6 + 5] = x + (y + 1) * WIDTH;
}
}
ib.SetData(indices, 0, LockFlags.None);
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0);
device.BeginScene();
device.VertexFormat = CustomVertex.PositionColored.Format;
device.SetStreamSource(0, vb, 0);
device.Indices = ib;
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, WIDTH * HEIGHT, 0, indices.Length / 3);
device.EndScene();
device.Present();
this.Invalidate();
angle += 0.05f;
}
public void LoadHeightData()
{
heightData = new int[4, 3];
heightData[0, 0] = 0;
heightData[1, 0] = 0;
heightData[2, 0] = 0;
heightData[3, 0] = 0;
heightData[0, 1] = 1;
heightData[1, 1] = 0;
heightData[2, 1] = 2;
heightData[3, 1] = 2;
heightData[0, 2] = 2;
heightData[1, 2] = 2;
heightData[2, 2] = 4;
heightData[3, 2] = 2;
}
}
}
namespace _DGame
{
partial class DirectXForm
{
/// <summary>
/// Required designer variable.
/// </summary>
// private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(500, 500);
this.Text = "DirectX Tutorial";
}
#endregion
}
} | |
|
|