Why doesn't this work?

Started by
1 comment, last by centipede 18 years, 7 months ago
I made my hello quad code, but it doesn't draw anything but garbage. Can someone help me figure out why? I'm sure it's something very simple and stupid, I'm not advanced enough to screw up badly ;) Tips and criticism welcome. -Sandra
[source lang=c#]
public class GamePanel : Form
{
private Device device;
private VertexBuffer vertices;
private Texture texture;
protected PresentParameters presentParams = new PresentParameters();

public GamePanel()
{
SetStyle(ControlStyles.Opaque,true); // don't paint background

Initialize();
device.VertexFormat = CustomVertex.PositionTextured.Format;
texture = new Texture(device, new Bitmap(Image.FromFile(@"M:\Images\5.jpg")), Usage.Dynamic, Pool.Default);
}

public static void Main()
{
using(GamePanel game = new GamePanel())
{
game.Show();
game.Render();
Application.Run(game);
}
}

protected override void OnPaint(PaintEventArgs e)
{
// Since we don't use/need a render loop, just render when we need to paint the window
Render(); 
}

private void Initialize()
{
presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.DeviceWindow = this;
presentParams.PresentationInterval = PresentInterval.One;
presentParams.SwapEffect = SwapEffect.Discard;

// store our default adapter
int adapterOrdinal = Manager.Adapters.Default.Adapter;
// get our device capabilities so we can check
Caps caps = Manager.GetDeviceCaps(adapterOrdinal, DeviceType.Hardware);
CreateFlags createFlags; 

if(caps.DeviceCaps.SupportsHardwareTransformAndLight)
createFlags = CreateFlags.HardwareVertexProcessing;
else
createFlags = CreateFlags.SoftwareVertexProcessing; 

if(caps.DeviceCaps.SupportsPureDevice)
createFlags |= CreateFlags.PureDevice;

// create our device
device = new Device(adapterOrdinal, DeviceType.Hardware, this.Handle, createFlags, presentParams);

SetupDevice();
SetupCamera();
SetupVertices();
}

private void SetupDevice()
{
device.RenderState.CullMode = Cull.None;
device.RenderState.Lighting = false;
}

private void SetupCamera()
{
device.Transform.Projection = Matrix.OrthoLH(Width, Height, 0.0f, 1.0f);
device.Transform.World = Matrix.Identity;
device.Transform.View = Matrix.Identity;
}

private void SetupVertices()
{
vertices = new VertexBuffer(typeof(CustomVertex.PositionTextured),
4, device, 0, CustomVertex.PositionTextured.Format, Pool.Default);

CustomVertex.PositionTextured [] quad = (CustomVertex.PositionTextured[]) vertices.Lock(0, 0);

// Bottom Left
quad[0] = new CustomVertex.PositionTextured(-1, -1, 0, 0,1);
// Top Left
quad[1] = new CustomVertex.PositionTextured(-1, 1, 0, 0,0);
// Bottom Right
quad[2] = new CustomVertex.PositionTextured(1, -1, 0, 1,1);
// Top Right
quad[3] = new CustomVertex.PositionTextured(1, 1, 0, 1,0);

vertices.Unlock();
}

protected void Render()
{
device.BeginScene();

// Draw the quads
device.SetTexture(0, texture);

device.SetStreamSource(0, vertices, 0);
device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);

device.EndScene();
device.Present();
}
} // end class

Advertisement
I don't know much about C# or managed DirectX, but I think you're forgetting to clear the back buffer and depth buffer...

If that doesn't help/is not the problem then can you explain what kind of garbage are you getting?
Like Kamikaze15 said, you're missing Clear() from the beginning of the frame. Also, you're not setting correct FVF. Insert this line before device.SetStreamSource():

device.FVF = CustomVertex.PositionTextured.Format;

This topic is closed to new replies.

Advertisement