0001:
0002:
0003:
0004:
0005:
0006:
0007:
0008:
0009: #define STRICT
0010:
0011: #include <windows.h>
0012: #include "main.h"
0013: #include "draw.h"
0014:
0015: void InitBg(LPDIRECT3DDEVICE8 lpD3DDev);
0016: void DrawBg(LPDIRECT3DDEVICE8 lpD3DDev, D3DXMATRIX &mV, D3DXMATRIX &mP );
0017: void CleanBg(LPDIRECT3DDEVICE8 lpD3DDev);
0018:
0019:
0020: LPDIRECT3DVERTEXBUFFER8 pMeshVB = NULL;
0021: LPDIRECT3DINDEXBUFFER8 pMeshIndex = NULL;
0022: D3DXATTRIBUTERANGE *pSubsetTable = NULL;
0023: DWORD nMeshFaces = 0;
0024: DWORD nMeshVertices = 0;
0025: D3DMATERIAL8 *pMeshMaterials = NULL;
0026: LPDIRECT3DTEXTURE8 *pMeshTextures = NULL;
0027: DWORD dwNumMaterials = 0L;
0028:
0029: DWORD hVertexShader=~0;
0030:
0031: FLOAT MeshRadius;
0032:
0033:
0034:
0035:
0036:
0037:
0038:
0039:
0040: typedef struct {
0041: float x,y,z;
0042: float nx,ny,nz;
0043: float tu0,tv0;
0044: }D3DVERTEX;
0045: #define D3DFVF_VERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)
0046:
0047: typedef struct {
0048: float x,y,z;
0049: float nx,ny,nz;
0050: float tu0,tv0;
0051: }D3D_CUSTOMVERTEX;
0052: #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL)
0053:
0054: static DWORD dwDecl[] = {
0055: D3DVSD_STREAM(0),
0056: D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3 ),
0057: D3DVSD_REG(D3DVSDE_NORMAL, D3DVSDT_FLOAT3 ),
0058: D3DVSD_REG(D3DVSDE_TEXCOORD0,D3DVSDT_FLOAT2 ),
0059: D3DVSD_END()
0060: };
0061:
0062:
0063:
0064:
0065:
0066: HRESULT LoadXFile(char* filename, LPDIRECT3DDEVICE8 lpD3DDev)
0067: {
0068: LPD3DXMESH pMesh, pMeshOpt;
0069: LPD3DXBUFFER pD3DXMtrlBuffer = NULL;
0070: DWORD i;
0071: HRESULT hr;
0072:
0073: hr = D3DXLoadMeshFromX(filename, D3DXMESH_MANAGED,
0074: lpD3DDev, NULL,
0075: &pD3DXMtrlBuffer, &dwNumMaterials,
0076: &pMesh);
0077: if(FAILED(hr)) return E_FAIL;
0078:
0079:
0080: pMesh->Optimize(D3DXMESHOPT_ATTRSORT, NULL, NULL, NULL, NULL, &pMeshOpt);
0081: RELEASE(pMesh);
0082:
0083:
0084: pMeshOpt->GetAttributeTable(NULL,&dwNumMaterials);
0085: pSubsetTable = new D3DXATTRIBUTERANGE[dwNumMaterials];
0086: pMeshOpt->GetAttributeTable(pSubsetTable, &dwNumMaterials);
0087:
0088:
0089: hr = pMeshOpt->CloneMeshFVF(pMeshOpt->GetOptions(), D3DFVF_VERTEX, lpD3DDev, &pMesh);
0090: if(FAILED(hr)) return E_FAIL;
0091: RELEASE(pMeshOpt);
0092: D3DXComputeNormals(pMesh, NULL);
0093:
0094:
0095: D3DVERTEX* pSrc;
0096: D3D_CUSTOMVERTEX* pDest;
0097: LPDIRECT3DINDEXBUFFER8 pSrcIndex;
0098: WORD* pISrc;
0099: WORD* pIDest;
0100:
0101: DWORD nMeshVertices = pMesh->GetNumVertices();
0102: DWORD nMeshFaces = pMesh->GetNumFaces();
0103: lpD3DDev->CreateVertexBuffer(nMeshVertices * sizeof(D3D_CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,&pMeshVB);
0104: lpD3DDev->CreateIndexBuffer(nMeshFaces * 3 * sizeof(WORD),0,D3DFMT_INDEX16,D3DPOOL_MANAGED,&pMeshIndex);
0105:
0106: LPDIRECT3DVERTEXBUFFER8 pVB;
0107: pMesh->GetVertexBuffer(&pVB);
0108: pVB->Lock(0,0,(BYTE**)&pSrc,0);
0109: pMeshVB->Lock(0,0,(BYTE**)&pDest,0);
0110: MeshRadius = 0.0f;
0111: for(i=0;i<nMeshVertices;i++){
0112: pDest->x = pSrc->x;
0113: pDest->y = pSrc->y;
0114: pDest->z = pSrc->z;
0115: pDest->nx = pSrc->nx;
0116: pDest->ny = pSrc->ny;
0117: pDest->nz = pSrc->nz;
0118:
0119: FLOAT radius = sqrtf( pSrc->x*pSrc->x + pSrc->y*pSrc->y + pSrc->z*pSrc->z );
0120: if (MeshRadius < radius) MeshRadius = radius;
0121:
0122: pSrc += 1;
0123: pDest += 1;
0124: }
0125: pVB->Unlock();
0126: pVB->Release();
0127: pMeshVB->Unlock();
0128:
0129:
0130: pMesh->GetIndexBuffer(&pSrcIndex);
0131: pSrcIndex->Lock(0,0,(BYTE**)&pISrc,0);
0132: pMeshIndex->Lock(0,0,(BYTE**)&pIDest,0);
0133: CopyMemory(pIDest,pISrc,nMeshFaces * 3 * sizeof(WORD));
0134: pSrcIndex->Unlock();
0135: pMeshIndex->Unlock();
0136: pSrcIndex->Release();
0137:
0138:
0139: D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
0140: pMeshTextures = new LPDIRECT3DTEXTURE8[dwNumMaterials];
0141: pMeshMaterials = new D3DMATERIAL8[dwNumMaterials];
0142:
0143: for(i = 0; i < dwNumMaterials; i++){
0144: pMeshMaterials[i] = d3dxMaterials[i].MatD3D;
0145: pMeshMaterials[i].Ambient = pMeshMaterials[i].Diffuse;
0146: hr = D3DXCreateTextureFromFile( lpD3DDev,
0147: d3dxMaterials[i].pTextureFilename,
0148: &pMeshTextures[i] );
0149: if(FAILED(hr)) pMeshTextures[i] = NULL;
0150: }
0151: RELEASE(pD3DXMtrlBuffer);
0152:
0153: RELEASE(pMesh);
0154:
0155: return S_OK;
0156: }
0157:
0158:
0159:
0160:
0161: HRESULT LoadVertexShader(char* filename, LPDIRECT3DDEVICE8 lpD3DDev, DWORD *phVertexShader, const DWORD *dwDecl)
0162: {
0163: HRESULT hr;
0164: ID3DXBuffer *pshader;
0165:
0166: if ( FAILED(hr = D3DXAssembleShaderFromFile(filename, 0,NULL,&pshader,NULL)) ) return hr;
0167:
0168: hr = lpD3DDev->CreateVertexShader( dwDecl, (DWORD*)pshader->GetBufferPointer(), phVertexShader, 0 );
0169: RELEASE(pshader);
0170: if ( FAILED(hr) ) return hr;
0171:
0172: return S_OK;
0173: }
0174:
0175:
0176:
0177:
0178: HRESULT InitRender(LPDIRECT3DDEVICE8 lpD3DDev)
0179: {
0180: HRESULT hr;
0181:
0182:
0183: if ( FAILED(hr = LoadXFile("nsx.x", lpD3DDev)) ) return hr;
0184:
0185:
0186: if ( FAILED(hr = LoadVertexShader("vs.vsh", lpD3DDev, &hVertexShader, dwDecl)) ) return hr;
0187:
0188: InitBg(lpD3DDev);
0189:
0190: lpD3DDev->SetRenderState(D3DRS_LIGHTING, FALSE);
0191:
0192: lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE);
0193: lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0194: lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE);
0195:
0196: lpD3DDev->SetVertexShader(hVertexShader);
0197: lpD3DDev->SetVertexShaderConstant(12, &D3DXVECTOR4(0.0f, 0.5f, 1.0f, 2.0f), 1);
0198:
0199:
0200: float start = 0.0f;
0201: float end = 1.0f;
0202: lpD3DDev->SetRenderState(D3DRS_FOGENABLE, TRUE);
0203: lpD3DDev->SetRenderState(D3DRS_FOGCOLOR, 0x00ffffff);
0204: lpD3DDev->SetRenderState(D3DRS_FOGSTART, *((DWORD *)(&start)));
0205: lpD3DDev->SetRenderState(D3DRS_FOGEND, *((DWORD *)(&end)) );
0206:
0207: float Near = 0.0f;
0208: float Far = 100.0f;
0209: lpD3DDev->SetVertexShaderConstant(20, &D3DXVECTOR4( -1.0f/(Far-Near), Far/(Far-Near), 0.0f, 0.0f), 1);
0210:
0211: return S_OK;
0212: }
0213:
0214:
0215:
0216:
0217:
0218:
0219: VOID Render(LPDIRECT3DDEVICE8 lpD3DDev)
0220: {
0221: static int cnt = 0;
0222: ++cnt;
0223:
0224: D3DXMATRIX mWorld, mView, mProj, m;
0225:
0226:
0227: D3DXVECTOR3 eye = D3DXVECTOR3(0.0f,MeshRadius,2.5f*MeshRadius);
0228: D3DXVECTOR3 lookAt = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
0229: D3DXVECTOR3 up = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
0230:
0231: D3DXMatrixLookAtLH(&mView, &eye, &lookAt, &up);
0232: D3DXMatrixPerspectiveFovLH(&mProj
0233: ,60.0f*PI/180.0f
0234: ,(float)WIDTH/(float)HEIGHT
0235: ,0.01f,100.0f
0236: );
0237:
0238:
0239: DrawBg(lpD3DDev, mView, mProj);
0240:
0241:
0242:
0243: D3DXMatrixRotationY( &mWorld, timeGetTime()/1000.0f );
0244:
0245: m = mWorld * mView;
0246: D3DXMatrixTranspose( &m , &m);
0247: lpD3DDev->SetVertexShaderConstant(4, &m, 4);
0248:
0249: m = mWorld * mView * mProj;
0250: D3DXMatrixTranspose( &m , &m);
0251: lpD3DDev->SetVertexShaderConstant(0, &m, 4);
0252:
0253: D3DXMatrixInverse( &m, NULL, &mWorld);
0254:
0255:
0256: D3DXVECTOR4 lightDir(0.5f, 1.0f, -0.5f, 0.0f);
0257: D3DXVec4Normalize(&lightDir, &lightDir);
0258: D3DXVec4Transform(&lightDir, &lightDir, &m);
0259: lightDir[3] = 0.3f;
0260: lpD3DDev->SetVertexShaderConstant(13, &lightDir, 1);
0261:
0262:
0263: lpD3DDev->SetStreamSource(0, pMeshVB, sizeof(D3D_CUSTOMVERTEX));
0264: lpD3DDev->SetIndices(pMeshIndex,0);
0265: for(DWORD i=0;i<dwNumMaterials;i++){
0266:
0267: D3DXVECTOR4 vl( pMeshMaterials[i].Diffuse.r,
0268: pMeshMaterials[i].Diffuse.g,
0269: pMeshMaterials[i].Diffuse.b,
0270: 0.0f);
0271: lpD3DDev->SetVertexShaderConstant(14, &vl, 1);
0272: lpD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
0273: pSubsetTable[i].VertexStart,
0274: pSubsetTable[i].VertexCount,
0275: pSubsetTable[i].FaceStart * 3,
0276: pSubsetTable[i].FaceCount);
0277: }
0278: }
0279:
0280:
0281:
0282: void DeleteMeshObject(void)
0283: {
0284: DWORD i;
0285:
0286: if(pMeshVB == NULL) return;
0287:
0288: for(i=0; i<dwNumMaterials; i++){
0289: RELEASE(pMeshTextures[i]);
0290: }
0291: delete[] pMeshTextures;
0292: delete[] pMeshMaterials;
0293: delete[] pSubsetTable;
0294:
0295: RELEASE(pMeshVB);
0296: RELEASE(pMeshIndex);
0297: }
0298:
0299:
0300:
0301:
0302: void CleanRender(LPDIRECT3DDEVICE8 lpD3DDev)
0303: {
0304: CleanBg(lpD3DDev);
0305:
0306: DeleteMeshObject();
0307:
0308: if ( hVertexShader != ~0 ){
0309: lpD3DDev->DeleteVertexShader( hVertexShader );
0310: hVertexShader = ~0;
0311: }
0312: }
0313: