Jump to content
  • Advertisement
Sign in to follow this  

Managed DirectX9: Mesh's DrawSubet() causing a crash

This topic is 4418 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've seen other threads like this but it doesn't seem to help my problem. I create a device like I've done many times before...
device = new Device(0,DeviceType.Hardware,this,CreateFlags.HardwareVertexProcessing,pp);
Then here's the code I use to create a mesh, which is just a cube to be drawn as a wireframe model(just a sample has no real prupose)
mesh = new Mesh(
				12,                       // 12 faces
				8,                        // 8 vertices
				0,                        // no flags
				VertexFormats.Position,   // Position information only

			// Set up the 8 corners of the cube
			float front   = -1; 
			float back    =  1; 
			float left    = -1; 
			float right   =  1; 
			float top     =  1; 
			float bottom  = -1;

			GraphicsStream data = mesh.VertexBuffer.Lock(0,0,LockFlags.None);
			data.Write( new CustomVertex.PositionOnly(left , bottom, front) );
			data.Write( new CustomVertex.PositionOnly(right, bottom, front) );
			data.Write( new CustomVertex.PositionOnly(left , top   , front) );
			data.Write( new CustomVertex.PositionOnly(right, top   , front) );
			data.Write( new CustomVertex.PositionOnly(left , bottom, back ) );
			data.Write( new CustomVertex.PositionOnly(right, bottom, back ) );
			data.Write( new CustomVertex.PositionOnly(left , top   , back ) );
			data.Write( new CustomVertex.PositionOnly(right, top   , back ) );

			short leftbottomfront   = 0; 
			short rightbottomfront  = 1; 
			short lefttopfront      = 2; 
			short righttopfront     = 3;
			short leftbottomback    = 4;
			short rightbottomback   = 5; 
			short lefttopback       = 6;
			short righttopback      = 7; 
			short[] indices = new short[]
				lefttopfront,     lefttopback,      leftbottomback,
				leftbottomback,   leftbottomfront,  lefttopfront,
				lefttopfront,     leftbottomfront,  rightbottomfront,
				rightbottomfront, righttopfront,    lefttopfront,
				righttopback,     righttopfront,    rightbottomfront,
				rightbottomfront, rightbottomback,  righttopback,
				leftbottomback,   lefttopback,      righttopback,
				righttopback,     rightbottomback,  leftbottomback,
				righttopfront,    righttopback,     lefttopback,
				lefttopback,      lefttopfront,     righttopfront,
				leftbottomfront,  leftbottomback,   rightbottomback,
				rightbottomback,  rightbottomfront, leftbottomfront


			int[] att = mesh.LockAttributeBufferArray(LockFlags.None);
			for(int i = 0; i < 12; i++)
				att = 0;

			AttributeRange r = new AttributeRange();
			r.AttributeId = 0;
			r.FaceCount = 12;
			r.FaceStart = 0;
			r.VertexCount = 8;
			r.VertexStart = 0;

			mesh.SetAttributeTable(new AttributeRange[]{r});

Here is the code that I use to render everything
if(device != null)
				device.Clear(ClearFlags.Target | ClearFlags.ZBuffer,System.Drawing.Color.FromArgb(40,40,40),1.0f,0);



If I comment out mesh.DrawSubset(0), it will run. There isn't any matrices being using or any renderstates set because I've tried to create a mesh before with all that and it didn't work, so I looked on a bunch of tutoirals sites for different ways of creating meshes and none of them work, so I just wanted to see if this would run. I run the debugger and I get this: "An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.directx.direct3dx.dll Additional information: Object reference not set to an instance of an object." The highlighted line in the debugger is "device.EndScene();" I've even put if statements to see if device isn't null at this point but it isn't null, and that line still executes, so I have no idea whats wrong. I never had this toruble with meshes with unmanaged DirectX9. I'll appreciate the help.

Share this post

Link to post
Share on other sites
I can't see anything wrong with your code at first glance, but you could try enabling the Debug Runtimes to see if they can tell you more. Just enable them on the DirectX control panel and enable the Unmanaged Debugging option on your project's debug property sheet.

Share this post

Link to post
Share on other sites
hardware vertex processing doesn't work with old hardware, use software. If it doesn't work use validate instruction. It return a string that explain possible error.
Another possible error is vertex format.
Try to set vertexformat to position only

Share this post

Link to post
Share on other sites
The unmanaged SDK docs seem to be quite supportive of calling Optimize instead of creating the AttributeTable yourself. They also state it isn't possible to set the AttributeTable yourself, and that only optimize can do it - yet the same docs have BaseMesh::SetAttributeTable. That part might be a bit outdated [smile]

Have you tried calling Optimize(InPlace) to generate the AttributeTable for you?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!