Directx3D中关于加载x文件的问题

hfchen890705 2011-05-12 11:31:52
代码如下,是从Directx9.0的sample的直接复制的一个例子
#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>
#include"math.h"
#pragma warning( disable : 4996 ) // disable deprecated warning
#include <strsafe.h>
#pragma warning( default : 4996 )
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device

LPD3DXMESH g_pMesh = NULL; // Our mesh object in sysmem
D3DMATERIAL9* g_pMeshMaterials = NULL; // Materials for our mesh
LPDIRECT3DTEXTURE9* g_pMeshTextures = NULL; // Textures for our mesh
DWORD g_dwNumMaterials = 0L; // Number of mesh materials

HRESULT InitD3D( HWND hWnd )
{
// Create the D3D object.
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

// Create the D3DDevice
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}

// Turn on the zbuffer
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );

// Turn on ambient lighting
g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0xffffffff );

return S_OK;
}


HRESULT InitGeometry()
{
LPD3DXBUFFER pD3DXMtrlBuffer;

// Load the mesh from the specified file
if( FAILED( D3DXLoadMeshFromX( "tiger.x", D3DXMESH_MANAGED,
g_pd3dDevice, NULL,
&pD3DXMtrlBuffer, NULL, &g_dwNumMaterials,
&g_pMesh ) ) )
{
// If model is not in current folder, try parent folder
if( FAILED( D3DXLoadMeshFromX( "..\tiger.x", D3DXMESH_MANAGED,
g_pd3dDevice, NULL,
&pD3DXMtrlBuffer, NULL, &g_dwNumMaterials,
&g_pMesh ) ) )
{
MessageBox( NULL, "Could not find tiger.x", "Meshes.exe", MB_OK );
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;
if( d3dxMaterials[i].pTextureFilename != NULL &&
lstrlenA( d3dxMaterials[i].pTextureFilename ) > 0 )
{
// Create the texture
if( FAILED( D3DXCreateTextureFromFileA( g_pd3dDevice,
d3dxMaterials[i].pTextureFilename,
&g_pMeshTextures[i] ) ) )
{
// If texture is not in current folder, try parent folder
const CHAR* strPrefix = "..\\";
CHAR strTexture[MAX_PATH];
strcpy_s( strTexture, MAX_PATH, strPrefix );
strcat_s( strTexture, MAX_PATH, d3dxMaterials[i].pTextureFilename );
// If texture is not in current folder, try parent folder
if( FAILED( D3DXCreateTextureFromFileA( g_pd3dDevice,
strTexture,
&g_pMeshTextures[i] ) ) )
{
MessageBox( NULL, "Could not find texture map", "Meshes.exe", MB_OK );
}
}
}
}

// Done with the material buffer
pD3DXMtrlBuffer->Release();

return S_OK;
}





VOID Cleanup()
{
if( g_pMeshMaterials != NULL )
delete[] g_pMeshMaterials;

if( g_pMeshTextures )
{
for( DWORD i = 0; i < g_dwNumMaterials; i++ )
{
if( g_pMeshTextures[i] )
g_pMeshTextures[i]->Release();
}
delete[] g_pMeshTextures;
}
if( g_pMesh != NULL )
g_pMesh->Release();

if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();

if( g_pD3D != NULL )
g_pD3D->Release();
}



//-----------------------------------------------------------------------------
VOID SetupMatrices()
{
// Set up world matrix
D3DXMATRIXA16 matWorld;
//D3DXMatrixRotationY( &matWorld, timeGetTime() / 1000.0f );
//D3DXMatrixTranslation(&matWorld,15.0f*cosf((2.0f*D3DX_PI)*timeGetTime() / 3000.0f),0.0f,
//15.0f*sinf((2.0f*D3DX_PI)*timeGetTime() / 3000.0f));
D3DXMatrixTranslation(&matWorld,0.0f,0.0f,0.0f);
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

// Set up our view matrix. A view matrix can be defined given an eye point,
// a point to lookat, and a direction for which way is up. Here, we set the
// eye five units back along the z-axis and up three units, look at the
// origin, and define "up" to be in the y-direction.
D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-25.0f );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );

