rendering meshes in c#

hi!

i've made a mesh
picture_1: http://toomaniac.com/kopi/blender/object_mode
_solid.gif

even on actually flat faces there are a lot of triangles i.e. many different faces
picture_2: http://toomaniac.com/kopi/blender/edit_mode.gif

when i view the mesh in the meshview - that one that comes with the sdk - it looks like that
picture_3: http://toomaniac.com/kopi/blender/directx.gif

and when i load the mesh to my device it looks like that too
why does the mesh don't look like on the first picture
are there any parameters or renderstates settings i've to make

here you can find my code: http://toomaniac.com/kopi/blender/Render_Mesh.rar

thx, kopi_b


Answer this question

rendering meshes in c#

  • sjohns

    as you can see here:
    http://www.toomaniac.com/kopi/mesh/dxviewer.gif
    and here:
    http://www.toomaniac.com/kopi/mesh/program.gif

    there are no more dark areas

    but when you compare the two pictures above you'll notice that e.g. the post in the front has some slots when i view the mesh in the dxviewer you can't see them when i render it with my code

    kopi

  • Greg Smalter

    I'm not entirely sure I follow your description - can you post screenshots showing the problem

    I, as I'm sure many other people, won't download your source code and debug it. Not only is it dangerous (unknown source code from an unknown source) but time consuming to download, decompress, setup the build environment, run it, observe your bug, read your code, fix the bug, post the results back here.

    Rather than dump your code archives on us, post back the relevant details - the parts where you set up render states, where you render the mesh, where you load it etc... that, combined with some screenshots of the problem, should allow us to help you.

    Cheers,
    Jack



  • Federico Alves

    also not sure if i can follow your description ...

    But sounds a lot like your faces aint normallized the correct way. That may result in dark (black faces) area's


  • Ali Pasha

    Hi. I came across this thread while researching my own "dark mesh" issue. I found something worth sharing.

    If you use GraphicsMesh or FrameworkMesh (from Dx Samples util classes), look for this portion of code, where materials are initialized:

    for (int i=0; i<Mat.Length; i++)

    {

    materials[ i ] = Mat[ i ].Material3D; // This does not take care of Ambient

    // Set the ambient color for the material (D3DX does not do this)

    //meshMaterials[ i ].Ambient = materials[ i ].Diffuse; // This does not work in VS2005/Dx9-2.0. Used to work in VS2003/Dx9-1.0.2902.0:GraphicsMesh.Create()

    meshMaterials[ i ].AmbientColor = materials[ i ].Material.DiffuseColor;  // VS2005/Dx9-2.0 solution. You have to add this - not in original framework

    ---

    This seems to be a bug in Microsoft.DirectX.dll-2.0.0: You cannot set Ambient, but you can set AmbientColor

    Now, RenderState.Ambient, Lights[ n ].Ambient settings will behave more as you expect. Have fun!


  • Tpaktop

    hi
    i've a problem when i render my meshes.
    because every mesh is made of triangles, even actually flat faces
    with more than three vertices have to be devided into more faces.
    unfortunately this causes the problems when i render the meshes.
    at the transitions from one of these faces to the next one i've dark
    areas. the more faces there are the darker are the areas although
    the mesh is made up of just one material.

    here is my code and the mesh: http://www.toomaniac.com/kopi/blender/Render_Mesh.rar

    thx, kopi_b

  • ToreS

    picture1: invalid link

    picture2: this seems like a wireframed picture out of a 3d design program

    picture3: rendered mesh ... seems ok


  • johnes

    the problem was this line:
    [code]dev.Lights[0].Direction = new Vector3(2.0f, 2.0f, 2.0f);[/code]

    the light came in an angle of 45°
    when i change the direction to a different angle it's much better

    kopi_b

  • alohasurf

    pic1: i've updated the link. sorry bout that!
    pic2: is a screenshot from the 3d graphic program where i've made the mesh
    pic3: rendered mesh, but absolutely not ok - the mesh should look approximately as on pic1

    besides i made some progress and therefore the problem slightly changed (don't know if i should start a new thread)

    since i recognized that the normals in directx hang on the vertices and not on the faces as in the 3d graphic program where

    i've made the mesh i concentrated on the nomals of the mesh.
    mostly every vertex belongs to more faces and i think that's what causes the problem. when e.g. three faces share one vertex

    the normal for that one is calculated by the intersection of the three normals of the faces that share the vertex. then

    directx interpolates between two normals that make the same edge. could this be right

    now i've made a new mesh where i've as many normals per vertex as i've faces that share that vertex and it works better
    in dxviewer it looks nearly how it should be:
    http://www.toomaniac.com/kopi/mesh/dxviewer.gif

    but in my program the shading isn't like in the dxviewer (e.g. the lots are missing):
    http://www.toomaniac.com/kopi/mesh/program.gif

    here are my parameters and my renderstates:
    parameters.Windowed = true;
    parameters.SwapEffect = SwapEffect.Discard;
    parameters.EnableAutoDepthStencil = true;
    parameters.AutoDepthStencilFormat = DepthFormat.D16;

    dev = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, parameters);
    dev.RenderState.ZBufferEnable = true;
    dev.RenderState.Lighting = true;
    dev.RenderState.CullMode = Cull.None;
    dev.RenderState.Ambient = System.Drawing.Color.White;
    dev.RenderState.SourceBlend = Blend.SourceAlpha;
    dev.RenderState.DestinationBlend = Blend.InvSourceAlpha;
    dev.RenderState.ZBufferWriteEnable = true;
    dev.RenderState.AlphaBlendEnable = false;
    dev.Lights[0].Type = LightType.Directional;
    dev.Lights[0].Diffuse = System.Drawing.Color.White;
    dev.Lights[0].Direction = new Vector3(-2.0f, -2.0f, -2.0f);
    dev.Lights[0].Enabled = true;
    dev.Lights[1].Type = LightType.Directional;
    dev.Lights[1].Diffuse = System.Drawing.Color.White;
    dev.Lights[1].Direction = new Vector3(2.0f, 2.0f, 2.0f);
    dev.Lights[1].Enabled = true;
    dev.RenderState.Ambient = System.Drawing.Color.FromArgb(0x202020);

    loading of the mesh:
    mesh = Mesh.FromFile(filename, MeshFlags.SystemMemory, dev, out materials);
    meshMaterials = new Material[materials.Length];
    for (int i = 0; i < materials.Length; i++)
    {
        meshMaterialsIdea = materialsIdea.Material3D;
        meshMaterialsIdea.Ambient = meshMaterialsIdea.Diffuse;
    }

    what do i have to do to see my mesh as in the dxviewer

    kopi_b

  • Donald Farmer

    when i wrote the first post i tried to write it in html, but all the links and the images where deleted when i previewed the post. even the whole post was deleted.
    i will try to make it better this time, but i don't know how to add a screenshot or a url here so i just write the links as a plain text.

    besides i made some progress and therefore the problem slightly changed (don't know if i should start a new thread)

    since i recognized that the normals in directx hang on the vertices and not on the faces as in the 3d graphic program where i've made the mesh i concentrated on the nomals of the mesh.
    mostly every vertex belongs to more faces and i think that's what causes the problem. when e.g. three faces share one vertex the normal for that one is calculated by the intersection of the three normals of the faces that share the vertex. then directx interpolates between two normals that make the same edge. could this be right

    now i've made a new mesh where i've as many normals per vertex as i've faces that share that vertex and it works better
    in dxviewer it looks nearly how it should be:
    http://www.toomaniac.com/kopi/mesh/dxviewer.gif

    but in my program the shading isn't like in the dxviewer (e.g. the lots are missing):
    http://www.toomaniac.com/kopi/mesh/program.gif

    here are my parameters and my renderstates:
    parameters.Windowed = true;
    parameters.SwapEffect = SwapEffect.Discard;
    parameters.EnableAutoDepthStencil = true;
    parameters.AutoDepthStencilFormat = DepthFormat.D16;

    dev = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, parameters);
    dev.RenderState.ZBufferEnable = true;
    dev.RenderState.Lighting = true;
    dev.RenderState.CullMode = Cull.None;
    dev.RenderState.Ambient = System.Drawing.Color.White;
    dev.RenderState.SourceBlend = Blend.SourceAlpha;
    dev.RenderState.DestinationBlend = Blend.InvSourceAlpha;
    dev.RenderState.ZBufferWriteEnable = true;
    dev.RenderState.AlphaBlendEnable = false;
    dev.Lights[0].Type = LightType.Directional;
    dev.Lights[0].Diffuse = System.Drawing.Color.White;
    dev.Lights[0].Direction = new Vector3(-2.0f, -2.0f, -2.0f);
    dev.Lights[0].Enabled = true;
    dev.Lights[1].Type = LightType.Directional;
    dev.Lights[1].Diffuse = System.Drawing.Color.White;
    dev.Lights[1].Direction = new Vector3(2.0f, 2.0f, 2.0f);
    dev.Lights[1].Enabled = true;
    dev.RenderState.Ambient = System.Drawing.Color.FromArgb(0x202020);

    loading of the mesh:
    mesh = Mesh.FromFile(filename, MeshFlags.SystemMemory, dev, out materials);
    meshMaterials = new Material[materials.Length];
    for (int i = 0; i < materials.Length; i++)
    {
    meshMaterialsIdea = materialsIdea.Material3D;
    meshMaterialsIdea.Ambient = meshMaterialsIdea.Diffuse;
    }

    what do i have to do to see my mesh as in the dxviewer

    kopi_b

  • andret1802

    Kopi_b, please try and keep your posts in one place. I have merged the other thread with the same topic to this one.
    Keeping your question in one place allows for a centralized answer and people to concentrate on it at one place.

    Thanks.


  • rendering meshes in c#