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: 
0015: 
0016: LPDIRECT3DSURFACE8      pBackbuffer = NULL;
0017: LPDIRECT3DTEXTURE8      pCatSeedTexture=NULL;
0018: LPDIRECT3DTEXTURE8      pCatTexture[2]={NULL,NULL};
0019: LPDIRECT3DSURFACE8      pCatSurface[2]={NULL,NULL};
0020: 
0021: // ----------------------------------------------------------------------------
0022: // Vertex format
0023: // ----------------------------------------------------------------------------
0024: struct TLVERTEX
0025: {
0026:     float x,y,z,rhw;
0027:     float tu,tv;
0028: };
0029: #define FVF_TLVERTEX (D3DFVF_XYZRHW | D3DFVF_TEX1)
0030: 
0031: //-----------------------------------------------------------------------------
0032: // Name: InitRender()
0033: //-----------------------------------------------------------------------------
0034: HRESULT InitRender(LPDIRECT3DDEVICE8 lpD3DDev)
0035: {
0036:     DWORD i;
0037:     HRESULT hr;
0038: 
0039:     // ==================================================
0040:     // 本絵の読み込み
0041:     // ==================================================
0042:     hr = D3DXCreateTextureFromFileEx(lpD3DDev, "cat.bmp",0,0,0,0,D3DFMT_A8R8G8B8,
0043:                                 D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR,
0044:                                 0, NULL, NULL, &pCatSeedTexture);
0045:     
0046: 
0047:     // ==================================================
0048:     // レンダリングターゲットの作成
0049:     // ==================================================
0050:     // バックバッファを保存する
0051:     D3DSURFACE_DESC Desc;
0052:     LPDIRECT3DSURFACE8 lpZbuffer = NULL;
0053:     if( FAILED(hr = lpD3DDev->GetRenderTarget(&pBackbuffer))) return hr;
0054:     if( FAILED(hr = pBackbuffer->GetDesc( &Desc ))) return hr;
0055:     if( FAILED(hr = lpD3DDev->GetDepthStencilSurface( &lpZbuffer ))) return hr;
0056: 
0057:     for(i=0;i<2;i++){
0058:         // CatMap
0059:         if( FAILED(hr = lpD3DDev->CreateTexture(128, 128, 1
0060:                                 , D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pCatTexture[i]))) return hr;
0061:         if( FAILED(hr = pCatTexture[i]->GetSurfaceLevel(0,&pCatSurface[i]))) return hr;
0062:     }
0063: 
0064: 
0065:     // ==================================================
0066:     // Initialize render states
0067:     // ==================================================
0068:     lpD3DDev->SetRenderState( D3DRS_ZENABLE, FALSE );
0069:     lpD3DDev->SetRenderState( D3DRS_LIGHTING,  FALSE );
0070:     lpD3DDev->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);
0071: 
0072:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0073:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0074:     lpD3DDev->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_POINT );
0075:     lpD3DDev->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_POINT );
0076: 
0077:     return S_OK;
0078: }
0079: 
0080: //-----------------------------------------------------------------------------
0081: // Name: Render()
0082: //-----------------------------------------------------------------------------
0083: VOID Render(LPDIRECT3DDEVICE8 lpD3DDev)
0084: {
0085:     static BOOL     init = TRUE;
0086:     static DWORD    id=0;
0087:     LPDIRECT3DSURFACE8 lpZbuffer = NULL;
0088:     lpD3DDev->GetDepthStencilSurface( &lpZbuffer );
0089: 
0090:     static DWORD last = 0;
0091:     DWORD now = timeGetTime();
0092:     DWORD cnt = now-last;
0093:     
0094:     if(init){
0095:         cnt = 0;
0096:         last = now;
0097:         id=0;
0098:         lpD3DDev->SetRenderTarget(pCatSurface[id], NULL);
0099:         float scale = 128.0f;
0100:         TLVERTEX Vertex[4] = {
0101:             // x  y  z rhw tu tv
0102:             {    0,    0,0, 1, 0, 0,},
0103:             {scale,    0,0, 1, 1, 0,},
0104:             {scale,scale,0, 1, 1, 1,},
0105:             {    0,scale,0, 1, 0, 1,},
0106:         };
0107:         lpD3DDev->SetTexture( 0, pCatSeedTexture);
0108:         lpD3DDev->SetVertexShader( FVF_TLVERTEX );
0109:         lpD3DDev->SetPixelShader(0);
0110:         lpD3DDev->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TLVERTEX ) );
0111:     }
0112: 
0113:     //-----------------------------------------------------------------------------
0114:     // CatMap
0115:     //-----------------------------------------------------------------------------
0116:     if(3000<cnt){
0117:         id=1-id;
0118:         last = now;
0119:         lpD3DDev->SetRenderTarget(pCatSurface[id], NULL);
0120: 
0121:         float size = 128.0f;
0122:         TLVERTEX Vertex[] = {
0123:             //   x          y     z rhw  tu    tv
0124:             {0.0f*size, 0.0f*size,0, 1, 0.0f, 0.0f,},
0125:             {0.5f*size, 0.0f*size,0, 1, 1.0f, 0.5f,},
0126:             {0.0f*size, 1.0f*size,0, 1, 1.0f, 1.0f,},
0127: 
0128:             {0.5f*size, 0.0f*size,0, 1, 0.0f, 0.5f,},
0129:             {1.0f*size, 0.0f*size,0, 1, 1.0f, 1.0f,},
0130:             {0.0f*size, 1.0f*size,0, 1, 0.0f, 1.0f,},
0131: 
0132:             {0.0f*size, 1.0f*size,0, 1, 0.0f, 0.0f,},
0133:             {1.0f*size, 0.0f*size,0, 1, 1.0f, 0.0f,},
0134:             {0.5f*size, 1.0f*size,0, 1, 1.0f, 0.5f,},
0135: 
0136:             {0.5f*size, 1.0f*size,0, 1, 0.0f, 0.5f,},
0137:             {1.0f*size, 0.0f*size,0, 1, 0.0f, 0.0f,},
0138:             {1.0f*size, 1.0f*size,0, 1, 1.0f, 1.0f,},
0139:         };
0140: 
0141:         lpD3DDev->SetTexture( 0, pCatTexture[1-id] );
0142:         lpD3DDev->SetVertexShader( FVF_TLVERTEX );
0143:         lpD3DDev->SetPixelShader(0);
0144:         lpD3DDev->DrawPrimitiveUP( D3DPT_TRIANGLELIST, 4, Vertex, sizeof( TLVERTEX ) );
0145:     }
0146:     lpD3DDev->SetRenderTarget(pBackbuffer, lpZbuffer);
0147: 
0148:     //-----------------------------------------------------------------------------
0149:     // 作成したテクスチャーを張る
0150:     //-----------------------------------------------------------------------------
0151:     {
0152:         lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0153:         lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0154:         lpD3DDev->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0155:         float scale = 512.0f;
0156:         TLVERTEX Vertex[4] = {
0157:             // x  y  z rhw tu tv
0158:             {    0,    0,0, 1, 0, 0,},
0159:             {scale,    0,0, 1, 1, 0,},
0160:             {scale,scale,0, 1, 1, 1,},
0161:             {    0,scale,0, 1, 0, 1,},
0162:         };
0163:         lpD3DDev->SetTexture( 0, pCatTexture[id] );
0164:         lpD3DDev->SetVertexShader( FVF_TLVERTEX );
0165:         lpD3DDev->SetPixelShader(0);
0166:         lpD3DDev->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TLVERTEX ) );
0167:     }
0168: 
0169:     init=FALSE;
0170: }
0171: //-----------------------------------------------------------------------------
0172: // Name: CleanRender()
0173: //-----------------------------------------------------------------------------
0174: void CleanRender(LPDIRECT3DDEVICE8 lpD3DDev)
0175: {
0176:     RELEASE(pCatSeedTexture);
0177:     RELEASE(pCatSurface[1]);
0178:     RELEASE(pCatSurface[0]);
0179:     RELEASE(pCatTexture[1]);
0180:     RELEASE(pCatTexture[0]);
0181: }
0182: