Sign in to follow this  

cant show bounding box around my charachter. code included

This topic is 4730 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

Ok, im trying to disply the bounding box around my character in directX 9 and c#. After I load the mesh I use this method to to get the bounding box:
public void CalculateBoundingBox()
		{
		flag = mesh.Options.Use32Bit ? MeshFlags.Use32Bit : 0;
			vBuffer = mesh.VertexBuffer;
			Vector3 max = new Vector3();
			Vector3 min = new Vector3();

			//lock vBuffer
			GraphicsStream vertexData = vBuffer.Lock(0,0,LockFlags.NoSystemLock);
			Geometry.ComputeBoundingBox(vertexData,mesh.NumberVertices,mesh.VertexFormat,out min,out max);
			vBuffer.Unlock();
			vBuffer.Dispose();

			float maxX = max.X;
			float maxY = max.Y;
			float maxZ = max.Z;	
			
			float minX = min.X;
			float minY = min.Y;
			float minZ = min.Z;
			
			objectBounds[0] = new Vector3(min.X,min.Y,min.Z);
			objectBounds[1] = new Vector3(max.X,min.Y,min.Z);
			objectBounds[2] = new Vector3(min.X,max.Y,min.Z);
			objectBounds[3] = new Vector3(max.X,max.Y,min.Z);
			objectBounds[4] = new Vector3(min.X,min.Y,max.Z);
			objectBounds[5] = new Vector3(max.X,min.Y,max.Z);
			objectBounds[6] = new Vector3(min.X,max.Y,max.Z);
			objectBounds[7] = new Vector3(max.X,max.Y,min.Z);

			for(int i =  0; i <=7; i++)
			{
worldBounds[i] = Vector3.TransformCoordinate(objectBounds[i],device.Transform.World); 
			}	
			
			for(int i = 0;i<8;i++)
			{
				verts[i].X = worldBounds[i].X;
				verts[i].Y = worldBounds[i].Y;
				verts[i].Z = worldBounds[i].Z;
				verts[i].Color = Color.Black.ToArgb();
				
			}	
		}

in the onPaint method, I use these lines to draw the bounding box around my character,
device.SetStreamSource(0,vb,0);
			device.VertexFormat = CustomVertex.TransformedColored.Format;
			device.DrawPrimitives(PrimitiveType.TriangleList,0,12);

but the problem Is that although the character is drawn to screen, the bounding box isn’t. any ideas why?

Share this post


Link to post
Share on other sites
Here's your problem:

device.VertexFormat = CustomVertex.TransformedColored.Format;

You cannot expect pre-transformed vertices to show up if you populate them using local mesh coordinates. This cannot work since transformed vertices are expected to be in screen-space rather than in model-space.
To overcome the issue, you need to use CustomVertex.PositionColored (or something like that - I don't remember the exact name, sorry) and apply the same tranformation to it as you do for the model.

[edit]
It seems that you actually do transform the vertices already - seems to me as if you either use the wrong colour (black-on-black for example) or another transformation for model and box. Try out my suggestion anyway. It might help.
[/edit]

Cheers,
Pat.

Share this post


Link to post
Share on other sites
the color of the vertex is black, and the color of the background is cyan so the bounding box should definately show up. what do you mean by me using another transform mode? in the onPaint method, im drawing the mesh to the scren first, but then , as shown i use theese lines, which i think swithch modes
im not sure what you mean about another transform for model and box.

Share this post


Link to post
Share on other sites
I addition to the use of lines, you should note that you will need 12 lines to display a box, which makes a total of 24 vertices (two verts per line).


// in your OnPaint-handler
device.SetStreamSource(0,vb,0);
device.VertexFormat = CustomVertex.TransformedColored.Format;
device.DrawPrimitives(PrimitiveType.LineList,0,12);

Share this post


Link to post
Share on other sites

This topic is 4730 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