I started learning managed directx 9 two days ago from sams book(Managed DirectX 9 Kick Start : Graphics and Game Programming)in C#,and i was going through the simple rendering techniqes chapter which is using a vertex buffer to draw a triangle,i got my code all right but the triangle never appeared,i compiled the sample in the book's Cd and it worked fine :blink: :blink:
here is my Code:
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 From_the_Graphics_Book_Ch3
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private Device dev = null;
private VertexBuffer vb = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.ClientSize = new Size(800,600);
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque,true);
}
public void InitializeGraphics()
{
PresentParameters param = new PresentParameters();
param.Windowed = true;
param.SwapEffect = SwapEffect.Discard;
dev = new Device(0 , DeviceType.Hardware,this,CreateFlags.SoftwareVertexProcessing,
param);
vb = new VertexBuffer(typeof(CustomVertex.PositionColored),3,dev,Usage.Dynamic |
Usage.WriteOnly,CustomVertex.PositionColored.Format,Pool.Default);
vb.Created += new EventHandler(vb_Created);
vb_Created(vb,null);
dev.RenderState.Lighting = false;
}
public void vb_Created(object sender,EventArgs e)
{
VertexBuffer buffer = (VertexBuffer)sender;
CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3];
verts[0].Position = new Vector3(400.0f,300.0f,1.0f);
verts[0].Color = System.Drawing.Color.Red.ToArgb();
verts[1].Position = new Vector3(400.0f,300.0f,1.0f);
verts[1].Color = System.Drawing.Color.HotPink.ToArgb();
verts[2].Position = new Vector3(400.0f,300.0f,1.0f);
verts[2].Color = System.Drawing.Color.Firebrick.ToArgb();
buffer.SetData(verts,0,LockFlags.None);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
using (Form1 frm = new Form1())
{
frm.Show();
frm.InitializeGraphics();
Application.Run(frm);
}
}
private void OnPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
dev.Clear(ClearFlags.Target,System.Drawing.Color.Black,1.0f,0);
dev.BeginScene();
dev.VertexFormat = CustomVertex.PositionColored.Format;
dev.SetStreamSource(0,vb,0);
dev.DrawPrimitives(PrimitiveType.TriangleList,0,1);
dev.EndScene();
dev.Present();
this.Invalidate();
}
}
}











