redneon

Is it possible to get a BasicEffect to draw wireframe I'm wanting a nice wireframe box around my models to show the bounding box. My code to draw a box around the object currently looks like this...


BasicEffect basicEffect = new BasicEffect(game.Graphics.GraphicsDevice, null);
basicEffect.EnableDefaultLighting();

//basicEffect.Texture = texture;
//basicEffect.TextureEnabled = true;

basicEffect.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f);

Matrix viewMatrix = game.Camera.View;
Matrix projectionMatrix = game.Camera.Projection;
Matrix worldMatrix = Matrix.CreateRotationX(0);

basicEffect.World = worldMatrix;
basicEffect.View = viewMatrix;
basicEffect.Projection = projectionMatrix;

VertexPositionNormalTexture[] vertices = new VertexPositionNormalTextureDevil;

Vector3 topLeft = new Vector3(objTopLeft.X, objTopLeft.Y, objTopLeft.Z);
Vector3 topRight = new Vector3(objTopRight.X, objTopRight.Y, objTopRight.Z);
Vector3 bottomLeft = new Vector3(objBottomLeft.X, objBottomLeft.Y, objBottomLeft.Z);
Vector3 bottomRight = new Vector3(objBottomRight.X, objBottomRight.Y, objBottomRight.Z);

Vector3 normal = new Vector3(0.0f, 0.0f, 1.0f);

Vector2 texBottomLeft = new Vector2(0.0f, 0.0f);
Vector2 texBottomRight = new Vector2(1.0f, 0.0f);
Vector2 texTopLeft = new Vector2(0.0f, 1.0f);
Vector2 texTopRight = new Vector2(1.0f, 1.0f);

vertices[0] = new VertexPositionNormalTexture(bottomLeft, normal, texBottomLeft);
vertices[1] = new VertexPositionNormalTexture(topLeft, normal, texTopLeft);
vertices[2] = new VertexPositionNormalTexture(topRight, normal, texTopRight);

vertices[3] = new VertexPositionNormalTexture(bottomLeft, normal, texBottomLeft);
vertices[4] = new VertexPositionNormalTexture(topRight, normal, texTopRight);
vertices[5] = new VertexPositionNormalTexture(bottomRight, normal, texBottomRight);

VertexBuffer vertexBuffer = new VertexBuffer(
game.Graphics.GraphicsDevice,
VertexPositionNormalTexture.SizeInBytes * vertices.Length,
ResourceUsage.None,
ResourceManagementMode.Automatic
);

vertexBuffer.SetData<VertexPositionNormalTexture>(vertices);

VertexDeclaration basicEffectVertexDeclaration = new VertexDeclaration(game.Graphics.GraphicsDevice, VertexPositionNormalTexture.VertexElements);

game.Graphics.GraphicsDevice.VertexDeclaration = basicEffectVertexDeclaration;

game.Graphics.GraphicsDevice.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionNormalTexture.SizeInBytes);

basicEffect.Begin();
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Begin();

game.Graphics.GraphicsDevice.DrawPrimitives(
PrimitiveType.TriangleList,
0,
2
);

pass.End();
}
basicEffect.End();

As you can see it was previously used to display a texture. I just need to get rid of the texture stuff =o) Currently, it just displays as a white box around my objects and I'd really like it to be just the outline.



Re: XNA Game Studio Express Get BasicEffect to draw wireframe

Gregory English

I believe its just a renderstate.

device.RenderState.FillMode = FillMode.Wire

something like that





Re: XNA Game Studio Express Get BasicEffect to draw wireframe

redneon

Gregory English wrote:

I believe its just a renderstate.

device.RenderState.FillMode = FillMode.Wire

something like that



That would render the whole scene wireframe though. I was just wondering if I could just apply it to one BasicEffect.





Re: XNA Game Studio Express Get BasicEffect to draw wireframe

SoopahMan

I'll take a stab at this although I'm still learning the 3D calls.

Have you tried switching RenderState.FillMode during the Render process So render in normal mode for the majority of the polygons, switch to FillMode.Wire, render the wireframe part. I'm not sure that it works that way but it seems like it ought to be that simple... .

As for using BasicEffect for something like this, that's not quite what Shaders were meant to do - because as noted above this is a built-in function of the graphics card that can be done much more cheaply. But, if you wanted to do it with Shaders anyway - because for example your desired effect was like a wireframe but just different enough that the simple wireframe didn't get the look you want - I think you'd need to get a little more advanced and use the PixelShader class instead of BasicEffect. You'd basically be writing texturing code in the .fx file that said if the pixel isn't an edge, make it translucent. If it is an edge, make it some color.




Re: XNA Game Studio Express Get BasicEffect to draw wireframe

redneon

I kind of got around it by just using PrimitiveType.LineList and just drawing four of those for the edges rather than using PrimitiveType.TriangleList :)





Re: XNA Game Studio Express Get BasicEffect to draw wireframe

The ZMan

You can change that render state between each draw call and it will affect only the things in that draw call.

But often that isn't what people want. You will see the edges of all triangles, and often people only want to see the face edges or for things like cell shading just some of the edges. This requires you to draw the edges in a seaprate pass using lines or other geometry. Then you need to make sure you avoid zfighting is you want to draw the edges on top of the existing meshes.






Re: XNA Game Studio Express Get BasicEffect to draw wireframe

SoopahMan

Draw call as in a call to draw a Mesh or draw call as in a call to draw a frame




Re: XNA Game Studio Express Get BasicEffect to draw wireframe

The ZMan

Draw call to draw a mesh or vertex buffer