D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, 1.0f, 1.0f, 1000.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}




VOID Render()
{
// Clear the backbuffer and the zbuffer
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );

// Begin the scene
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
// Setup the world, view, and projection matrices
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
g_pd3dDevice->SetMaterial( &g_pMeshMaterials[i] );
g_pd3dDevice->SetTexture( 0, g_pMeshTextures[i] );

// Draw the mesh subset
g_pMesh->DrawSubset( i );
}

// End the scene
g_pd3dDevice->EndScene();
}

// Present the backbuffer contents to the display
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}




LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
}

return DefWindowProc( hWnd, msg, wParam, lParam );
}




INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
UNREFERENCED_PARAMETER( hInst );

// Register the window class
WNDCLASSEX wc =
{
sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
"D3D Tutorial", NULL
};
RegisterClassEx( &wc );

// Create the application's window
HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 06: Meshes",
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
NULL, NULL, wc.hInstance, NULL );

// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Create the scene geometry
if( SUCCEEDED( InitGeometry() ) )
{
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );

// Enter the message loop
MSG msg;
ZeroMemory( &msg, sizeof( msg ) );
while( msg.message != WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
Render();
}
}
}

UnregisterClass( "D3D Tutorial", wc.hInstance );
return 0;
}

...全文
318 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ryfdizuo 2011-05-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hfchen890705 的回复:]

我试过把x文件放到工程文件目录中了,调试的时候还是找不到文件,但把x文件和编译后生成的exe文件放在一起,exe就能运行也能加载得到 x文件,就是调试的时候到加载x文件那一步就停了
[/Quote]
工程属性的工作目录是不是改过了。
hfchen890705 2011-05-13
  • 打赏
  • 举报
回复
会不会是我的vs设置有问题?
hfchen890705 2011-05-13
  • 打赏
  • 举报
回复
我试过把x文件放到工程文件目录中了,调试的时候还是找不到文件,但把x文件和编译后生成的exe文件放在一起,exe就能运行也能加载得到 x文件,就是调试的时候到加载x文件那一步就停了
ryfdizuo 2011-05-13
  • 打赏
  • 举报
回复
if( FAILED( D3DXLoadMeshFromX( "tiger.x", D3DXMESH_MANAGED,
===
需要把x文件放到和工程文件同级目录下。
hfchen890705 2011-05-13
  • 打赏
  • 举报
回复
问题解决了,原来要在调试之前把文件话在工程目录下才行- -,谢谢大家
zldrobit 2011-05-13
  • 打赏
  • 举报
回复
把x文件放到Debug目录下试试?

而且路径也有问题"..\tiger.x"应该改成"..\\tiger.x"

实在不行试试绝对路径? 多放几个文件夹?

楼主如果觉得满意的话,有空到我博客看看呀:
关于C语言实现面向对象的: http://blog.csdn.net/zldrobit/archive/2011/05/12/6415143.aspx
hfchen890705 2011-05-12
  • 打赏
  • 举报
回复
我用的是vs2005和vs2010,编译都能通过,问题就是,调试的时候,却说找不到x文件,就在下面这段代码中退出来了
if( FAILED( D3DXLoadMeshFromX( "tiger.x", D3DXMESH_MANAGED,
g_pd3dDevice, NULL,
&pD3DXMtrlBuffer, NULL, &g_dwNumMaterials,
&g_pMesh ) ) )
{
// If model is not in current folder, try parent folder
if( FAILED( D3DXLoadMeshFromX( "..\tiger.x", D3DXMESH_MANAGED,
g_pd3dDevice, NULL,
&pD3DXMtrlBuffer, NULL, &g_dwNumMaterials,
&g_pMesh ) ) )
{
MessageBox( NULL, "Could not find tiger.x", "Meshes.exe", MB_OK );
return E_FAIL;
}
我就想是不是文件路径的问题,后来我把x文件和生成的.exe放在同一个文件夹中,在文件夹中执行.exe文件,结果程序就能运行了,可是没有纹理,后来我又把纹理文件放到和x文件跟.exe文件一起,结果就能显示纹理了,可是在vs中调试的时候仍然是说找不到x文件,到底是怎么回事啊,求大哥们指点下

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