Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
#include <Windows.h> #include <stdio.h> #include <tchar.h> #include <d3dx9.h> #include "DXUtil.h" #include "SkinnedMesh.h" //----------------------------------------------------------------------------- // Global variables //----------------------------------------------------------------------------- LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device D3DFORMAT g_d3dfmtFullscreen; // Backbuffer-Format HWND g_MainWindowHandle; // GLOBALES WINDOW HANDLE BOOL g_bFullscreen = TRUE; // FULLSCREEN JA/NEIN int g_ScreenWidth = 800; // SCREEN X int g_ScreenHeight = 600; // SCREEN Y CD3DSkinMesh *g_pSkinMesh; D3DXMATRIX g_matView; D3DXMATRIX g_matProj; float g_fTime; HRESULT InitD3D( HWND hWnd ) { // **************** InitD3D ********************* D3DDISPLAYMODE d3ddm; D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL; if(g_bFullscreen == FALSE) g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm); if(SUCCEEDED( g_pD3D->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_R5G6B5, D3DFMT_R5G6B5, FALSE))) { g_d3dfmtFullscreen = D3DFMT_R5G6B5; } d3dpp.Windowed = !g_bFullscreen; d3dpp.BackBufferCount = 1; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16; if( g_bFullscreen ) { d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; d3dpp.hDeviceWindow = g_MainWindowHandle; d3dpp.BackBufferWidth = g_ScreenWidth; d3dpp.BackBufferHeight = g_ScreenHeight; d3dpp.BackBufferFormat = g_d3dfmtFullscreen; } else { d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = d3ddm.Format; } g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_MainWindowHandle, D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice); // **************** LOAD THE MESH & ANIM ********************* g_pSkinMesh = new CD3DSkinMesh(); DXUtil_GetDXSDKMediaPathCb( g_pSkinMesh->m_strInitialDir, sizeof(g_pSkinMesh->m_strInitialDir) ); _tcscpy( g_pSkinMesh->m_strMeshFilename, _T("dwarf1.x") ); TCHAR strMeshPath[MAX_PATH]; HRESULT hr; hr = DXUtil_FindMediaFileCb( strMeshPath, sizeof(strMeshPath), g_pSkinMesh->m_strMeshFilename ); g_pSkinMesh->InitDeviceObjects( &g_pd3dDevice, strMeshPath, NULL, &g_pSkinMesh->m_vObjectCenter, &g_pSkinMesh->m_fObjectRadius ); // **************** SET MATRICES ********************* D3DXMatrixTranslation( &g_pSkinMesh->m_matWorld, -g_pSkinMesh->m_vObjectCenter.x, -g_pSkinMesh->m_vObjectCenter.y, -g_pSkinMesh->m_vObjectCenter.z ); g_pd3dDevice->SetTransform( D3DTS_WORLD, &g_pSkinMesh->m_matWorld ); D3DXVECTOR3 vEye( 0, 0, 5*g_pSkinMesh->m_fObjectRadius ); D3DXVECTOR3 vAt( 0, 0, 0 ); D3DXVECTOR3 vUp( 0, 1, 0 ); D3DXMatrixLookAtLH( &g_matView, &vEye, &vAt, &vUp); g_pd3dDevice->SetTransform( D3DTS_VIEW, &g_matView ); FLOAT fAspect = (float)(800/600); D3DXMatrixPerspectiveFovLH( &g_matProj, D3DX_PI/4, fAspect, g_pSkinMesh->m_fObjectRadius/64.0f, g_pSkinMesh->m_fObjectRadius*200.0f ); g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &g_matProj ); // **************** GLOBAL RENDER STATE ********************* g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE); g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); return S_OK; } //----------------------------------------------------------------------------- // Name: Cleanup() // Desc: Releases all previously initialized objects //----------------------------------------------------------------------------- VOID Cleanup() { if( g_pSkinMesh != NULL ) g_pSkinMesh->DeleteDeviceObjects(); if( g_pd3dDevice != NULL ) g_pd3dDevice->Release(); if( g_pD3D != NULL ) g_pD3D->Release(); } //----------------------------------------------------------------------------- // Name: Render() // Desc: Draws the scene //----------------------------------------------------------------------------- VOID Render() { // RENDER g_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0x0000ffff, 1.0f, 0L ); if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { g_pSkinMesh->FrameMove(g_fTime); g_pSkinMesh->Render(); g_pd3dDevice->EndScene(); } // Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); } //----------------------------------------------------------------------------- // Name: MsgProc() // Desc: The window's message handler //----------------------------------------------------------------------------- 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 ); } //----------------------------------------------------------------------------- // Name: WinMain() // Desc: The application's entry point //----------------------------------------------------------------------------- INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ) { // 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, 0, 0, g_ScreenWidth, g_ScreenHeight, NULL, NULL, hInst, NULL ); g_MainWindowHandle = hWnd; // Initialize Direct3D if( SUCCEEDED( InitD3D( hWnd ) ) ) { // Show the window ShowWindow( hWnd, SW_SHOWDEFAULT ); UpdateWindow( hWnd ); // Enter the message loop MSG msg; ZeroMemory( &msg, sizeof(msg) ); while( msg.message!=WM_QUIT ) { long t1 = timeGetTime(); if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else Render(); long t2 = timeGetTime(); g_fTime = ((float)(t2-t1)*0.001f); } } UnregisterClass( "D3D Tutorial", wc.hInstance ); return 0; } |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
//----------------------------------------------------------------------------- // File: SkinnedMesh-Mesh.h // // Desc: CD3DSkinMesh definition // // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // Name: struct D3DXFRAME_DERIVED // Desc: Structure derived from D3DXFRAME so we can add some app-specific // info that will be stored with each frame //----------------------------------------------------------------------------- struct D3DXFRAME_DERIVED: public D3DXFRAME { D3DXMATRIX CombinedTransformationMatrix; }; //----------------------------------------------------------------------------- // Name: struct D3DXMESHCONTAINER_DERIVED // Desc: Structure derived from D3DXMESHCONTAINER so we can add some app-specific // info that will be stored with each mesh //----------------------------------------------------------------------------- struct D3DXMESHCONTAINER_DERIVED: public D3DXMESHCONTAINER { LPDIRECT3DTEXTURE9* ppTextures; // array of textures, entries are NULL if no texture specified // SkinMesh info LPD3DXMESH pOrigMesh; DWORD NumAttributeGroups; DWORD NumInfl; LPD3DXBUFFER pBoneCombinationBuf; D3DXMATRIX** ppBoneMatrixPtrs; D3DXMATRIX* pBoneOffsetMatrices; DWORD iAttributeSW; // used to denote the split between SW and HW if necessary for non-indexed skinning }; //----------------------------------------------------------------------------- // Name: class CD3DSkinMesh // Desc: skinned mesh class. //----------------------------------------------------------------------------- class CD3DSkinMesh { public: CD3DSkinMesh(); TCHAR m_strMeshFilename[MAX_PATH]; TCHAR m_strInitialDir[MAX_PATH]; D3DXVECTOR3 m_vObjectCenter; // Center of bounding sphere of object FLOAT m_fObjectRadius; // Radius of bounding sphere of object D3DXMATRIX m_matWorld; // mini-API HRESULT InitDeviceObjects(LPDIRECT3DDEVICE9 *m_pd3dDevice, TCHAR *strMeshPath, TCHAR *strSkinnedMeshPath, D3DXVECTOR3 *m_vObjectCenter, FLOAT *m_fObjectRadius); HRESULT FrameMove(float ftime); HRESULT Render(); HRESULT DeleteDeviceObjects(); //public helpers LPD3DXFRAME getFrameRoot() { return m_pFrameRoot; }; HRESULT GenerateSkinnedMesh( D3DXMESHCONTAINER_DERIVED *pMeshContainer); void UpdateSkinningMethod( LPD3DXFRAME pFrameBase); protected: LPDIRECT3DDEVICE9 m_pd3dDevice; LPD3DXFRAME m_pFrameRoot; LPD3DXANIMATIONCONTROLLER m_pAnimController; D3DXMATRIX* m_pBoneMatrices; UINT m_NumBoneMatricesMax; void DrawMeshContainer( LPD3DXMESHCONTAINER pMeshContainerBase, LPD3DXFRAME pFrameBase); void DrawFrame( LPD3DXFRAME pFrame); HRESULT SetupBoneMatrixPointersOnMesh( LPD3DXMESHCONTAINER pMeshContainer ); HRESULT SetupBoneMatrixPointers ( LPD3DXFRAME pFrame ); void UpdateFrameMatrices( LPD3DXFRAME pFrameBase, LPD3DXMATRIX pParentMatrix ); }; |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 |
//----------------------------------------------------------------------------- // File: SkinnedMesh-Mesh.cpp // // Desc: CD3DSkinMesh implementation for sample showing skinned meshes in D3D // // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #define STRICT #include <windows.h> #include <commdlg.h> #include <tchar.h> #include <stdio.h> #include <d3dx9.h> #include <d3dx9anim.h> #include "DXUtil.h" #include "skinnedmesh.h" class CD3DSkinMesh; //----------------------------------------------------------------------------- // Name: class CAllocateHierarchy // Desc: Custom version of ID3DXAllocateHierarchy with custom methods to create // frames and meshcontainers. //----------------------------------------------------------------------------- class CAllocateHierarchy: public ID3DXAllocateHierarchy { public: STDMETHOD(CreateFrame)(THIS_ LPCTSTR Name, LPD3DXFRAME *ppNewFrame); STDMETHOD(CreateMeshContainer)(THIS_ LPCTSTR Name, LPD3DXMESHDATA pMeshData, LPD3DXMATERIAL pMaterials, LPD3DXEFFECTINSTANCE pEffectInstances, DWORD NumMaterials, DWORD *pAdjacency, LPD3DXSKININFO pSkinInfo, LPD3DXMESHCONTAINER *ppNewMeshContainer); STDMETHOD(DestroyFrame)(THIS_ LPD3DXFRAME pFrameToFree); STDMETHOD(DestroyMeshContainer)(THIS_ LPD3DXMESHCONTAINER pMeshContainerBase); CAllocateHierarchy(CD3DSkinMesh *pSkinMesh) :m_pSkinMesh(pSkinMesh) {} public: CD3DSkinMesh* m_pSkinMesh; }; //----------------------------------------------------------------------------- // Name: AllocateName() // Desc: Allocates memory for a string to hold the name of a frame or mesh //----------------------------------------------------------------------------- HRESULT AllocateName( LPCTSTR Name, LPTSTR *pNewName ) { UINT cbLength; if (Name != NULL) { cbLength = lstrlen(Name) + 1; *pNewName = new TCHAR[cbLength]; if (*pNewName == NULL) return E_OUTOFMEMORY; memcpy(*pNewName, Name, cbLength*sizeof(TCHAR)); } else { *pNewName = NULL; } return S_OK; } //----------------------------------------------------------------------------- // Name: CAllocateHierarchy::CreateFrame() // Desc: //----------------------------------------------------------------------------- HRESULT CAllocateHierarchy::CreateFrame(LPCTSTR Name, LPD3DXFRAME *ppNewFrame) { HRESULT hr = S_OK; D3DXFRAME_DERIVED *pFrame; *ppNewFrame = NULL; pFrame = new D3DXFRAME_DERIVED; if (pFrame == NULL) { hr = E_OUTOFMEMORY; goto e_Exit; } hr = AllocateName(Name, &pFrame->Name); if (FAILED(hr)) goto e_Exit; // initialize other data members of the frame D3DXMatrixIdentity(&pFrame->TransformationMatrix); D3DXMatrixIdentity(&pFrame->CombinedTransformationMatrix); pFrame->pMeshContainer = NULL; pFrame->pFrameSibling = NULL; pFrame->pFrameFirstChild = NULL; *ppNewFrame = pFrame; pFrame = NULL; e_Exit: delete pFrame; return hr; } //----------------------------------------------------------------------------- // Name: CAllocateHierarchy::CreateMeshContainer() // Desc: //----------------------------------------------------------------------------- HRESULT CAllocateHierarchy::CreateMeshContainer(LPCTSTR Name, LPD3DXMESHDATA pMeshData, LPD3DXMATERIAL pMaterials, LPD3DXEFFECTINSTANCE pEffectInstances, DWORD NumMaterials, DWORD *pAdjacency, LPD3DXSKININFO pSkinInfo, LPD3DXMESHCONTAINER *ppNewMeshContainer) { HRESULT hr; D3DXMESHCONTAINER_DERIVED *pMeshContainer = NULL; UINT NumFaces; UINT iMaterial; UINT iBone, cBones; LPDIRECT3DDEVICE9 pd3dDevice = NULL; LPD3DXMESH pMesh = NULL; *ppNewMeshContainer = NULL; // this sample does not handle patch meshes, so fail when one is found if (pMeshData->Type != D3DXMESHTYPE_MESH) { hr = E_FAIL; goto e_Exit; } // get the pMesh interface pointer out of the mesh data structure pMesh = pMeshData->pMesh; // this sample does not FVF compatible meshes, so fail when one is found if (pMesh->GetFVF() == 0) { hr = E_FAIL; goto e_Exit; } // allocate the overloaded structure to return as a D3DXMESHCONTAINER pMeshContainer = new D3DXMESHCONTAINER_DERIVED; if (pMeshContainer == NULL) { hr = E_OUTOFMEMORY; goto e_Exit; } memset(pMeshContainer, 0, sizeof(D3DXMESHCONTAINER_DERIVED)); // make sure and copy the name. All memory as input belongs to caller, interfaces can be addref'd though hr = AllocateName(Name, &pMeshContainer->Name); if (FAILED(hr)) goto e_Exit; pMesh->GetDevice(&pd3dDevice); NumFaces = pMesh->GetNumFaces(); // if no normals are in the mesh, add them if (!(pMesh->GetFVF() & D3DFVF_NORMAL)) { pMeshContainer->MeshData.Type = D3DXMESHTYPE_MESH; // pMesh->GetOptions() | // clone the mesh to make room for the normals hr = pMesh->CloneMeshFVF( D3DXMESH_MANAGED, pMesh->GetFVF() | D3DFVF_NORMAL, pd3dDevice, &pMeshContainer->MeshData.pMesh ); if (FAILED(hr)) goto e_Exit; // get the new pMesh pointer back out of the mesh container to use // NOTE: we do not release pMesh because we do not have a reference to it yet pMesh = pMeshContainer->MeshData.pMesh; // now generate the normals for the pmesh D3DXComputeNormals( pMesh, NULL ); } else // if no normals, just add a reference to the mesh for the mesh container { pMeshContainer->MeshData.pMesh = pMesh; pMeshContainer->MeshData.Type = D3DXMESHTYPE_MESH; pMesh->AddRef(); } // allocate memory to contain the material information. This sample uses // the D3D9 materials and texture names instead of the EffectInstance style materials pMeshContainer->NumMaterials = max(1, NumMaterials); pMeshContainer->pMaterials = new D3DXMATERIAL[pMeshContainer->NumMaterials]; pMeshContainer->ppTextures = new LPDIRECT3DTEXTURE9[pMeshContainer->NumMaterials]; pMeshContainer->pAdjacency = new DWORD[NumFaces*3]; if ((pMeshContainer->pAdjacency == NULL) || (pMeshContainer->pMaterials == NULL)) { hr = E_OUTOFMEMORY; goto e_Exit; } memcpy(pMeshContainer->pAdjacency, pAdjacency, sizeof(DWORD) * NumFaces*3); memset(pMeshContainer->ppTextures, 0, sizeof(LPDIRECT3DTEXTURE9) * pMeshContainer->NumMaterials); // if materials provided, copy them if (NumMaterials > 0) { memcpy(pMeshContainer->pMaterials, pMaterials, sizeof(D3DXMATERIAL) * NumMaterials); for (iMaterial = 0; iMaterial < NumMaterials; iMaterial++) { if (pMeshContainer->pMaterials[iMaterial].pTextureFilename != NULL) { TCHAR strTexturePath[MAX_PATH] = _T(""); DXUtil_FindMediaFileCb( strTexturePath, sizeof(strTexturePath), pMeshContainer->pMaterials[iMaterial].pTextureFilename ); if( FAILED( D3DXCreateTextureFromFile( pd3dDevice, strTexturePath, &pMeshContainer->ppTextures[iMaterial] ) ) ) pMeshContainer->ppTextures[iMaterial] = NULL; // don't remember a pointer into the dynamic memory, just forget the name after loading pMeshContainer->pMaterials[iMaterial].pTextureFilename = NULL; } } } else // if no materials provided, use a default one { pMeshContainer->pMaterials[0].pTextureFilename = NULL; memset(&pMeshContainer->pMaterials[0].MatD3D, 0, sizeof(D3DMATERIAL9)); pMeshContainer->pMaterials[0].MatD3D.Diffuse.r = 0.5f; pMeshContainer->pMaterials[0].MatD3D.Diffuse.g = 0.5f; pMeshContainer->pMaterials[0].MatD3D.Diffuse.b = 0.5f; pMeshContainer->pMaterials[0].MatD3D.Specular = pMeshContainer->pMaterials[0].MatD3D.Diffuse; } // if there is skinning information, save off the required data and then setup for HW skinning if (pSkinInfo != NULL) { // first save off the SkinInfo and original mesh data pMeshContainer->pSkinInfo = pSkinInfo; pSkinInfo->AddRef(); pMeshContainer->pOrigMesh = pMesh; pMesh->AddRef(); // Will need an array of offset matrices to move the vertices from the figure space to the bone's space cBones = pSkinInfo->GetNumBones(); pMeshContainer->pBoneOffsetMatrices = new D3DXMATRIX[cBones]; if (pMeshContainer->pBoneOffsetMatrices == NULL) { hr = E_OUTOFMEMORY; goto e_Exit; } // get each of the bone offset matrices so that we don't need to get them later for (iBone = 0; iBone < cBones; iBone++) { pMeshContainer->pBoneOffsetMatrices[iBone] = *(pMeshContainer->pSkinInfo->GetBoneOffsetMatrix(iBone)); } // GenerateSkinnedMesh will take the general skinning information and transform it to a HW friendly version hr = m_pSkinMesh->GenerateSkinnedMesh(pMeshContainer); if (FAILED(hr)) goto e_Exit; } *ppNewMeshContainer = pMeshContainer; pMeshContainer = NULL; e_Exit: SAFE_RELEASE(pd3dDevice); // call Destroy function to properly clean up the memory allocated if (pMeshContainer != NULL) { DestroyMeshContainer(pMeshContainer); } return hr; } //----------------------------------------------------------------------------- // Name: CAllocateHierarchy::DestroyFrame() // Desc: //----------------------------------------------------------------------------- HRESULT CAllocateHierarchy::DestroyFrame(LPD3DXFRAME pFrameToFree) { SAFE_DELETE_ARRAY( pFrameToFree->Name ); SAFE_DELETE( pFrameToFree ); return S_OK; } //----------------------------------------------------------------------------- // Name: CAllocateHierarchy::DestroyMeshContainer() // Desc: //----------------------------------------------------------------------------- HRESULT CAllocateHierarchy::DestroyMeshContainer(LPD3DXMESHCONTAINER pMeshContainerBase) { UINT iMaterial; D3DXMESHCONTAINER_DERIVED *pMeshContainer = (D3DXMESHCONTAINER_DERIVED*)pMeshContainerBase; SAFE_DELETE_ARRAY( pMeshContainer->Name ); SAFE_DELETE_ARRAY( pMeshContainer->pAdjacency ); SAFE_DELETE_ARRAY( pMeshContainer->pMaterials ); SAFE_DELETE_ARRAY( pMeshContainer->pBoneOffsetMatrices ); // release all the allocated textures if (pMeshContainer->ppTextures != NULL) { for (iMaterial = 0; iMaterial < pMeshContainer->NumMaterials; iMaterial++) { SAFE_RELEASE( pMeshContainer->ppTextures[iMaterial] ); } } SAFE_DELETE_ARRAY( pMeshContainer->ppTextures ); SAFE_DELETE_ARRAY( pMeshContainer->ppBoneMatrixPtrs ); SAFE_RELEASE( pMeshContainer->pBoneCombinationBuf ); SAFE_RELEASE( pMeshContainer->MeshData.pMesh ); SAFE_RELEASE( pMeshContainer->pSkinInfo ); SAFE_RELEASE( pMeshContainer->pOrigMesh ); SAFE_DELETE( pMeshContainer ); return S_OK; } //----------------------------------------------------------------------------- // Name: CD3DSkinMesh() // Desc: skin mesh character constructor. Sets attributes for the skn mesh. //----------------------------------------------------------------------------- CD3DSkinMesh::CD3DSkinMesh() { m_pAnimController = NULL; m_pFrameRoot = NULL; m_pBoneMatrices = NULL; m_NumBoneMatricesMax = 0; } //----------------------------------------------------------------------------- // Name: GenerateSkinnedMesh() // Desc: Called either by CreateMeshContainer when loading a skin mesh, or when // changing methods. This function uses the pSkinInfo of the mesh // container to generate the desired drawable mesh and bone combination // table. //----------------------------------------------------------------------------- HRESULT CD3DSkinMesh::GenerateSkinnedMesh(D3DXMESHCONTAINER_DERIVED *pMeshContainer) { HRESULT hr = S_OK; if (pMeshContainer->pSkinInfo == NULL) return hr; SAFE_RELEASE( pMeshContainer->MeshData.pMesh ); SAFE_RELEASE( pMeshContainer->pBoneCombinationBuf ); hr = pMeshContainer->pSkinInfo->ConvertToBlendedMesh ( pMeshContainer->pOrigMesh, D3DXMESH_MANAGED | D3DXMESHOPT_VERTEXCACHE, pMeshContainer->pAdjacency, NULL, NULL, NULL, &pMeshContainer->NumInfl, &pMeshContainer->NumAttributeGroups, &pMeshContainer->pBoneCombinationBuf, &pMeshContainer->MeshData.pMesh ); LPD3DXBONECOMBINATION rgBoneCombinations = reinterpret_cast<LPD3DXBONECOMBINATION>(pMeshContainer->pBoneCombinationBuf->GetBufferPointer()); // look for any set of bone combinations that do not fit the caps for (pMeshContainer->iAttributeSW = 0; pMeshContainer->iAttributeSW < pMeshContainer->NumAttributeGroups; pMeshContainer->iAttributeSW++) { DWORD cInfl = 0; for (DWORD iInfl = 0; iInfl < pMeshContainer->NumInfl; iInfl++) { if (rgBoneCombinations[pMeshContainer->iAttributeSW].BoneId[iInfl] != UINT_MAX) { ++cInfl; } } } return hr; } //----------------------------------------------------------------------------- // Name: DrawMeshContainer() // Desc: Called to render a mesh in the hierarchy //----------------------------------------------------------------------------- void CD3DSkinMesh::DrawMeshContainer(LPD3DXMESHCONTAINER pMeshContainerBase, LPD3DXFRAME pFrameBase) { D3DXMESHCONTAINER_DERIVED *pMeshContainer = (D3DXMESHCONTAINER_DERIVED*)pMeshContainerBase; D3DXFRAME_DERIVED *pFrame = (D3DXFRAME_DERIVED*)pFrameBase; UINT iMaterial; UINT NumBlend; UINT iAttrib; DWORD AttribIdPrev; LPD3DXBONECOMBINATION pBoneComb; UINT iMatrixIndex; D3DXMATRIX matTemp; // first check for skinning if (pMeshContainer->pSkinInfo != NULL) { AttribIdPrev = UNUSED32; pBoneComb = reinterpret_cast<LPD3DXBONECOMBINATION>(pMeshContainer->pBoneCombinationBuf->GetBufferPointer()); // Draw using default vtx processing of the device (typically HW) for (iAttrib = 0; iAttrib < pMeshContainer->NumAttributeGroups; iAttrib++) { NumBlend = 0; for (DWORD i = 0; i < pMeshContainer->NumInfl; ++i) { if (pBoneComb[iAttrib].BoneId[i] != UINT_MAX) { NumBlend = i; } } if (true) { // first calculate the world matrices for the current set of blend weights and get the accurate count of the number of blends for (DWORD i = 0; i < pMeshContainer->NumInfl; ++i) { iMatrixIndex = pBoneComb[iAttrib].BoneId[i]; if (iMatrixIndex != UINT_MAX) { D3DXMatrixMultiply( &matTemp, &pMeshContainer->pBoneOffsetMatrices[iMatrixIndex], pMeshContainer->ppBoneMatrixPtrs[iMatrixIndex] ); m_pd3dDevice->SetTransform( D3DTS_WORLDMATRIX( i ), &matTemp ); } } m_pd3dDevice->SetRenderState(D3DRS_VERTEXBLEND, NumBlend); // lookup the material used for this subset of faces if ((AttribIdPrev != pBoneComb[iAttrib].AttribId) || (AttribIdPrev == UNUSED32)) { m_pd3dDevice->SetMaterial( &pMeshContainer->pMaterials[pBoneComb[iAttrib].AttribId].MatD3D ); m_pd3dDevice->SetTexture( 0, pMeshContainer->ppTextures[pBoneComb[iAttrib].AttribId] ); AttribIdPrev = pBoneComb[iAttrib].AttribId; } // draw the subset now that the correct material and matrices are loaded pMeshContainer->MeshData.pMesh->DrawSubset(iAttrib); } } m_pd3dDevice->SetRenderState(D3DRS_VERTEXBLEND, 0); } else // standard mesh, just draw it after setting material properties { m_pd3dDevice->SetTransform(D3DTS_WORLD, &pFrame->CombinedTransformationMatrix); for (iMaterial = 0; iMaterial < pMeshContainer->NumMaterials; iMaterial++) { m_pd3dDevice->SetMaterial( &pMeshContainer->pMaterials[iMaterial].MatD3D ); m_pd3dDevice->SetTexture( 0, pMeshContainer->ppTextures[iMaterial] ); pMeshContainer->MeshData.pMesh->DrawSubset(iMaterial); } } } //----------------------------------------------------------------------------- // Name: DrawFrame() // Desc: Called to render a frame in the hierarchy //----------------------------------------------------------------------------- void CD3DSkinMesh::DrawFrame(LPD3DXFRAME pFrame) { LPD3DXMESHCONTAINER pMeshContainer; pMeshContainer = pFrame->pMeshContainer; while (pMeshContainer != NULL) { DrawMeshContainer(pMeshContainer, pFrame); pMeshContainer = pMeshContainer->pNextMeshContainer; } if (pFrame->pFrameSibling != NULL) { DrawFrame(pFrame->pFrameSibling); } if (pFrame->pFrameFirstChild != NULL) { DrawFrame(pFrame->pFrameFirstChild); } } //----------------------------------------------------------------------------- // Name: SetupBoneMatrixPointersOnMesh() // Desc: Called to setup the pointers for a given bone to its transformation matrix //----------------------------------------------------------------------------- HRESULT CD3DSkinMesh::SetupBoneMatrixPointersOnMesh(LPD3DXMESHCONTAINER pMeshContainerBase) { UINT iBone, cBones; D3DXFRAME_DERIVED *pFrame; D3DXMESHCONTAINER_DERIVED *pMeshContainer = (D3DXMESHCONTAINER_DERIVED*)pMeshContainerBase; // if there is a skinmesh, then setup the bone matrices if (pMeshContainer->pSkinInfo != NULL) { cBones = pMeshContainer->pSkinInfo->GetNumBones(); pMeshContainer->ppBoneMatrixPtrs = new D3DXMATRIX*[cBones]; if (pMeshContainer->ppBoneMatrixPtrs == NULL) return E_OUTOFMEMORY; for (iBone = 0; iBone < cBones; iBone++) { pFrame = (D3DXFRAME_DERIVED*)D3DXFrameFind(m_pFrameRoot, pMeshContainer->pSkinInfo->GetBoneName(iBone)); if (pFrame == NULL) return E_FAIL; pMeshContainer->ppBoneMatrixPtrs[iBone] = &pFrame->CombinedTransformationMatrix; } } return S_OK; } //----------------------------------------------------------------------------- // Name: SetupBoneMatrixPointers() // Desc: Called to setup the pointers for a given bone to its transformation matrix //----------------------------------------------------------------------------- HRESULT CD3DSkinMesh::SetupBoneMatrixPointers(LPD3DXFRAME pFrame) { HRESULT hr; if (pFrame->pMeshContainer != NULL) { hr = SetupBoneMatrixPointersOnMesh(pFrame->pMeshContainer); if (FAILED(hr)) return hr; } if (pFrame->pFrameSibling != NULL) { hr = SetupBoneMatrixPointers(pFrame->pFrameSibling); if (FAILED(hr)) return hr; } if (pFrame->pFrameFirstChild != NULL) { hr = SetupBoneMatrixPointers(pFrame->pFrameFirstChild); if (FAILED(hr)) return hr; } return S_OK; } //----------------------------------------------------------------------------- // Name: DrawFrame() // Desc: Called to render a frame in the hierarchy //----------------------------------------------------------------------------- void CD3DSkinMesh::UpdateFrameMatrices(LPD3DXFRAME pFrameBase, LPD3DXMATRIX pParentMatrix) { D3DXFRAME_DERIVED *pFrame = (D3DXFRAME_DERIVED*)pFrameBase; if (pParentMatrix != NULL) D3DXMatrixMultiply(&pFrame->CombinedTransformationMatrix, &pFrame->TransformationMatrix, pParentMatrix); else pFrame->CombinedTransformationMatrix = pFrame->TransformationMatrix; if (pFrame->pFrameSibling != NULL) { UpdateFrameMatrices(pFrame->pFrameSibling, pParentMatrix); } if (pFrame->pFrameFirstChild != NULL) { UpdateFrameMatrices(pFrame->pFrameFirstChild, &pFrame->CombinedTransformationMatrix); } } //----------------------------------------------------------------------------- // Name: DrawFrame() // Desc: Called to render a frame in the hierarchy //----------------------------------------------------------------------------- void CD3DSkinMesh::UpdateSkinningMethod(LPD3DXFRAME pFrameBase) { D3DXFRAME_DERIVED *pFrame = (D3DXFRAME_DERIVED*)pFrameBase; D3DXMESHCONTAINER_DERIVED *pMeshContainer; pMeshContainer = (D3DXMESHCONTAINER_DERIVED *)pFrame->pMeshContainer; while (pMeshContainer != NULL) { GenerateSkinnedMesh(pMeshContainer); pMeshContainer = (D3DXMESHCONTAINER_DERIVED *)pMeshContainer->pNextMeshContainer; } if (pFrame->pFrameSibling != NULL) { UpdateSkinningMethod(pFrame->pFrameSibling); } if (pFrame->pFrameFirstChild != NULL) { UpdateSkinningMethod(pFrame->pFrameFirstChild); } } //----------------------------------------------------------------------------- // Name: InitDeviceObjects() // Desc: This creates all device-dependent managed objects, such as managed // textures and managed vertex buffers. //----------------------------------------------------------------------------- HRESULT CD3DSkinMesh::InitDeviceObjects(LPDIRECT3DDEVICE9 *g_pd3dDevice, TCHAR* strMeshPath, TCHAR* strSkinnedMeshFXPath, D3DXVECTOR3* m_vObjectCenter, FLOAT* m_fObjectRadius) { HRESULT hr; m_pd3dDevice = *g_pd3dDevice; CAllocateHierarchy Alloc(this); hr = D3DXLoadMeshHierarchyFromX(strMeshPath, D3DXMESH_MANAGED, m_pd3dDevice, &Alloc, NULL, &m_pFrameRoot, &m_pAnimController); if (FAILED(hr)) return hr; hr = SetupBoneMatrixPointers(m_pFrameRoot); if (FAILED(hr)) return hr; hr = D3DXFrameCalculateBoundingSphere(m_pFrameRoot, m_vObjectCenter, m_fObjectRadius); if (FAILED(hr)) return hr; if (FAILED(hr)) return hr; return S_OK; } //----------------------------------------------------------------------------- // Name: FrameMove() // Desc: Called once per frame, the call is the entry point for animating // the scene. //----------------------------------------------------------------------------- HRESULT CD3DSkinMesh::FrameMove(float ftime) { if (m_pAnimController != NULL) { m_pAnimController->SetTime(m_pAnimController->GetTime()+(ftime*2.5f)); } UpdateFrameMatrices(m_pFrameRoot, &m_matWorld); return S_OK; } //----------------------------------------------------------------------------- // Name: Render() // Desc: Called once per frame, the call is the entry point for 3d // rendering. This function sets up render states, clears the // viewport, and renders the scene. //----------------------------------------------------------------------------- HRESULT CD3DSkinMesh::Render() { DrawFrame(m_pFrameRoot); return S_OK; } //----------------------------------------------------------------------------- // Name: DeleteDeviceObjects() // Desc: Called when the app is exiting, or the device is being changed, // this function deletes any device dependent objects. //----------------------------------------------------------------------------- HRESULT CD3DSkinMesh::DeleteDeviceObjects() { CAllocateHierarchy Alloc(this); D3DXFrameDestroy(m_pFrameRoot, &Alloc); SAFE_RELEASE(m_pAnimController); return S_OK; } |
Werbeanzeige