Skip to content

Commit 3ef042b

Browse files
committed
整理了代码,将绘制更新相关的代码和状态转移到mesh类中,解决了一次加载所有动作文件,通过代码控制只播放某一段动作的问题,下一步加入entity类表示动态物体,可以共享同一mesh但有不同的位置和动画,也需要增加同一entity做出不同动作的功能
1 parent a530dfb commit 3ef042b

14 files changed

+278118
-290
lines changed

Camera.VC.db

0 Bytes
Binary file not shown.

CameraDemo.cpp

+20-166
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "Camera.h"
2222
#include "skinnedmesh.h"
2323
#include "mesh.h"
24-
#include "D3DCompiler.h"
24+
2525

2626
long long m_startTime;
2727
bool start = false;
@@ -48,15 +48,6 @@ class CameraApp : public D3DApp
4848
public:
4949
SkinnedMesh* skinnedmesh;
5050
Mesh* background_mesh;
51-
ID3DX11Effect* mFX;
52-
ID3DX11EffectTechnique* mTech;
53-
ID3DX11Effect* mStaticMeshFX;
54-
ID3DX11EffectTechnique* mStaticMeshTech;
55-
ID3DX11EffectMatrixVariable* mfxWorldViewProj;
56-
ID3DX11EffectShaderResourceVariable* DiffuseMap;
57-
ID3DX11EffectMatrixVariable* m_StaticMesh_fxWorldViewProj;
58-
ID3DX11EffectShaderResourceVariable* StaticMesh_DiffuseMap;
59-
ID3DX11EffectMatrixVariable* BoneTransforms;
6051

6152
CameraApp(HINSTANCE hInstance);
6253
~CameraApp();
@@ -98,33 +89,13 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
9889
return theApp.Run();
9990
}
10091

101-
ID3D11InputLayout* mInputLayout;
102-
const D3D11_INPUT_ELEMENT_DESC PosTexSkinned[4] =
103-
{
104-
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
105-
//{"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
106-
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
107-
{"WEIGHTS", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0},
108-
{"BONEINDICES", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 36, D3D11_INPUT_PER_VERTEX_DATA, 0}
109-
};
110-
111-
112-
ID3D11InputLayout* mInputLayout_staticmesh;
113-
const D3D11_INPUT_ELEMENT_DESC PosTex[2] =
114-
{
115-
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
116-
//{"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
117-
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
118-
};
119-
120-
12192
CameraApp::CameraApp(HINSTANCE hInstance)
12293
: D3DApp(hInstance)
12394
{
12495
mMainWndCaption = L"Camera Demo";
12596
mLastMousePos.x = 0;
12697
mLastMousePos.y = 0;
127-
mCam.SetPosition(0.0f, 30.0f, -0.0f);
98+
mCam.SetPosition(0.0f, 10.0f, 20.0f);
12899
}
129100

