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_SIZE  (10.0f)
0026: #define FLOOR_UV    (10.0f)     // テクスチャーの繰り返し回数
0027: 
0028: // ----------------------------------------------------------------------------
0029: // 空
0030: LPDIRECT3DVERTEXBUFFER8     pCylinderVB;
0031: LPDIRECT3DINDEXBUFFER8      pCylinderIB;
0032: LPDIRECT3DTEXTURE8          pCylinderTex;
0033: // 床
0034: LPDIRECT3DVERTEXBUFFER8     pFloorVB;
0035: LPDIRECT3DINDEXBUFFER8      pFloorIB;
0036: LPDIRECT3DTEXTURE8          pFloorTex;
0037: 
0038: // ----------------------------------------------------------------------------
0039: void InitBg(LPDIRECT3DDEVICE8 lpD3DDev)
0040: {
0041:     //
0042:     // 円柱
0043:     //
0044:     // 頂点バッファの作成 
0045:     D3DVERTEX *pDest;
0046:     lpD3DDev->CreateVertexBuffer( NUM_VERTICES * sizeof(D3DVERTEX),
0047:                                 D3DUSAGE_WRITEONLY, 
0048:                                 D3DFVF_VERTEX, D3DPOOL_MANAGED,
0049:                                 &pCylinderVB );
0050: 
0051:     // 頂点をセットアップ
0052:     WORD k=0;
0053:     pCylinderVB->Lock ( 0, 0, (BYTE**)&pDest, 0 );
0054:     float r = 10.0f;
0055:     float h = 10.0f;
0056:     for (DWORD i = 0; i <= NUM_CIRCLE; i++) {
0057:         float theta = (2*PI*(float)i)/(float)NUM_CIRCLE;
0058:         for (DWORD j = 0; j <= NUM_Y; j++) {
0059:             pDest->x = r * (float)cos(theta);
0060:             pDest->z = r * (float)sin(theta);
0061:             pDest->y = h*((float)j/(float)NUM_Y-0.0f);
0062:             pDest->nx = 0;
0063:             pDest->nz = 0;
0064:             pDest->ny = 0;
0065:             pDest->tu0 = (float)i / (float)NUM_CIRCLE;
0066:             pDest->tv0 = 1.0f-(float)j / (float)NUM_Y;
0067:             pDest += 1;
0068:         }
0069:     }       
0070:     pCylinderVB->Unlock ();
0071: 
0072: 
0073:     // インデックスをセットアップ
0074:     WORD *pIndex;
0075:     lpD3DDev->CreateIndexBuffer( NUM_INDICES_PERFACE  * NUM_FACES * sizeof(WORD),
0076:                                      0 ,
0077:                                      D3DFMT_INDEX16, D3DPOOL_DEFAULT,
0078:                                      &pCylinderIB );
0079:     pCylinderIB->Lock ( 0, 0, (BYTE**)&pIndex, 0 );
0080:     {
0081:     for (WORD i = 0; i < NUM_CIRCLE; i++) {
0082:         for (WORD j = 0; j < NUM_Y; j++) {
0083:             *pIndex++ = j + 0 + (i+0) * (NUM_Y+1);
0084:             *pIndex++ = j + 0 + (i+1) * (NUM_Y+1);
0085:             *pIndex++ = j + 1 + (i+0) * (NUM_Y+1);
0086: 
0087:             *pIndex++ = j + 1 + (i+0) * (NUM_Y+1);
0088:             *pIndex++ = j + 0 + (i+1) * (NUM_Y+1);
0089:             *pIndex++ = j + 1 + (i+1) * (NUM_Y+1);
0090:         }
0091:     }
0092:     }
0093:     pCylinderIB->Unlock ();
0094: 
0095:     CTextureMgr::Load(lpD3DDev, "sky.bmp", &pCylinderTex);
0096:     
0097:     //
0098:     // 床
0099:     //
0100:     // 頂点バッファの作成 
0101:     D3DVERTEX *pFloorDest;
0102:     lpD3DDev->CreateVertexBuffer( 4 * sizeof(D3DVERTEX),
0103:                                 D3DUSAGE_WRITEONLY, D3DFVF_VERTEX, D3DPOOL_MANAGED,
0104:                                 &pFloorVB );
0105:     // 頂点をセットアップ
0106:     {
0107:     pFloorVB->Lock ( 0, 0, (BYTE**)&pFloorDest, 0 );
0108:     for (DWORD i = 0; i < 4; i++) {
0109:         pFloorDest->x   = (i == 0 || i == 1)?(-FLOOR_SIZE):(+FLOOR_SIZE);
0110:         pFloorDest->z   = (i == 0 || i == 2)?(-FLOOR_SIZE):(+FLOOR_SIZE);
0111:         pFloorDest->y   = 0.0f;
0112:         pFloorDest->nx  = 0.0f;
0113:         pFloorDest->nz  = 0.0f;
0114:         pFloorDest->ny  = 1.0f;
0115:         pFloorDest->tu0 = (i == 0 || i == 1)?0:FLOOR_UV;
0116:         pFloorDest->tv0 = (i == 0 || i == 2)?0:FLOOR_UV;
0117:         pFloorDest += 1;
0118:     }       
0119:     pFloorVB->Unlock ();
0120:     }
0121: 
0122: 
0123:     // インデックスをセットアップ
0124:     lpD3DDev->CreateIndexBuffer( 6 * sizeof(WORD),
0125:                                0,
0126:                                D3DFMT_INDEX16, D3DPOOL_MANAGED,
0127:                                &pFloorIB );
0128:     pFloorIB->Lock ( 0, 0, (BYTE**)&pIndex, 0 );
0129:     pIndex[0] = 0;  pIndex[1] = 1;  pIndex[2] = 2;
0130:     pIndex[3] = 1;  pIndex[4] = 3;  pIndex[5] = 2;
0131:     pFloorIB->Unlock ();
0132: 
0133:     CTextureMgr::Load(lpD3DDev, "tile.bmp", &pFloorTex);
0134: }
0135: // ----------------------------------------------------------------------------
0136: void DrawBg(LPDIRECT3DDEVICE8 lpD3DDev)
0137: {
0138:     lpD3DDev->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);
0139:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_MODULATE);
0140:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0141:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG2,  D3DTA_DIFFUSE);
0142:     lpD3DDev->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0143:     // 空
0144:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSU,   D3DTADDRESS_WRAP);
0145:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSV,   D3DTADDRESS_CLAMP);
0146:     lpD3DDev->SetTexture(0,pCylinderTex);
0147:     lpD3DDev->SetStreamSource(0, pCylinderVB, sizeof(D3DVERTEX));
0148:     lpD3DDev->SetIndices(pCylinderIB,0);
0149:     lpD3DDev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, NUM_VERTICES, 0 , NUM_FACES*2 );
0150:     
0151:     // 床
0152:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSU,   D3DTADDRESS_WRAP);
0153:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSV,   D3DTADDRESS_WRAP);
0154:     lpD3DDev->SetTexture(0,pFloorTex);
0155: 
0156:     lpD3DDev->SetStreamSource(0, pFloorVB, sizeof(D3DVERTEX));
0157:     lpD3DDev->SetIndices(pFloorIB,0);
0158:     lpD3DDev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 4, 0, 2 );
0159: }
0160: // ----------------------------------------------------------------------------
0161: void CleanBg(LPDIRECT3DDEVICE8 lpD3DDev)
0162: {
0163:     CTextureMgr::Release(pFloorTex);
0164:     CTextureMgr::Release(pCylinderTex);
0165: }