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: static 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);
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.3f, 0.3f, 0.3f, 0.0f), 1);
0100: lpD3DDEV->SetVertexShaderConstant(19, D3DXVECTOR4(0.7f, 0.3f, 0.1f, 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: float s = 1.0f / mesh.radius;
0125: D3DXMatrixTranslation(&mLocal,-mesh.center.x,0,-mesh.center.z);
0126: D3DXMatrixScaling(&m, s,s,s);
0127: mLocal = mLocal * m;
0128: mLW = mLocal*mWorld;
0129:
0130:
0131: lpD3DDEV->SetTransform( D3DTS_WORLD, &mWorld );
0132: lpD3DDEV->SetTransform( D3DTS_VIEW, &view.Get() );
0133: lpD3DDEV->SetTransform( D3DTS_PROJECTION, &mProj );
0134: DrawBg(lpD3DDEV);
0135:
0136:
0137: m = mLW * view.Get() * mProj;
0138: D3DXMatrixTranspose( &m , &m);
0139: lpD3DDEV->SetVertexShaderConstant(0,&m, 4);
0140: D3DXMatrixTranspose( &m , &mLW);
0141: lpD3DDEV->SetVertexShaderConstant(4, &m, 4);
0142: D3DXMatrixInverse( &m, NULL, &mLW);
0143: lpD3DDEV->SetVertexShaderConstant(8, &m, 4);
0144:
0145:
0146: D3DXVECTOR4 lightDir;
0147: D3DXMatrixInverse( &m, NULL, &mLW);
0148:
0149:
0150: D3DXVec4Transform(&lightDir, &lpos, &m);
0151: D3DXVec4Normalize(&lightDir, &lightDir);
0152: lightDir[3] =0.3f;
0153: lpD3DDEV->SetVertexShaderConstant(13, &lightDir, 1);
0154:
0155:
0156: D3DXVECTOR4 eye_pos;
0157: D3DXVec4Transform(&eye_pos, &view.GetEye(), &m);
0158: lpD3DDEV->SetVertexShaderConstant(14, &eye_pos, 1);
0159:
0160:
0161: D3DXVECTOR4 axis(0.0f, 1.0f, 0.0f, 0.1f);
0162: D3DXVec4Transform(&axis, &axis, &m);
0163: D3DXVec4Normalize(&axis, &axis);
0164: lpD3DDEV->SetVertexShaderConstant(20, &axis, 1);
0165:
0166:
0167: lpD3DDEV->SetVertexShaderConstant(40
0168: , D3DXVECTOR4(0.015f*mesh.radius,
0169: 0.015f*mesh.radius,
0170: -0.0002f*mesh.radius, 0.0f), 1);
0171:
0172: lpD3DDEV->SetStreamSource(0, mesh.pVB, sizeof(D3D_CUSTOMVERTEX));
0173: lpD3DDEV->SetIndices(mesh.pIndex,0);
0174:
0175:
0176: if(gEdge){
0177: lpD3DDEV->SetVertexShader(hVsEdge);
0178: lpD3DDEV->SetTexture(0,NULL);
0179: lpD3DDEV->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );
0180: lpD3DDEV->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE);
0181: for(DWORD i=0;i<mesh.dwNumMaterials;i++){
0182:
0183: lpD3DDEV->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
0184: mesh.pSubsetTable[i].VertexStart,
0185: mesh.pSubsetTable[i].VertexCount,
0186: mesh.pSubsetTable[i].FaceStart * 3,
0187: mesh.pSubsetTable[i].FaceCount);
0188: }
0189: lpD3DDEV->SetRenderState( D3DRS_ZWRITEENABLE, TRUE );
0190: lpD3DDEV->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW);
0191: }
0192:
0193: lpD3DDEV->SetVertexShader(hVertexShader[gVs]);
0194:
0195: switch(gVs){
0196: case 0:
0197: case 1:
0198: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE);
0199: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0200: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE);
0201: lpD3DDEV->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_DISABLE);
0202: break;
0203: case 2:
0204: lpD3DDEV->SetTexture(1, pSpecTexture);
0205: case 3:
0206: if(3==gVs)lpD3DDEV->SetTexture(1, pMetalTexture);
0207: lpD3DDEV->SetTextureStageState(0,D3DTSS_MAGFILTER , D3DTEXF_LINEAR);
0208: lpD3DDEV->SetTextureStageState(0,D3DTSS_MINFILTER, D3DTEXF_LINEAR);
0209: lpD3DDEV->SetTextureStageState(1,D3DTSS_MAGFILTER , D3DTEXF_LINEAR);
0210: lpD3DDEV->SetTextureStageState(1,D3DTSS_MINFILTER, D3DTEXF_LINEAR);
0211: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE);
0212: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0213: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE);
0214: lpD3DDEV->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_ADD);
0215: lpD3DDEV->SetTextureStageState(1,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0216: lpD3DDEV->SetTextureStageState(1,D3DTSS_COLORARG2, D3DTA_CURRENT);
0217: lpD3DDEV->SetTextureStageState(1,D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
0218: break;
0219: case 4:
0220: case 5:
0221: case 6:
0222: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE);
0223: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0224: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE);
0225: lpD3DDEV->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_DISABLE);
0226: break;
0227: case 7:
0228: lpD3DDEV->SetTexture(1, pSoftTexture);
0229: lpD3DDEV->SetTextureStageState(0,D3DTSS_MAGFILTER , D3DTEXF_LINEAR);
0230: lpD3DDEV->SetTextureStageState(0,D3DTSS_MINFILTER, D3DTEXF_LINEAR);
0231: lpD3DDEV->SetTextureStageState(1,D3DTSS_MAGFILTER , D3DTEXF_LINEAR);
0232: lpD3DDEV->SetTextureStageState(1,D3DTSS_MINFILTER, D3DTEXF_LINEAR);
0233: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE);
0234: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0235: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE);
0236: lpD3DDEV->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_MODULATE);
0237: lpD3DDEV->SetTextureStageState(1,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0238: lpD3DDEV->SetTextureStageState(1,D3DTSS_COLORARG2, D3DTA_CURRENT);
0239: lpD3DDEV->SetTextureStageState(1,D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
0240: break;
0241: }
0242:
0243:
0244: for(DWORD i=0;i<mesh.dwNumMaterials;i++){
0245:
0246: D3DXVECTOR4 vl(
0247:
0248: mesh.pMaterials[i].Diffuse.r*0.7f,
0249: mesh.pMaterials[i].Diffuse.g*0.7f,
0250: mesh.pMaterials[i].Diffuse.b*0.7f,
0251: 1.0f);
0252: lpD3DDEV->SetVertexShaderConstant(15,&vl,1);
0253: if(mesh.pTextures[i]){
0254: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE);
0255: lpD3DDEV->SetTexture(0, mesh.pTextures[i]);
0256: }else{
0257: lpD3DDEV->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG2);
0258: }
0259: lpD3DDEV->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
0260: mesh.pSubsetTable[i].VertexStart,
0261: mesh.pSubsetTable[i].VertexCount,
0262: mesh.pSubsetTable[i].FaceStart * 3,
0263: mesh.pSubsetTable[i].FaceCount);
0264: }
0265: lpD3DDEV->SetTexture(0,NULL);
0266: lpD3DDEV->SetTexture(1,NULL);
0267:
0268:
0269: DrawLightEff(lpD3DDEV, view.Get(), mProj);
0270: }
0271:
0272:
0273:
0274:
0275: void SetLight(LPDIRECT3DDEVICE8 lpD3DDEV, D3DXVECTOR4 *pRot)
0276: {
0277: D3DXMATRIX m;
0278: D3DXMatrixRotationYawPitchRoll(&m, pRot->y, pRot->x, pRot->z);
0279: D3DXVec4Transform(&lpos, &D3DXVECTOR4(0.0f, 0.0f,-1.0f, 0.0f), &m);
0280: D3DXVec4Normalize(&lpos, &lpos);
0281: InitLightEff(lpD3DDEV, lpos);
0282: }
0283:
0284:
0285:
0286:
0287: void CleanRender(LPDIRECT3DDEVICE8 lpD3DDEV)
0288: {
0289: CleanBg(lpD3DDEV);
0290: CleanLightEff();
0291:
0292: RELEASE(pSoftTexture);
0293: RELEASE(pMetalTexture);
0294: RELEASE(pSpecTexture);
0295:
0296:
0297: for(int i = 0; i < NUM_VS; i++){
0298: CVertexShaderMgr::Release(lpD3DDEV, &hVertexShader[i]);
0299: }
0300: CVertexShaderMgr::Release(lpD3DDEV, &hVsEdge);
0301:
0302: mesh.Release();
0303: }
0304: