0001: // ----------------------------------------------------------------------------
0002: //
0003: // draw.cpp - Rendering
0004: // 
0005: // Copyright (c) 2002 IMAGIRE Takashi (imagire@gmail.com)
0006: // All Rights Reserved.
0007: //
0008: // ----------------------------------------------------------------------------
0009: #define STRICT
0010: 
0011: #include <windows.h>
0012: #include "main.h"
0013: #include "draw.h"
0014: #include "load.h"
0015: 
0016: #include "Cg/cgD3D.h"
0017: 
0018: // Cg Env
0019: cgDirect3D cg;
0020: cgContextContainer * pContextContainer = 0;
0021: 
0022: // Ordinal shader
0023: cgProgramContainer *pVertexProgramContainer = 0;
0024: cgBindIter * vertex_mat_iter = 0;
0025: cgProgramContainer  *pPixelProgramContainer = 0;
0026: cgBindIter * tex0_iter = 0;
0027: cgBindIter * tex1_iter = 0;
0028: 
0029: // Collision
0030: cgProgramContainer *pCollVC = 0;
0031: cgBindIter * trans_iter           = 0;
0032: cgBindIter * velocity_iter        = 0;
0033: cgBindIter * viewproj_matrix_iter = 0;
0034: cgBindIter * color_iter           = 0;
0035: 
0036: // Burn
0037: cgProgramContainer *pBurnVC = 0;
0038: cgProgramContainer *pBurnPC = 0;
0039: cgBindIter * BurnTex0_iter = 0;
0040: cgBindIter * BurnTex1_iter = 0;
0041: cgBindIter * BurnTex2_iter = 0;
0042: 
0043: // Black spot
0044: cgProgramContainer *pBlackVC = 0;
0045: 
0046: CMyMesh mesh;                       // ground
0047: CMyView view;                       // camera
0048: CBulletMgr bullet;                  // bullet
0049: float       mouse[2]={150.f,200.f}; // cursol position
0050: 
0051: D3DXMATRIX gProj;                   // Projectio matrix
0052: 
0053: LPDIRECT3DSURFACE8      pBackbuffer = NULL;
0054: LPDIRECT3DTEXTURE8      pTexture=NULL;
0055: LPDIRECT3DSURFACE8      pTextureSurface=NULL;
0056: 
0057: // catmap
0058: LPDIRECT3DTEXTURE8      pCatTexture[2]={NULL,NULL};
0059: LPDIRECT3DSURFACE8      pCatTextureSurface[2]={NULL,NULL};
0060: LPDIRECT3DTEXTURE8      pCatSeedTexture=NULL;
0061: 
0062: // envmap
0063: LPDIRECT3DTEXTURE8      pEnvTexture=NULL;
0064: LPDIRECT3DSURFACE8      pEnvTextureSurface=NULL;
0065: 
0066: // Result of the collision check
0067: LPDIRECT3DTEXTURE8      pHitTexture[2]={NULL,NULL};
0068: LPDIRECT3DSURFACE8      pHitTextureSurface[2]={NULL,NULL};
0069: LPDIRECT3DTEXTURE8      pBurnTexture=NULL;
0070: LPDIRECT3DSURFACE8      pBurnTextureSurface=NULL;
0071: 
0072: // Black spot
0073: LPDIRECT3DTEXTURE8      pBlackTexture=NULL;
0074: LPDIRECT3DSURFACE8      pBlackTextureSurface=NULL;
0075: 
0076: 
0077: // ----------------------------------------------------------------------------
0078: // Vertex format
0079: // ----------------------------------------------------------------------------
0080: struct TLVERTEX
0081: {
0082:     float x,y,z,rhw;
0083:     float tu,tv;
0084: };
0085: #define FVF_TLVERTEX (D3DFVF_XYZRHW | D3DFVF_TEX1)
0086: 
0087: // ----------------------------------------------------------------------------
0088: void InitBg(LPDIRECT3DDEVICE8 lpD3DDev);
0089: void DrawBg(LPDIRECT3DDEVICE8 lpD3DDev, D3DXMATRIX &mVP, bool texture);
0090: void CleanBg(LPDIRECT3DDEVICE8 lpD3DDev);
0091: 
0092: 
0093: //-----------------------------------------------------------------------------
0094: // Name: InitRender()
0095: //-----------------------------------------------------------------------------
0096: HRESULT InitRender(LPDIRECT3DDEVICE8 lpD3DDev)
0097: {
0098:     HRESULT hr;
0099:     DWORD i;
0100: 
0101:     cgVertexDefinition vertex_attributes[] = {
0102:         {D3DVSDT_FLOAT4, "position",  0},
0103:         {D3DVSDT_FLOAT2, "texcoord0", 0},
0104:         CGVERTEXDEFINITIONEND
0105:     };
0106: 
0107:     // ==================================================
0108:     // Initialization of the sky
0109:     // ==================================================
0110:     InitBg(lpD3DDev);
0111:     
0112:     // ==================================================
0113:     // read the ground mesh
0114:     // ==================================================
0115:     mesh.Load(lpD3DDev, "map.x");
0116: 
0117:     // ==================================================
0118:     // Initializatioon of the Cg
0119:     // ==================================================
0120:     cg.AddFilePath("..");
0121:     pContextContainer = cg.CreateContextContainer(lpD3DDev);
0122: 
0123:     // ==================================================
0124:     // Almost fixed function
0125:     // ==================================================
0126:     pVertexProgramContainer = pContextContainer->LoadCGProgramFromFile(
0127:         "vs.cg", "Vertex Shader", cgDX8VertexProfile, vertex_attributes);
0128: 
0129:     if (pVertexProgramContainer == NULL) {// error
0130:         const char * listing = pContextContainer->GetLastListing();
0131:         if (listing == 0) listing = "Could not find cgc.exe.";
0132:         cg.NotePad("頂点シェーダープログラムの生成に失敗しました\n\n", listing);
0133:         exit(1);    // end
0134:     }
0135:     vertex_mat_iter = pVertexProgramContainer->GetParameterBindByName("worldviewproj_matrix");
0136: 
0137:     pPixelProgramContainer = pContextContainer->LoadCGProgramFromFile(
0138:                                 "ps.cg", "test", cgDX8PixelProfile);
0139:     if (NULL == pPixelProgramContainer) { // error
0140:         const char * error_text = pContextContainer->GetLastListing();
0141:         cg.NotePad("ピクセルシェーダープログラムの生成に失敗しました\n\n\n", error_text);
0142:         exit(1);    // end
0143:     }
0144:     tex0_iter = pPixelProgramContainer->GetTextureBindByName("tex0");
0145:     tex1_iter = pPixelProgramContainer->GetTextureBindByName("tex1");
0146:     
0147:     // ==================================================
0148:     // Collision Shader
0149:     // ==================================================
0150:     pCollVC = pContextContainer->LoadCGProgramFromFile(
0151:         "CollisionV.cg", "Vertex Shader", cgDX8VertexProfile, vertex_attributes);
0152:     if (NULL == pCollVC) {
0153:         const char * listing = pContextContainer->GetLastListing();
0154:         if (listing == 0) listing = "Could not find cgc.exe.";
0155:         cg.NotePad("頂点シェーダープログラムの生成に失敗しました\n\n", listing);
0156:         exit(1);
0157:     }
0158:     trans_iter          = pCollVC->GetParameterBindByName("trans");
0159:     velocity_iter       = pCollVC->GetParameterBindByName("velocity");
0160:     viewproj_matrix_iter= pCollVC->GetParameterBindByName("viewproj_matrix");
0161:     color_iter          = pCollVC->GetParameterBindByName("color");
0162: 
0163:     // ==================================================
0164:     // This program creates the fire effect
0165:     // ==================================================
0166:     pBurnVC = pContextContainer->LoadCGProgramFromFile(
0167:         "BurnV.cg", "Vertex Shader", cgDX8VertexProfile, vertex_attributes);
0168:     if (NULL == pBurnVC) {
0169:         const char * listing = pContextContainer->GetLastListing();
0170:         if (listing == 0) listing = "Could not find cgc.exe.";
0171:         cg.NotePad("頂点シェーダープログラムの生成に失敗しました\n\n", listing);
0172:         exit(1);
0173:     }
0174: 
0175:     pBurnPC = pContextContainer->LoadCGProgramFromFile(
0176:         "BurnP.cg", "test", cgDX8PixelProfile);
0177:     if (NULL == pBurnPC) {
0178:         const char * error_text = pContextContainer->GetLastListing();
0179:         cg.NotePad("ピクセルシェーダープログラムの生成に失敗しました\n\n\n", error_text);
0180:         exit(1);
0181:     }
0182:     BurnTex0_iter = pBurnPC->GetTextureBindByName("tex0");
0183:     BurnTex1_iter = pBurnPC->GetTextureBindByName("tex1");
0184:     BurnTex2_iter = pBurnPC->GetTextureBindByName("tex2");
0185: 
0186:     // ==================================================
0187:     // This program creates the black spots
0188:     // ==================================================
0189:     pBlackVC = pContextContainer->LoadCGProgramFromFile(
0190:         "BlackV.cg", "Vertex Shader", cgDX8VertexProfile, vertex_attributes);
0191:     if (NULL == pBlackVC) {
0192:         const char * listing = pContextContainer->GetLastListing();
0193:         if (listing == 0) listing = "Could not find cgc.exe.";
0194:         cg.NotePad("頂点シェーダープログラムの生成に失敗しました\n\n", listing);
0195:         exit(1);
0196:     }
0197:     // ==================================================
0198:     // Texture for the seed of the fire (CatMap)
0199:     // ==================================================
0200:     D3DXCreateTextureFromFileEx(lpD3DDev, "fire.bmp",0,0,0,0,D3DFMT_A8R8G8B8,
0201:                                 D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR,
0202:                                 0, NULL, NULL, &pCatSeedTexture);
0203:     
0204: 
0205:     // ==================================================
0206:     // Create Rendering Texture
0207:     // ==================================================
0208:     // Backup back buffers
0209:     D3DSURFACE_DESC Desc;
0210:     LPDIRECT3DSURFACE8 lpZbuffer = NULL;
0211:     if( FAILED(hr = lpD3DDev->GetRenderTarget(&pBackbuffer))) return hr;
0212:     if( FAILED(hr = pBackbuffer->GetDesc( &Desc ))) return hr;
0213:     if( FAILED(hr = lpD3DDev->GetDepthStencilSurface( &lpZbuffer ))) return hr;
0214: 
0215:     // Collision map
0216:     if( FAILED(hr = lpD3DDev->CreateTexture(Desc.Width, Desc.Height, 1
0217:                             , D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pTexture))) return hr;
0218:     if( FAILED(hr = pTexture->GetSurfaceLevel(0,&pTextureSurface))) return hr;
0219:     if( FAILED(hr = lpD3DDev->SetRenderTarget(pTextureSurface, lpZbuffer ))) return hr;
0220: 
0221:     // Collision result in this frame
0222:     if( FAILED(hr = lpD3DDev->CreateTexture(Desc.Width, Desc.Height, 1
0223:                             , D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pBurnTexture))) return hr;
0224:     if( FAILED(hr = pBurnTexture->GetSurfaceLevel(0,&pBurnTextureSurface))) return hr;
0225:     if( FAILED(hr = lpD3DDev->SetRenderTarget(pBurnTextureSurface, NULL ))) return hr;
0226: 
0227:     if( FAILED(hr = lpD3DDev->CreateTexture(Desc.Width, Desc.Height, 1
0228:                             , D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pBlackTexture))) return hr;
0229:     if( FAILED(hr = pBlackTexture->GetSurfaceLevel(0,&pBlackTextureSurface))) return hr;
0230:     if( FAILED(hr = lpD3DDev->SetRenderTarget(pBlackTextureSurface, NULL ))) return hr;
0231: 
0232:     // Total collision result
0233:     if( FAILED(hr = lpD3DDev->CreateTexture(Desc.Width, Desc.Height, 1
0234:                             , D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pEnvTexture))) return hr;
0235:     if( FAILED(hr = pEnvTexture->GetSurfaceLevel(0,&pEnvTextureSurface))) return hr;
0236:     if( FAILED(hr = lpD3DDev->SetRenderTarget(pEnvTextureSurface, lpZbuffer ))) return hr;
0237: 
0238:     for(i=0;i<2;i++){
0239:         // CatMap
0240:         if( FAILED(hr = lpD3DDev->CreateTexture(128, 128, 1
0241:                                 , D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pCatTexture[i]))) return hr;
0242:         if( FAILED(hr = pCatTexture[i]->GetSurfaceLevel(0,&pCatTextureSurface[i]))) return hr;
0243:         // hit map
0244:         if( FAILED(hr = lpD3DDev->CreateTexture(Desc.Width, Desc.Height, 1
0245:                                 , D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pHitTexture[i]))) return hr;
0246:         if( FAILED(hr = pHitTexture[i]->GetSurfaceLevel(0,&pHitTextureSurface[i]))) return hr;
0247:     }
0248: 
0249:     // Set rendering target to back buffer
0250:     lpD3DDev->SetRenderTarget(pBackbuffer, lpZbuffer );
0251: 
0252:     // ==================================================
0253:     // Initialize render states
0254:     // ==================================================
0255:     lpD3DDev->SetRenderState( D3DRS_ZENABLE, TRUE );
0256:     lpD3DDev->SetRenderState( D3DRS_LIGHTING,  FALSE );
0257:     lpD3DDev->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);
0258:     lpD3DDev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
0259:     lpD3DDev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCCOLOR);
0260: 
0261:     D3DXMatrixPerspectiveFovLH(&gProj
0262:         ,60.0f*D3DX_PI/180.0f
0263:         ,(float)WIDTH/(float)HEIGHT
0264:         ,0.01f,100.0f
0265:         );
0266: 
0267:     return S_OK;
0268: }
0269: 
0270: //-----------------------------------------------------------------------------
0271: // Name: Render()
0272: // Desc: Draws the scene
0273: //    type 0 normal
0274: //    type 1 burn envmap
0275: //    type 2 collision depth
0276: //-----------------------------------------------------------------------------
0277: VOID DrawModel(LPDIRECT3DDEVICE8 lpD3DDev, D3DXMATRIX &mVP, DWORD type)
0278: {
0279:     if(2!=type)DrawBg(lpD3DDev, mVP, 0==type);
0280: 
0281:     if(0==type) bullet.Render(lpD3DDev, mVP);
0282: 
0283:     D3DXMATRIX m;
0284:     D3DXMatrixTranspose( &m, &mVP );
0285:     pVertexProgramContainer->SetShaderConstant( vertex_mat_iter, &m  );
0286:     
0287:     lpD3DDev->SetStreamSource(0, mesh.pVB, sizeof(D3D_CUSTOMVERTEX));
0288:     lpD3DDev->SetIndices(mesh.pIndex,0);
0289: 
0290:     switch(type){
0291:     case 1:
0292:         pPixelProgramContainer->SetTexture(tex0_iter, pBurnTexture);
0293:         pPixelProgramContainer->SetTexture(tex1_iter, NULL);
0294:         break;
0295:     case 2:
0296:         pPixelProgramContainer->SetTexture(tex0_iter, NULL);
0297:         pPixelProgramContainer->SetTexture(tex1_iter, NULL);
0298:         break;
0299:     default:
0300:         pPixelProgramContainer->SetTexture(tex0_iter, mesh.pTextures[0]);
0301:         pPixelProgramContainer->SetTexture(tex1_iter, pBlackTexture);
0302:         break;
0303:     }
0304: 
0305:     lpD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 
0306:                                     mesh.pSubsetTable[0].VertexStart,
0307:                                     mesh.pSubsetTable[0].VertexCount,
0308:                                     mesh.pSubsetTable[0].FaceStart * 3,
0309:                                     mesh.pSubsetTable[0].FaceCount);
0310: }
0311: //-----------------------------------------------------------------------------
0312: // Name: Render()
0313: //-----------------------------------------------------------------------------
0314: static BOOL     init = TRUE;
0315: VOID Render(LPDIRECT3DDEVICE8 lpD3DDev)
0316: {
0317:     D3DXMATRIX      mWorld, mView, mProj, m, mat;
0318:     DWORD           i;
0319:     static DWORD    id=0;
0320:     LPDIRECT3DSURFACE8 lpZbuffer = NULL;
0321:     lpD3DDev->GetDepthStencilSurface( &lpZbuffer );
0322: 
0323:     static DWORD last = 0;
0324:     DWORD now = timeGetTime();
0325:     DWORD cnt = now-last;
0326:     last = now;
0327:     
0328:     if(init){
0329:         cnt = 0;
0330:         bullet.Reset();
0331:         //-----------------------------------------------------------------------------
0332:         // Clear the burning information
0333:         //-----------------------------------------------------------------------------
0334:         lpD3DDev->SetRenderTarget(pBurnTextureSurface, NULL );
0335:         lpD3DDev->Clear(0,NULL,D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0),1.0f,0);
0336:         lpD3DDev->SetRenderTarget(pBackbuffer, lpZbuffer );
0337:     }
0338: 
0339:     //-----------------------------------------------------------------------------
0340:     // Sometimes shooting a star
0341:     //-----------------------------------------------------------------------------
0342:     static DWORD start_cnt=0xffffffff;
0343:     if(now<start_cnt || 3000<now-start_cnt){
0344:         D3DXVECTOR4 x, v;
0345:         x.x = (10.0f/4096.0f)*(float)(rand()%0xfff)-5.0f;
0346:         x.z = (10.0f/4096.0f)*(float)(rand()%0xfff)-5.0f;
0347:         x.y = 10.0f;
0348:         v.x = (10.0f/4096.0f)*(float)(rand()%0xfff)-5.0f;
0349:         v.z = (10.0f/4096.0f)*(float)(rand()%0xfff)-5.0f;
0350:         v.y = 0.0f;
0351:         v = v - x;
0352:         D3DXVec4Normalize(&v,&v);
0353:         bullet.Add(x, BULLET_SPEED*v);
0354: 
0355:         start_cnt = now;
0356:     }
0357:     
0358:     //-----------------------------------------------------------------------------
0359:     // Move bullets
0360:     //-----------------------------------------------------------------------------
0361:     bullet.Update(cnt);
0362: 
0363:     //-----------------------------------------------------------------------------
0364:     // CatMap
0365:     //-----------------------------------------------------------------------------
0366:     {
0367:         lpD3DDev->SetRenderTarget(pCatTextureSurface[id], NULL);
0368: 
0369:         lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0370:         lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0371:         lpD3DDev->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_POINT );
0372:         lpD3DDev->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_POINT );
0373:         float size = 128.0f;
0374:         TLVERTEX Vertex[] = {
0375:             //   x          y     z rhw  tu    tv
0376:             {0.0f*size, 0.0f*size,0, 1, 0.0f, 0.0f,},
0377:             {0.5f*size, 0.0f*size,0, 1, 1.0f, 0.5f,},
0378:             {0.0f*size, 1.0f*size,0, 1, 1.0f, 1.0f,},
0379: 
0380:             {0.5f*size, 0.0f*size,0, 1, 0.0f, 0.5f,},
0381:             {1.0f*size, 0.0f*size,0, 1, 1.0f, 1.0f,},
0382:             {0.0f*size, 1.0f*size,0, 1, 0.0f, 1.0f,},
0383: 
0384:             {0.0f*size, 1.0f*size,0, 1, 0.0f, 0.0f,},
0385:             {1.0f*size, 0.0f*size,0, 1, 1.0f, 0.0f,},
0386:             {0.5f*size, 1.0f*size,0, 1, 1.0f, 0.5f,},
0387: 
0388:             {0.5f*size, 1.0f*size,0, 1, 0.0f, 0.5f,},
0389:             {1.0f*size, 0.0f*size,0, 1, 0.0f, 0.0f,},
0390:             {1.0f*size, 1.0f*size,0, 1, 1.0f, 1.0f,},
0391:         };
0392: 
0393:         if(init){
0394:             lpD3DDev->SetTexture( 0, pCatSeedTexture);      // initialize
0395:         }else{
0396:             lpD3DDev->SetTexture( 0, pCatTexture[1-id] );   // ordinary
0397:         }
0398:         lpD3DDev->SetVertexShader( FVF_TLVERTEX );
0399:         lpD3DDev->SetPixelShader(0);
0400:         lpD3DDev->DrawPrimitiveUP( D3DPT_TRIANGLELIST, 4, Vertex, sizeof( TLVERTEX ) );
0401:     }
0402: 
0403:     //-----------------------------------------------------------------------------
0404:     // Create Collision Map
0405:     //-----------------------------------------------------------------------------
0406:     lpD3DDev->SetRenderTarget(pTextureSurface, lpZbuffer);
0407:     lpD3DDev->Clear(0,NULL,D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0),1.0f,0);
0408:     
0409:     // Create Depth Map
0410:     D3DXVECTOR3 eye    = D3DXVECTOR3(0.0f,-10.0f,  0.0f);
0411:     D3DXVECTOR3 lookAt = D3DXVECTOR3(0.0f,  0.0f,  0.0f);
0412:     D3DXVECTOR3 up     = D3DXVECTOR3(0.0f,  0.0f,  1.0f);
0413:     D3DXMatrixLookAtLH(&mView, &eye, &lookAt, &up);
0414:     D3DXMatrixPerspectiveFovLH(&mProj
0415:         ,D3DX_PI/3.5f
0416:         ,1.0f
0417:         ,0.01f,100.0f
0418:         );
0419:     mat = mView * mProj;
0420:     lpD3DDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
0421:     lpD3DDev->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
0422:     pVertexProgramContainer->SetShaderActive();
0423:     pPixelProgramContainer->SetShaderActive();
0424:     DrawModel(lpD3DDev, mat, 2);
0425:     lpD3DDev->SetRenderState(D3DRS_COLORWRITEENABLE, 0xf);
0426:     lpD3DDev->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
0427:     
0428:     D3DXMatrixTranspose( &m, &mat );
0429:     pCollVC->SetShaderConstant( viewproj_matrix_iter, &m  );
0430: 
0431:     for(i=0;i<bullet.GetNum();i++){
0432:         if(!bullet.IsActive(i)) continue;
0433:         D3DXVECTOR4 &x = bullet.GetPosition(i);
0434:         D3DXVECTOR4 &v = bullet.GetVelosity(i);
0435: 
0436:         lpD3DDev->SetRenderTarget(pTextureSurface, lpZbuffer);
0437:         lpD3DDev->Clear(0,NULL,D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0),1.0f,0);
0438:         // Draw bullte from Front side
0439:         lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0440:         lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_DIFFUSE);
0441:         lpD3DDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
0442:         pCollVC->SetShaderActive();
0443:         lpD3DDev->SetPixelShader(0);
0444: 
0445:         pCollVC->SetShaderConstant( trans_iter, &x  );
0446:         pCollVC->SetShaderConstant( velocity_iter, &v  );
0447:         pCollVC->SetShaderConstant( color_iter, D3DXVECTOR4(1.0f,1.0f,1.0f,1.0f)  );
0448:         bullet.Draw(i, lpD3DDev);
0449: 
0450:         // Draw bullte from Back side
0451:         lpD3DDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
0452:         pCollVC->SetShaderConstant( color_iter, D3DXVECTOR4(0.0f,0.0f,0.0f,1.0f)  );
0453:         bullet.Draw(i, lpD3DDev);
0454:         lpD3DDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
0455: 
0456:         // Add 
0457:         lpD3DDev->SetRenderTarget(pBurnTextureSurface, NULL );
0458:         lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0459:         lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0460:         lpD3DDev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
0461:         float size = 512.0f;
0462:         TLVERTEX Vertex[4] = {
0463:             // x    y   z rhw tu tv
0464:             {   0,    0,0, 1, 1, 0,},
0465:             {size,    0,0, 1, 0, 0,},
0466:             {size, size,0, 1, 0, 1,},
0467:             {   0, size,0, 1, 1, 1,},
0468:         };
0469:         lpD3DDev->SetTexture( 0, pTexture);
0470:         lpD3DDev->SetVertexShader( FVF_TLVERTEX );
0471:         lpD3DDev->SetPixelShader(0);
0472:         lpD3DDev->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TLVERTEX ) );
0473:         lpD3DDev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
0474:     }
0475:     lpD3DDev->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
0476:     lpD3DDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
0477: 
0478:     //-----------------------------------------------------------------------------
0479:     // Create 16 box sampling of the pBurnTexture
0480:     //-----------------------------------------------------------------------------
0481:     lpD3DDev->SetRenderTarget(pBlackTextureSurface, NULL );
0482:     lpD3DDev->Clear(0,NULL,D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0),1.0f,0);
0483:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0484:     lpD3DDev->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_ADD);
0485:     lpD3DDev->SetTextureStageState(2,D3DTSS_COLOROP,    D3DTOP_ADD);
0486:     lpD3DDev->SetTextureStageState(3,D3DTSS_COLOROP,    D3DTOP_ADD);
0487:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0488:     lpD3DDev->SetTextureStageState(1,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0489:     lpD3DDev->SetTextureStageState(2,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0490:     lpD3DDev->SetTextureStageState(3,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0491:     lpD3DDev->SetTextureStageState(1,D3DTSS_COLORARG2,  D3DTA_CURRENT);
0492:     lpD3DDev->SetTextureStageState(2,D3DTSS_COLORARG2,  D3DTA_CURRENT);
0493:     lpD3DDev->SetTextureStageState(3,D3DTSS_COLORARG2,  D3DTA_CURRENT);
0494:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSU,   D3DTADDRESS_CLAMP);
0495:     lpD3DDev->SetTextureStageState(1,D3DTSS_ADDRESSU,   D3DTADDRESS_CLAMP);
0496:     lpD3DDev->SetTextureStageState(2,D3DTSS_ADDRESSU,   D3DTADDRESS_CLAMP);
0497:     lpD3DDev->SetTextureStageState(3,D3DTSS_ADDRESSU,   D3DTADDRESS_CLAMP);
0498:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSV,   D3DTADDRESS_CLAMP);
0499:     lpD3DDev->SetTextureStageState(1,D3DTSS_ADDRESSV,   D3DTADDRESS_CLAMP);
0500:     lpD3DDev->SetTextureStageState(2,D3DTSS_ADDRESSV,   D3DTADDRESS_CLAMP);
0501:     lpD3DDev->SetTextureStageState(3,D3DTSS_ADDRESSV,   D3DTADDRESS_CLAMP);
0502: 
0503:     TLVERTEX BlackVertex[] = {
0504:         // x    y   z   rhw tu tv
0505:         { -1,  1, 0.1f, 1.f, 0, 0,},
0506:         {  1,  1, 0.1f, 1.f, 1, 0,},
0507:         {  1, -1, 0.1f, 1.f, 1, 1,},
0508:         { -1, -1, 0.1f, 1.f, 0, 1,},
0509:     };
0510: 
0511:     pBlackVC->SetShaderActive();
0512:     lpD3DDev->SetTexture(0, pBurnTexture);
0513:     lpD3DDev->SetTexture(1, pBurnTexture);
0514:     lpD3DDev->SetTexture(2, pBurnTexture);
0515:     lpD3DDev->SetTexture(3, pBurnTexture);
0516:     lpD3DDev->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, BlackVertex, sizeof( TLVERTEX ) );
0517:     lpD3DDev->SetTexture(0, NULL);
0518:     lpD3DDev->SetTexture(1, NULL);
0519:     lpD3DDev->SetTexture(2, NULL);
0520:     lpD3DDev->SetTexture(3, NULL);
0521: 
0522:     //-----------------------------------------------------------------------------
0523:     // Create 3D Collision map
0524:     //-----------------------------------------------------------------------------
0525:     eye    = D3DXVECTOR3(0.0f,  2.0f,  4.0f);
0526:     lookAt = D3DXVECTOR3(0.0f,  1.0f,  0.0f);
0527:     up     = D3DXVECTOR3(0.0f,  1.0f,  0.0f);
0528:     D3DXMatrixLookAtLH(&mView, &eye, &lookAt, &up);
0529:     mat = view.Get() * gProj;
0530: 
0531:     lpD3DDev->SetRenderTarget(pEnvTextureSurface, lpZbuffer );
0532:     lpD3DDev->Clear(0,NULL,D3DCLEAR_ZBUFFER, 0,1.0f,0);
0533:     pVertexProgramContainer->SetShaderActive();
0534:     pPixelProgramContainer->SetShaderActive();
0535:     DrawModel(lpD3DDev, mat, 1);
0536:         
0537:     //-----------------------------------------------------------------------------
0538:     // Create fire effect
0539:     //-----------------------------------------------------------------------------
0540:     lpD3DDev->SetRenderTarget(pHitTextureSurface[id], NULL);
0541:     lpD3DDev->Clear(0,NULL,D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0),1.0f,0);
0542:         
0543:     if(!init){
0544:         lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0545:         lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0546:         lpD3DDev->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0547:         lpD3DDev->SetTextureStageState(1,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0548:         lpD3DDev->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_POINT );
0549:         lpD3DDev->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_POINT );
0550:         lpD3DDev->SetTextureStageState(1, D3DTSS_MAGFILTER, D3DTEXF_POINT );
0551:         lpD3DDev->SetTextureStageState(1, D3DTSS_MINFILTER, D3DTEXF_POINT );
0552:         lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSU,   D3DTADDRESS_CLAMP);
0553:         lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSV,   D3DTADDRESS_CLAMP);
0554:         lpD3DDev->SetTextureStageState(1,D3DTSS_ADDRESSU,   D3DTADDRESS_CLAMP);
0555:         lpD3DDev->SetTextureStageState(1,D3DTSS_ADDRESSV,   D3DTADDRESS_CLAMP);
0556:         TLVERTEX Vertex[] = {
0557:             // x    y   z   rhw tu tv
0558:             { -1,  1, 0.1f, 1.f, 0+0.5f/512.0f, 1+0.5f/512.0f,},
0559:             {  1,  1, 0.1f, 1.f, 1+0.5f/512.0f, 1+0.5f/512.0f,},
0560:             {  1, -1, 0.1f, 1.f, 1+0.5f/512.0f, 0+0.5f/512.0f,},
0561:             { -1, -1, 0.1f, 1.f, 0+0.5f/512.0f, 0+0.5f/512.0f,},
0562:         };
0563: 
0564:         pBurnVC->SetShaderActive();
0565:         pBurnPC->SetShaderActive();
0566:         pBurnPC->SetTexture(BurnTex0_iter, pHitTexture[1-id]);
0567:         pBurnPC->SetTexture(BurnTex1_iter, pEnvTexture);
0568:         pBurnPC->SetTexture(BurnTex2_iter, pCatTexture[id]);
0569:         lpD3DDev->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TLVERTEX ) );
0570:     }
0571: 
0572:     //-----------------------------------------------------------------------------
0573:     // Make final result
0574:     //-----------------------------------------------------------------------------
0575:     lpD3DDev->SetRenderTarget(pBackbuffer, lpZbuffer );
0576:     lpD3DDev->Clear(0,NULL,D3DCLEAR_ZBUFFER, 0,1.0f,0);
0577: 
0578:     pVertexProgramContainer->SetShaderActive();
0579:     pPixelProgramContainer->SetShaderActive();
0580:     lpD3DDev->SetTextureStageState(1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
0581:     lpD3DDev->SetTextureStageState(1, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
0582:     DrawModel(lpD3DDev, mat, 0);
0583: 
0584:     // Add burned result
0585:     {
0586:         lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0587:         lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0588:         lpD3DDev->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0589:         lpD3DDev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
0590:         float size = 512.0f;
0591:         TLVERTEX Vertex[4] = {
0592:             // x    y   z rhw tu tv
0593:             {   0,    0,0, 1, 0, 0,},
0594:             {size,    0,0, 1, 1, 0,},
0595:             {size, size,0, 1, 1, 1,},
0596:             {   0, size,0, 1, 0, 1,},
0597:         };
0598:         lpD3DDev->SetTexture( 0, pHitTexture[id] );
0599:         lpD3DDev->SetVertexShader( FVF_TLVERTEX );
0600:         lpD3DDev->SetPixelShader(0);
0601:         lpD3DDev->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TLVERTEX ) );
0602:         lpD3DDev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
0603:     }
0604: 
0605: #if 0
0606:     //-----------------------------------------------------------------------------
0607:     // For debugging, this part shows rendered texture.
0608:     //-----------------------------------------------------------------------------
0609:     {
0610:         lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0611:         lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0612:         lpD3DDev->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0613:         float scale = 128.0f;
0614:         TLVERTEX Vertex[4] = {
0615:             // x  y  z rhw tu tv
0616:             {    0,    0,0, 1, 0, 0,},
0617:             {scale,    0,0, 1, 1, 0,},
0618:             {scale,scale,0, 1, 1, 1,},
0619:             {    0,scale,0, 1, 0, 1,},
0620:         };
0621: //      lpD3DDev->SetTexture( 0, pCatTexture[id] );
0622: //      lpD3DDev->SetTexture( 0, pHitTexture[id] );
0623: //      lpD3DDev->SetTexture( 0, pEnvTexture );
0624:         lpD3DDev->SetTexture( 0, pBlackTexture );
0625: //      lpD3DDev->SetTexture( 0, pBurnTexture );
0626: //      lpD3DDev->SetTexture( 0, pTexture );
0627:         lpD3DDev->SetVertexShader( FVF_TLVERTEX );
0628:         lpD3DDev->SetPixelShader(0);
0629:         lpD3DDev->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TLVERTEX ) );
0630:     }
0631: #endif
0632:     init=FALSE;
0633:     id=1-id;
0634: }
0635: //-----------------------------------------------------------------------------
0636: // Name: CleanRender()
0637: //-----------------------------------------------------------------------------
0638: void CleanRender(LPDIRECT3DDEVICE8 lpD3DDev)
0639: {
0640:     RELEASE(pCatSeedTexture);
0641:     RELEASE(pCatTextureSurface[1]);
0642:     RELEASE(pCatTextureSurface[0]);
0643:     RELEASE(pCatTexture[1]);
0644:     RELEASE(pCatTexture[0]);
0645: 
0646:     RELEASE(pHitTextureSurface[1]);
0647:     RELEASE(pHitTextureSurface[0]);
0648:     RELEASE(pHitTexture[1]);
0649:     RELEASE(pHitTexture[0]);
0650: 
0651:     RELEASE(pEnvTextureSurface);
0652:     RELEASE(pEnvTexture);
0653: 
0654:     RELEASE(pBurnTextureSurface);
0655:     RELEASE(pBurnTexture);
0656: 
0657:     RELEASE(pBlackTextureSurface);
0658:     RELEASE(pBlackTexture);
0659: 
0660:     RELEASE(pTextureSurface);
0661:     RELEASE(pTexture);
0662:     RELEASE(pBackbuffer);
0663: 
0664:     CleanBg(lpD3DDev);
0665: }
0666: //-----------------------------------------------------------------------------
0667: // eazy camera setting
0668: //-----------------------------------------------------------------------------
0669: static void set_camera(float cam_r, float cam_theta, float cam_phi, const D3DXVECTOR4 &cam_tr)
0670: {
0671:     D3DXVECTOR4 v = cam_tr;
0672:     view.SetLookAt(v);
0673:     v.z += cam_r * (float)cos(cam_phi) * (float)cos(cam_theta);
0674:     v.x += cam_r * (float)cos(cam_phi) * (float)sin(cam_theta);
0675:     v.y += cam_r * (float)sin(cam_phi);
0676:     view.SetEye(v);
0677: }
0678: //-----------------------------------------------------------------------------
0679: // Name: MyMsgProc()
0680: //-----------------------------------------------------------------------------
0681: LRESULT CALLBACK MyMsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
0682: {
0683:     static int  type = 0;
0684:     // mouse position
0685:     static long prevX = 0;
0686:     static long prevY = 0;
0687:     long x,y,dx,dy;
0688:     // camera
0689:     static D3DXVECTOR4 cam_tr = D3DXVECTOR4(0.0f, 0.5f, 0.0f, 0.0f);
0690:     static float cam_r      = 5.0f;
0691:     static float cam_theta  = 0.0f;
0692:     static float cam_phi    = 0.1f*D3DX_PI;
0693: 
0694:     switch(msg){
0695:     case WM_CREATE:
0696:         set_camera(cam_r, cam_theta, cam_phi, cam_tr);
0697:         break;
0698:     case WM_RBUTTONDOWN:
0699:         type=1;
0700:         prevX = (lParam << 16) >> 16;
0701:         prevY = lParam >> 16;
0702:         SetCapture(hWnd);
0703:         break;
0704:     case WM_LBUTTONDOWN:
0705:         {
0706:             D3DXMATRIX mat = view.Get() * gProj;
0707:             D3DXVECTOR4 c = D3DXVECTOR4(mouse[0]/256.0f-1,1-mouse[1]/256.0f,1.0f,1.f);
0708:             D3DXMatrixInverse(&mat, NULL, &mat);
0709:             D3DXVec4Transform (&c, &c, &mat);
0710:             D3DXVECTOR4 x = view.GetEye(); x.y -= 0.2f;
0711:             D3DXVECTOR4 v = 100.0f*c-x;
0712:             D3DXVec4Normalize(&v,&v);
0713:             bullet.Add(x, BULLET_SPEED*v);
0714:         }
0715:         break;
0716:     case WM_RBUTTONUP:
0717:         type=0;
0718:         ReleaseCapture();
0719:         break;
0720:     case WM_MOUSEMOVE:
0721:         x = (lParam << 16) >> 16;
0722:         y = lParam >> 16;
0723:         mouse[0] = (float)x;
0724:         mouse[1] = (float)y;
0725:         dx = x - prevX;
0726:         dy = y - prevY;
0727:         switch(type){
0728:         case 1:// camera
0729:             if(wParam & MK_RBUTTON){
0730:                 if(dy != 0){
0731:                     cam_phi += 0.005f*dy;
0732:                     if( 0.499f*D3DX_PI<cam_phi)cam_phi = 0.499f*D3DX_PI;
0733:                     if(cam_phi<-0.000f*D3DX_PI)cam_phi =-0.000f*D3DX_PI;
0734:                     set_camera(cam_r, cam_theta, cam_phi, cam_tr);
0735:                 }
0736:                 if(dx != 0){
0737:                     cam_theta += 0.005f*dx;
0738:                     set_camera(cam_r, cam_theta, cam_phi, cam_tr);
0739:                 }
0740:             }
0741:             break;
0742:         }
0743:         prevX = x;
0744:         prevY = y;
0745:         break;
0746:     case 0x020A:// WM_MOUSEWHEEL
0747:         cam_r += 0.001f*(float)(short)HIWORD(wParam);
0748:         if(cam_r<=0.1f)cam_r = 0.1f;
0749:         if(5.0f<cam_r)cam_r = 5.0f;
0750: 
0751:         set_camera(cam_r, cam_theta, cam_phi, cam_tr);
0752:         break;
0753:     case WM_KEYDOWN:
0754:         switch(wParam){
0755:         case VK_SPACE:
0756:             init = TRUE;
0757:             break;
0758:         default:
0759:             break;
0760:         }
0761:         break;
0762:     }
0763: 
0764:     return 0L;
0765: }