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