Skip to main content
GameDev.net gamedev.net
🔒 Locked

Mesh rendering problem

Started by emrexnet Nov 27, 2010 at 7:13 PM 21 replies 5.1k views
Original Post
emrexnet
emrexnet
Hi,

I am developing a DirectX application in C#. The number of vertices is not much, it is about 240 vertices. I use DirectX 9.0c. I tried everything! I have enabled AutoDepthStencil (I use D16 format buffer), I keep my near plane as far and far plean as near as possible, I clear my ZBuffer properly, and so on. But nevertheless, I get an incorrect visual result. Some of the surfaces, that must reside behind, are drawn at the the front. When I test with another mesh, the application, for instance draws the directx standard teapot properly. What could be the reason for that problem??
mps1028
mps1028
Did you enable the Zbuffer?

I dont know how it's done in C# but in C++ it's:

pdev->SetRenderState( D3DRS_ZENABLE, TRUE );

You said you properly D3DCLEAR_ZBUFFER and I'm assuming you also use D3DCLEAR_TARGET every frame?

If you did all of that, I can't imagine it having the Zcoords out of order.
emrexnet
emrexnet
Yes, I enable ZBuffer, and I clear Target as you told.

What do you mean by saying Zcoords out of order??
unbird
unbird
Can you show us a screenshot and all relevant rendering code ?
mps1028
mps1028
From what you were saying, I thought the problem was that objects are not displaying in the proper depth order (z order) So if you are looking down the negative Z axis, there are negative z objects in front of positive z objects. Is this what your problem is? That would mean your Z buffer is not working right obviously. Or I suppose it could mean that your world matrix is set up wrong? Not sure about that one as I have never messed around with different matrix configurations.

The only time I had objects appearing in an incorrect way was when my Zbuffer wasn't enabled and properly cleared after every pass. I don't personally use a stencil buffer but I do have one enabled (since it is linked with the Zbuffer)
emrexnet
emrexnet
Quote:
Original post by unbird
Can you show us a screenshot and all relevant rendering code ?


The mesh is supposed to generate a slender object (I am working on a mechanical engineering application, in which machine parts are visualized). Because I could not add a screenshots or attach files, I added the pictures in the links and I write code as below:

The front view of the object : http://img152.imageshack.us/i/frontb0.jpg/

The rotated view (which is obviously rendered false!) : http://img823.imageshack.us/img823/4237/sideut.jpg

I give some of the code as below:

...
InitilizeGraphics()
{
...
pres.Windowed = true;
pres.SwapEffect = SwapEffect.Discard;
pres.AutoDepthStencilFormat = DepthFormat.D16;
pres.EnableAutoDepthStencil = true;

device = new Microsoft.DirectX.Direct3D.Device(0,
Microsoft.DirectX.Direct3D.DeviceType.Hardware, this.panel1,
CreateFlags.SoftwareVertexProcessing | CreateFlags.FpuPreserve,
pres);

device.RenderState.ZBufferEnable = true;

...
}

protected void SetupMatrices()
{
device.Transform.World = Matrix.RotationYawPitchRoll(angle, angle /
3.0F, 0);
device.Transform.View =
Matrix.LookAtLH(new Vector3(0, 0.5F, -20),
new Vector3(0, 0.5F, 0), new Vector3(0, 1, 0));
device.Transform.Projection = Matrix.OrthoOffCenterLH(-10, 10, -10, 10, 1,
100);
}

protected void SetupLights()
{
device.RenderState.Lighting = true;
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Diffuse = Color.White;
device.Lights[0].Direction = new Vector3(0, 0, 1);
device.Lights[0].Update();
device.Lights[0].Enabled = true;
}

protected void SetupMaterials(Color color)
{
Material mat = new Material();
mat.Diffuse = Color.Blue;
device.Material = mat;
}

protected void Render()
{
device.Clear(ClearFlags.ZBuffer | ClearFlags.Target, Color.Black, 1.0F, 0);
device.BeginScene();
SetupMatrices();
device.RenderState.FillMode = FillMode.Solid;
SetupMaterials(Color.Blue);
mesh.DrawSubset(0);
device.EndScene();
device.Present();
}
...


mps1028
mps1028
Ok, so the problem is that your mesh doesnt look right when rotated? I don't understand this unless you havent set up matrix properly for adjusting meshes properties. Ill show you my code for such things but realize it is C++, not C# but maybe still you get an idea.

void CDirect3D_2::RenderMesh(int MeshInst, D3DXVECTOR3 trans, int angleX, int angleY, int angleZ,
FLOAT scale)
{
// Render a mesh based off of Instance number
D3DXMatrixTranslation( &matTrans, trans.x, trans.y, trans.z );
D3DXMatrixRotationX( &matRotX, DEGTORAD(angleX) );
D3DXMatrixRotationY( &matRotY, DEGTORAD(angleY) );
D3DXMatrixRotationZ( &matRotZ, DEGTORAD(angleZ) );
D3DXMatrixScaling(&matScale, scale, scale, scale);
matWorld = matScale * (matRotZ*matRotX*matRotY) * matTrans;
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

// Set states
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);

for( DWORD i = 0; i < g_dwNumMaterials[MeshInst]; i++ )
{
// Set the material and texture for this subset
g_pd3dDevice->SetMaterial( &g_pMeshMaterials[MeshInst] );
g_pd3dDevice->SetTexture( 0, g_pMeshTextures[MeshInst] );

// Draw the mesh subset
g_pMesh[MeshInst]->DrawSubset( i );
}
return;
}
unbird
unbird
There are other render states that have to be set to make the depth test work as desired. Reminds me of this.
mps1028
mps1028
What other states are you referring to?
unbird
unbird
The ones discussed in the link I provided (depth-write enable, compare function). I'm not claiming it's the issue here, but looking at the screenshot very much looks like a not properly enabled depth-test. And from the code emrexnet provided I see said states missing.
emrexnet
emrexnet
Quote:
Original post by unbird
The ones discussed in the link I provided (depth-write enable, compare function). I'm not claiming it's the issue here, but looking at the screenshot very much looks like a not properly enabled depth-test. And from the code emrexnet provided I see said states missing.


