VOID Cleanup() { // meshes 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_pMeshMaterialsSecond != NULL ) delete[] g_pMeshMaterialsSecond; if ( g_pMeshTexturesSecond ) { for ( DWORD i = 0; i < g_dwNumMaterialsSecond; ++i ) { if ( g_pMeshTexturesSecond[i] ) g_pMeshTexturesSecond[i]->Release(); } delete[] g_pMeshTexturesSecond; } if ( g_pMeshSecond != NULL ) g_pMeshSecond->Release(); // cylinder if ( g_pTexture != NULL ) g_pTexture->Release(); if ( g_pVB != NULL ) g_pVB->Release(); if ( g_pd3dDevice != NULL ) g_pd3dDevice->Release(); if ( g_pD3D != NULL ) g_pD3D->Release(); }
void D3D9Mesh::CreateText(const char *string, char *FontName, int FontHeight, float deviation, float depth, bool bold, bool italic) { //just call the DirectX function to create the text FreeMemory(); LPD3DXMESH TextMesh; HDC hdc = CreateCompatibleDC( NULL ); HFONT hFont; HFONT hFontOld; INT nHeight = -MulDiv( FontHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72 ); hFont = CreateFont(nHeight, 0, 0, 0, bold ? FW_BOLD : FW_NORMAL, italic, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, FontName); hFontOld = (HFONT)SelectObject(hdc, hFont); if(FAILED(D3DXCreateText(GetD3DDevice(), hdc, string, deviation, depth, &TextMesh, NULL, NULL))) _asm int 3; SelectObject(hdc, hFontOld); DeleteObject( hFont ); DeleteDC( hdc ); TextMesh->CloneMeshFVF(D3DMeshOptions, D3DMeshFVF, GetD3DDevice(), &_Mesh); TextMesh->Release(); SetColor(RGBColor::White); GenerateNormals(); }
//----------------------------------------------------------------------------- // Desc: 释放创建的对象 //----------------------------------------------------------------------------- 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(); //释放Direct3D设备对象 if( g_pd3dDevice != NULL ) g_pd3dDevice->Release(); //释放Direct3D对象 if( g_pD3D != NULL ) g_pD3D->Release(); }
//------------------------------------------------------------------------------------------------ // Name: obtainSourceGeometry // Desc: Loads geometry from the source file into the output subset geometry //------------------------------------------------------------------------------------------------ bool XMesh::obtainSourceGeometry(LPDIRECT3DDEVICE9 pd3dDevice, SubsetGeometry* subsetGeometry) const { // Fail without a device or if something is wrong with the output pointer if (APP_ERROR(!pd3dDevice || !subsetGeometry)("Invalid paramter to obtainSourceGeometry") || APP_ERROR(!subsetGeometry->empty())("Provided geometry subset for obtainSourceGeometry must be empty")) return false; // Check to make sure a file exists (if not, just exit) if (mySourceFile.getValue().empty()) return true; // Keeps track of how many subsets this mesh contains DWORD subsets = 0; // Stores the mesh that was loaded from the file LPD3DXMESH pXMesh = NULL; // Load the mesh from the specified file if (APP_ERROR(D3DXLoadMeshFromX(mySourceFile.getValue().c_str(), D3DXMESH_SYSTEMMEM, pd3dDevice, NULL, NULL, NULL, &subsets, &pXMesh))("XMesh couldn't load \"%s\"", mySourceFile.getValue().c_str())) return false; // Convert the mesh bool succeeded = buildGeometryFromD3DXMesh(pXMesh, subsetGeometry, subsets); // Release the mesh pXMesh->Release(); // Return an error code if an error occurred if (APP_ERROR(!succeeded)("Unable to build geometry for mesh from \"%s\"", mySourceFile.getValue().c_str())) return false; // Success return true; }
VOID Cleanup() { if ( NULL != g_pMeshMaterials0 ) { delete[] g_pMeshMaterials0; } if ( g_pMeshTextures0 ) { for ( DWORD i = 0; i < g_dwNumMaterials; ++i ) { if ( g_pMeshTextures0[i] ) { g_pMeshTextures0[i]->Release(); } } delete[] g_pMeshTextures0; } if ( NULL != g_pMesh0 ) { g_pMesh0->Release(); } if ( NULL != g_pD3DDevice ) { g_pD3DDevice->Release(); } if ( NULL != g_pD3D ) { g_pD3D->Release(); } }
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(); } } } if ( g_pMesh ) { g_pMesh->Release(); } if ( g_pd3dDevice != NULL ) { g_pd3dDevice->Release(); } if ( g_pD3D != NULL ) { g_pD3D->Release(); } }
//----------------------------------------------------------------------------- // Name: shutDown() // Desc: //----------------------------------------------------------------------------- void shutDown_1( void ) { if( g_pTeapotMesh_1 != NULL ) g_pTeapotMesh_1->Release(); if( g_pd3dDevice_1 != NULL ) g_pd3dDevice_1->Release(); }
LPD3DXMESH SkyBox::CreateMappedSphere(float fRad, UINT slices, UINT stacks) { // create the sphere LPD3DXMESH mesh; if (FAILED(D3DXCreateSphere(GameManager::GetDevice( ), fRad, slices, stacks, &mesh, NULL))) return NULL; // create a copy of the mesh with texture coordinates, // since the D3DX function doesn't include them LPD3DXMESH texMesh; if (FAILED(mesh->CloneMeshFVF(D3DXMESH_SYSTEMMEM, FVF_PositionNormalTexture::FVF, GameManager::GetDevice( ), &texMesh))) return mesh; // failed, return un-textured mesh mesh->Release( ); // finished with the original mesh, release it // lock the vertex buffer FVF_PositionNormalTexture* pVerts; //if (SUCCEEDED(texMesh->LockVertexBuffer(0, (BYTE **)&pVerts))) if (SUCCEEDED(texMesh->LockVertexBuffer(0, (LPVOID*)&pVerts))) { int numVerts = texMesh->GetNumVertices( ); // get vertex count // loop through the vertices for (int i = 0; i < numVerts; i++) { // calculate texture coordinates pVerts->tex.x = asinf(pVerts->normal.x) / D3DX_PI + 0.5f; pVerts->tex.y = asinf(pVerts->normal.y) / D3DX_PI + 0.5f; //pVerts->tex.x = 0.5f - (atan2f(pVerts->normal.z, pVerts->normal.x) / (2 * D3DX_PI)); //pVerts->tex.y = 0.5f - asinf(pVerts->normal.y) / (D3DX_PI); //pVerts->tex.y = pVerts->normal.y * 0.5 + 0.5; //if (pVerts->tex.x <(FLOAT)0.9) pVerts->tex.x = (FLOAT)0.0; //pVerts->tex.x = pVerts->pos.x / sqrtf((pVerts->pos.x * pVerts->pos.x) + (pVerts->pos.y * pVerts->pos.x) + (pVerts->pos.z * pVerts->pos.z)); //pVerts->tex.y = pVerts->pos.y / sqrtf((pVerts->pos.x * pVerts->pos.x) + (pVerts->pos.y * pVerts->pos.x) + (pVerts->pos.z * pVerts->pos.z)); //float theta = asinf(pVerts->normal.z); //float phi = atan2(pVerts->normal.y, pVerts->normal.x); // //pVerts->tex = D3DXVECTOR2(phi / 2 / 3.14159265, theta / 3.14159265); // go to next vertex pVerts++; } texMesh->UnlockVertexBuffer( ); // unlock the vertex buffer } // return pointer to caller return texMesh; }
void Game_End() { //free memory and shut down mesh->Release(); DirectSound_Shutdown(); DirectInput_Shutdown(); Direct3D_Shutdown(); }
void Cleanup() { // 폰트를 release 한다. if (gpFont) { gpFont->Release(); gpFont = NULL; } // 모델을 release 한다. if (gpSphere) { gpSphere->Release(); gpSphere = NULL; } // 쉐이더를 release 한다. if (gpNormalMappingShader) { gpNormalMappingShader->Release(); gpNormalMappingShader = NULL; } // 텍스처를 release 한다. if (gpStoneDM) { gpStoneDM->Release(); gpStoneDM = NULL; } if (gpStoneSM) { gpStoneSM->Release(); gpStoneSM = NULL; } if (gpStoneNM) { gpStoneNM->Release(); gpStoneNM = NULL; } // D3D를 release 한다. if (gpD3DDevice) { gpD3DDevice->Release(); gpD3DDevice = NULL; } if (gpD3D) { gpD3D->Release(); gpD3D = NULL; } }
//----------------------------------------------------------------------------- // Name: shutDown() // Desc: //----------------------------------------------------------------------------- void shutDown_0( void ) { if( g_pTeapotMesh_0 != NULL ) g_pTeapotMesh_0->Release(); if( g_pd3dDevice_0 != NULL ) g_pd3dDevice_0->Release(); if( g_pD3D != NULL ) g_pD3D->Release(); }
void cleanD3d(void) { d3d9VertexBuffer1->Release(); d3d9VertexBuffer2->Release(); if (mesh1 != NULL) mesh1->Release(); if (mesh2 != NULL) mesh2->Release(); if (mesh3 != NULL) mesh3->Release(); if (texture1 != NULL) texture1->Release(); if (texture2 != NULL) texture2->Release(); if (texture3 != NULL) texture3->Release(); if (sprite != NULL) sprite->Release(); if (spriteTexture != NULL) spriteTexture->Release(); if (legendSprite != NULL) legendSprite->Release(); if (legendTexture != NULL) legendTexture->Release(); for (DWORD i = 0; i < numMaterials; i++) // customized cause I use textures from resources if ((i != 1) && (i != 3) && (i != 4) && (i != 6) && planeTexture[i] != NULL) planeTexture[i]->Release(); d3d9dev->Release(); d3d9->Release(); return; }
void D3D9Mesh::CreateTeapot(float radius) { //just call the DirectX function to create the teapot FreeMemory(); LPD3DXMESH teapot; D3DXCreateTeapot(GetD3DDevice(), &teapot, NULL); teapot->CloneMeshFVF(D3DMeshOptions, D3DMeshFVF, GetD3DDevice(), &_Mesh); teapot->Release(); Stretch(radius); SetColor(RGBColor::White); GenerateNormals(); }
void CBoundBox::CreateBox( const D3DXVECTOR3 *pVMin, const D3DXVECTOR3 *pVMax ) { m_fWidth = pVMax->x - pVMin->x; m_fHeight = pVMax->y - pVMin->y; m_fDepth = pVMax->z - pVMin->z; LPD3DXMESH tmpMesh; D3DXCreateBox( m_pDevice, m_fWidth, m_fHeight, m_fDepth, &tmpMesh, NULL ); tmpMesh->CloneMeshFVF( tmpMesh->GetOptions(), D3DFVF_XYZ|D3DFVF_DIFFUSE, m_pDevice, &m_box ); tmpMesh->CloneMeshFVF( tmpMesh->GetOptions(), D3DFVF_XYZ|D3DFVF_DIFFUSE, m_pDevice, &m_boxOrig ); tmpMesh->Release(); }
//----------------------------------------------------------------------------- // Name: Cleanup() // Desc: Releases all previously initialized objects //----------------------------------------------------------------------------- VOID Cleanup() { if( g_pMeshMaterials != NULL ) delete[] g_pMeshMaterials; if( g_pMeshMaterials2 != NULL ) delete[] g_pMeshMaterials2; if( g_pMeshTextures ) { for( DWORD i = 0; i < g_dwNumMaterials; i++ ) { if( g_pMeshTextures[i] ) g_pMeshTextures[i]->Release(); } delete[] g_pMeshTextures; } if( g_pMeshTextures2 ) { for( DWORD i = 0; i < g_dwNumMaterials2; i++ ) { if( g_pMeshTextures2[i] ) g_pMeshTextures2[i]->Release(); } delete[] g_pMeshTextures2; } if( g_pMesh != NULL ) g_pMesh->Release(); if( g_pMesh2 != NULL ) g_pMesh2->Release(); if( g_pd3dDevice != NULL ) g_pd3dDevice->Release(); if( g_pD3D != NULL ) g_pD3D->Release(); }
PROTECTED void _MDLOptimize(LPD3DXMESH mesh, const LPD3DXBUFFER pAdjacencyBuffer, int numInd, LPD3DXMESH *optMesh) { HRESULT hr; DWORD *rgdwAdjacencyTemp = 0; LPD3DXMESH tempMesh; DWORD dw32Bit = mesh->GetOptions() & D3DXMESH_32BIT; // allocate a second adjacency buffer to store post attribute sorted adjacency if(MemAlloc((void**)&rgdwAdjacencyTemp, sizeof(DWORD)*numInd, M_ZERO) != RETCODE_SUCCESS) { ASSERT_MSG(0, "Unable to allocate rgdwAdjacencyTemp", "Error in _MDLOptimize"); goto End; } // attribute sort - the un-optimized mesh option // remember the adjacency for the vertex cache optimization hr = mesh->OptimizeInplace( D3DXMESHOPT_COMPACT|D3DXMESHOPT_ATTRSORT, (DWORD*)pAdjacencyBuffer->GetBufferPointer(), rgdwAdjacencyTemp, NULL, NULL); if( FAILED(hr) ) goto End; // snapshot the attribute sorted mesh, shown as the un-optimized version hr = mesh->CloneMeshFVF( dw32Bit|D3DXMESH_MANAGED, mesh->GetFVF(), g_p3DDevice, &tempMesh ); if( FAILED(hr) ) goto End; // actually do the vertex cache optimization hr = mesh->OptimizeInplace( D3DXMESHOPT_COMPACT|D3DXMESHOPT_ATTRSORT|D3DXMESHOPT_VERTEXCACHE, rgdwAdjacencyTemp, NULL, NULL, NULL); if( FAILED(hr) ) goto End; // snapshot as the optimized mesh hr = mesh->CloneMeshFVF( dw32Bit|D3DXMESH_MANAGED, mesh->GetFVF(), g_p3DDevice, optMesh ); if( FAILED(hr) ) goto End; End: if(rgdwAdjacencyTemp) MemFree((void**)&rgdwAdjacencyTemp); if(tempMesh) tempMesh->Release(); }
void D3D9Mesh::NPatchEnhance(float segs, bool quadratic) { DWORD *adj; Unlock(); //get the _Mesh adjacency (needed by NPatchEnhance) GenerateAdj(adj); LPD3DXMESH sphere; //do the n-patching using the DirectX function D3DXTessellateNPatches(_Mesh, adj, segs, quadratic, &sphere, NULL); FreeMemory(); //load the data into sphere->CloneMeshFVF(D3DMeshOptions, D3DMeshFVF, GetD3DDevice(), &_Mesh); sphere->Release(); delete[] adj; }
void D3D9Mesh::LoadFromXFile(const String &Filename) { String FilenameCopy = Filename; FreeMemory(); LPD3DXBUFFER Adjacency, Materials, EffectInstances; DWORD MaterialCount; LPD3DXMESH LoadedMesh; //call the DirectX function to load from an XFile, D3DXLoadMeshFromX(FilenameCopy.CString(), D3DMeshOptions, GetD3DDevice(), &Adjacency, &Materials, &EffectInstances, &MaterialCount, &LoadedMesh); //get rid of the components we don't care about Adjacency->Release(); Materials->Release(); EffectInstances->Release(); //and copy the part we do care about (the _Mesh) into our _Mesh in the right format. LoadedMesh->CloneMeshFVF(D3DMeshOptions, D3DMeshFVF, GetD3DDevice(), &_Mesh); LoadedMesh->Release(); }
void D3D9Mesh::SimplifyToVertices(UINT Count) { Lock(); Unlock(); DWORD *Adj; Clean(1e-6f, Adj); LPD3DXMESH NewMesh = NULL; D3DAlwaysValidate(D3DXSimplifyMesh(_Mesh, Adj, NULL, NULL, Count, D3DXMESHSIMP_VERTEX, &NewMesh), "D3DXSimplifyMesh"); _Mesh->Release(); D3DAlwaysValidate(NewMesh->CloneMeshFVF(D3DMeshOptions, D3DMeshFVF, GetD3DDevice(), &_Mesh), "CloneMeshFVF"); NewMesh->Release(); delete[] Adj; Lock(); Unlock(); }
void D3D9Mesh::Clean(float Epsilon, DWORD* &AdjDataOut) { Assert(_Mesh != NULL, "Clean called on empty _Mesh."); LPD3DXMESH NewMesh; DWORD *AdjData = new DWORD[3 * _Mesh->GetNumFaces()]; AdjDataOut = new DWORD[3 * _Mesh->GetNumFaces()]; DWORD *FaceRemap = new DWORD[3 * _Mesh->GetNumFaces()]; /*D3DXWELDEPSILONS Eps; Eps.Position = Epsilon; Eps.BlendWeights = 1.0f; Eps.Normal = 1.0f; Eps.PSize = 1.0f; Eps.Specular = 1.0f; Eps.Diffuse = 1.0f; Eps.Tangent = 1.0f; Eps.Binormal = 1.0f; Eps.TessFactor = 1.0f; for(UINT _Indices = 0; _Indices < 8; _Indices++) { Eps.Texcoord[_Indices] = 1.0f; }*/ CleanVerticesAndTriangles(); Unlock(); //D3DAlwaysValidate(_Mesh->GenerateAdjacency(Epsilon, AdjDataOut)); //D3DAlwaysValidate(D3DXWeldVertices(_Mesh, D3DXWELDEPSILONS_WELDPARTIALMATCHES, &Eps, AdjDataOut, AdjData, FaceRemap, NULL)); //D3DAlwaysValidate(D3DXWeldVertices(_Mesh, D3DXWELDEPSILONS_WELDALL | D3DXWELDEPSILONS_WELDPARTIALMATCHES, NULL, AdjDataOut, AdjData, FaceRemap, NULL)); //CleanTriangles(); D3DAlwaysValidate(_Mesh->GenerateAdjacency(Epsilon, AdjData), "GenerateAdjacency"); D3DAlwaysValidate(D3DXCleanMesh(D3DXCLEAN_SIMPLIFICATION, _Mesh, AdjData, &NewMesh, AdjDataOut, NULL), "D3DXCleanMesh"); _Mesh->Release(); D3DAlwaysValidate(NewMesh->CloneMeshFVF(D3DMeshOptions, D3DMeshFVF, GetD3DDevice(), &_Mesh), "CloneMeshFVF"); NewMesh->Release(); Lock(); Unlock(); delete[] AdjData; delete[] FaceRemap; }
//----------------------------------------------------------------------------- // Name: Cleanup() // Desc: Releases all previously initialized objects //----------------------------------------------------------------------------- VOID Cleanup() { if (g_pMeshTextures != NULL) { for (int i = 0; i < g_dwNumMaterials; i++) { g_pMeshTextures[i]->Release(); } delete[] g_pMeshTextures; } if (g_pMesh != NULL) { g_pMesh->Release(); } if (g_pEffect != NULL) { g_pEffect->Release(); } if (g_pd3dDevice != NULL) g_pd3dDevice->Release(); if (g_pD3D != NULL) g_pD3D->Release(); }
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_pDevice != NULL) g_pDevice->Release(); if(g_pD3D != NULL) g_pD3D->Release(); }
VOID Cleanup() { if ( g_pMeshMaterials ) { delete [] g_pMeshMaterials; g_pMeshMaterials = NULL; } if ( g_pMeshTextures ) { for (int i = 0; i < g_materialNums; ++i) { if ( !g_pMeshTextures[i] ) continue; g_pMeshTextures[i]->Release(); g_pMeshTextures[i] = NULL; } } if ( g_pMesh ) { g_pMesh->Release(); g_pMesh = NULL; } if ( g_pd3dDevice ) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; } if ( g_pD3D ) { g_pD3D->Release(); g_pD3D = NULL; } }
void Cleanup() { // 폰트를 release 한다. if(gpFont) { gpFont->Release(); gpFont = NULL; } // 모델을 release 한다. if (gpSphere) { gpSphere->Release(); gpSphere = NULL; } // 쉐이더를 release 한다. if (gpTextureMappingShader) { gpTextureMappingShader->Release(); gpTextureMappingShader = NULL; } // 텍스처를 release 한다. if (gpEarthDM) { gpEarthDM->Release(); gpEarthDM = NULL; } // D3D를 release 한다. if(gpD3DDevice) { gpD3DDevice->Release(); gpD3DDevice = NULL; } if(gpD3D) { gpD3D->Release(); gpD3D = NULL; } }
VOID Cleanup( ) { if (NULL != g_pFont) { g_pFont->Release(); } if (NULL != g_pMesh) { g_pMesh->Release(); } if ( NULL != g_pD3DDevice ) { g_pD3DDevice->Release(); } if ( NULL != g_pD3D ) { g_pD3D->Release(); } }
//============================================================================= // メッシュのコンバート関数 //============================================================================ void CInheritanceHierarchy::ConvertMesh(LPD3DXMESH* pMesh) { LPDIRECT3DDEVICE9 *pDevice = CRenderer::GetDevice(); // クローン作製 D3DVERTEXELEMENT9 elements[] = { // 頂点ストリーム(パイプライン)番号, オフセット(頂点の型の先頭からのバイト数), データ型, DEFAULTでOK, 使用用途, 使用用途が同じものを複数使うときに仕分ける番号 { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 }, { 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDWEIGHT, 0 }, { 0, 24, D3DDECLTYPE_UBYTE4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDINDICES, 0 }, { 0, 28, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 }, { 0, 40, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 }, { 0, 48, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 }, D3DDECL_END() // 定義終了 絶対必要 }; HRESULT hr = 0; LPD3DXMESH pOldMesh = *pMesh; hr = pOldMesh->CloneMesh(D3DXMESH_MANAGED , elements , *pDevice , pMesh); pOldMesh->Release(); }
//----------------------------------------------------------------------------- // Name: Cleanup() // Desc: Releases all previously initialized objects //----------------------------------------------------------------------------- VOID Cleanup() { // release all textures used if ( g_pTexture != NULL ) g_pTexture->Release(); if ( g_pTexture2 != NULL ) g_pTexture2->Release(); if ( marbleTexture != NULL ) marbleTexture->Release(); if ( backgroundTexture != NULL ) backgroundTexture->Release(); 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 Cleanup() { if (g_pMeshMaterials) 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) g_pMesh->Release(); if (g_pd3dDevice) g_pd3dDevice->Release(); if (g_pD3D) g_pD3D->Release(); }
HRESULT InitGeometry() { LPD3DXBUFFER pD3DXMtrlBuffer; if ( FAILED( D3DXLoadMeshFromX( L"./Resource/girl.x", D3DXMESH_SYSTEMMEM, g_pd3dDevice, NULL, &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials, &g_pMesh ) ) ) { MessageBox( NULL, L"Could not find girl.x", L"D3D Tutorial", 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; } if ( !( g_pMesh->GetFVF() & D3DFVF_NORMAL ) ) { //가지고 있지 않다면 메쉬를 복제하고 D3DFVF_NORMAL을 추가한다. ID3DXMesh* pTempMesh = 0; g_pMesh->CloneMeshFVF( D3DXMESH_MANAGED, g_pMesh->GetFVF() | D3DFVF_NORMAL, g_pd3dDevice, &pTempMesh ); // 법선을 계산한다. D3DXComputeNormals( pTempMesh, 0 ); g_pMesh->Release(); // 기존메쉬를 제거한다 g_pMesh = pTempMesh; // 기존메쉬를 법선이 계산된 메쉬로 지정한다. } for ( DWORD i = 0; i < g_dwNumMaterials; ++i ) { g_pMeshMaterials[i] = d3dxMaterials[i].MatD3D; g_pMeshMaterials[i].Ambient = g_pMeshMaterials[i].Diffuse; g_pMeshTextures[i] = NULL; if ( d3dxMaterials[i].pTextureFilename != NULL && lstrlenA( d3dxMaterials[i].pTextureFilename ) > 0 ) { if ( FAILED( D3DXCreateTextureFromFileA( g_pd3dDevice, d3dxMaterials[i].pTextureFilename, &g_pMeshTextures[i] ) ) ) { const CHAR* strPrefix = "./Resource/"; CHAR strTexture[MAX_PATH]; strcpy_s( strTexture, MAX_PATH, strPrefix ); strcat_s( strTexture, MAX_PATH, d3dxMaterials[i].pTextureFilename ); if ( FAILED( D3DXCreateTextureFromFileA( g_pd3dDevice, strTexture, &g_pMeshTextures[i] ) ) ) { MessageBox( NULL, L"Could not find texture map", L"D3D Tutorial", MB_OK ); } } } } pD3DXMtrlBuffer->Release(); return S_OK; }
//----------------------------------------------------------------------------- // Name: InitDeviceObjects() // Desc: Initialize scene objects. //----------------------------------------------------------------------------- HRESULT CMeshRender::InitDeviceObjects() { DWORD cVerticesPerMesh; // Load mesh LPD3DXBUFFER pAdjacencyBuffer = NULL; LPDIRECT3DVERTEXBUFFER9 pVertexBuffer = NULL; LPD3DXMESH pMesh = NULL; LPD3DXPMESH pPMesh = NULL; LPD3DXMESH pTempMesh; LPD3DXBUFFER pD3DXMtrlBuffer = NULL; void* pVertices; TCHAR strMediaPath[512]; HRESULT hr; DWORD dw32BitFlag; DWORD cVerticesMin; DWORD cVerticesMax; DWORD iPMesh; D3DXWELDEPSILONS Epsilons; DWORD i; D3DXMATERIAL* d3dxMaterials; // Find the path to the mesh if( FAILED( DXUtil_FindMediaFileCb( strMediaPath, sizeof(strMediaPath), m_strMeshFilename ) ) ) return E_FAIL;//D3DAPPERR_MEDIANOTFOUND; // Load the mesh from the specified file if( FAILED( hr = D3DXLoadMeshFromX( strMediaPath, D3DXMESH_MANAGED, m_pd3dDevice, &pAdjacencyBuffer, &pD3DXMtrlBuffer, NULL, &m_dwNumMaterials, &pMesh ) ) ) { // hide error so that device changes will not cause exit, shows blank screen instead goto End; } dw32BitFlag = (pMesh->GetOptions() & D3DXMESH_32BIT); // perform simple cleansing operations on mesh if( FAILED( hr = D3DXCleanMesh( pMesh, (DWORD*)pAdjacencyBuffer->GetBufferPointer(), &pTempMesh, (DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL ) ) ) { m_dwNumMaterials = 0; goto End; } SAFE_RELEASE(pMesh); pMesh = pTempMesh; // Perform a weld to try and remove excess vertices like the model bigship1.x in the DX9.0 SDK (current model is fixed) // Weld the mesh using all epsilons of 0.0f. A small epsilon like 1e-6 works well too memset(&Epsilons, 0, sizeof(D3DXWELDEPSILONS)); if( FAILED( hr = D3DXWeldVertices( pMesh, 0, &Epsilons, (DWORD*)pAdjacencyBuffer->GetBufferPointer(), (DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL, NULL ) ) ) { m_dwNumMaterials = 0; goto End; } // verify validity of mesh for simplification if( FAILED( hr = D3DXValidMesh( pMesh, (DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL ) ) ) { m_dwNumMaterials = 0; goto End; } // Allocate a material/texture arrays d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer(); m_mtrlMeshMaterials = new D3DMATERIAL9[m_dwNumMaterials]; m_pMeshTextures = new LPDIRECT3DTEXTURE9[m_dwNumMaterials]; // Copy the materials and load the textures for( i=0; i<m_dwNumMaterials; i++ ) { m_mtrlMeshMaterials[i] = d3dxMaterials[i].MatD3D; m_mtrlMeshMaterials[i].Ambient = m_mtrlMeshMaterials[i].Diffuse; // Find the path to the texture and create that texture DXUtil_FindMediaFileCb( strMediaPath, sizeof(strMediaPath), d3dxMaterials[i].pTextureFilename ); if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, strMediaPath, &m_pMeshTextures[i] ) ) ) m_pMeshTextures[i] = NULL; } pD3DXMtrlBuffer->Release(); pD3DXMtrlBuffer = NULL; // Lock the vertex buffer, to generate a simple bounding sphere hr = pMesh->GetVertexBuffer( &pVertexBuffer ); if( FAILED(hr) ) goto End; hr = pVertexBuffer->Lock( 0, 0, &pVertices, D3DLOCK_NOSYSLOCK ); if( FAILED(hr) ) goto End; hr = D3DXComputeBoundingSphere( (D3DXVECTOR3*)pVertices, pMesh->GetNumVertices(), D3DXGetFVFVertexSize(pMesh->GetFVF()), &m_vObjectCenter, &m_fObjectRadius ); pVertexBuffer->Unlock(); pVertexBuffer->Release(); if( FAILED(hr) || m_dwNumMaterials == 0 ) goto End; if ( !(pMesh->GetFVF() & D3DFVF_NORMAL) ) { hr = pMesh->CloneMeshFVF( dw32BitFlag|D3DXMESH_MANAGED, pMesh->GetFVF() | D3DFVF_NORMAL, m_pd3dDevice, &pTempMesh ); if (FAILED(hr)) goto End; D3DXComputeNormals( pTempMesh, NULL ); pMesh->Release(); pMesh = pTempMesh; } hr = D3DXGeneratePMesh( pMesh, (DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL, NULL, 1, D3DXMESHSIMP_VERTEX, &pPMesh); if( FAILED(hr) ) goto End; cVerticesMin = pPMesh->GetMinVertices(); cVerticesMax = pPMesh->GetMaxVertices(); cVerticesPerMesh = (cVerticesMax - cVerticesMin) / 10; m_cPMeshes = max(1, (DWORD)ceil((cVerticesMax - cVerticesMin) / (float)cVerticesPerMesh)); m_pPMeshes = new LPD3DXPMESH[m_cPMeshes]; if (m_pPMeshes == NULL) { hr = E_OUTOFMEMORY; goto End; } memset(m_pPMeshes, 0, sizeof(LPD3DXPMESH) * m_cPMeshes); // clone full size pmesh hr = pPMesh->ClonePMeshFVF( D3DXMESH_MANAGED | D3DXMESH_VB_SHARE, pPMesh->GetFVF(), m_pd3dDevice, &m_pPMeshFull ); if (FAILED(hr)) goto End; // clone all the separate pmeshes for (iPMesh = 0; iPMesh < m_cPMeshes; iPMesh++) { hr = pPMesh->ClonePMeshFVF( D3DXMESH_MANAGED | D3DXMESH_VB_SHARE, pPMesh->GetFVF(), m_pd3dDevice, &m_pPMeshes[iPMesh] ); if (FAILED(hr)) goto End; // trim to appropriate space hr = m_pPMeshes[iPMesh]->TrimByVertices(cVerticesMin + cVerticesPerMesh * iPMesh, cVerticesMin + cVerticesPerMesh * (iPMesh+1), NULL, NULL); if (FAILED(hr)) goto End; hr = m_pPMeshes[iPMesh]->OptimizeBaseLOD(D3DXMESHOPT_VERTEXCACHE, NULL); if (FAILED(hr)) goto End; } // set current to be maximum number of vertices m_iPMeshCur = m_cPMeshes - 1; hr = m_pPMeshes[m_iPMeshCur]->SetNumVertices(cVerticesMax); if (FAILED(hr)) goto End; hr = m_pPMeshFull->SetNumVertices(cVerticesMax); if (FAILED(hr)) goto End; End: SAFE_RELEASE( pAdjacencyBuffer ); SAFE_RELEASE( pD3DXMtrlBuffer ); SAFE_RELEASE( pMesh ); SAFE_RELEASE( pPMesh ); if (FAILED(hr)) { for (iPMesh = 0; iPMesh < m_cPMeshes; iPMesh++) { SAFE_RELEASE( m_pPMeshes[iPMesh] ); } delete []m_pPMeshes; m_cPMeshes = 0; m_pPMeshes = NULL; SAFE_RELEASE( m_pPMeshFull ) } return hr; }