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: // ----------------------------------------------------------------------------
0043: // Initialization of the back ground object
0044: // ----------------------------------------------------------------------------
0045: void InitBg(LPDIRECT3DDEVICE8 lpD3DDev)
0046: {
0047:     //
0048:     // Sky
0049:     //
0050:     // Create the vertex buffer 
0051:     MyVertex *pDest;
0052:     lpD3DDev->CreateVertexBuffer( NUM_VERTICES * sizeof(MyVertex),
0053:                                 0, 0, D3DPOOL_MANAGED, &pCylinderVB );
0054: 
0055:     // Set up vertexs
0056:     WORD k=0;
0057:     pCylinderVB->Lock ( 0, 0, (BYTE**)&pDest, 0 );
0058:     float r = 5.0f;
0059:     float h = 10.0f;
0060:     for (DWORD i = 0; i <= NUM_CIRCLE; i++) {
0061:         float theta = (2*D3DX_PI*(float)i)/(float)NUM_CIRCLE;
0062:         for (DWORD j = 0; j <= NUM_Y; j++) {
0063:             pDest->x = r * (float)cos(theta);
0064:             pDest->z = r * (float)sin(theta);
0065:             pDest->y = h*((float)j/(float)NUM_Y-0.0f);
0066:             pDest->w   = 1.0f;
0067:             pDest->tu = (float)i / (float)NUM_CIRCLE;
0068:             pDest->tv = 1.0f-(float)j / (float)NUM_Y;
0069:             pDest += 1;
0070:         }
0071:     }       
0072:     pCylinderVB->Unlock ();
0073: 
0074: 
0075:     // Set up indexs
0076:     WORD *pIndex;
0077:     lpD3DDev->CreateIndexBuffer( NUM_INDICES_PERFACE  * NUM_FACES * sizeof(WORD),
0078:                                      0 ,
0079:                                      D3DFMT_INDEX16, D3DPOOL_DEFAULT,
0080:                                      &pCylinderIB );
0081:     pCylinderIB->Lock ( 0, 0, (BYTE**)&pIndex, 0 );
0082:     {
0083:     for (WORD i = 0; i < NUM_CIRCLE; i++) {
0084:         for (WORD j = 0; j < NUM_Y; j++) {
0085:             *pIndex++ = j + 0 + (i+0) * (NUM_Y+1);
0086:             *pIndex++ = j + 0 + (i+1) * (NUM_Y+1);
0087:             *pIndex++ = j + 1 + (i+0) * (NUM_Y+1);
0088: 
0089:             *pIndex++ = j + 1 + (i+0) * (NUM_Y+1);
0090:             *pIndex++ = j + 0 + (i+1) * (NUM_Y+1);
0091:             *pIndex++ = j + 1 + (i+1) * (NUM_Y+1);
0092:         }
0093:     }
0094:     }
0095:     pCylinderIB->Unlock ();
0096: 
0097:     D3DXCreateTextureFromFileEx(lpD3DDev, "sky.bmp", 0,0,0,0,D3DFMT_A8R8G8B8,
0098:                                 D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR,
0099:                                 0, NULL, NULL, &pCylinderTex);
0100: 
0101: }
0102: // ----------------------------------------------------------------------------
0103: void CleanBg(LPDIRECT3DDEVICE8 lpD3DDev)
0104: {
0105:     RELEASE(pCylinderTex);
0106:     RELEASE(pCylinderIB);
0107:     RELEASE(pCylinderVB);
0108: }
0109: // ----------------------------------------------------------------------------
0110: void DrawBg(LPDIRECT3DDEVICE8 lpD3DDev, D3DXMATRIX &mVP, bool texture)
0111: {
0112:     D3DXMATRIX mWorld, m;
0113: 
0114:     D3DXMatrixTranspose( &m, &mVP );
0115:     pVertexProgramContainer->SetShaderConstant( vertex_mat_iter, &m  );
0116: 
0117:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_MODULATE);
0118:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0119:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG2,  D3DTA_DIFFUSE);
0120:     lpD3DDev->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0121:     lpD3DDev->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
0122:     lpD3DDev->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
0123:     
0124:     //
0125:     // Sky
0126:     //
0127:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSU,   D3DTADDRESS_WRAP);
0128:     lpD3DDev->SetTextureStageState(0,D3DTSS_ADDRESSV,   D3DTADDRESS_CLAMP);
0129: 
0130:     pPixelProgramContainer->SetTexture(tex0_iter, texture?pCylinderTex:NULL);
0131:     pPixelProgramContainer->SetTexture(tex1_iter, NULL);
0132:     lpD3DDev->SetStreamSource(0, pCylinderVB, sizeof(MyVertex));
0133:     lpD3DDev->SetIndices(pCylinderIB,0);
0134:     lpD3DDev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, NUM_VERTICES, 0 , NUM_FACES*2 );
0135: }
0136: