views:

61

answers:

1

ok so i can load a mesh perfectly but loading its texture is not working. im not sure what im doing wrong.
here is my code..

//render a single frame
void RenderFrame(void)
{
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    d3ddev->BeginScene();

    d3ddev->SetFVF(CUSTOMFVF);

    InitMatrices();

    D3DXMATRIX matTran;    // a matrix to store the rotation for each triangle
    D3DXMATRIX matRotz;
    D3DXMATRIX matRoty;
    D3DXMATRIX matRotx;
    D3DXMatrixTranslation(&matTran, x, y, z);   
    D3DXMatrixRotationZ(&matRotz, D3DXToRadian(rz));
    D3DXMatrixRotationY(&matRoty, D3DXToRadian(ry));
    D3DXMatrixRotationX(&matRotx, D3DXToRadian(rx));

    d3ddev->SetTransform(D3DTS_WORLD, &(matTran * matRotz * matRoty * matRotx));    // set the world transform

    // draw the spaceship
    for(DWORD i = 0; i < numMaterials; i++)    // loop through each subset
    {
        d3ddev->SetMaterial(&material[i]);    // set the material for the subset
        if(texture[i] != NULL)    // if the subset has a texture (if texture is not NULL)
            d3ddev->SetTexture(0, texture[i]);    // ...then set the texture

        meshSpaceship->DrawSubset(i);    // draw the subset
    }

    d3ddev->EndScene(); 

    d3ddev->Present(NULL, NULL, NULL, NULL);
}

// cleans up Direct3D and COM
void CleanD3D(void)
{
    meshSpaceship->Release();
    d3ddev->Release();    // close and release the 3D device
    d3d->Release();    // close and release Direct3D
}

VOID InitGraphic()
{
    LPD3DXBUFFER bufShipMaterial;

    D3DXLoadMeshFromX(L"ramiz.x",    // load this file
                      D3DXMESH_SYSTEMMEM,    // load the mesh into system memory
                      d3ddev,    // the Direct3D Device
                      NULL,    // we aren't using adjacency
                      &bufShipMaterial,    // put the materials here
                      NULL,    // we aren't using effect instances
                      &numMaterials,    // the number of materials in this model
                      &meshSpaceship);    // put the mesh here

    // retrieve the pointer to the buffer containing the material information
    D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufShipMaterial->GetBufferPointer();

    // create a new material buffer and texture for each material in the mesh
    material = new D3DMATERIAL9[numMaterials];
    texture = new LPDIRECT3DTEXTURE9[numMaterials];

    for(DWORD i = 0; i < numMaterials; i++)    // for each material...
    {
        material[i] = tempMaterials[i].MatD3D;    // get the material info
        material[i].Ambient = material[i].Diffuse;    // make ambient the same as diffuse
        D3DXCreateTextureFromFile(d3ddev,
                                    L"ramiz.x"
                                    ,
                                    &texture[i]);
        texture[i]=NULL;
     }
}
+2  A: 

First of all, you're setting all the textures to NULL in your for loop, so of course there's no texture to render!

for(DWORD i = 0; i < numMaterials; i++)
{ 
    /* ... */
    texture[i]=NULL; // <--- You're setting all your textures to NULL!
} 

Also:

D3DXCreateTextureFromFile(d3ddev, L"ramiz.x", &texture[i]);  

The function D3DXCreateTextureFromFile() only supports loading textures from these file types: .bmp, .dds, .dib, .hdr, .jpg, .pfm, .png, .ppm, and .tga. You're attempting to load textures from an .x file, which can't possibly be right.

I think you meant to pass tempMaterials[i].pTextureFilename to D3DXCreateTextureFromFile() to load the texture files, no?

Here's how to properly load texture files for (simple) .x file rendering:

//////////////////////////////////////////////////////////////////////////////
// Code snippet from http://www.toymaker.info/Games/html/load_x_simply.html //
//////////////////////////////////////////////////////////////////////////////

for (DWORD i=0; i<m_numMaterials; i++)
{

 // Copy the material
 meshMaterials[i] = d3dxMaterials[i].MatD3D;

 // Set the ambient color for the material (D3DX does not do this)
 meshMaterials[i].Ambient = meshMaterials[i].Diffuse;

 // Create the texture if it exists - it may not
 meshTextures[i] = NULL;
 if (d3dxMaterials[i].pTextureFilename)
     D3DXCreateTextureFromFile(gD3dDevice,
                               d3dxMaterials[i].pTextureFilename,
                               &meshTextures[i]);
}
In silico
than how can you load a texture to a mesh
Ramiz Toma
@Ramiz Toma: I've updated my post to answer that comment.
In silico
error C2664: 'D3DXCreateTextureFromFileW' : cannot convert parameter 2 from 'LPSTR' to 'LPCWSTR'1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Ramiz Toma
@Ramiz Toma: You're using the Unicode version of `D3DXCreateTextureFromFile()`. You either have to convert the string to Unicode, or directly call `D3DXCreateTextureFromFileA()` instead.
In silico
ok i went with `D3DXCreateTextureFromFileA()` but still the texture doesn't apper
Ramiz Toma
any idea? to get the texture to apper
Ramiz Toma
@Ramiz Toma: You need to give me more information. Is the `D3DXCreateTextureFromFile()` function failing? What `HRESULT` did it return? Can the function find the texture file?
In silico
ohh well when i compile my program it doesn't give any errors or warings it runs fine but it doesn't show the texture
Ramiz Toma
@Ramiz Toma: What I meant was that you need to see if the call to `D3DXCreateTextureFromFile()` succeeded or not. If it fails for any reason (for example, it can't find the texture file), then no textures will render. If the device was not configured properly (for example, the lighting may be off), that also might make textures not appear.
In silico
ok i just put little check to see the result and it failed
Ramiz Toma
@Ramiz Toma: What was the failure code? The `D3DXCreateTextureFromFile()` function returns an `HRESULT` code that can tell you what's wrong.
In silico
im lost. how can get the hresult?, all i did was use the `FAILED()` function
Ramiz Toma
@Ramiz Toma: Never mind. My guess is that the function cannot find the file. Check the values of `pTextureFilename` - it might not have the right file path to your texture files. Use something like `MessageBoxA(NULL, tempMaterials[i].pTextureFilename, "", MB_OK)` in the `for` loop as a quick and dirty way to check them.
In silico
ok i got it working it seemed that it didn't give the full path for teh file, it just gave the name of the file thanks again
Ramiz Toma
@Ramiz Toma: I highly recommend that you pick up a [beginner's DirectX book](http://stackoverflow.com/questions/1290157/learning-directx), or even a beginner's game programming in DirectX. DirectX programming is not trivial. I would also recommend that you pick up a [good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).
In silico
i would, most of the books that i found were old
Ramiz Toma
@Ramiz Toma: As long as it's for DirectX 9, that shouldn't matter too much. I have books published in 2005 that I still refer back to. Age shouldn't be the make-or-break factor when it comes to books - it's much better than reading no books at all.
In silico