Sign in to follow this  

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

This topic is 4108 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
				device);                  


			// 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 ) );
			mesh.VertexBuffer.Unlock();

			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
		   };

			mesh.IndexBuffer.SetData(indices,0,LockFlags.None);

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

			mesh.UnlockAttributeBuffer();
			
			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);
				device.BeginScene();

				mesh.DrawSubset(0);

				device.EndScene();
				device.Present();
			}

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

This topic is 4108 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this