0001: // ----------------------------------------------------------------------------
0002: //
0003: // draw.cpp - 背景
0004: // 
0005: // Copyright (c) 2001 if (if@edokko.com)
0006: // All Rights Reserved.
0007: //
0008: // ----------------------------------------------------------------------------
0009: #define STRICT
0010: 
0011: 
0012: #include "main.h"
0013: #include "load.h"
0014: 
0015: // 空
0016: #define NUM_Y       1   // 縦方向に何ポリゴン使うか
0017: #define NUM_CIRCLE  32  // 一回り何ポリゴンか
0018: #define NUM_VERTICES            ((NUM_Y+1)*(NUM_CIRCLE+1))
0019: #define NUM_INDICES_PERFACE     (3*2)
0020: #define NUM_FACES               (NUM_Y*NUM_CIRCLE)
0021: #define NUM_VERTICES_PERFACE    4
0022: 
0023: // 床
0024: #define FLOOR_POLYGONE  10
0025: #define FLOOR_VERTEXES  ((FLOOR_POLYGONE+1)*(FLOOR_POLYGONE+1))
0026: #define FLOOR_FACES     ((FLOOR_POLYGONE)*(FLOOR_POLYGONE))
0027: #define FLOOR_SIZE      (10.0f)
0028: 
0029: // ----------------------------------------------------------------------------
0030: typedef struct{
0031:     float x,y,z;
0032:     float nx,ny,nz;
0033:     float tu,tv;
0034: } CylinderVertex;
0035: #define CYLINDER_VERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)
0036: 
0037: typedef struct{
0038:     float x,y,z;
0039:     float nx,ny,nz;
0040:     float  tu, tv;
0041: } FloorVertex;
0042: #define FLOOR_VERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)
0043: 
0044: // ----------------------------------------------------------------------------
0045: // 空
0046: LPDIRECT3DVERTEXBUFFER8     pCylinderVB;
0047: LPDIRECT3DINDEXBUFFER8      pCylinderIB;
0048: LPDIRECT3DTEXTURE8          pCylinderTex;
0049: // 床
0050: LPDIRECT3DVERTEXBUFFER8     pFloorVB;
0051: LPDIRECT3DINDEXBUFFER8      pFloorIB;
0052: LPDIRECT3DTEXTURE8          pFloorTex;
0053: 
0054: // ----------------------------------------------------------------------------
0055: void InitBg(LPDIRECT3DDEVICE8 lpD3DDev)
0056: {
0057:     //
0058:     // 円柱
0059:     //
0060:     // 頂点バッファの作成 
0061:     CylinderVertex *pDest;
0062:     lpD3DDev->CreateVertexBuffer( NUM_VERTICES * sizeof(CylinderVertex),
0063:                                 D3DUSAGE_WRITEONLY, 
0064:                                 CYLINDER_VERTEX, D3DPOOL_MANAGED,
0065:                                 &pCylinderVB );
0066: 
0067:     // 頂点をセットアップ
0068:     WORD k=0;
0069:     pCylinderVB->Lock ( 0, 0, (BYTE**)&pDest, 0 );
0070:     float r = 50.0f;
0071:     float h = 50.0f;
0072:     for (DWORD i = 0; i <= NUM_CIRCLE; i++) {
0073:         float theta = (2*PI*(float)i)/(float)NUM_CIRCLE;
0074:         for (DWORD j = 0; j <= NUM_Y; j++) {
0075:             pDest->x = r * (float)cos(theta);
0076:             pDest->z = r * (float)sin(theta);
0077:             pDest->y = h*((float)j/(float)NUM_Y-0.0f);
0078:             pDest->nx   =-(float)sin(theta);
0079:             pDest->ny   = 0.0f;
0080:             pDest->nz   = (float)cos(theta);
0081:             pDest->tu = (float)i / (float)NUM_CIRCLE;
0082:             pDest->tv = 1.0f-(float)j / (float)NUM_Y;
0083:             pDest += 1;
0084:         }
0085:     }       
0086:     pCylinderVB->Unlock ();
0087: 
0088: 
0089:     // インデックスをセットアップ
0090:     WORD *pIndex;
0091:     lpD3DDev->CreateIndexBuffer( NUM_INDICES_PERFACE  * NUM_FACES * sizeof(WORD),
0092:                                      0 ,
0093:                                      D3DFMT_INDEX16, D3DPOOL_DEFAULT,
0094:                                      &pCylinderIB );
0095:     pCylinderIB->Lock ( 0, 0, (BYTE**)&pIndex, 0 );
0096:     {
0097:     for (WORD i = 0; i < NUM_CIRCLE; i++) {
0098:         for (WORD j = 0; j < NUM_Y; j++) {
0099:             *pIndex++ = j + 0 + (i+0) * (NUM_Y+1);
0100:             *pIndex++ = j + 0 + (i+1) * (NUM_Y+1);
0101:             *pIndex++ = j + 1 + (i+0) * (NUM_Y+1);
0102: 
0103:             *pIndex++ = j + 1 + (i+0) * (NUM_Y+1);
0104:             *pIndex++ = j + 0 + (i+1) * (NUM_Y+1);
0105:             *pIndex++ = j + 1 + (i+1) * (NUM_Y+1);
0106:         }
0107:     }
0108:     }
0109:     pCylinderIB->Unlock ();
0110: 
0111:     CTextureMgr::Load(lpD3DDev, "sky.bmp", &pCylinderTex);
0112:     
0113:     //
0114:     // 床
0115:     //
0116:     // 頂点バッファの作成 
0117:     FloorVertex *pFloorDest;
0118:     lpD3DDev->CreateVertexBuffer( FLOOR_VERTEXES * sizeof(FloorVertex),
0119:                                 D3DUSAGE_WRITEONLY, FLOOR_VERTEX, D3DPOOL_MANAGED,
0120:                                 &pFloorVB );
0121:     // 頂点をセットアップ
0122:     {
0123:     pFloorVB->Lock ( 0, 0, (BYTE**)&pFloorDest, 0 );
0124:     for (DWORD i = 0; i < FLOOR_POLYGONE+1; i++) {
0125:     for (DWORD j = 0; j < FLOOR_POLYGONE+1; j++) {
0126:         pFloorDest->x   = FLOOR_SIZE*((float)i-FLOOR_POLYGONE/2);
0127:         pFloorDest->z   = FLOOR_SIZE*((float)j-FLOOR_POLYGONE/2);
0128:         pFloorDest->y   = 0.0f;
0129:         pFloorDest->nx  = 0.0f;
0130:         pFloorDest->ny  = 1.0f;
0131:         pFloorDest->nz  = 0.0f;
0132:         pFloorDest->tu  = (float)i;
0133:         pFloorDest->tv  = (float)j;
0134:         pFloorDest++;
0135:     }
0136:     }
0137:     pFloorVB->Unlock ();
0138:     }
0139: 
0140: 
0141:     // インデックスをセットアップ
0142:     lpD3DDev->CreateIndexBuffer( NUM_INDICES_PERFACE*FLOOR_FACES * sizeof(WORD),
0143:                                0,
0144:                                D3DFMT_INDEX16, D3DPOOL_MANAGED,
0145:                                &pFloorIB );
0146:     {
0147:     pFloorIB->Lock ( 0, 0, (BYTE**)&pIndex, 0 );
0148:     for (WORD i = 0; i < FLOOR_POLYGONE; i++) {
0149:     for (WORD j = 0; j < FLOOR_POLYGONE; j++) {
0150:         *pIndex++ = j + 0 + (i+0) * (FLOOR_POLYGONE+1);
0151:         *pIndex++ = j + 1 + (i+0) * (FLOOR_POLYGONE+1);
0152:         *pIndex++ = j + 0 + (i+1) * (FLOOR_POLYGONE+1);
0153: 
0154:         *pIndex++ = j + 0 + (i+1) * (FLOOR_POLYGONE+1);
0155:         *pIndex++ = j + 1 + (i+0) * (FLOOR_POLYGONE+1);
0156:         *pIndex++ = j + 1 + (i+1) * (FLOOR_POLYGONE+1);
0157:     }
0158:     }
0159:     pFloorIB->Unlock ();
0160:     }
0161: 
0162:     CTextureMgr::Load(lpD3DDev, "tile.bmp", &pFloorTex);
0163: }
0164: // ----------------------------------------------------------------------------
0165: void DrawBg(LPDIRECT3DDEVICE8 lpD3DDev, D3DXMATRIX &mV, D3DXMATRIX &mP )
0166: {
0167:     D3DXMATRIX m;
0168:     
0169:     D3DXMatrixIdentity( &m );
0170:     lpD3DDev->SetVertexShaderConstant(4,&m, 4);
0171: 
0172:     m = mV * mP;
0173:     D3DXMatrixTranspose( &m ,  &m);
0174:     lpD3DDev->SetVertexShaderConstant(0,&m, 4);
0175: 
0176:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_MODULATE);
0177: 
0178:     // 環境光のみ
0179:     lpD3DDev->SetVertexShaderConstant(13, &D3DXVECTOR4(0.0f, 0.0f, 0.0f, 1.0f), 1);
0180:     lpD3DDev->SetVertexShaderConstant(14, &D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f), 1);
0181: 
0182:     // 空
0183:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSU,   D3DTADDRESS_WRAP);
0184:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSV,   D3DTADDRESS_CLAMP);
0185:     lpD3DDev->SetTexture(0,pCylinderTex);
0186:     lpD3DDev->SetStreamSource(0, pCylinderVB, sizeof(CylinderVertex));
0187:     lpD3DDev->SetIndices(pCylinderIB,0);
0188:     lpD3DDev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, NUM_VERTICES, 0 , NUM_FACES*2 );
0189:     
0190:     // 床
0191:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSU,   D3DTADDRESS_WRAP);
0192:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSV,   D3DTADDRESS_WRAP);
0193:     lpD3DDev->SetTexture(0,pFloorTex);
0194:     lpD3DDev->SetStreamSource(0, pFloorVB, sizeof(FloorVertex));
0195:     lpD3DDev->SetIndices(pFloorIB,0);
0196:     lpD3DDev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, FLOOR_VERTEXES, 0, FLOOR_FACES*2 );
0197:     
0198:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG2);
0199: }
0200: // ----------------------------------------------------------------------------
0201: void CleanBg(LPDIRECT3DDEVICE8 lpD3DDev)
0202: {
0203:     RELEASE(pFloorVB);
0204:     RELEASE(pFloorIB);
0205:     
0206:     RELEASE(pCylinderVB);
0207:     RELEASE(pCylinderIB);
0208: 
0209:     CTextureMgr::Release(pFloorTex);
0210:     CTextureMgr::Release(pCylinderTex);
0211: }