I loaded a mesh and it gets drawn weirdly....


I'm trying to load the airplane formthe dx9 samples. For some reason it gets drawn weirdly: http://img20.imageshack.us/my.php image=dx9thinger4vh.jpg

Here's how I'm doing it:

public class Model
{
Mesh mesh = null;
Material[] materials = null;
Texture[] textures = null;

public Model()
{

}

public Model(Device device, String filepath)
{
ExtendedMaterial[] tempMaterials;
mesh = Mesh.FromFile(filepath, MeshFlags.IbManaged, device, out tempMaterials);
if (tempMaterials != null && tempMaterials.Length > 0)
{
materials = new Material[tempMaterials.Length];
textures = new Texture[tempMaterials.Length];
for(int i = 0; i < tempMaterials.Length; i++)
{
materialsIdea = tempMaterialsIdea.Material3D;
materialsIdea.Ambient = materialsIdea.Diffuse;
if( tempMaterialsIdea.TextureFilename != null && tempMaterialsIdea.TextureFilename != String.Empty)
{
texturesIdea = TextureLoader.FromFile (device, tempMaterialsIdea.TextureFilename );
}
}
}
}

public void Draw(Device d)
{

for (int i = 0; i < materials.Length; i++)
{
d.Material = materialsIdea;
d.SetTexture(0, texturesIdea);
mesh.DrawSubset(i);
}
}
}


Answer this question

I loaded a mesh and it gets drawn weirdly....

  • Mario Delamboy

    Thanks guys! What I ended up doing was properly setting up the zbuffer along with the autodepthstencil. I also needed to change the device.Clear params. Everything works now!

  • Kevinxox

    I support the previous poster's answer. Make sure that you have a zbuffer setup. This will eradicate your problem.


  • Arnar Vilhj&amp;#225;lmsson

    Try adding setting the autodepthstencil to true and specifying the format for it.
    //24 bits for the depth and 8 bits for the stencil
    presentParams.AutoDepthStencilFormat = DepthFormat.D24S8;
    //Let direct3d handle the depth buffers for the application
    presentParams.EnableAutoDepthStencil = true;

    Also change your near clipping plane of your projection matrix to 1.0f instead of 0.1f
    device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI/4, 1.0f, 1.0f, 1000.0f);

    I hope this helps.
    Take care.


  • efontana

    I do have zbufferenable set to true, and it still doesn't work. I bet I'm doing something stupid somewhere, but I don't know what it is...

    Anyway, here's what I have to set up the device:

    public bool InitializeGraphics()
    {
    try {
    PresentParameters presentParams = new PresentParameters();
    presentParams.Windowed = true;
    presentParams.SwapEffect = SwapEffect.Discard;

    // Create the device
    device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

    // Setup the event handlers for the device
    device.DeviceLost += new EventHandler(this.InvalidateDeviceObjects);
    device.DeviceReset += new EventHandler(this.RestoreDeviceObjects);
    device.Disposing += new EventHandler(this.DeleteDeviceObjects);
    device.DeviceResizing += new CancelEventHandler(this.EnvironmentResizing);

    // renderstates
    device.RenderState.Lighting = false;
    device.RenderState.ZBufferEnable = true;

    device.Transform.Projection = Matrix.PerspectiveFovLH((float)(Math.PI / 3), (float)(4/3), 0.1f, 1000f);

    model = new Model[1];
    model[0] = new Model(device, "airplane 2.x");

    return true;
    } catch (DirectXException) {
    return false;
    }
    }

    As you can see, zbuffer is enabled, lighting is off (I'll figure that out later). As for the missing textures, I'm not concerned about that as much as I am about drawing the mesh correctly to begin with.

  • asbailey

    It look like you don't have Z-buffer Enable, so each triangle are drawn on top of each other, Maybe you didn't set a Z-buffer in the present param or Z-Enable is not set to True

    To draw you need 3 matrix, Proj, View and Word and they most all be setup properly

    Mesh like the plane has texture and material so when you don't have a texture for a subset you have to Set a material like you do for your texture, your white propeler may aslo be because one texture file is missing, I think the plane use 2 texture file (another texture might be found in the media directory) You can add extention TXT to your x File and open it in Notepad and Search for BMP, TGA or JPG extension in the material section near the end this will tell you which texture the X file use (change back to X extension after reading your info



  • Mike Hernandez - MSFT

    Your present parameter seem to have declare the Z-Buffer, that's a good thing
    Now you need to tell your device to use it, this way :

    device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
    device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
    device->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);

    When we render 3D geometry to the screen it is projected onto the 2D screen space and hence we lose the z (depth) value. It is useful however when rendering to store a depth value for each pixel on the screen to avoid drawing pixels when something is behind something else.

    • D3DRS_ZENABLE - used to set the buffering method. Choices are D3DZB_TRUE to enable Z buffering, D3DZB_USEW to enable W buffering (rarely used) or D3DZB_FALSE to disable Z buffering. To enable a Z buffer also see the Z buffer page.
    • D3DRS_ZWRITEENABLE - you can turn on or off the writing of data to the Z buffer (TRUE or FALSE). Normally you want it enabled so that geometry is only rendered if it is in front of previous rendered geometry (relative to the camera). You can however turn this off at particular times so you do not write to the Z buffer but can still read from it. This can be useful for advanced rendering techniques. E.g. if you are trying to save fill rate you may want to render something late but you do not want to write into the z buffer but simply read from it.
    • D3DRS_ZFUNC - allows control of the way the depth buffer value is used in determining if a pixel should be rendered. Normally this is D3DCMP_LESSEQUAL which means that a pixel is rendered if it's distance (depth) is less or equal to what was rendered at that pixel previously. There are many other advanced options like: D3DCMP_NEVER, D3DCMP_LESS, D3DCMP_EQUAL etc. look in the help file for full details

    Example

    To enable the z buffer for tests and writes we could do this:

    device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
    device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
    device->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);

    This site is very helpful :

    http://www.toymaker.info/Games/html/render_states.html


  • I loaded a mesh and it gets drawn weirdly....