cant show bounding box around my charachter. code included

Started by
4 comments, last by fguihen 19 years, 3 months ago
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 = Vector3.TransformCoordinate(objectBounds,device.Transform.World); 
			}	
			
			for(int i = 0;i<8;i++)
			{
				verts.X = worldBounds.X;
				verts.Y = worldBounds.Y;
				verts.Z = worldBounds.Z;
				verts.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?
I currently only use c# and directX9, in case its relivant and i didnt mention it in the post
Advertisement
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.
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.
I currently only use c# and directX9, in case its relivant and i didnt mention it in the post
I guess your error lies somewhere else. You should use lines to display the box, not triangles.
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);

i was using lines, but i just changed it to triangles to see if it would display anything. lines or tirangles, still nothing is drawn to screen
I currently only use c# and directX9, in case its relivant and i didnt mention it in the post

This topic is closed to new replies.

Advertisement