I write this program based on Direct3D Tutorial 03 from DxSDK
but when Im run it i see only black screen. Whats is wrong ?. Thx for help.
but when Im run it i see only black screen. Whats is wrong ?. Thx for help.
Code:
/************************************************************************/
using System;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
/************************************************************************/
namespace mdx
{
class Direct3DTutor : Form
{
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
private Device device = null;
private VertexBuffer vertexBuffer = null;
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public Direct3DTutor()
{
this.ClientSize = new System.Drawing.Size(400, 300);
this.Text = "Direct3D Tutorials";
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
private bool InitializeDirect3D()
{
try
{
PresentParameters presentParams = new PresentParameters();
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.Windowed = true;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
device.DeviceCreated += new EventHandler(this.OnDeviceCreated);
device.DeviceReset += new EventHandler(this.OnDeviceReset);
this.OnDeviceCreated(device, null);
this.OnDeviceReset(device, null);
return true;
}
catch(DirectXException e)
{
MessageBox.Show("InitializeDirect3D(): " + e.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
private void OnDeviceCreated(Object sender, EventArgs e)
{
Device dev = (Device)sender;
vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 3, dev, 0, CustomVertex.PositionColored.Format, Pool.Default);
vertexBuffer.Created += new EventHandler(this.OnVertexBufferCreated);
this.OnVertexBufferCreated(vertexBuffer, null);
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
private void OnDeviceReset(Object sender, EventArgs e)
{
Device dev = (Device)sender;
dev.RenderState.CullMode = Cull.None;
dev.RenderState.Lighting = false;
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
private void OnVertexBufferCreated(Object sender, EventArgs e)
{
VertexBuffer vb = (VertexBuffer)sender;
CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3];
GraphicsStream gs = vb.Lock(0, 0, 0);
verts[0].X = 150;
verts[0].Y = 50;
verts[0].Z = 0.5f;
verts[0].Color = System.Drawing.Color.SpringGreen.ToArgb();
verts[1].X = 250;
verts[1].Y = 250;
verts[1].Z = 0.5f;
verts[1].Color = System.Drawing.Color.SkyBlue.ToArgb();
verts[2].X = 50;
verts[2].Y = 250;
verts[2].Z = 0.5f;
verts[2].Color = System.Drawing.Color.Silver.ToArgb();
gs.Write(verts);
vb.Unlock();
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
private void SetupMatrices()
{
device.Transform.World = Matrix.RotationY(Environment.TickCount / 150.0f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 3.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 / 4, 1.0f, 1.0f, 100.0f);
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
private void Render()
{
if(device == null)
return;
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
device.BeginScene();
SetupMatrices();
device.SetStreamSource(0, vertexBuffer, 0);
device.VertexFormat = CustomVertex.PositionColored.Format;
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
device.EndScene();
device.Present();
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
[STAThread]
static void Main()
{
Direct3DTutor frm = new Direct3DTutor();
if(!frm.InitializeDirect3D())
{
MessageBox.Show("Direct3D Initialization Failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
frm.Show();
while(frm.Created)
{
frm.Render();
Application.DoEvents();
}
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
}
}