Jump to content
  • Advertisement
Sign in to follow this  
DuckInBrandy

Facet / Triangle Drawing Glitches (Managed DX9c C#)

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

I'm using the Managed 9.0c (summer 2004) in C#. I'm rendering a moderately complex mesh and throwing the camera around a little. I'm getting strange facet glitches. That is, facets are appearing in front of ones they should not (i.e. they are further back in Z range than one they are in front of). When I load the same mesh in to MeshViewer, there are no strange artefacts. Does anyone know why this happens (or what I need to do the resolve the problem)? I have messed around with different z-buffer, stencil, etc settings (shots in the dark!), but can't seem to remedy the problem. Interestingly, I put the mesh thru the mesh viewer "optimse attribute sort" (plus others). This helped a little, but still did not get rid of all facet errors. [Edited by - DuckInBrandy on September 27, 2004 5:15:20 PM]

Share this post


Link to post
Share on other sites
Advertisement

No need to set any stencil buffer stuff - in your case it sounds like you just need to enable the z buffer like this:

pDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);

if this dose not solve it, post the central code ...

(in C# replace "->" with ".")



Share this post


Link to post
Share on other sites
I've tried it with;

device.RenderState.ZBufferEnable=true;

And get the same effect (still have the problem).

I think you may have found the issue though. I've tried the code with the z-buffer disabled and enabled with out any change in outcome. I'm going to run a few more tests thru, dx debug etc. Will post back code if no joy.

Share this post


Link to post
Share on other sites
No joy. It appears to be a z-buffer issue, but I can't find it :(
The mesh is drawn better (still a few glitches) if I don't do any mesh optimisations after loading it.

device creation code (its running windowed)...


/// <summary>
/// Initialise the Direct3D graphic card
/// </summary>
/// <returns></returns>
public void InitializeGraphics()
{
// Now let's setup our D3D stuff

// Get current default adaptor (card)
int adapterOrdinal = Manager.Adapters.Default.Adapter;
CreateFlags flags = CreateFlags.SoftwareVertexProcessing;

Caps caps = Manager.GetDeviceCaps(adapterOrdinal,DeviceType.Hardware);

// Card capable of Hardware vertex processing?
if (caps.DeviceCaps.SupportsHardwareTransformAndLight)
flags = CreateFlags.HardwareVertexProcessing;

// Do we support a pure device?
//if (caps.DeviceCaps.SupportsPureDevice)
// flags |= CreateFlags.PureDevice;

PresentParameters presentParams = new PresentParameters();
presentParams.BackBufferCount = 2;
presentParams.SwapEffect = SwapEffect.Discard;
if (!FullScreen)
{
presentParams.Windowed=true;
}
else
{
presentParams.Windowed=false;
presentParams.BackBufferCount = 3;
presentParams.BackBufferFormat = CurrentFullScreenGeometry.DisplayMode.Format;
presentParams.BackBufferWidth = CurrentFullScreenGeometry.DisplayMode.Width;
presentParams.BackBufferHeight = CurrentFullScreenGeometry.DisplayMode.Height;
}

device = new Device(0, DeviceType.Hardware, this, flags, presentParams);

}





Render call..


device.SetRenderState(RenderStates.ZEnable,true);
BrandyMesh cabinet = drez.BrandyMeshLibrary.UseMesh("tank2");
Helper.Render3D.RenderAt(device, cabinet,new Vector3(0,0,0),new Vector3(0,-(float)drez.FrameCount/200,(float)drez.FrameCount/300),new Vector3(1f,1f,1f));



Render Code...


/// <summary>
/// Renders the given Mesh at the given location with the given orientation.
/// </summary>
/// <param name="device"></param>
/// <param name="position"></param>
/// <param name="orientation"></param>
public static void RenderAt(Device device, BrandyMesh mesh, Vector3 position, Vector3 orientation, Vector3 scaling)
{
// Model size correction...
Matrix mtx = Matrix.Scaling(scaling);
// Local orientation...
mtx *= Matrix.RotationYawPitchRoll(orientation.X,orientation.Y,orientation.Z);
// Global Position...
mtx *= Matrix.Translation(position);

// Draw the part...
device.Transform.World = mtx;
mesh.Render(device,true,true);
}





BrandyMesh (Based on the 9.0 sdk mesh class...)


/// <summary>Renders this mesh</summary>
public void Render(Device device, bool canDrawOpaque, bool canDrawAlpha)
{
if (localMemoryMesh == null)
throw new InvalidOperationException("No local memory mesh.");

// Frist, draw the subsets without alpha
if (canDrawOpaque)
{
for (int i = 0; i < meshMaterials.Length; i++)
{
if (isUsingMeshMaterials)
{
if (meshMaterials.DiffuseColor.Alpha < 1.0f)
continue; // Only drawing opaque right now

// set the device material and texture
device.Material = meshMaterials;
device.SetTexture(0, meshTextures);
}
localMemoryMesh.DrawSubset(i);
}
}

// Then, draw the subsets with alpha
if (canDrawAlpha)
{
for (int i = 0; i < meshMaterials.Length; i++)
{
if (meshMaterials.DiffuseColor.Alpha == 1.0f)
continue; // Only drawing non-opaque right now

// set the device material and texture
device.Material = meshMaterials;
device.SetTexture(0, meshTextures);
localMemoryMesh.DrawSubset(i);
}
}
}





Any ideas?

[Edited by - Coder on September 28, 2004 11:51:30 PM]

Share this post


Link to post
Share on other sites
Set (just before end scene and present) using...


device.Transform.View = Matrix.LookAtLH(Camera.SpacialAttributes.Position,Camera.Target,new Vector3( 0.0f, 1.0f, 0f ) );
device.Transform.Projection = Matrix.PerspectiveFovLH( (float)Math.PI / 4, (float)drez.Width/(float)drez.Height, 10.0f, 5000.0f );

Share this post


Link to post
Share on other sites
Looks like its a z-buffer issue. Thru debug, z-buffer is enabled (I've confirmed that). The near/far clipping values seem reasonable to me (10f to 5000f). I've tried different projection matrices (Orth, etc), Still the same issue.

What is a little puzzling is that if I don't optimise the mesh, it draws better (not perfect).

Its as if the z-buffer is back to front. I did wonder if it was something to do with z-buffer Compare function. I've tried various different settings hoping(!) to see some change. Alas none.

I've tried the reference software renderer as well. Exactly the same behaviour :(

....Help !?!

Share this post


Link to post
Share on other sites
You need to actually create the z-buffer for it to work - modify the present parameters a little:

AutoDepthStencil = true;
AutoDepthStencilFormat = DepthFormat.D16;

It is not enough to just set the z-enable renderstate [smile]

Kind regards,
-Nik

EDIT: Now that you have a z-buffer, remember to clear it also, in addition to the color buffer!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!