130101
CameraApp::~CameraApp()
@@ -149,74 +120,14 @@ bool CameraApp::Init()
149120
L"Textures/floor.dds", 0, 0, &mFloorTexSRV, 0 ));
150121
BuildSkullGeometryBuffers();
151122
*/
152-
DWORD shaderFlags = 0;
153-
#if defined( DEBUG ) || defined( _DEBUG )
154-
shaderFlags |= D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
155-
#endif
156-
157-
ID3D10Blob* compiledShader = 0;
158-
ID3D10Blob* compilationMsgs = 0;
159-
HRESULT hr;
160-
hr = D3DX11CompileFromFile(L"FX/color.fx", 0, 0, 0, "fx_5_0", shaderFlags,0, 0, &compiledShader, &compilationMsgs, 0);
161-
// compilationMsgs can store errors or warnings.
162-
if (compilationMsgs != 0)
163-
{
164-
MessageBoxA(0, (char*)compilationMsgs->GetBufferPointer(), 0, 0);
165-
ReleaseCOM(compilationMsgs);
166-
}
167-
// Even if there are no compilationMsgs, check to make sure there were no other errors.
168-
if (FAILED(hr))
169-
{
170-
DXTrace(__FILE__, (DWORD)__LINE__, hr, L"D3DX11CompileFromFile", true);
171-
}
172-
HR(D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(),
173-
0, md3dDevice, &mFX));
174-
175-
// Done with compiled shader.
176-
ReleaseCOM(compiledShader);
177-
178-
hr = D3DX11CompileFromFile(L"FX/staticMesh.fx", 0, 0, 0, "fx_5_0", shaderFlags, 0, 0, &compiledShader, &compilationMsgs, 0);
179-
// compilationMsgs can store errors or warnings.
180-
if (compilationMsgs != 0)
181-
{
182-
MessageBoxA(0, (char*)compilationMsgs->GetBufferPointer(), 0, 0);
183-
ReleaseCOM(compilationMsgs);
184-
}
185-
// Even if there are no compilationMsgs, check to make sure there were no other errors.
186-
if (FAILED(hr))
187-
{
188-
DXTrace(__FILE__, (DWORD)__LINE__, hr, L"D3DX11CompileFromFile", true);
189-
}
190-
HR(D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(),0, md3dDevice, &mStaticMeshFX));
191-
192-
// Done with compiled shader.
193-
ReleaseCOM(compiledShader);
194-
195-
mTech = mFX->GetTechniqueByName("ColorTech");
196-
mfxWorldViewProj = mFX->GetVariableByName("gWorldViewProj")->AsMatrix();
197-
DiffuseMap = mFX->GetVariableByName("gDiffuseMap")->AsShaderResource();
198-
BoneTransforms = mFX->GetVariableByName("gBoneTransforms")->AsMatrix();
199-
200-
mStaticMeshTech = mStaticMeshFX->GetTechniqueByName("StaticMeshTech");
201-
m_StaticMesh_fxWorldViewProj = mStaticMeshFX->GetVariableByName("gWorldViewProj")->AsMatrix();
202-
StaticMesh_DiffuseMap = mStaticMeshFX->GetVariableByName("gDiffuseMap")->AsShaderResource();
203-
204-
D3DX11_PASS_DESC passDesc;
205-
mTech->GetPassByIndex(0)->GetDesc(&passDesc);
206-
HR(md3dDevice->CreateInputLayout(PosTexSkinned, 4, passDesc.pIAInputSignature, passDesc.IAInputSignatureSize, &mInputLayout));
207-
208-
209-
mStaticMeshTech->GetPassByIndex(0)->GetDesc(&passDesc);
210-
HR(md3dDevice->CreateInputLayout(PosTex, 2, passDesc.pIAInputSignature, passDesc.IAInputSignatureSize, &mInputLayout_staticmesh));
211123

212124
skinnedmesh = new SkinnedMesh();
213-
skinnedmesh->device = md3dDevice;
214-
const char filename[256] = "assert\\mesh\\DAORU.DAE";
215-
skinnedmesh->LoadMesh(filename);
125+
skinnedmesh->Init(md3dDevice);
126+
skinnedmesh->LoadMesh("assert\\mesh\\cloud_all_action.DAE");
127+
216128
background_mesh = new Mesh();
217-
background_mesh->device = md3dDevice;
218-
const char filename1[256] = "assert\\mesh\\1V1.DAE";
219-
background_mesh->LoadMesh(filename1);
129+
background_mesh->Init(md3dDevice);
130+
background_mesh->LoadMesh("assert\\mesh\\1V1.DAE");
220131
return true;
221132
}
222133

@@ -242,86 +153,29 @@ void CameraApp::UpdateScene(float dt)
242153
if( GetAsyncKeyState('D') & 0x8000 )
243154
mCam.Strafe(10.0f*dt);
244155

245-
float RunningTime = GetRunningTime();
156+
mCam.UpdateViewMatrix();
246157

158+
XMMATRIX view = mCam.View();
159+
XMMATRIX proj = mCam.Proj();
160+
XMMATRIX viewProj = mCam.ViewProj();
161+
XMMATRIX world = XMMatrixIdentity();
162+
XMMATRIX worldViewProj = world*view*proj;
163+
skinnedmesh->Update(dt, worldViewProj);
164+
background_mesh->Update(dt, worldViewProj);
165+
float RunningTime = GetRunningTime();
247166
skinnedmesh->BoneTransform(RunningTime, skinnedmesh->Transforms);
248167
}
249168

