0001: // ----------------------------------------------------------------------------
0002: //
0003: // bg.cpp - Back ground objects
0004: // 
0005: // Copyright (c) 2002 IMAGIRE Takashi (imagire@gmail.com)
0006: // All Rights Reserved.
0007: //
0008: // ----------------------------------------------------------------------------
0009: #define STRICT
0010: 
0011: #include <d3dx8.h>
0012: #include <Cg/cgD3D.h>
0013: #include "main.h"
0014: #include "draw.h"
0015: 
0016: extern cgProgramContainer *pVertexProgramContainer;
0017: extern cgProgramContainer  *pPixelProgramContainer;
0018: extern cgBindIter * vertex_mat_iter;
0019: extern cgBindIter * tex0_iter;
0020: extern cgBindIter * tex1_iter;
0021: 
0022: // Sky
0023: #define NUM_Y       1
0024: #define NUM_CIRCLE  32
0025: #define NUM_VERTICES            ((NUM_Y+1)*(NUM_CIRCLE+1))
0026: #define NUM_INDICES_PERFACE     (3*2)
0027: #define NUM_FACES               (NUM_Y*NUM_CIRCLE)
0028: #define NUM_VERTICES_PERFACE    4
0029: 
0030: // ----------------------------------------------------------------------------
0031: typedef struct{
0032:     float x,y,z,w;
0033:     float tu,tv;
0034: } MyVertex;
0035: 
0036: // ----------------------------------------------------------------------------
0037: // Sky
0038: LPDIRECT3DVERTEXBUFFER8     pCylinderVB;
0039: LPDIRECT3DINDEXBUFFER8      pCylinderIB;
0040: LPDIRECT3DTEXTURE8          pCylinderTex;
0041: 
0042: // Bullet
0043: LPDIRECT3DVERTEXBUFFER8     pEarthVB;
0044: LPDIRECT3DINDEXBUFFER8      pEarthIB;
0045: LPDIRECT3DTEXTURE8          pEarthTex;
0046: #define EARTH_R             (0.1f)
0047: #define NUM_EARTH_X         32
0048: #define NUM_EARTH_Y         16
0049: #define NUM_EARTH_VERTICES  ((NUM_EARTH_X+1)*(NUM_EARTH_Y+1))
0050: #define NUM_EARTH_POLYGONE  (2*NUM_EARTH_X*NUM_EARTH_Y)
0051: #define NUM_EARTH_INDEX     (6*NUM_EARTH_X*NUM_EARTH_Y)
0052: 
0053: // ----------------------------------------------------------------------------
0054: // Initialization of the Bullet
0055: // ----------------------------------------------------------------------------
0056: static void InitEarth(LPDIRECT3DDEVICE8 lpD3DDev)
0057: {
0058:     WORD i, j;
0059:     
0060:     // Create the vertex buffer 
0061:     MyVertex *pEarthDest;
0062:     lpD3DDev->CreateVertexBuffer( NUM_EARTH_VERTICES * sizeof(MyVertex),
0063:                                 0, 0, D3DPOOL_MANAGED,
0064:                                 &pEarthVB );
0065:     // Set up vertexs
0066:     pEarthVB->Lock ( 0, 0, (BYTE**)&pEarthDest, 0 );
0067:     for ( j = 0; j <= NUM_EARTH_Y; j++) {
0068:     for ( i = 0; i <= NUM_EARTH_X; i++) {
0069:         float theta = ((float)i)*2*D3DX_PI/NUM_EARTH_X;
0070:         float phi   = ((float)j)*  D3DX_PI/NUM_EARTH_Y-D3DX_PI/2;
0071:         pEarthDest->x   = EARTH_R * (float)cos(phi) * (float)cos(theta);
0072:         pEarthDest->z   = EARTH_R * (float)cos(phi) * (float)sin(theta);
0073:         pEarthDest->y   = EARTH_R * (float)sin(phi);
0074:         pEarthDest->w   = 1.0f;
0075:         pEarthDest->tu = (float)i/NUM_EARTH_X;
0076:         pEarthDest->tv = 1.0f-(float)j/NUM_EARTH_Y;
0077:         pEarthDest++;
0078:     }
0079:     }
0080:     pEarthVB->Unlock ();
0081: 
0082: 
0083:     // Set up indexs
0084:     WORD *pIndex;
0085:     lpD3DDev->CreateIndexBuffer( NUM_EARTH_INDEX * sizeof(WORD),
0086:                                0,
0087:                                D3DFMT_INDEX16, D3DPOOL_MANAGED,
0088:                                &pEarthIB );
0089:     pEarthIB->Lock ( 0, 0, (BYTE**)&pIndex, 0 );
0090:     for ( j = 0; j < NUM_EARTH_Y; j++) {
0091:     for ( i = 0; i < NUM_EARTH_X; i++) {
0092:         *pIndex++ = (j+0)*(NUM_EARTH_X+1)+i+0;
0093:         *pIndex++ = (j+1)*(NUM_EARTH_X+1)+i+0;
0094:         *pIndex++ = (j+0)*(NUM_EARTH_X+1)+i+1;
0095:         *pIndex++ = (j+0)*(NUM_EARTH_X+1)+i+1;
0096:         *pIndex++ = (j+1)*(NUM_EARTH_X+1)+i+0;
0097:         *pIndex++ = (j+1)*(NUM_EARTH_X+1)+i+1;
0098:     }
0099:     }
0100:     pEarthIB->Unlock ();
0101: 
0102:     D3DXCreateTextureFromFileEx(lpD3DDev, "earth.bmp", 0,0,0,0,D3DFMT_A8R8G8B8,
0103:                                 D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR,
0104:                                 0, NULL, NULL, &pEarthTex);
0105: 
0106: }
0107: 
0108: // ----------------------------------------------------------------------------
0109: // Initialization of the back ground object
0110: // ----------------------------------------------------------------------------
0111: void InitBg(LPDIRECT3DDEVICE8 lpD3DDev)
0112: {
0113:     //
0114:     // Sky
0115:     //
0116:     // Create the vertex buffer 
0117:     MyVertex *pDest;
0118:     lpD3DDev->CreateVertexBuffer( NUM_VERTICES * sizeof(MyVertex),
0119:                                 0, 0, D3DPOOL_MANAGED, &pCylinderVB );
0120: 
0121:     // Set up vertexs
0122:     WORD k=0;
0123:     pCylinderVB->Lock ( 0, 0, (BYTE**)&pDest, 0 );
0124:     float r = 5.0f;
0125:     float h = 10.0f;
0126:     for (DWORD i = 0; i <= NUM_CIRCLE; i++) {
0127:         float theta = (2*D3DX_PI*(float)i)/(float)NUM_CIRCLE;
0128:         for (DWORD j = 0; j <= NUM_Y; j++) {
0129:             pDest->x = r * (float)cos(theta);
0130:             pDest->z = r * (float)sin(theta);
0131:             pDest->y = h*((float)j/(float)NUM_Y-0.0f);
0132:             pDest->w   = 1.0f;
0133:             pDest->tu = (float)i / (float)NUM_CIRCLE;
0134:             pDest->tv = 1.0f-(float)j / (float)NUM_Y;
0135:             pDest += 1;
0136:         }
0137:     }       
0138:     pCylinderVB->Unlock ();
0139: 
0140: 
0141:     // Set up indexs
0142:     WORD *pIndex;
0143:     lpD3DDev->CreateIndexBuffer( NUM_INDICES_PERFACE  * NUM_FACES * sizeof(WORD),
0144:                                      0 ,
0145:                                      D3DFMT_INDEX16, D3DPOOL_DEFAULT,
0146:                                      &pCylinderIB );
0147:     pCylinderIB->Lock ( 0, 0, (BYTE**)&pIndex, 0 );
0148:     {
0149:     for (WORD i = 0; i < NUM_CIRCLE; i++) {
0150:         for (WORD j = 0; j < NUM_Y; j++) {
0151:             *pIndex++ = j + 0 + (i+0) * (NUM_Y+1);
0152:             *pIndex++ = j + 0 + (i+1) * (NUM_Y+1);
0153:             *pIndex++ = j + 1 + (i+0) * (NUM_Y+1);
0154: 
0155:             *pIndex++ = j + 1 + (i+0) * (NUM_Y+1);
0156:             *pIndex++ = j + 0 + (i+1) * (NUM_Y+1);
0157:             *pIndex++ = j + 1 + (i+1) * (NUM_Y+1);
0158:         }
0159:     }
0160:     }
0161:     pCylinderIB->Unlock ();
0162: 
0163:     D3DXCreateTextureFromFileEx(lpD3DDev, "sky.bmp", 0,0,0,0,D3DFMT_A8R8G8B8,
0164:                                 D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR,
0165:                                 0, NULL, NULL, &pCylinderTex);
0166: 
0167:     
0168:     //
0169:     // Bullet
0170:     //
0171:     InitEarth(lpD3DDev);
0172: }
0173: // ----------------------------------------------------------------------------
0174: void CleanBg(LPDIRECT3DDEVICE8 lpD3DDev)
0175: {
0176:     RELEASE(pEarthTex);
0177:     RELEASE(pEarthIB);
0178:     RELEASE(pEarthVB);
0179: 
0180:     RELEASE(pCylinderTex);
0181:     RELEASE(pCylinderIB);
0182:     RELEASE(pCylinderVB);
0183: }
0184: // ----------------------------------------------------------------------------
0185: void DrawBg(LPDIRECT3DDEVICE8 lpD3DDev, D3DXMATRIX &mVP, bool texture)
0186: {
0187:     D3DXMATRIX mWorld, m;
0188: 
0189:     D3DXMatrixTranspose( &m, &mVP );
0190:     pVertexProgramContainer->SetShaderConstant( vertex_mat_iter, &m  );
0191: 
0192:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_MODULATE);
0193:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0194:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG2,  D3DTA_DIFFUSE);
0195:     lpD3DDev->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0196:     lpD3DDev->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
0197:     lpD3DDev->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
0198:     
0199:     //
0200:     // Sky
0201:     //
0202:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSU,   D3DTADDRESS_WRAP);
0203:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSV,   D3DTADDRESS_CLAMP);
0204: 
0205:     pPixelProgramContainer->SetTexture(tex0_iter, texture?pCylinderTex:NULL);
0206:     pPixelProgramContainer->SetTexture(tex1_iter, NULL);
0207:     lpD3DDev->SetStreamSource(0, pCylinderVB, sizeof(MyVertex));
0208:     lpD3DDev->SetIndices(pCylinderIB,0);
0209:     lpD3DDev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, NUM_VERTICES, 0 , NUM_FACES*2 );
0210: }
0211: // ----------------------------------------------------------------------------
0212: void CBullet::Draw(LPDIRECT3DDEVICE8 lpD3DDev)
0213: {
0214:     lpD3DDev->SetStreamSource(0, pEarthVB, sizeof(MyVertex));
0215:     lpD3DDev->SetIndices(pEarthIB,0);
0216:     lpD3DDev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, NUM_EARTH_VERTICES, 0 , NUM_EARTH_POLYGONE );
0217: }
0218: // ----------------------------------------------------------------------------
0219: void CBullet::Render(LPDIRECT3DDEVICE8 lpD3DDev, D3DXMATRIX &mVP)
0220: {
0221:     if(!bActive) return;
0222:     
0223:     D3DXMATRIX m;
0224:     D3DXMatrixTranslation(&m, pos.x, pos.y, pos.z);
0225:     m = m*mVP;
0226:     D3DXMatrixTranspose( &m, &m );
0227:     pVertexProgramContainer->SetShaderConstant( vertex_mat_iter, &m  );
0228:     pPixelProgramContainer->SetTexture(tex0_iter, pEarthTex);
0229:     pPixelProgramContainer->SetTexture(tex1_iter, NULL);
0230:     
0231:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSU,   D3DTADDRESS_WRAP);
0232:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSV,   D3DTADDRESS_CLAMP);
0233: 
0234:     Draw(lpD3DDev);
0235: }
0236: