本文由zhangbaochong原创,转载请注明出处:http://www.cnblogs.com/zhangbaochong/p/5788482.html
现在directx已经不再支持.x文件了,意味着D3DXLoadMeshFromX加载mesh的方法已经不能用了。要加载mesh除了自己解析文件外,最简单的方法是利用微软开源的工具DirectXTK中的Model类或者DXUT中的CDXUTSDKMesh类。这里以DirectXTK为例,看看如何加载的吧!
1.格式转化
DirectXTK中的Model类支持.sdkmesh和.cmo格式,所以下载的.obj、.fbx等格式的文件必须转化成支持的格式才行。利用vs2015中的content pipeline可以很方便的转化成.cmo,下面是具体步骤:
首先,右键项目->生成依赖项->生成自定义->勾上MeshContentTask->点击ok
添加模型文件到项目工程中,这里以.fbx文件为例,右键模型文件,在常规中选择Mesh Content Pipeline,然后确定。
最后,右键模型选择编译,然后就可以在Debug目录下(如果没改生成目录)找到.cmo以及贴图等相关文件了,是不是很简单呢?
2.加载Mesh
首先要去下载DirectXTK,然后在工程中添加引用。
用DirectXTK加载mesh其实很简单,主要就是两个步骤:加载和绘制。
加载需要调用Model类的一个方法,针对sdkmesh是CreateFromSDKMESH方法,针对cmo就是CreateFromCMO了。这里以sdkmesh格式为例,cmo与此类似不再说明了。
CreateFromSDKMESH函数参数如下:
std::unique_ptr<Model> DirectX::Model::CreateFromSDKMESH( ID3D11Device* d3dDevice,
const wchar_t* szFileName,
IEffectFactory& fxFactory,
bool ccw,
bool pmalpha )
第一个参数是一个设备指针,第二个参数是模型路径,第三个参数是一个IEffectFactory,后面两个参数设为true。
绘制时需要调用Model::Draw方法,参数如下:
Model::Draw(
ID3D11DeviceContext* deviceContext,
CommonStates& states,
FXMMATRIX world,
CXMMATRIX view,
CXMMATRIX projection,
bool wireframe,
std::function<void()> setCustomState )
第一个参数是设备上下文指针,第二个参数是CommonStates对象,接下来三个参数是world view proj矩阵,下个参数是是否采用线框模式绘制默认为false,最后一个
参数默认为nullptr。
下面是全部代码,其中使用了上个教程实现的Camera:
MeshDemo.h
#pragma once
#include <memory>
#include "Dx11Base.h"
#include "CommonStates.h"
#include "Model.h"
#include "Effects.h"
#include "Camera.h" class MeshDemo : public Dx11Base
{
public:
MeshDemo(HINSTANCE hInst, std::wstring title = L"BlendDemo", int width = , int height = );
~MeshDemo(); bool Init() override;
void Update(float dt);
void Render(); bool OnResize() override; void OnMouseDown(WPARAM btnState, int x, int y);
void OnMouseUp(WPARAM btnState, int x, int y);
void OnMouseMove(WPARAM btnState, int x, int y); private:
bool BuildModels(); //创建mesh对象
private:
std::unique_ptr<Model> m_model;
std::unique_ptr<EffectFactory> m_fxFactory;
std::unique_ptr<CommonStates> m_states; Camera m_camera; XMFLOAT4X4 m_world;
XMFLOAT4X4 m_view;
XMFLOAT4X4 m_proj; //鼠标控制参数
POINT m_lastMousePos;
};
MeshDemo.cpp
#include "MeshDemo.h"
#include "Utility.h"
using namespace DirectX; MeshDemo::MeshDemo(HINSTANCE hInst, std::wstring title, int width, int height)
:Dx11Base(hInst,title,width,height)
{
XMVECTOR Eye = XMVectorSet(0.0f, 3.0f, -10.0f, 0.0f);
XMVECTOR At = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
XMVECTOR Up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
m_camera.LookAtXM(Eye, At, Up);
//设置投影矩阵
m_camera.SetLens(XM_PIDIV4, AspectRatio(), 0.1f, .f); XMStoreFloat4x4(&m_world, XMMatrixIdentity());
} MeshDemo::~MeshDemo()
{
} bool MeshDemo::Init()
{
if (!Dx11Base::Init())
return false;
if (!BuildModels())
return false;
return true;
} void MeshDemo::Update(float dt)
{
//前后左右行走
if (KeyDown('A'))
{
m_camera.Strafe(-.f*dt);
}
else if (KeyDown('D'))
{
m_camera.Strafe(.f*dt);
}
if (KeyDown('W'))
{
m_camera.Walk(.f*dt);
}
else if (KeyDown('S'))
{
m_camera.Walk(-.f*dt);
}
m_camera.UpdateViewMatrix(); XMStoreFloat4x4(&m_view, m_camera.GetView());
XMStoreFloat4x4(&m_proj, m_camera.GetProj());
} void MeshDemo::Render()
{
m_pImmediateContext->ClearRenderTargetView(m_pRenderTargetView, Colors::Silver);
m_pImmediateContext->ClearDepthStencilView(m_pDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, ); XMVECTOR qid = XMQuaternionIdentity();
const XMVECTORF32 scale = { 0.01f, 0.01f, 0.01f };
const XMVECTORF32 translate = { .f, .f, .f };
XMMATRIX world = XMLoadFloat4x4(&m_world);
XMVECTOR rotate = XMQuaternionRotationRollPitchYaw(, XM_PI / .f, XM_PI / .f);
rotate = XMQuaternionRotationRollPitchYaw(, XM_PI / .f, XM_PI / .f);
XMMATRIX local = XMMatrixMultiply(world, XMMatrixTransformation(
g_XMZero, qid, scale, g_XMZero, rotate, translate));
local *= XMMatrixRotationZ(XM_PIDIV2);
m_model->Draw(m_pImmediateContext, *m_states, local, XMLoadFloat4x4(&m_view),
XMLoadFloat4x4(&m_proj)); m_pSwapChain->Present(, );
} bool MeshDemo::OnResize()
{
if (!Dx11Base::OnResize())
return false;
//更新camera参数
m_camera.SetLens(XM_PIDIV4, AspectRatio(), .f, .f); return true;
} void MeshDemo::OnMouseDown(WPARAM btnState, int x, int y)
{
m_lastMousePos.x = x;
m_lastMousePos.y = y;
SetCapture(m_hWnd);
} void MeshDemo::OnMouseUp(WPARAM btnState, int x, int y)
{
ReleaseCapture();
} void MeshDemo::OnMouseMove(WPARAM btnState, int x, int y)
{
if ((btnState & MK_LBUTTON) != )
{
float dx = XMConvertToRadians(0.25f*(x - m_lastMousePos.x));
float dy = XMConvertToRadians(0.25f*(y - m_lastMousePos.y)); m_camera.Pitch(dy);
m_camera.RotateY(dx);
} m_lastMousePos.x = x;
m_lastMousePos.y = y;
} bool MeshDemo::BuildModels()
{
m_fxFactory.reset(new EffectFactory(m_pd3dDevice));
m_states.reset(new CommonStates(m_pd3dDevice));
m_model = Model::CreateFromSDKMESH(m_pd3dDevice, L"tiny.sdkmesh", *m_fxFactory,true,true);
return true;
} int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int cmdShow)
{
std::shared_ptr<Dx11Base> bd(new MeshDemo(hInstance));
if (!bd->Init())
return -;
return bd->Run();
}
3.效果截图