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