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