❗ In case anyone got stuck trying to use debugdraw in game studio 4.0, you need to wrap their debugdraw object a little. Namely, the way I found was to create a basic effect, which includes setting up a simple orthogonal matrix for our standard 2D settings, and pushing the debug vertex data through them to the graphics card.
Now note, we have the two simple functions that initialize stuff. The transform is set to be a straight forward camera looking down the z axis, is not offcentered anymore.
The Initialize basic effect function call is two fold. It sets up the effect and vertex coloring, and we also create our rotation / transformation matrices for our "perspective (which is just orthogonal)".
We also scale by 30. This is that defacto pixel scaling number i've heard around for Box2D so i stuck with that and that gave results just like pre-box2d with my objects.
Then we have an important step here which is creating a vertex buffer that knows how we're presenting data to it, which is the idea of the next code snipper for a vertexDeclaration.
So, there we initialize a member object (a VertexDeclaration instance) to be that nice horrible array of information. This tells the renderer how the data will be presented when we pass it down the pipeline. The first 12 bytes are for 3 elements of a vector for position xyz. and the last set of bytes for color.
Now, the coup de grace is the changes to the FinishDrawShapes() function that DebugDraw implements:
This effectively gives us a pass through on the basiceffect we create (that is essentially providing position information with the matrices we provided to it upon initialization. Each time we're creating a new vertex buffer for the number of points we want. I realize this might be a HORRIBLY bad way to do things, but I just got this to work and it's really for debug purposes anyhow.
Make sure that Dispose() call is in there, otherwise you blow the heap in about.... 5 seconds.
Thats about it I think....?
If you have questions, feel free to ask, I'll explain in some more detail, (but i'm by now way an expert, I'd thought i'd share this nice little snippet to help people out)
Code: Select all
Matrix worldMatrix;
Matrix viewMatrix;
Matrix projectionMatrix;
BasicEffect basicEffect;
private void InitializeEffect()
{
basicEffect = new BasicEffect(gameRef.GraphicsDevice);
basicEffect.VertexColorEnabled = true;
worldMatrix = Matrix.CreateTranslation(0.0f,0.0f, 0);
worldMatrix *= Matrix.CreateScale(30.0f);
basicEffect.World = worldMatrix;
basicEffect.View = viewMatrix;
basicEffect.Projection = projectionMatrix;
}
private void InitializeTransform()
{
viewMatrix = Matrix.CreateLookAt(
new Vector3(0.0f, 0.0f, 1.0f),
Vector3.Zero,
Vector3.Up
);
projectionMatrix = Matrix.CreateOrthographicOffCenter(
0,
(float)gameRef.GraphicsDevice.Viewport.Width,
(float)gameRef.GraphicsDevice.Viewport.Height,
0,
1.0f, 2.0f);
}
Now note, we have the two simple functions that initialize stuff. The transform is set to be a straight forward camera looking down the z axis, is not offcentered anymore.
The Initialize basic effect function call is two fold. It sets up the effect and vertex coloring, and we also create our rotation / transformation matrices for our "perspective (which is just orthogonal)".
We also scale by 30. This is that defacto pixel scaling number i've heard around for Box2D so i stuck with that and that gave results just like pre-box2d with my objects.
Then we have an important step here which is creating a vertex buffer that knows how we're presenting data to it, which is the idea of the next code snipper for a vertexDeclaration.
Code: Select all
vertexDeclaration = new VertexDeclaration(new VertexElement[]
{
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
new VertexElement(12, VertexElementFormat.Color, VertexElementUsage.Color, 0)
}
);
// I also stuffed this in just to show it's application to the vertex buffer we also need to create
vertexBuffer = new VertexBuffer(gameRef.GraphicsDevice, vertexDeclaration, 10000, BufferUsage.None);
So, there we initialize a member object (a VertexDeclaration instance) to be that nice horrible array of information. This tells the renderer how the data will be presented when we pass it down the pipeline. The first 12 bytes are for 3 elements of a vector for position xyz. and the last set of bytes for color.
Now, the coup de grace is the changes to the FinishDrawShapes() function that DebugDraw implements:
Code: Select all
public void FinishDrawShapes()
{
//_device.RenderState.CullMode = CullMode.None;
// _device.RenderState.AlphaBlendEnable = true;
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
if (_fillCount > 0)
{
vertexBuffer = new VertexBuffer(gameRef.GraphicsDevice, vertexDeclaration, _vertsFill.Length, BufferUsage.None);
vertexBuffer.SetData<VertexPositionColor>(_vertsFill);
_device.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, _vertsFill, 0, _fillCount);
vertexBuffer.Dispose();
}
if (_lineCount > 0)
{
vertexBuffer = new VertexBuffer(gameRef.GraphicsDevice, vertexDeclaration, _vertsLines.Length, BufferUsage.None);
vertexBuffer.SetData<VertexPositionColor>(_vertsLines);
_device.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, _vertsLines, 0, _lineCount);
vertexBuffer.Dispose();
}
}
_lineCount = _fillCount = 0;
}
This effectively gives us a pass through on the basiceffect we create (that is essentially providing position information with the matrices we provided to it upon initialization. Each time we're creating a new vertex buffer for the number of points we want. I realize this might be a HORRIBLY bad way to do things, but I just got this to work and it's really for debug purposes anyhow.
Make sure that Dispose() call is in there, otherwise you blow the heap in about.... 5 seconds.
Thats about it I think....?
If you have questions, feel free to ask, I'll explain in some more detail, (but i'm by now way an expert, I'd thought i'd share this nice little snippet to help people out)