Can u recommend any explanatory source to figure out stencil buffers for managed directx in c#??
mps1028
mps1028
Quote:
Original post by unbird
The ones discussed in the link I provided (depth-write enable, compare function). I'm not claiming it's the issue here, but looking at the screenshot very much looks like a not properly enabled depth-test. And from the code emrexnet provided I see said states missing.


I use depth tests too but those are only for the purposes of alpha textures. I don't think there is any purpose besides those (none that I have ever needed) Technically, even those arent absolutely necessary if you correctly order your alpha layers but that is too frustrating to worry about so it is better to just do alpha value comparisons.

The following has always worked well for me:

// Set alpha issues
g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
g_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, true );
g_pd3dDevice->SetRenderState( D3DRS_ALPHAREF, 0x01 );
g_pd3dDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL );

unbird
unbird
Er, no. Is your original issue solved then ? Or is there some confusion now ? I don't think you need stencil to achieve what you want, i.e. render a mesh correctly. Have you checked the render states I mentioned and did the problem persist ?
Rosalia
Rosalia
Did you test changing your culling mode (CW, CCW)? I don't really think this looks like a depth test problem..

Good Luck!
Rosália G. Schneiderhttp://rosaliaschneider.wordpress.com/
emrexnet
emrexnet
Quote:
Original post by unbird
Er, no. Is your original issue solved then ? Or is there some confusion now ? I don't think you need stencil to achieve what you want, i.e. render a mesh correctly. Have you checked the render states I mentioned and did the problem persist ?


No, not solved yet. I've checked render states, but the problem persists.

Quote:
Original post by Rosalia
Did you test changing your culling mode (CW, CCW)? I don't really think this looks like a depth test problem..

Good Luck!


No, this is not a culling mode issue. Cull mode is CCW..
programci_84
programci_84
Sanki sen bunu 3ds Max'ten export etmişsin de normal vektörleri ve UV koordinatlarını ters döndürmüşsün gibime geliyor. Kullandığın aracın export ayarlarına tekrar bir göz at. Modeli right-handed göndermiş olabilirsin. Y-eksenini "Up" olarak ayarlamamamış olabilirsin vs. Öyle durumda bu tip hatalar alman olası.

[In English]
Looks like you've exported that model from 3ds max and something wrong with normal vectors and UV coords. Check your exporter's export settings. Maybe you exported your model "right-handed" or didn't set the y-axis "up".
[/In English]

hth.
-R
There's no "hard", and "the impossible" takes just a little time.
emrexnet
emrexnet
Quote:
Original post by programci_84
Sanki sen bunu 3ds Max'ten export etmişsin de normal vektörleri ve UV koordinatlarını ters döndürmüşsün gibime geliyor. Kullandığın aracın export ayarlarına tekrar bir göz at. Modeli right-handed göndermiş olabilirsin. Y-eksenini "Up" olarak ayarlamamamış olabilirsin vs. Öyle durumda bu tip hatalar alman olası.

[In English]
Looks like you've exported that model from 3ds max and something wrong with normal vectors and UV coords. Check your exporter's export settings. Maybe you exported your model "right-handed" or didn't set the y-axis "up".
[/In English]

hth.
-R


Aslinda mesh datasini kendim yarattım. Vertex koordinatlarini, normallerini ve index datası programın icinde oluşturuluyor. Bu nedenle, yuzey normallerinde bir hata olmadigini biliyorum. Baska neden olabilecegi konunda fikrin var mi?

[In English]
Actually I have created my mesh data by myself. Vertices, their coords and normals, and indices, they are all created in the application. So, i know that normals are set properly. Any other possibility??
[/quote]
Buckeye
Buckeye
Quote:
the application, for instance draws the directx standard teapot properly ... I have created my mesh data by myself.

If your program displays others meshes properly, then there's a problem with how you create your mesh.

I'd suggest you take one small step at a time. Create a mesh with just one or triangles and get that to display properly. Then add a few more triangles and test. Keep doing that until you find the problem with your creation routine.

As an aid to trouble shooting you might want to add an option to toggle between a wireframe and solidfill display, and to incorporate a moveable camera so you can move around your mesh in 3D to examine it "real-time."
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.
emrexnet
emrexnet
Quote:
Original post by Buckeye
Quote:
the application, for instance draws the directx standard teapot properly ... I have created my mesh data by myself.

If your program displays others meshes properly, then there's a problem with how you create your mesh.

I'd suggest you take one small step at a time. Create a mesh with just one or triangles and get that to display properly. Then add a few more triangles and test. Keep doing that until you find the problem with your creation routine.

As an aid to trouble shooting you might want to add an option to toggle between a wireframe and solidfill display, and to incorporate a moveable camera so you can move around your mesh in 3D to examine it "real-time."


I did it just a few minutes ago. Imagine that a cylindrical object with several sections with different diameters, is cut into slices. And then, all the slices are assigned into different meshes. When they are rendered one at a time they give the desired result. But when all the meshes are rendered altogether, the same messy result..
Manpreet
Manpreet
You mentioned that you had culling set to CCW , but are you sure that you provided the vertices in clockwise order?(or indices,if you are using indexed primitives)

Ignore if you already tried this

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.