VertexBuffer creation in December 2005 SDK (C#)

Started by
16 comments, last by Proudest 18 years, 4 months ago
Quote:Original post by remigius
Alternatively you can use System.Runtime.InteropServices.Marshal.SizeOf() to the same effect.


Thanks, I think it will work now that I passed the sizeInBytes as

"4 * Marshal.SizeOf(new PositionColored()"

No compiler/runtime errors, I just need to attempt to render it now.
Advertisement
The polygons either aren't rendering or aren't visible, I don't know why. Maybe the vertex buffer isn't being loaded from the graphics buffer correctly. Here's the relevant code:

using System;using System.Drawing;using System.Windows.Forms;using Microsoft.DirectX;using Microsoft.DirectX.Direct3D;using Microsoft.DirectX.Generic;using Microsoft.DirectX.Direct3D.CustomVertex;using System.Runtime.InteropServices;class Program : Form{	Device device = null;	VertexBuffer vb = null;		public void Init()	{		PresentParameters pp = new PresentParameters();		pp.IsWindowed = true;		pp.SwapEffect = SwapEffect.Discard;		pp.AutoDepthStencilFormat = DepthFormat.D16;		pp.EnableAutoDepthStencil = true; 				device = new Device(0, DeviceType.Hardware, this.Handle, CreateFlags.HardwareVertexProcessing, pp);		device.Transform.World = Matrix.Identity;		device.Transform.View = Matrix.Identity;		float aspect = (float)ClientSize.Width / (float)ClientSize.Height;		device.Transform.Projection = Matrix.PerspectiveFieldOfViewLeftHanded((float)Math.PI / 3.0f, aspect, 0.1f, 1000.0f);		device.RenderState.Lighting = false;		device.RenderState.FillMode = FillMode.Solid;		device.RenderState.CullMode = Cull.None;  		device.VertexFormat = PositionColored.Format;						vb = new VertexBuffer(device, 6 * Marshal.SizeOf(new PositionColored()), Usage.WriteOnly, PositionColored.Format, Pool.Managed, vb_Created);		vb.Created += new EventHandler(vb_Created);		vb_Created(vb, null);	}		void vb_Created(object sender, EventArgs e)	{		VertexBuffer vb = (VertexBuffer)sender;		PositionColored[] vertices = new PositionColored[6];		vertices[0] = new PositionColored(-1.0f, -1.0f, 5.0f, Color.Red.ToArgb());		vertices[1] = new PositionColored(-1.0f, 1.0f, 5.0f, Color.Yellow.ToArgb());		vertices[2] = new PositionColored(1.0f, -1.0f, 5.0f, Color.Green.ToArgb());		vertices[3] = new PositionColored(-1.0f, 1.0f, 5.0f, Color.Violet.ToArgb());		vertices[4] = new PositionColored(1.0f, 1.0f, 5.0f, Color.Blue.ToArgb());		vertices[5] = new PositionColored(1.0f, -1.0f, 5.0f, Color.Orange.ToArgb()); 		GraphicsBuffer<PositionColored> gb = vb.Lock<PositionColored>(0, 6, LockFlags.None);		gb.Write(vertices);		vb.Unlock();	}		void renderFrame()	{		device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.LightBlue, 1.0f, 0);		device.BeginScene();		device.SetStreamSource(0, vb, 0);		device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);		device.EndScene();		device.Present();	}}


I can't find anything wrong with this code. I also don't have any comparison, because I can't find any .NET 2.0 documentation for this SDK. If anyone can test this code with .Net 2.0 and the latest SDK and help me find the problem, it would be appreciated.
device.SetStreamSource(0, vb, 0);


That's the overload I used. Use the 4 argument overload, dunno what's wrong with this one.
Sirob Yes.» - status: Work-O-Rama.
First off, I think it would be more elegant to use:
Marshal.SizeOf( typeof(PositionColored) )
...instead of passing it a new instance of the actual vertex type.

As for your triangles not rendering, it's tough to just off the top of my head judge if you've set everything you need to, but I'd suggest trying it with TransformedColored vertices first, which will give you an indication of whether the problem is with your rendering code or the way you have the transformation pipeline set up.
Quote:Original post by sirob
device.SetStreamSource(0, vb, 0);


That's the overload I used. Use the 4 argument overload, dunno what's wrong with this one.


I was having this same problem. But as sirob says, using the 4 argument overload works:
device.SetStreamSource( 0, vb, 0, TransformedColored.StrideSize );

Quote:Original post by chadmv
Quote:Original post by sirob
device.SetStreamSource(0, vb, 0);


That's the overload I used. Use the 4 argument overload, dunno what's wrong with this one.


I was having this same problem. But as sirob says, using the 4 argument overload works:
device.SetStreamSource( 0, vb, 0, TransformedColored.StrideSize );


Thanks guys, that did the trick.

Quote:Thanks, I think it will work now that I passed the sizeInBytes as

"4 * Marshal.SizeOf(new PositionColored())"


Why not just doing :

4 * PositionColored.StrideSize;
Quote:Original post by Anonymous Poster
Quote:Thanks, I think it will work now that I passed the sizeInBytes as

"4 * Marshal.SizeOf(new PositionColored())"


Why not just doing :

4 * PositionColored.StrideSize;


Thanks, that's probably how it was meant to be done, I haven't used stridesize before so didn't know what it was for.

This topic is closed to new replies.

Advertisement