0001: // ----------------------------------------------------------------------------
0002: //
0003: // bg.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: #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  (10.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 = 10.0f;
0056:     float h = 10.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:             pDest->x = r * (float)cos(theta);
0061:             pDest->z = r * (float)sin(theta);
0062:             pDest->y = h*((float)j/(float)NUM_Y-0.0f);
0063:             pDest->nx = 0;
0064:             pDest->nz = 0;
0065:             pDest->ny = 0;
0066:             pDest->tu0 = (float)i / (float)NUM_CIRCLE;
0067:             pDest->tv0 = 1.0f-(float)j / (float)NUM_Y;
0068:             pDest += 1;
0069:         }
0070:     }       
0071:     pCylinderVB->Unlock ();
0072: 
0073: 
0074:     // インデックスをセットアップ
0075:     WORD *pIndex;
0076:     lpD3DDev->CreateIndexBuffer( NUM_INDICES_PERFACE  * NUM_FACES * sizeof(WORD),
0077:                                      0 ,
0078:                                      D3DFMT_INDEX16, D3DPOOL_DEFAULT,
0079:                                      &pCylinderIB );
0080:     pCylinderIB->Lock ( 0, 0, (BYTE**)&pIndex, 0 );
0081:     {
0082:     for (WORD i = 0; i < NUM_CIRCLE; i++) {
0083:         for (WORD j = 0; j < NUM_Y; j++) {
0084:             *pIndex++ = j + 0 + (i+0) * (NUM_Y+1);
0085:             *pIndex++ = j + 0 + (i+1) * (NUM_Y+1);
0086:             *pIndex++ = j + 1 + (i+0) * (NUM_Y+1);
0087: 
0088:             *pIndex++ = j + 1 + (i+0) * (NUM_Y+1);
0089:             *pIndex++ = j + 0 + (i+1) * (NUM_Y+1);
0090:             *pIndex++ = j + 1 + (i+1) * (NUM_Y+1);
0091:         }
0092:     }
0093:     }
0094:     pCylinderIB->Unlock ();
0095: 
0096:     CTextureMgr::Load(lpD3DDev, "sky.bmp", &pCylinderTex);
0097:     
0098:     //
0099:     // 床
0100:     //
0101:     // 頂点バッファの作成 
0102:     D3DVERTEX *pFloorDest;
0103:     lpD3DDev->CreateVertexBuffer( (FLOOR_GRID+1)*(FLOOR_GRID+1)*sizeof(D3DVERTEX),
0104:                                 D3DUSAGE_WRITEONLY, D3DFVF_VERTEX, D3DPOOL_MANAGED,
0105:                                 &pFloorVB );
0106:     // 頂点をセットアップ
0107:     {
0108:     pFloorVB->Lock ( 0, 0, (BYTE**)&pFloorDest, 0 );
0109:     float pos_z = -FLOOR_SIZE;
0110:     for(DWORD z = 0; z <= FLOOR_GRID; z++){
0111:         float pos_x = -FLOOR_SIZE;
0112:         for(DWORD x = 0; x <= FLOOR_GRID; x++){
0113:             pFloorDest->x   = pos_x;
0114:             pFloorDest->z   = pos_z;
0115:             pFloorDest->y   = 0.0f;
0116:             pFloorDest->nx  = 0.0f;
0117:             pFloorDest->nz  = 0.0f;
0118:             pFloorDest->ny  = 1.0f;
0119:             pFloorDest->tu0 = FLOOR_UV*(pos_x+FLOOR_SIZE)/(2.0f*FLOOR_SIZE);
0120:             pFloorDest->tv0 = FLOOR_UV*(pos_z+FLOOR_SIZE)/(2.0f*FLOOR_SIZE);
0121:             pFloorDest++;
0122:             pos_x += 2*FLOOR_SIZE/FLOOR_GRID;
0123:         }
0124:         pos_z += 2*FLOOR_SIZE/FLOOR_GRID;
0125:     }
0126:     pFloorVB->Unlock ();
0127:     }
0128: 
0129: 
0130:     // インデックスをセットアップ
0131:     lpD3DDev->CreateIndexBuffer( 3*2*FLOOR_GRID*FLOOR_GRID * sizeof(WORD),
0132:                                0,
0133:                                D3DFMT_INDEX16, D3DPOOL_MANAGED,
0134:                                &pFloorIB );
0135:     pFloorIB->Lock ( 0, 0, (BYTE**)&pIndex, 0 );
0136:     {
0137:     for (WORD i = 0; i < FLOOR_GRID; i++) {
0138:         for (WORD j = 0; j < FLOOR_GRID; j++) {
0139:             *pIndex++ = j + 0 + (i+0) * (FLOOR_GRID+1);
0140:             *pIndex++ = j + 0 + (i+1) * (FLOOR_GRID+1);
0141:             *pIndex++ = j + 1 + (i+0) * (FLOOR_GRID+1);
0142: 
0143:             *pIndex++ = j + 1 + (i+0) * (FLOOR_GRID+1);
0144:             *pIndex++ = j + 0 + (i+1) * (FLOOR_GRID+1);
0145:             *pIndex++ = j + 1 + (i+1) * (FLOOR_GRID+1);
0146:         }
0147:     }
0148:     }
0149: 
0150: //  pIndex[0] = 0;  pIndex[1] = 1;  pIndex[2] = 2;
0151: //  pIndex[3] = 1;  pIndex[4] = 3;  pIndex[5] = 2;
0152:     pFloorIB->Unlock ();
0153: 
0154:     CTextureMgr::Load(lpD3DDev, "tile.bmp", &pFloorTex);
0155: }
0156: // ----------------------------------------------------------------------------
0157: void DrawBg(LPDIRECT3DDEVICE8 lpD3DDev)
0158: {
0159:     lpD3DDev->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
0160:     lpD3DDev->SetTextureStageState(0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1);
0161: 
0162:     // 床
0163:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSU,   D3DTADDRESS_WRAP);
0164:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSV,   D3DTADDRESS_WRAP);
0165:     lpD3DDev->SetTexture(0,pFloorTex);
0166: 
0167:     lpD3DDev->SetStreamSource(0, pFloorVB, sizeof(D3DVERTEX));
0168:     lpD3DDev->SetIndices(pFloorIB,0);
0169:     lpD3DDev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, (FLOOR_GRID+1)*(FLOOR_GRID+1), 0, 2*FLOOR_GRID*FLOOR_GRID );
0170: 
0171:     // 空
0172:     lpD3DDev->SetPixelShader(0);
0173:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSU,   D3DTADDRESS_WRAP);
0174:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSV,   D3DTADDRESS_CLAMP);
0175:     lpD3DDev->SetTexture(0,pCylinderTex);
0176:     lpD3DDev->SetStreamSource(0, pCylinderVB, sizeof(D3DVERTEX));
0177:     lpD3DDev->SetIndices(pCylinderIB,0);
0178:     lpD3DDev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, NUM_VERTICES, 0 , NUM_FACES*2 );
0179: }
0180: // ----------------------------------------------------------------------------
0181: void CleanBg(LPDIRECT3DDEVICE8 lpD3DDev)
0182: {
0183:     CTextureMgr::Release(pFloorTex);
0184:     CTextureMgr::Release(pCylinderTex);
0185: }