Problems loading X files into Direct3D

Started by
6 comments, last by MrDoomMaster 18 years, 8 months ago
[This was previously another post, but I think it belongs here more] I'm using Truespace 6.5 now, since I'm pretty familiar with it. It has the option to "save as" an X file. I have done this, and I kind of have it displaying in my game. It is very distorted though. Have a look: http://img.photobucket.com/albums/v356/MrDoomMaster/SS009.jpg The link above shows the camera located at (0,3,3) and looking at (0,0,0). The Cube is centered and normalized. As you can see, the shape of the cube is fully intact, but the texture is all screwed up. In Truespace I didn't use any colors. I simply created a cube, and saved it. No modifications. I loaded the X file into the DXViewer, which shows you a preview of the cube. Below is the preview: http://img.photobucket.com/albums/v356/MrDoomMaster/SS010.jpg As you can see, the cube is completely fine. I just can't seem to get it displaying properly. I have no lights in my application, would this effect anything? Below I will paste the code contained in a class of mine I use to load and display meshes, named CMesh. I do apologize for the length of the code, I do not expect anyone to read it. I simply provide this much code because I can't seem to put a pin on the actual source of the problem:

//====================================================================================
// Defines/Macros
//====================================================================================
//====================================================================================



//====================================================================================
// Includes
//====================================================================================
#include    <windows.h>
#include    <d3d9.h>
#include    <d3dx9.h>
#include    <dxerr9.h>
#include    <atlstr.h>
#include    <math.h>
#include    <vector>
#include    "Shared.h"
#include    "Display.h"
#include    "Mesh.h"
//====================================================================================



//====================================================================================
// Function Definitions
//====================================================================================
/**
**
**/
DXLIB::CMesh::CMesh(CDisplay* DisplayObject) :
    m_Display(DisplayObject),
    m_Materials(NULL),
    m_Textures(NULL),
    m_DefaultTexture(NULL),
    m_Mesh(NULL),
    m_NumMaterials(0)
{
    // TODO
}

//====================================================================================
DXLIB::CMesh::~CMesh(void)
{
    Cleanup();
}

//====================================================================================
/**
**
**/
int DXLIB::CMesh::LoadMesh(const char* Path)
{
    if(!m_Display->IsInitialized())
        return 0;

    if(!m_DefaultTexture)
        return 0;

    ID3DXBuffer* MatBuffer;
    D3DXMATERIAL* pMaterials;

    if(FAILED(hr = D3DXLoadMeshFromX(Path, D3DXMESH_SYSTEMMEM, m_Display->GetDevice(),
        NULL, &MatBuffer, NULL, &m_NumMaterials, &m_Mesh)))
    {
        return 0;
    }

    pMaterials = (D3DXMATERIAL*)MatBuffer->GetBufferPointer();

    m_Materials = new D3DMATERIAL9[m_NumMaterials];
    m_Textures  = new IDirect3DTexture9*[m_NumMaterials];

    memset(m_Materials, 0, sizeof(D3DMATERIAL9) * m_NumMaterials);
    memset(m_Textures, 0, sizeof(IDirect3DTexture9*) * m_NumMaterials);

    for(UINT i = 0; i < m_NumMaterials; ++i)
    {
        m_Materials = pMaterials.MatD3D;
        m_Materials.Ambient = m_Materials.Diffuse;

        if(pMaterials.pTextureFilename)
        {
            if(FAILED(D3DXCreateTextureFromFile(m_Display->GetDevice(),
                pMaterials.pTextureFilename, &m_Textures)))
            {
                Cleanup();
                return 0;
            }
        }
        else
            m_Textures = m_DefaultTexture;
    }

    MatBuffer->Release();
    return 1;
}

//====================================================================================
/**
**
**/
int DXLIB::CMesh::LoadDefaultTexture(const char* Path)
{
    if(!m_Display->IsInitialized())
        return 0;

    if(m_DefaultTexture)
        return 0;

    if(FAILED(hr = D3DXCreateTextureFromFile(m_Display->GetDevice(), Path, &m_DefaultTexture)))
        return 0;

    return 1;
}

