0001: // ----------------------------------------------------------------------------
0002: //
0003: // load.cpp - Load the Object
0004: // 
0005: // Copyright (c) 2002 IMAGIRE Takashi (imagire@gmail.com)
0006: // All Rights Reserved.
0007: //
0008: // ----------------------------------------------------------------------------
0009: #define STRICT
0010: 
0011: #include "main.h"
0012: #include "load.h"
0013: 
0014: // ----------------------------------------------------------------------------
0015: // Declaration
0016: // ----------------------------------------------------------------------------
0017: // Original model
0018: typedef struct {
0019:     float x,y,z;
0020:     float nx,ny,nz;
0021:     float tu0,tv0;
0022: }D3DVERTEX;
0023: #define D3DFVF_VERTEX       (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)
0024: 
0025: // D3D_CUSTOMVERTEX
0026: DWORD dwDecl[] = {
0027:     D3DVSD_STREAM(0),
0028:     D3DVSD_REG( D3DVSDE_POSITION,   D3DVSDT_FLOAT3 ),           // D3DVSDE_POSITION = 0
0029:     D3DVSD_REG( D3DVSDE_BLENDWEIGHT,D3DVSDT_FLOAT4 ),           // D3DVSDE_BLENDWEIGHT = 1
0030:     D3DVSD_REG( D3DVSDE_NORMAL,     D3DVSDT_FLOAT3 ),           // D3DVSDE_NORMAL,  = 3
0031:     D3DVSD_REG( D3DVSDE_TEXCOORD0,  D3DVSDT_FLOAT2 ),           // D3DVSDE_TEXCOORD0= 7
0032:     D3DVSD_END()
0033: };
0034: 
0035: // ----------------------------------------------------------------------------
0036: // モデル
0037: // ----------------------------------------------------------------------------
0038: HRESULT CMyMesh::Load(LPDIRECT3DDEVICE8 lpD3DDev, char *filename)
0039: {
0040:     LPD3DXMESH pMesh, pMeshOpt;
0041:     LPD3DXBUFFER pD3DXMtrlBuffer = NULL;
0042:     DWORD i;
0043:     HRESULT hr;
0044:     
0045:     this->Release();
0046:     hr = D3DXLoadMeshFromX(filename, D3DXMESH_MANAGED,
0047:                                 lpD3DDev, NULL,
0048:                                 &pD3DXMtrlBuffer, &this->dwNumMaterials,
0049:                                 &pMesh);
0050:     if(FAILED(hr)) return E_FAIL;
0051: 
0052:     // sort the data
0053:     pMesh->Optimize(D3DXMESHOPT_ATTRSORT, NULL, NULL, NULL, NULL, &pMeshOpt);
0054:     RELEASE(pMesh);
0055: 
0056:     // search the size of the mesh
0057:     LPDIRECT3DVERTEXBUFFER8 pVB;
0058:     BYTE* pVByte;
0059:     pMeshOpt->GetVertexBuffer(&pVB);
0060:     pVB->Lock(0,0,&pVByte,0);
0061:     D3DXComputeBoundingSphere(pVByte, pMeshOpt->GetNumVertices(), pMeshOpt->GetFVF(), &this->center, &this->radius);
0062:     pVB->Unlock();
0063:     pVB->Release();
0064: 
0065:     pMeshOpt->GetAttributeTable(NULL,&this->dwNumMaterials);
0066:     this->pSubsetTable = new D3DXATTRIBUTERANGE[this->dwNumMaterials];
0067:     pMeshOpt->GetAttributeTable(this->pSubsetTable, &this->dwNumMaterials);
0068: 
0069:     // transformation of the FVF
0070:     hr = pMeshOpt->CloneMeshFVF(pMeshOpt->GetOptions(), D3DFVF_VERTEX, lpD3DDev, &pMesh);
0071:     if(FAILED(hr)) return E_FAIL;
0072:     RELEASE(pMeshOpt);
0073:     D3DXComputeNormals(pMesh,NULL);
0074: 
0075:     // Copy to the Vertex Buffer
0076:     D3DVERTEX* pSrc;
0077:     D3D_CUSTOMVERTEX* pDest;
0078:     LPDIRECT3DINDEXBUFFER8 pSrcIndex;
0079:     WORD* pISrc;
0080:     WORD* pIDest;
0081: 
0082:     DWORD nMeshVertices = pMesh->GetNumVertices();
0083:     DWORD nMeshFaces = pMesh->GetNumFaces();
0084:     lpD3DDev->CreateVertexBuffer(nMeshVertices * sizeof(D3D_CUSTOMVERTEX),
0085:                             D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &this->pVB);
0086:     lpD3DDev->CreateIndexBuffer(nMeshFaces * 3 * sizeof(WORD),0,D3DFMT_INDEX16,D3DPOOL_MANAGED,&this->pIndex);
0087: 
0088:     pMesh->GetVertexBuffer(&pVB);
0089:     pVB->Lock(0,0,(BYTE**)&pSrc,0);
0090:     this->pVB->Lock(0,0,(BYTE**)&pDest,0);
0091:     for(i=0;i<nMeshVertices;i++){
0092:         pDest->position[0] = pSrc->x;
0093:         pDest->position[1] = pSrc->y;
0094:         pDest->position[2] = pSrc->z;
0095:         pDest->position[3] = 1.0f;
0096:         pDest->texcoord0[0] = pSrc->tu0;
0097:         pDest->texcoord0[1] = pSrc->tv0;
0098: 
0099:         pSrc++;
0100:         pDest++;
0101:     }
0102:     pVB->Unlock();
0103:     pVB->Release();
0104:     this->pVB->Unlock();
0105: 
0106:     // Copy the Index buffer
0107:     pMesh->GetIndexBuffer(&pSrcIndex);
0108:     pSrcIndex->Lock(0,0,(BYTE**)&pISrc,0);
0109:     this->pIndex->Lock(0,0,(BYTE**)&pIDest,0);
0110:     CopyMemory(pIDest,pISrc,nMeshFaces * 3 * sizeof(WORD));
0111:     pSrcIndex->Unlock();
0112:     this->pIndex->Unlock();
0113:     pSrcIndex->Release();
0114: 
0115:     // Read the material information
0116:     D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
0117:     this->pTextures = new LPDIRECT3DTEXTURE8[this->dwNumMaterials];
0118:     this->pMaterials = new D3DMATERIAL8[this->dwNumMaterials];
0119: 
0120:     for(i = 0; i < this->dwNumMaterials; i++){
0121:         this->pMaterials[i] = d3dxMaterials[i].MatD3D;
0122:         this->pMaterials[i].Ambient = this->pMaterials[i].Diffuse;
0123:         hr = D3DXCreateTextureFromFile( lpD3DDev, 
0124:                                         d3dxMaterials[i].pTextureFilename, 
0125:                                         &this->pTextures[i] );
0126:         if(FAILED(hr)) this->pTextures[i] = NULL;
0127:     }
0128:     RELEASE(pD3DXMtrlBuffer);
0129:     
0130:     RELEASE(pMesh);
0131:     
0132:     this->bActive = true;       // OK. Use this mesh!
0133:     
0134:     return S_OK;
0135: }
0136: // ----------------------------------------------------------------------------
0137: void CMyMesh::Release()
0138: {
0139:     DWORD i;
0140: 
0141:     if(this->pVB == NULL) return;
0142: 
0143:     for(i=0; i<this->dwNumMaterials; i++){
0144:         RELEASE(this->pTextures[i]);
0145:     }
0146:     delete[] this->pTextures;
0147:     delete[] this->pMaterials;
0148:     delete[] this->pSubsetTable;
0149: 
0150:     RELEASE(this->pVB);
0151:     RELEASE(this->pIndex);
0152:     
0153:     this->bActive = false;
0154: }
0155: // ----------------------------------------------------------------------------
0156: // Texture
0157: // ----------------------------------------------------------------------------
0158: HRESULT CTextureMgr::Load(LPDIRECT3DDEVICE8 lpD3DDev, const char *filename, LPDIRECT3DTEXTURE8 *ppTexture)
0159: {
0160:     D3DXCreateTextureFromFileEx(lpD3DDev, filename,0,0,0,0,D3DFMT_A8R8G8B8,
0161:                                 D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR,
0162:                                 0, NULL, NULL, ppTexture);
0163:     
0164:     return S_OK;
0165: }
0166: // ----------------------------------------------------------------------------
0167: void CTextureMgr::Release(LPDIRECT3DTEXTURE8 pTexture)
0168: {
0169:     RELEASE(pTexture);
0170: }
0171: