Sign in to follow this  

Problem with Meshes

This topic is 3739 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 would post a screen shot but not sure how or if you can in these forums. The problem I am having is my meshes are rendering through each other I thought if I turned culling on it would fix this problem but nope...it improves performance but they still render through each other. I thought the problem was with my modeler which I originally used 3ds max 9 so I remodeled the object in MilkShape. I remodeled it instead ofo importing because I wanted to remake it anyway, but thats besides the point. After exporting my Milkshape model into a directX(.x) file I tried loading it up too. The meshes are rendering through each other still. Also on complex models some of the walls do not render or render mashed together...I am unsure if this is because of code, the directX Exporter plugin or what. Any thoughts would be great, thanks.

Share this post


Link to post
Share on other sites
Thank you for the quick reply. I am currently not using the zBuffer....should I? I am new to game programming and am just trying to build a scene right now. Here is a screen shot I have taken. BTW the modeling looks like crap not only because of the problems but because I am currently doing my own modeling.

Click here to enlarge screen shot

Thank you again for any advice or suggestions.

Share this post


Link to post
Share on other sites
Yes, you'll want to enable z-buffering. Z-buffering ensures that what you see on the screen is in fact what's closest to the camera (which makes sense!). Without z-buffering, instead you'll see the last thing you rendered to the screen. It also looks like you have front-face culling enabled instead of back-faced culling. Fortunately its very easy to enable both:



//use z-buffering
d3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
d3dDevice->SetRenderState(D3DRS_ZWRITEENABLE , TRUE);

// Turn on back-face culling
d3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );





Hopefully this solves your problems. :)

Share this post


Link to post
Share on other sites
Hello and thank you again MJP. Yes it makes perfect sense what you are saying about the zbuffer. The problem still consists, I think it may be because one line of my code which I will show in a minute.
This is my method that sets up my graphic device. I am not sure if it is setup correctly for zBuffer use even now that I have added what you suggested. I have tried changing the swap effect from Discard to copy and flip and no dice still.
private bool setGraphics(Form1 frm)
{
bool flag = false;
try
{
PresentParameters param = new PresentParameters();
param.Windowed = true;
param.SwapEffect = SwapEffect.Discard;
frm.getGraphics = new Microsoft.DirectX.Direct3D.Device(0, D3D.DeviceType.Hardware, frm,
CreateFlags.SoftwareVertexProcessing, param);
frm.getGraphics.RenderState.Lighting = false;
frm.getGraphics.RenderState.CullMode = Cull.CounterClockwise;
//frm.getGraphics.RenderState.FillMode = FillMode.WireFrame;
frm.getGraphics.RenderState.ZBufferEnable = true;
frm.getGraphics.RenderState.ZBufferWriteEnable = true;
flag = true;
}
catch
{
flag = false;
}
return flag;
}



Here is a screen shot of Clockwise culling;
Culling Clockwise
And here is a screen shot of CounterClockwise cullig;
Culling CounterClockwise
The building does look better when culling is CounterClockwise but my terrain disappears but thats something I can look into....the terrain is also temporary. I think the zBuffer is the issue I am going to go with here.

I misewell show my render method because then it should display the whole picture of how things are working...so here it is;

protected override void OnPaint(PaintEventArgs e)
{
actions.getRealTimeUserInput();
this.Text = string.Format(FrameRate.CalculateFrameRate().ToString());
d3d.Clear(ClearFlags.Target, Color.Yellow, 1.0f, 0);
// d3d.Clear(ClearFlags.ZBuffer, Color.Yellow, 1.0f, 0); //experiment
d3d.BeginScene();
#region Meshes
twoMach1.renderAllMeshes();
#endregion
d3d.EndScene();
d3d.Present();
this.Invalidate();
}




Again any suggestions or thoughts are greatly appreciated. Thank you again ahead of time.

**EDIT** I am still thinking there might be a problem with my 3ds exporter but I am not sure, I am gonna try out some other exporters and see what comes up.
XXChester

Share this post


Link to post
Share on other sites
Quote:
Original post by XXChester
The building does look better when culling is CounterClockwise but my terrain disappears but thats something I can look into....the terrain is also temporary. I think the zBuffer is the issue I am going to go with here.


Sounds like a normals issue, IE: you're rendering the terrain's triangles clockwise or counterclockwise, whatever it is the wrong way to go [smile].

Share this post


Link to post
Share on other sites
Hello and thanks Kwizatz; Inside 3ds max9 export plugin there is a option to flip normals. I have tried rendering a mesh of each type and it doesn't seem to be doing anything.
I think I am missing something within my code. I just found an old tutorial that shows how to compute normals with code....the way I am currently doing it I guess is wrong. Basically the way I was shown from a tutorial and book to do is like this;

private Mesh towerMesh;
private Texture[] towerTxs;
private Material[] towerMtrls;
private void loadMesh(string filename, ref Mesh mesh, ref Material[] meshMats ref Texture[] mesh textures)
{
ExtendedMaterial[] materialArray;
mesh = Mesh.FromFile(filename, MeshFlags.Managed, d3d, out materialArray);
if ((materialArray != null) && (materialArray.Length > 0))
{
meshMaterials = new Material[materialArray.Length];
meshTextures = new Texture[materialArray.Length];
for (int i = 0; i < materialArray.Length; i++)
{
meshMaterials[i] = materialArray[i].Material3D;
meshMaterials[i].Ambient = meshMaterials[i].Diffuse;
if ((materialArray[i].TextureFilename != null) &&
(materialArray[i].TextureFilename != string.Empty))
{
meshTextures[i] = TextureLoader.FromFile(d3d,
materialArray[i].TextureFilename);
}
}
}
mesh = mesh.Clone(mesh.Options.Value, CustomVertex.PositionNormalTextured.Format, d3d);
mesh.ComputeNormals();
}
private void loadMeshes()
{
loadMesh("Structure1.x", ref steelStruc, ref sStruckMats, ref sStructTextures);
}
private void drawMeshes(Mesh mesh, Material[] meshMaterials, Texture[] meshTextures)
{
for (int i = 0; i < meshMaterials.Length; i++)
{
d3d.Material = meshMaterials[i];
d3d.SetTexture(0, meshTextures[i]);
mesh.DrawSubset(i);
}
}
public void renderAllMeshes()
{
d3d.Transform.World = Matrix.Scaling(0.1f, 0.1f, 0.1f)
* Matrix.RotationY(60f) * Matrix.Translation(-2f, -3.5f, -12.5f);
drawMeshes(getSteelMesh, getSteelMats, getSteelTxs);
}


Anyway I am not sure as to what it going on. Any thoughts would be mucho appreciated.
XXChester

Share this post


Link to post
Share on other sites
I don't think you understood, what you have to do is instead of passing the 3 points of each terrain triangle as A-B-C, pass them as A-C-B, that's what counterclockwise or clockwise means, the order in which you pass the vertices of a triangle. That order defines the direction of the triangle normal (unless you explicitly define it).

Share this post


Link to post
Share on other sites
Off-topic hint: When snapping them screenshots hold down the alt-key (for a total of alt+printscreen) and you only get the active window, saves some time ;)

Share this post


Link to post
Share on other sites
Sorry I forgot to post that I found the solution. I did some reasearching of ZBuffers and I was missing a couple lines of the zBuffer initialization so as soon as I added them the meshes and what not all rendered correctly. Thank you all for your help, its greatly appreciated.

Share this post


Link to post
Share on other sites

This topic is 3739 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this