250169
void CameraApp::DrawScene()
251170
{
252171
md3dImmediateContext->ClearRenderTargetView(mRenderTargetView, reinterpret_cast<const float*>(&Colors::Silver));
253172
md3dImmediateContext->ClearDepthStencilView(mDepthStencilView, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);
254-
255-
256-
md3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
257-
258-
md3dImmediateContext->RSSetState(RenderStates::WireframeRS);
173+
//md3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
174+
//md3dImmediateContext->RSSetState(RenderStates::WireframeRS);
259175
//md3dImmediateContext->OMSetDepthStencilState(RenderStates::MarkMirrorDSS, 1);
260176
//md3dImmediateContext->OMSetDepthStencilState(0, 0);
261-
UINT stride = sizeof(Vertex::Basic32);
262-
UINT offset = 0;
263-
264-
mCam.UpdateViewMatrix();
265-
266-
XMMATRIX view = mCam.View();
267-
XMMATRIX proj = mCam.Proj();
268-
XMMATRIX viewProj = mCam.ViewProj();
269-
270-
//绘制背景场景环境
271-
D3DX11_TECHNIQUE_DESC techDesc;
272-
mStaticMeshTech = mStaticMeshFX->GetTechniqueByName("StaticMeshTech");
273-
m_StaticMesh_fxWorldViewProj = mStaticMeshFX->GetVariableByName("gWorldViewProj")->AsMatrix();
274-
StaticMesh_DiffuseMap = mStaticMeshFX->GetVariableByName("gDiffuseMap")->AsShaderResource();
275-
md3dImmediateContext->IASetInputLayout(mInputLayout_staticmesh);
276-
for (int i = 0; i < background_mesh->m_Entries.size(); i++)
277-
{
278-
mStaticMeshTech->GetDesc(&techDesc);
279-
for (UINT p = 0; p < techDesc.Passes; ++p)
280-
{
281-
UINT stride = sizeof(Vertex::Basic32);
282-
UINT offset = 0;
283-
stride = sizeof(MeshVertex);
284-
offset = 0;
285-
XMMATRIX world = XMMatrixIdentity();
286-
XMMATRIX rotation = XMMatrixRotationX(-0);
287-
XMMATRIX translation = XMMatrixTranslation(-0, -0, -0);
288-
world = world * rotation * translation;
289-
XMMATRIX worldViewProj = world*view*proj;
290-
m_StaticMesh_fxWorldViewProj->SetMatrix(reinterpret_cast<float*>(&worldViewProj));
291-
StaticMesh_DiffuseMap = mStaticMeshFX->GetVariableByName("gDiffuseMap")->AsShaderResource();
292-
ID3D11ShaderResourceView* tex = background_mesh->m_Textures[background_mesh->m_Entries[i].MaterialIndex]->mDiffuseMapSRV;
293-
StaticMesh_DiffuseMap->SetResource(tex);
294-
md3dImmediateContext->IASetVertexBuffers(0, 1, &background_mesh->m_Entries[i].mVB, &stride, &offset);
295-
md3dImmediateContext->IASetIndexBuffer(background_mesh->m_Entries[i].mIB, DXGI_FORMAT_R32_UINT, 0);
296-
mStaticMeshTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);
297-
md3dImmediateContext->DrawIndexed(background_mesh->m_Entries[i].m_Indices.size(), 0, 0);
298-
}
299-
}
300-
md3dImmediateContext->RSSetState(0);
301-
//绘制人物动画
302-
md3dImmediateContext->IASetInputLayout(mInputLayout);
303-
BoneTransforms->SetMatrixArray(reinterpret_cast<const float*>(&skinnedmesh->Transforms[0]), 0, skinnedmesh->Transforms.size());
304-
for (int i = 0; i < skinnedmesh->m_Entries.size(); i++)
305-
{
306-
mTech->GetDesc(&techDesc);
307-
for (UINT p = 0; p < techDesc.Passes; ++p)
308-
{
309-
UINT stride = sizeof(Vertex::Basic32);
310-
UINT offset = 0;
311-
stride = sizeof(SkinnedVertex);
312-
offset = 0;
313-
XMMATRIX world = XMMatrixIdentity();
314-
XMMATRIX worldViewProj = world*view*proj;
315-
mfxWorldViewProj->SetMatrix(reinterpret_cast<float*>(&worldViewProj));
316-
DiffuseMap = mFX->GetVariableByName("gDiffuseMap")->AsShaderResource();
317-
ID3D11ShaderResourceView* tex = skinnedmesh->m_Textures[skinnedmesh->m_Entries[i].MaterialIndex]->mDiffuseMapSRV;
318-
DiffuseMap->SetResource(tex);
319-
md3dImmediateContext->IASetVertexBuffers(0, 1, &skinnedmesh->m_Entries[i].mVB, &stride, &offset);
320-
md3dImmediateContext->IASetIndexBuffer(skinnedmesh->m_Entries[i].mIB, DXGI_FORMAT_R32_UINT, 0);
321-
mTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);
322-
md3dImmediateContext->DrawIndexed(skinnedmesh->m_Entries[i].m_Indices.size(), 0, 0);
323-
}
324-
}
177+
background_mesh->Render(md3dImmediateContext);
178+
skinnedmesh->Render(md3dImmediateContext);
325179

326180
HR(mSwapChain->Present(0, 0));
327181
}

assert/mesh/cloud.jpg

15 KB
Loading

0 commit comments

Comments
 (0)