//====================================================================================
/**
**
**/
int DXLIB::CMesh::DrawMesh(void)
{
    if(!m_Display->IsInitialized())
        return 0;

    for(UINT i = 0; i < m_NumMaterials; ++i)
    {
        m_Display->SetMaterial(&m_Materials);
        m_Display->SetTexture(m_Textures);

        if(FAILED(hr = m_Mesh->DrawSubset(i)))
            return 0;
    }

    return 1;
}

//====================================================================================
/**
**
**/
void DXLIB::CMesh::Cleanup(void)
{
    if(m_DefaultTexture)
    {
        m_DefaultTexture->Release();
        m_DefaultTexture = 0;
    }

    if(m_Materials)
        delete[] m_Materials;

    if(m_Textures)
    {
        for(UINT i = 0; i < m_NumMaterials; ++i)
        {
            if(m_Textures)
                m_Textures->Release();
        }

        delete[] m_Textures;
        m_Textures = 0;
    }

    if(m_Mesh)
    {
        m_Mesh->Release();
        m_Mesh = 0;
    }
}

//====================================================================================




If you'll take a look at specifically the DrawMesh() and LoadMesh() functions, you can see how I load the X file and display it. Perhaps some of you more professional game developers can point out a mistake I'm making. Above you'll notice that if a texture is attempting to be set which is actually a null pointer, I assign a default texture to it, which is nothing more than a simple 64x64 white bitmap. Thanks, I hope someone can help!
Advertisement
Did you clear your ZBuffer? I was messing around a bit today, and had the exact same disturbance. I had to think about your post, so perhaps it's because you don't clear your zbuffer.

Toolmaker

I actually had cleared my Z Buffer, and then nothing is displayed at that point. I use D3DCLEAR_ZBUFFER, via the IDirect3DDevice9::Clear() function, before I call BeginScene(). The screen remains black. I first clear the ZBUFFER then I clear the FRAME buffer. I set both to black. I am very confused, however, at the last 2 parameters in the ::Clear() function. One is for a value for the Z Buffer, and another for the Stencil. MSDN doesn't cover these very well. If you wouldn't mind showing me a good example of the parameters I should use in the ::Clear() function, I would greatly appreciate it.

Any other tips? Thank you!

[Edited by - MrDoomMaster on August 13, 2005 4:50:39 PM]
Try saving the .x file in text mode and looking at it in notepad. Look especially at the materials sections to see what is there.
------------------------See my games programming site at: www.toymaker.info
Oh my god, I just fixed it.

Here was my previous call to IDirect3DDevice9::Clear():

            if(FAILED(hr = m_Device->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 0, 0)))                return 0;


This call actually made my screen look black, as I said, no cube at all. So I simply changed the second to last parameter to 1:

            if(FAILED(hr = m_Device->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1, 0)))                return 0;


Now it works perfectly, no lighting of course. Why in the world does this help? As I stated before, MSDN does *NOT* discuss the purpose of the last 2 parameters in this function. Can anyone clarify? Thank you!

Also, I am interested in adding lighting to this cube, yet I don't know how to create a light when you're not using a vector buffer. Any tips?

Thank you all for your help.

[Edited by - MrDoomMaster on August 13, 2005 5:51:31 PM]
Can anyone provide me an example please? See last post. I apologize for the bump, but I don't really have any other sources of information...
The last two parameters ar ethe zdepth and stencil depth (in that order). If you set the zbuffer dpeth to 1.0f, it will clear the whole thing. How are you getting away without clearing the render target, though? That code is only clearing the zbuffer, not the target.
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
I do clear the render target, I am just not showing it in any of my code snippets above.

So, the range 0-1 for the ZBuffer parameter means exactly what?

What does the value for the stencil parameter mean, and what do the ranges represent?

This topic is closed to new replies.

Advertisement