Jump to content


(C++) DXUT & ID3DXMesh not rendering


2 replies to this topic

#1 IPKnightly

    New Member

  • Members
  • PipPip
  • 12 posts

Posted 21 March 2006 - 04:06 AM

Hi. I was reading over the DX documentation for C++ and came across their own framework for applications. Thus, I gave it a try and whipped up an example using their VC.NET project installer. The example is basically the mesh tutorial.

Here is the important parts.

This is called after the D3D device is created.

 HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )

{

	HRESULT hr;


	V( pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE ) );


    // Turn on ambient lighting 

    

	V( pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0xffffffff ) );


	pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC );

	

	pd3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_ANISOTROPIC );

	

	pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC );	


	V( loadXFile( L"temple.x" ) );


	return S_OK;

}

This the called every frame to render onscreen.

void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )

{

    HRESULT hr;


    // Clear the render target and the zbuffer 

    

	V( pd3dDevice->Clear( 0, NULL, ( D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER ), D3DCOLOR_XRGB(0,0,255), 1.0f, 0  ) );


    // Render the scene

    

	if( SUCCEEDED( pd3dDevice->BeginScene( ) ) )

    {

		void SetupMatrices( );


		// Meshes are divided into subsets, one for each material. Render them in

        // a loop

        

		for( DWORD i = 0; i < g_dwNumMaterials; i++ )

        {

            // Set the material and texture for this subset

            

			pd3dDevice->SetMaterial( &g_pMeshMaterials[ i ] );

            

			pd3dDevice->SetTexture( 0, g_pMeshTextures[ i ] );

        

            // Draw the mesh subset

            

			g_pMesh->DrawSubset( i );

        }


        V( pd3dDevice->EndScene( ) );

    }

}

And this is the function for loading the XMesh.

HRESULT loadXFile( LPWSTR fileName )

{

	LPD3DXBUFFER pD3DXMtrlBuffer;


	if( FAILED( D3DXLoadMeshFromX( fileName, D3DXMESH_SYSTEMMEM, DXUTGetD3DDevice( ), NULL, &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials, &g_pMesh ) ) )

    {

        MessageBox( NULL, L"File not found.", L"ERROR", ( MB_OK | MB_ICONEXCLAMATION ) );

        

		return E_FAIL;

    }


	D3DXMATERIAL* d3dxMaterials = ( D3DXMATERIAL* ) pD3DXMtrlBuffer->GetBufferPointer( );

    

	g_pMeshMaterials = new D3DMATERIAL9[ g_dwNumMaterials ];

    

	if( g_pMeshMaterials == NULL ) return E_OUTOFMEMORY;

    

	g_pMeshTextures = new LPDIRECT3DTEXTURE9[ g_dwNumMaterials ];

    

	if( g_pMeshTextures == NULL ) return E_OUTOFMEMORY;

    

	for( DWORD i = 0; i < g_dwNumMaterials; i++ )

    {

        // Copy the material

        g_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;


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

        g_pMeshMaterials[i].Ambient = g_pMeshMaterials[i].Diffuse;


        g_pMeshTextures[i] = NULL;


		int size = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, d3dxMaterials[i].pTextureFilename, -1, NULL, 0 );  

 

		LPWSTR buffer = new WCHAR[ size ];

 

		MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, d3dxMaterials[i].pTextureFilename, -1, buffer, size );


        if( d3dxMaterials[i].pTextureFilename != NULL && lstrlenW(buffer) > 0 )

        {

            // Create the texture

            if( FAILED( D3DXCreateTextureFromFile( DXUTGetD3DDevice( ), buffer, &g_pMeshTextures[i] ) ) )

            {

				MessageBox( NULL, L"File not found.", L"ERROR", ( MB_OK | MB_ICONEXCLAMATION ) );

            }

        }


		delete [ ] buffer;

    }

    // Done with the material buffer

    pD3DXMtrlBuffer->Release();

    return S_OK;

}

Yet, despite all of this, the mesh does not render on screen. I have tried different .X mesh files and even played around with the view matrix and near and far clipping planes.

Here is the complete file: demo.cpp.

Note that it compiles fine with no errors/warnings and does not crash what so ever.

Thank you for any help you can provide. I'm sure I'm just missing something stupid.

#2 bignobody

    Valued Member

  • Members
  • PipPipPip
  • 155 posts

Posted 21 March 2006 - 09:29 PM

Just a quick guess, (I haven't used this framework and am still using DX8) but after EndScene you should call the device's Present method.


hr = pd3dDevice->Present(NULL,  //Source rectangle to display, NULL for all of it

                         NULL,  //Destination rectangle, NULL to fill whole display

                         NULL,  //Target window, if NULL uses device window set in CreateDevice

                         NULL );//Unused parameter, set it to NULL


-bignobody
notsoftgames.com - Creator of Shlongg!

#3 IPKnightly

    New Member

  • Members
  • PipPip
  • 12 posts

Posted 21 March 2006 - 10:11 PM

Unfortunately, that does not work (although at first I thought that might be what I was forgetting). And in fact it makes the program crash. So i assume they're calling present some where in the frame work. I'm using MSVC++.NET 7.0 w/ 7.1 compiler. Anyone else see anything I'm not? :wacko:

Thank you.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users