写了个装载显示xfile的类。有点问题
本帖最后由 kfbyj 于 2013-05-12 17:17:37 编辑 经常导入xfile 就写了个类
装载显示都正常。
为啥摄像机 一移动 用xfile类显示的物体也动。而且是和摄像机方向相反。
摄像机是按照 浅墨 博客里写的,应该没问题。
我写的这个类 也在 浅墨 的 代码里 移动摄像机 装载的物体也不会移动。。
很郁闷啊,找了一下午原因了!
#pragma once
#include <d3d9.h>
#include <d3dx9.h>
#include <vector>
#include "common.h"
class XFileObject
{
public:
XFileObject(IDirect3DDevice9 *d3d9Dev);
~XFileObject();
bool InitXFile(wchar_t *fileName);
bool UpdateXFile(IDirect3DDevice9 *d3d9Dev);
void RenderXFile(D3DXMATRIX *pMatWorld);
bool XFileRelease();
private:
LPDIRECT3DDEVICE9 lpD3dDev_;
float x_,y_,z_;
ID3DXMesh *mesh_;
std::vector<D3DMATERIAL9> mtrls_;
std::vector<IDirect3DTexture9*> textures_;
ID3DXBuffer *adjBuffer_;
ID3DXBuffer *mtrlBuffer_;
DWORD numMtrls_;
};
#include "XFileObject.h"
XFileObject::XFileObject(IDirect3DDevice9 *d3d9Dev)
{
lpD3dDev_ = d3d9Dev;
adjBuffer_ = NULL;
mtrlBuffer_ = NULL;
numMtrls_ = 0;
x_ = 0;
y_ = 0;
z_ = 0;
mesh_ = NULL;
}
XFileObject::~XFileObject()
{
SAFE_RELEASE(adjBuffer_);
SAFE_RELEASE(mtrlBuffer_);
SAFE_RELEASE(mesh_);
}
bool XFileObject::InitXFile(wchar_t *fileName)
{//载入XFile
HRESULT hr;
hr = D3DXLoadMeshFromX(fileName,D3DXMESH_MANAGED,lpD3dDev_,&adjBuffer_,&mtrlBuffer_,0,&numMtrls_,&mesh_);
if (FAILED(hr))
{
MessageBox(0,L"D3DXLoadMeshFromX()---FAILED",L"",0);
return false;
}
D3DXMATERIAL *mtrls = (D3DXMATERIAL*) mtrlBuffer_->GetBufferPointer();
for (int i = 0;i < numMtrls_; ++i)
{
mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse;
mtrls_.push_back(mtrls[i].MatD3D);
if (mtrls[i].pTextureFilename != NULL )
{
IDirect3DTexture9 *tex = NULL;
HRESULT hr = D3DXCreateTextureFromFileA(lpD3dDev_,mtrls[i].pTextureFilename,&tex);
if(FAILED(hr)){
MessageBox(NULL,L"D3DXCreateTextureFromFileA - failed",L"",0);
}
textures_.push_back(tex);
}
else
textures_.push_back(0);
}
mesh_->OptimizeInplace(D3DXMESHOPT_ATTRSORT |
D3DXMESHOPT_COMPACT |
D3DXMESHOPT_VERTEXCACHE,
(DWORD*)adjBuffer_->GetBufferPointer(),
0, 0, 0);
return true;
}
bool XFileObject::UpdateXFile(IDirect3DDevice9 *d3d9Dev)
{
lpD3dDev_ = d3d9Dev;
return true;
}
void XFileObject::RenderXFile(D3DXMATRIX *pMatWorld)
{
lpD3dDev_->SetTransform(D3DTS_WORLD, pMatWorld);
for (int i = 0;i<mtrls_.size();i++)
{
lpD3dDev_->SetMaterial(&mtrls_[i]);
lpD3dDev_->SetTexture(0,textures_[i]);
mesh_->DrawSubset(i);
}
}
先是全局申明
XFileObject *airplane;
然后init函数里
airplane = new XFileObject(g_pD3D9Dev);
airplane->InitXFile(L"airplane.x");
然后render函数里
D3DXMATRIX matAirplane;
D3DXMatrixTranslation(&matAirplane,-10.0f, 10000.0f,-10.0f);
airplane->RenderXFile(&matAirplane);