0001: // ----------------------------------------------------------------------------
0002: //
0003: // draw.cpp - 描画部分
0004: // 
0005: // Copyright (c) 2001 imagire (imagire@gmail.com)
0006: // All Rights Reserved.
0007: //
0008: // ----------------------------------------------------------------------------
0009: #define STRICT
0010: 
0011: #include <windows.h>
0012: #include "main.h"
0013: #include "draw.h"
0014: #include "load.h"
0015: #include "rigidbody.h"
0016: 
0017: CMyMesh mesh;
0018: CMyView view;
0019: 
0020: D3DXVECTOR4 rot = D3DXVECTOR4(PI/12.0f, -PI, 0.0f, 0.0f);
0021: 
0022: // ライトの方向
0023: static D3DXVECTOR4 lpos;
0024: D3DXVECTOR4 lrot = D3DXVECTOR4(PI/6.0f, PI/4.0f, 0.0f, 0.0f);
0025: 
0026: // ----------------------------------------------------------------------------
0027: // 外部変数
0028: void SetLight(LPDIRECT3DDEVICE8 lpD3DDEV, D3DXVECTOR4 *pRot);
0029: void InitLightEff(LPDIRECT3DDEVICE8 lpD3DDEV, D3DXVECTOR4 pos);
0030: void DrawLightEff(LPDIRECT3DDEVICE8 lpD3DDEV, D3DXMATRIX mView, D3DXMATRIX mProj);
0031: void CleanLightEff();
0032: 
0033: void InitBg(LPDIRECT3DDEVICE8 lpD3DDev);
0034: void DrawBg(LPDIRECT3DDEVICE8 lpD3DDev, D3DXMATRIX *pWorld);
0035: void CleanBg(LPDIRECT3DDEVICE8 lpD3DDev);
0036: 
0037: // ----------------------------------------------------------------------------
0038: // Name: Render()
0039: // Desc: ポリゴンの初期化
0040: //-----------------------------------------------------------------------------
0041: typedef struct{
0042:     char *name;
0043: }VS_INFO;
0044: HRESULT InitRender(LPDIRECT3DDEVICE8 lpD3DDEV)
0045: {
0046:     HRESULT hr;
0047: 
0048:     hr = mesh.Load(lpD3DDEV, "nsx.x");
0049:  
0050:     // レンダリングの状態の設定
0051:     lpD3DDEV->SetRenderState( D3DRS_ZENABLE, TRUE);
0052:     lpD3DDEV->SetRenderState( D3DRS_AMBIENT, 0x00404040 );
0053:     lpD3DDEV->SetRenderState( D3DRS_LIGHTING,  FALSE );
0054:     SetLight(lpD3DDEV, &lrot);  // 光源の初期化
0055:     
0056:     InitBg(lpD3DDEV);
0057: 
0058:     car.Init();
0059: 
0060:     return S_OK;
0061: }
0062: // ----------------------------------------------------------------------------
0063: // Name: Render()
0064: // Desc: ポリゴンの描画
0065: //-----------------------------------------------------------------------------
0066: void Render(LPDIRECT3DDEVICE8 lpD3DDEV)
0067: {
0068:     if(!mesh.bActive) return;
0069:     
0070:     car.Control();
0071: 
0072:     D3DXMATRIX m, mp, mn, mLocal, mWorld, mLW, mProj;
0073:     D3DXVECTOR4 tr;
0074: 
0075:     D3DXMatrixPerspectiveFovLH(&mProj
0076:         ,60.0f*PI/180.0f                        // 視野角
0077:         ,(float)WIDTH/(float)HEIGHT             // アスペクト比
0078:         ,0.1f                                   // 最近接距離
0079:         ,500.0f                                 // 最遠方距離
0080:         );
0081:     D3DXMatrixRotationYawPitchRoll(&mWorld, rot.y, rot.x, rot.z);
0082:     tr = car.GetTranslate();
0083:     D3DXMatrixTranslation(&m, -tr.x, -tr.y, -tr.z);
0084:     mWorld = m * mWorld;
0085: 
0086:     D3DXMatrixTranslation(&mp, 0.0f, 2.2f, 0.0f);
0087:     D3DXMatrixTranslation(&mn, 0.0f,-2.2f, 0.0f);
0088:     mLocal = mn * car.GetRotation() * mp;// 重心は少し上にあるので、その効果
0089:     D3DXMatrixTranslation(&m, tr.x, tr.y, tr.z);
0090:     mLocal = mLocal * m;
0091:     mLW = mLocal*mWorld;
0092:     
0093:     // 背景描画
0094:     lpD3DDEV->SetTransform( D3DTS_WORLD, &mWorld );
0095:     lpD3DDEV->SetTransform( D3DTS_VIEW, &view.Get() );
0096:     lpD3DDEV->SetTransform( D3DTS_PROJECTION, &mProj );
0097:     DrawBg(lpD3DDEV, &mWorld);
0098: 
0099:     // ライト
0100:     D3DLIGHT8 light;
0101:     ZeroMemory( &light, sizeof(D3DLIGHT8) );
0102:     light.Type       = D3DLIGHT_DIRECTIONAL;
0103:     light.Diffuse.r  = 0.7f;
0104:     light.Diffuse.g  = 0.7f;
0105:     light.Diffuse.b  = 0.7f;
0106:     light.Direction.x = -lpos[0];
0107:     light.Direction.y = -lpos[1];
0108:     light.Direction.z = -lpos[2];
0109:     lpD3DDEV->SetLight( 0, &light );
0110:     lpD3DDEV->LightEnable( 0, TRUE );
0111:     lpD3DDEV->SetRenderState( D3DRS_LIGHTING,  TRUE );
0112:     
0113: 
0114:     // モデル描画
0115:     lpD3DDEV->SetTransform( D3DTS_WORLD, &mLW );
0116:     lpD3DDEV->SetVertexShader(D3DFVF_CUSTOMVERTEX);
0117:     lpD3DDEV->SetStreamSource(0, mesh.pVB, sizeof(D3D_CUSTOMVERTEX));
0118:     lpD3DDEV->SetIndices(mesh.pIndex,0);
0119:     lpD3DDEV->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0120:     
0121:     for(DWORD i=0;i<mesh.dwNumMaterials;i++){
0122:         lpD3DDEV->SetMaterial( &mesh.pMaterials[i] );
0123:         lpD3DDEV->SetTexture( 0, mesh.pTextures[i] );
0124: 
0125:         lpD3DDEV->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 
0126:                                         mesh.pSubsetTable[i].VertexStart,
0127:                                         mesh.pSubsetTable[i].VertexCount,
0128:                                         mesh.pSubsetTable[i].FaceStart * 3,
0129:                                         mesh.pSubsetTable[i].FaceCount);
0130:     }
0131:     
0132:    lpD3DDEV->SetRenderState( D3DRS_LIGHTING,  FALSE );
0133: 
0134:     // 光源の位置を描く
0135:     DrawLightEff(lpD3DDEV, view.Get(), mProj);
0136: }
0137: 
0138: //-----------------------------------------------------------------------------
0139: // 光源の設定
0140: //-----------------------------------------------------------------------------
0141: void SetLight(LPDIRECT3DDEVICE8 lpD3DDEV, D3DXVECTOR4 *pRot)
0142: {
0143:     D3DXMATRIX m;
0144:     D3DXMatrixRotationYawPitchRoll(&m, pRot->y, pRot->x, pRot->z);
0145:     D3DXVec4Transform(&lpos, &D3DXVECTOR4(0.0f, 0.0f,-1.0f, 0.0f), &m);
0146:     D3DXVec4Normalize(&lpos, &lpos);
0147:     InitLightEff(lpD3DDEV, lpos);
0148: }
0149: //-----------------------------------------------------------------------------
0150: // Name: CleanRender()
0151: // Desc: 後始末
0152: //-----------------------------------------------------------------------------
0153: void CleanRender(LPDIRECT3DDEVICE8 lpD3DDEV)
0154: {
0155:     CleanBg(lpD3DDEV);
0156:     CleanLightEff();
0157:     
0158:     mesh.Release();
0159: }
0160: