Neil
2007-12-10 18:47:52 UTC
Can someone tell me what I am doing wrong here?
I am rendering a scene. The application has two render modes, "Design" and
"Preview" If in design mode, a plane is rendered as a grid of lines. In
preview mode, the plane is rendered as a texture. When I render with no
stencils (Setup and Remove stencil function calls are commented out), both
planes function as desired.
But when I turn on my stencil, only the design mode works properly. In
preview mode the stenciled area becomes washed out, with no visible texture.
Here is my code:
if(mode == Mode.Design)
{
SetupStencil(device);
// Make sure the device is using correct vertex format
device.VertexFormat = CustomVertex.PositionColored.Format;
// Set the device index buffer and vertex buffer
device.Indices = DesignPlaneIBuff;
device.SetStreamSource(0, DesignPlane, 0);
// Draw the grid plane
device.DrawIndexedPrimitives(PrimitiveType.LineList, 0, 0, 596, 0, 300);
RemoveStencil(device);
}
else
{
SetupStencil(device);
// Set the devices index buffer and vertex buffer
device.Indices = ProductPlaneIBuff;
device.SetStreamSource(0, ProductPlane, 0);
// Draw the textured plane
device.DrawIndexedPrimitives(PrimitiveType.TriangleStrip, 0, 0, 44848,
0, 44846);
RemoveStencil(device);
}
private void SetupStencil(Device device)
{
//Enable the stencil buffer/tests
device.RenderState.StencilEnable = true;
device.RenderState.StencilFunction = Compare.Always; //always
pass the stencil test
//Set the render states as to what will happen if individual
states fail.
device.RenderState.StencilPass = StencilOperation.Replace;
device.RenderState.StencilWriteMask = -1;
//Disable writes to the zbuffer and disabling writes to the
color buffer
device.RenderState.ZBufferWriteEnable = false;
device.RenderState.AlphaBlendEnable = true;
device.RenderState.SourceBlend = Blend.One;
device.RenderState.DestinationBlend = Blend.One;
//Render the polygon to the stencil buffer (masking the
bits)
CustomVertex.Transformed[] verts = new
CustomVertex.Transformed[PointList.Count];
for (int i = 0; i < PointList.Count; i++)
{
verts[i] = new CustomVertex.Transformed(PointList[i].X,
PointList[i].Y, 0f, 0f);
}
// Set vertex format
device.VertexFormat = CustomVertex.Transformed.Format;
//m_pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP,6,verts,sizeof(rhwTUVert));
device.DrawUserPrimitives(PrimitiveType.TriangleStrip,
PointList.Count/3, verts);
//Enable depth buffer and back buffer writes
device.RenderState.ZBufferWriteEnable = true;
device.RenderState.ReferenceStencil = 0xf;// 0x7f; //The
reference value to test against the value in the stencil buffer
device.RenderState.StencilFunction = Compare.Equal;
device.RenderState.StencilPass = StencilOperation.Keep;
// Set appropriate blending states
device.RenderState.SourceBlend = Blend.SourceAlpha;
device.RenderState.DestinationBlend = Blend.InvSourceAlpha;
}
private void RemoveStencil(Device device)
{
// Reset render states
device.RenderState.AlphaBlendEnable = false;
device.RenderState.StencilEnable = false;
}
I am rendering a scene. The application has two render modes, "Design" and
"Preview" If in design mode, a plane is rendered as a grid of lines. In
preview mode, the plane is rendered as a texture. When I render with no
stencils (Setup and Remove stencil function calls are commented out), both
planes function as desired.
But when I turn on my stencil, only the design mode works properly. In
preview mode the stenciled area becomes washed out, with no visible texture.
Here is my code:
if(mode == Mode.Design)
{
SetupStencil(device);
// Make sure the device is using correct vertex format
device.VertexFormat = CustomVertex.PositionColored.Format;
// Set the device index buffer and vertex buffer
device.Indices = DesignPlaneIBuff;
device.SetStreamSource(0, DesignPlane, 0);
// Draw the grid plane
device.DrawIndexedPrimitives(PrimitiveType.LineList, 0, 0, 596, 0, 300);
RemoveStencil(device);
}
else
{
SetupStencil(device);
// Set the devices index buffer and vertex buffer
device.Indices = ProductPlaneIBuff;
device.SetStreamSource(0, ProductPlane, 0);
// Draw the textured plane
device.DrawIndexedPrimitives(PrimitiveType.TriangleStrip, 0, 0, 44848,
0, 44846);
RemoveStencil(device);
}
private void SetupStencil(Device device)
{
//Enable the stencil buffer/tests
device.RenderState.StencilEnable = true;
device.RenderState.StencilFunction = Compare.Always; //always
pass the stencil test
//Set the render states as to what will happen if individual
states fail.
device.RenderState.StencilPass = StencilOperation.Replace;
device.RenderState.StencilWriteMask = -1;
//Disable writes to the zbuffer and disabling writes to the
color buffer
device.RenderState.ZBufferWriteEnable = false;
device.RenderState.AlphaBlendEnable = true;
device.RenderState.SourceBlend = Blend.One;
device.RenderState.DestinationBlend = Blend.One;
//Render the polygon to the stencil buffer (masking the
bits)
CustomVertex.Transformed[] verts = new
CustomVertex.Transformed[PointList.Count];
for (int i = 0; i < PointList.Count; i++)
{
verts[i] = new CustomVertex.Transformed(PointList[i].X,
PointList[i].Y, 0f, 0f);
}
// Set vertex format
device.VertexFormat = CustomVertex.Transformed.Format;
//m_pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP,6,verts,sizeof(rhwTUVert));
device.DrawUserPrimitives(PrimitiveType.TriangleStrip,
PointList.Count/3, verts);
//Enable depth buffer and back buffer writes
device.RenderState.ZBufferWriteEnable = true;
device.RenderState.ReferenceStencil = 0xf;// 0x7f; //The
reference value to test against the value in the stencil buffer
device.RenderState.StencilFunction = Compare.Equal;
device.RenderState.StencilPass = StencilOperation.Keep;
// Set appropriate blending states
device.RenderState.SourceBlend = Blend.SourceAlpha;
device.RenderState.DestinationBlend = Blend.InvSourceAlpha;
}
private void RemoveStencil(Device device)
{
// Reset render states
device.RenderState.AlphaBlendEnable = false;
device.RenderState.StencilEnable = false;
}