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: #include "load.h"
0015:
0016: CMyMesh mesh;
0017: CMyView view;
0018:
0019: #define NUM_VS 8
0020: DWORD gVs = 0;
0021: DWORD hVertexShader[NUM_VS]={
0022: ~0,~0,~0,~0,~0,~0,~0,~0
0023: };
0024: DWORD hVsEdge=~0;
0025: DWORD gEdge = 0;
0026: LPDIRECT3DTEXTURE8 pSpecTexture = NULL;
0027: LPDIRECT3DTEXTURE8 pMetalTexture = NULL;
0028: LPDIRECT3DTEXTURE8 pSoftTexture = NULL;
0029:
0030: D3DXVECTOR4 rot = D3DXVECTOR4(-PI/12.0f, 0.0f, 0.0f, 0.0f);
0031:
0032:
0033: D3DXVECTOR4 lpos;
0034: D3DXVECTOR4 lrot = D3DXVECTOR4(PI/6.0f, PI/4.0f, 0.0f, 0.0f);
0035:
0036:
0037:
0038: void SetLight(LPDIRECT3DDEVICE8 lpD3DDEV, D3DXVECTOR4 *pRot);
0039: void InitLightEff(LPDIRECT3DDEVICE8 lpD3DDEV, D3DXVECTOR4 pos);
0040: void DrawLightEff(LPDIRECT3DDEVICE8 lpD3DDEV, D3DXMATRIX mView, D3DXMATRIX mProj);
0041: void CleanLightEff();
0042:
0043: void InitBg(LPDIRECT3DDEVICE8 lpD3DDev);
0044: void DrawBg(LPDIRECT3DDEVICE8 lpD3DDev, D3DXMATRIX mWorld, D3DXMATRIX mView, D3DXMATRIX mProj);
0045: void CleanBg(LPDIRECT3DDEVICE8 lpD3DDev);
0046:
0047:
0048:
0049:
0050:
0051: typedef struct{
0052: char *name;
0053: }VS_INFO;
0054: HRESULT InitRender(LPDIRECT3DDEVICE8 lpD3DDEV)
0055: {
0056: HRESULT hr;
0057:
0058: hr = mesh.Load(lpD3DDEV, "nsx.x");
0059:
0060:
0061: if ( FAILED(CVertexShaderMgr::Load(lpD3DDEV, "edge.vsh", &hVsEdge, dwDecl)) ) return hr;
0062:
0063: VS_INFO vs_info[] = {
0064: "diffuse.vsh",
0065: "specular.vsh",
0066: "texspec.vsh",
0067: "texspec.vsh",
0068: "hemisphere.vsh",
0069: "hemi_amb.vsh",
0070: "hemi_spec.vsh",
0071: "soft.vsh",
0072: };
0073: for(int i = 0; i < NUM_VS; i++){
0074: if ( FAILED(CVertexShaderMgr::Load(lpD3DDEV, vs_info[i].name, &hVertexShader[i], dwDecl)) ) return hr;
0075: }
0076:
0077:
0078: D3DXCreateTextureFromFileEx(lpD3DDEV, "spec.bmp",0,0,0,0,D3DFMT_A8R8G8B8,
0079: D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR,
0080: 0, NULL, NULL, &pSpecTexture);
0081:
0082: D3DXCreateTextureFromFileEx(lpD3DDEV, "metal.bmp",0,0,0,0,D3DFMT_A8R8G8B8,
0083: D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR,
0084: 0, NULL, NULL, &pMetalTexture);
0085:
0086: D3DXCreateTextureFromFileEx(lpD3DDEV, "soft.bmp",0,0,0,0,D3DFMT_A8R8G8B8,
0087: D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR,
0088: 0, NULL, NULL, &pSoftTexture);
0089:
0090:
0091: lpD3DDEV->SetRenderState( D3DRS_ZENABLE, TRUE);
0092: lpD3DDEV->SetRenderState( D3DRS_LIGHTING, FALSE );
0093: SetLight(lpD3DDEV, &lrot);
0094:
0095:
0096: lpD3DDEV->SetVertexShaderConstant(12, D3DXVECTOR4(0.0f, 0.5f, 1.0f, 2.0f), 1);
0097: lpD3DDEV->SetVertexShaderConstant(16, D3DXVECTOR4(0.7f, 0.6f, 0.4f, 0.0f), 1);
0098: lpD3DDEV->SetVertexShaderConstant(17, D3DXVECTOR4(0.5f, 0.5f, 0.5f, 1.0f), 1);
0099: lpD3DDEV->SetVertexShaderConstant(18, D3DXVECTOR4(0.4f, 0.4f, 0.4f, 0.0f), 1);
0100: lpD3DDEV->SetVertexShaderConstant(19, D3DXVECTOR4(0.6f, 0.3f, 0.2f, 0.0f), 1);
0101:
0102: InitBg(lpD3DDEV);
0103:
0104: return S_OK;
0105: }
0106:
0107:
0108:
0109:
0110: void Render(LPDIRECT3DDEVICE8 lpD3DDEV)
0111: {
0112: if(!mesh.bActive) return;
0113:
0114: D3DXMATRIX m, mLocal, mWorld, mLW, mProj;
0115:
0116: D3DXMatrixPerspectiveFovLH(&mProj
0117: ,60.0f*PI/180.0f
0118: ,(float)WIDTH/(float)HEIGHT
0119: ,0.01f
0120: ,100.0f
0121: );
0122: D3DXMatrixRotationYawPitchRoll(&mWorld, rot.y, rot.x, rot.z);
0123:
0124:
0125: DrawBg(lpD3DDEV, mWorld, view.Get(), mProj);
0126:
0127: float s = 1.0f / mesh.radius;
0128: D3DXMatrixTranslation(&mLocal,-mesh.center.x,0,-mesh.center.z);
0129: D3DXMatrixScaling(&m, s,s,s);
0130: mLocal = mLocal * m;
0131: mLW = mLocal*mWorld;
0132:
0133:
0134: m = mLW * view.Get() * mProj;
0135: D3DXMatrixTranspose( &m , &m);
0136: lpD3DDEV->SetVertexShaderConstant(0,&m, 4);
0137: D3DXMatrixTranspose( &m , &mLW);
0138: lpD3DDEV->SetVertexShaderConstant(4, &m, 4);
0139: D3DXMatrixInverse( &m, NULL, &mLW);
0140: lpD3DDEV->SetVertexShaderConstant(8, &m, 4);
0141:
0142:
0143: D3DXVECTOR4 lightDir;
0144: D3DXMatrixInverse( &m, NULL, &mLW);
0145:
0146:
0147: D3DXVec4Transform(&lightDir, &lpos, &m);
0148: D3DXVec4Normalize(&lightDir, &lightDir);
0149: lightDir[3] =0.3f;
0150: lpD3DDEV->SetVertexShaderConstant(13, &lightDir, 1);
0151:
0152:
0153: D3DXVECTOR4 eye_pos;
0154: D3DXVec4Transform(&eye_pos, &view.GetEye(), &m);
0155: lpD3DDEV->SetVertexShaderConstant(14, &eye_pos, 1);
0156:
0157:
0158: D3DXVECTOR4 axis(0.0f, 1.0f, 0.0f, 0.1f);
0159: D3DXVec4Transform(&axis, &axis, &m);
0160: D3DXVec4Normalize(&axis, &axis);
0161: lpD3DDEV->SetVertexShaderConstant(20, &axis, 1);
0162:
0163:
0164: lpD3DDEV->SetVertexShaderConstant(40
0165: , D3DXVECTOR4(0.015f*mesh.radius,
0166: 0.015f*mesh.radius,
0167: -0.0002f*mesh.radius, 0.0f), 1);
0168:
0169: lpD3DDEV->SetStreamSource(0, mesh.pVB, sizeof(D3D_CUSTOMVERTEX));
0170: lpD3DDEV->SetIndices(mesh.pIndex,0);
0171:
0172:
0173: if(gEdge){
0174: lpD3DDEV->SetVertexShader(hVsEdge);
0175: lpD3DDEV->SetTexture(0,NULL);
0176: lpD3DDEV->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );
0177: lpD3DDEV->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE);
0178: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG2);
0179: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE);
0180: lpD3DDEV->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_DISABLE);
0181: for(DWORD i=0;i<mesh.dwNumMaterials;i++){
0182: lpD3DDEV->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
0183: mesh.pSubsetTable[i].VertexStart,
0184: mesh.pSubsetTable[i].VertexCount,
0185: mesh.pSubsetTable[i].FaceStart * 3,
0186: mesh.pSubsetTable[i].FaceCount);
0187: }
0188: lpD3DDEV->SetRenderState( D3DRS_ZWRITEENABLE, TRUE );
0189: lpD3DDEV->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW);
0190: }
0191:
0192: lpD3DDEV->SetVertexShader(hVertexShader[gVs]);
0193:
0194: switch(gVs){
0195: case 0:
0196: case 1:
0197: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE);
0198: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0199: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE);
0200: lpD3DDEV->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_DISABLE);
0201: break;
0202: case 2:
0203: lpD3DDEV->SetTexture(1, pSpecTexture);
0204: case 3:
0205: if(3==gVs)lpD3DDEV->SetTexture(1, pMetalTexture);
0206: lpD3DDEV->SetTextureStageState(0,D3DTSS_MAGFILTER , D3DTEXF_LINEAR);
0207: lpD3DDEV->SetTextureStageState(0,D3DTSS_MINFILTER, D3DTEXF_LINEAR);
0208: lpD3DDEV->SetTextureStageState(1,D3DTSS_MAGFILTER , D3DTEXF_LINEAR);
0209: lpD3DDEV->SetTextureStageState(1,D3DTSS_MINFILTER, D3DTEXF_LINEAR);
0210: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE);
0211: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0212: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE);
0213: lpD3DDEV->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_ADD);
0214: lpD3DDEV->SetTextureStageState(1,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0215: lpD3DDEV->SetTextureStageState(1,D3DTSS_COLORARG2, D3DTA_CURRENT);
0216: lpD3DDEV->SetTextureStageState(1,D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
0217: break;
0218: case 4:
0219: case 5:
0220: case 6:
0221: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE);
0222: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0223: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE);
0224: lpD3DDEV->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_DISABLE);
0225: break;
0226: case 7:
0227: lpD3DDEV->SetTexture(1, pSoftTexture);
0228: lpD3DDEV->SetTextureStageState(0,D3DTSS_MAGFILTER , D3DTEXF_LINEAR);
0229: lpD3DDEV->SetTextureStageState(0,D3DTSS_MINFILTER, D3DTEXF_LINEAR);
0230: lpD3DDEV->SetTextureStageState(1,D3DTSS_MAGFILTER , D3DTEXF_LINEAR);
0231: lpD3DDEV->SetTextureStageState(1,D3DTSS_MINFILTER, D3DTEXF_LINEAR);
0232: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE);
0233: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0234: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE);
0235: lpD3DDEV->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_MODULATE);
0236: lpD3DDEV->SetTextureStageState(1,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0237: lpD3DDEV->SetTextureStageState(1,D3DTSS_COLORARG2, D3DTA_CURRENT);
0238: lpD3DDEV->SetTextureStageState(1,D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
0239: break;
0240: }
0241:
0242:
0243: for(DWORD i=0;i<mesh.dwNumMaterials;i++){
0244:
0245: D3DXVECTOR4 vl(
0246:
0247: mesh.pMaterials[i].Diffuse.r*0.7f,
0248: mesh.pMaterials[i].Diffuse.g*0.7f,
0249: mesh.pMaterials[i].Diffuse.b*0.7f,
0250: 1.0f);
0251: lpD3DDEV->SetVertexShaderConstant(15,&vl,1);
0252: if(mesh.pTextures[i]){
0253: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE);
0254: lpD3DDEV->SetTexture(0, mesh.pTextures[i]);
0255: }else{
0256: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG2);
0257: }
0258: lpD3DDEV->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
0259: mesh.pSubsetTable[i].VertexStart,
0260: mesh.pSubsetTable[i].VertexCount,
0261: mesh.pSubsetTable[i].FaceStart * 3,
0262: mesh.pSubsetTable[i].FaceCount);
0263: }
0264: lpD3DDEV->SetTexture(0,NULL);
0265: lpD3DDEV->SetTexture(1,NULL);
0266:
0267:
0268: DrawLightEff(lpD3DDEV, view.Get(), mProj);
0269: }
0270:
0271:
0272:
0273:
0274: void SetLight(LPDIRECT3DDEVICE8 lpD3DDEV, D3DXVECTOR4 *pRot)
0275: {
0276: D3DXMATRIX m;
0277: D3DXMatrixRotationYawPitchRoll(&m, pRot->y, pRot->x, pRot->z);
0278: D3DXVec4Transform(&lpos, &D3DXVECTOR4(0.0f, 0.0f,-1.0f, 0.0f), &m);
0279: D3DXVec4Normalize(&lpos, &lpos);
0280: InitLightEff(lpD3DDEV, lpos);
0281: }
0282:
0283:
0284:
0285:
0286: void CleanRender(LPDIRECT3DDEVICE8 lpD3DDEV)
0287: {
0288: mesh.Release();
0289:
0290: RELEASE(pSoftTexture);
0291: RELEASE(pMetalTexture);
0292: RELEASE(pSpecTexture);
0293:
0294:
0295: CVertexShaderMgr::Release(lpD3DDEV, &hVsEdge);
0296: for(int i = 0; i < NUM_VS; i++){
0297: CVertexShaderMgr::Release(lpD3DDEV, &hVertexShader[i]);
0298: }
0299:
0300: CleanBg(lpD3DDEV);
0301: CleanLightEff();
0302: }
0303: