Boids.cpp
资源名称:DXGuide.zip [点击查看]
上传用户:wymy58
上传日期:2007-01-07
资源大小:2086k
文件大小:19k
源码类别:
DirextX编程
开发平台:
Visual C++
- // Boids.cpp : Defines the class behaviors for the application.
- #include "stdafx.h"
- #include "Boids.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- const bool g_bFullScreen
- #ifdef _DEBUG
- = false; // true;
- #else
- = true; // false;
- #endif // _DEBUG
- const bool g_bUseZBuffer = true; // false;
- const DWORD g_dwWidth = 640; // 800; // 1024;
- const DWORD g_dwHeight = 480; // 600; // 768;
- const DWORD g_dwBPP = 16; // 32;
- const bool g_bUseVoodoo12 = false; // true;
- const _GUID* const g_lpGuidD3DDevice
- = NULL;
- // = &IID_IDirect3DTnLHalDevice;
- // = &IID_IDirect3DRGBDevice;
- // = &IID_IDirect3DHALDevice;
- // = &IID_IDirect3DRefDevice;
- #define NUM_BOIDS 13
- #define NUM_OBSTACLES 8
- #define OBSTACLE_RADIUS 3.0f
- #define NUM_GRID 20
- #define GRID_WIDTH 190.0
- D3DVERTEX pvGridVertices[NUM_GRID * NUM_GRID];
- D3DVERTEX m_pvBoidVertices[16];
- WORD pwBoidIndices[30];
- CBoidsApp::CBoidsApp(void)
- {
- m_dwAppInitFlags &= ~CDirectXApp::DXAPPIF_DD;
- m_dwAppInitFlags |= CDirectXApp::DXAPPIF_D3DIM;
- SetFullScreen(g_bFullScreen);
- SetPackFileName(_T("Data"), _T("gui"));
- // Called during initial app startup, this function
- // performs all the permanent initialization.
- m_pD3DDevice = NULL;
- m_pD3DMaterial = NULL;
- // add your permanent init code here !
- m_pLight1 = m_pLight2 = NULL;
- m_pTexture = NULL;
- // generate the sphere data
- m_pSphere = new CD3DSphere(OBSTACLE_RADIUS, 10);
- }
- // Called before the app exits, this function gives the app
- // the chance to cleanup after itself.
- CBoidsApp::~CBoidsApp()
- {
- // add your cleaup code here !
- if (m_pSphere != NULL)
- {
- delete m_pSphere;
- m_pSphere = NULL;
- }
- }
- bool CBoidsApp::GetDXInitSettings(void)
- {
- if (m_pDirectSound != NULL)
- {
- if (m_pDirectSound->SelectDSDriver(0) == false)
- return false;
- }
- if (m_pDirectMusic != NULL)
- {
- if (m_pDirectMusic->SelectDefaultDMusPort() == false)
- return false;
- }
- if ((g_bUseVoodoo12 == false)
- || m_pDirectDraw->SelectDDDevice(0, 1) == false)
- {
- if (m_pDirectDraw->SelectDDDevice(0) == false)
- return false;
- }
- if (g_bUseZBuffer)
- m_dwDDInitFlags |= CDDDevice::D3DIF_ZBUFFER;
- if (GetFirstDDDevice()->IsCanRenderWindowed() == false)
- {
- m_dwDDInitFlags &= ~CDDDevice::DDIF_WINDOWED;
- m_dwDDInitFlags &= ~CDDDevice::DDIF_NO_FLIP;
- }
- else
- {
- m_dwDDInitFlags |= CDDDevice::DDIF_NO_FLIP;
- }
- if (g_lpGuidD3DDevice == NULL)
- {
- if (GetFirstDDDevice()->SelectDefaultD3DDevice() == false)
- return false;
- }
- else
- {
- if (GetFirstDDDevice()->SelectD3DDevice(g_lpGuidD3DDevice) == false)
- return false;
- }
- if (GetFirstDDDevice()->SelectDisplayMode(
- IsFullScreen(), g_dwWidth, g_dwHeight, g_dwBPP) == false)
- return false;
- return true;
- }
- // Structure for holding data for each boid
- struct Boid
- {
- D3DVECTOR dvLoc;
- D3DVECTOR dvDir; // Current direction
- D3DVECTOR dvDeltaPos; // Change in position from flock centering
- D3DVECTOR dvDeltaDir; // Change in direction
- WORD wDeltaCnt; // Number of boids that influence this dvDeltaDir
- FLOAT fSpeed;
- FLOAT fYaw, fPitch, fRoll, fDYaw;
- FLOAT r, g, b; // Color of the boid
- FLOAT afDist[NUM_BOIDS]; // Array of boid distances, yuk what a waste
- CD3DMatrix matLocal;
- };
- Boid g_pBoids[NUM_BOIDS];
- D3DVECTOR g_vObstacleLocations[NUM_OBSTACLES];
- D3DVECTOR g_vGoal;
- CD3DMatrix g_matGrid;
- const FLOAT InfluenceRadius = 20.0f;
- const FLOAT InfluenceRadiusSquared = InfluenceRadius * InfluenceRadius;
- const FLOAT CollisionFraction = 0.8f;
- const FLOAT InvCollisionFraction = 1.0f/(1.0f-CollisionFraction);
- const FLOAT NormalSpeed = 0.1f;
- const FLOAT AngleTweak = 0.02f;
- const FLOAT PitchToSpeedRatio = 0.002f;
- // Called during device intialization, this code checks the device
- // for some minimum set of capabilities, Initialize scene objects.
- bool CBoidsApp::InitDXObjects(void)
- {
- m_pD3DDevice = GetFirstD3DDevice();
- ASSERT(m_pD3DDevice != NULL);
- m_pD3DMaterial = m_pD3DDevice->GetD3DMaterial();
- // add your init code here !
- g_vGoal = D3DVECTOR(0.0f, 0.0f, 0.0f);
- for (WORD i = 0; i < NUM_BOIDS; ++ i)
- {
- g_pBoids[i].dvLoc = D3DVECTOR(100.0f*(CDirectX::rnd()-CDirectX::rnd()), 10.0f*CDirectX::rnd(), 100.0f*(CDirectX::rnd()-CDirectX::rnd()));
- g_pBoids[i].dvDir = Normalize(D3DVECTOR(CDirectX::rnd()-CDirectX::rnd(), CDirectX::rnd()-CDirectX::rnd(), CDirectX::rnd()-CDirectX::rnd()));
- g_pBoids[i].fYaw = 0.0f;
- g_pBoids[i].fPitch = 0.0f;
- g_pBoids[i].fRoll = 0.0f;
- g_pBoids[i].fDYaw = 0.0f;
- g_pBoids[i].fSpeed = 0.1f;
- g_pBoids[i].r = CDirectX::rnd();
- g_pBoids[i].g = CDirectX::rnd();
- g_pBoids[i].b = CDirectX::rnd();
- FLOAT fMin = min( g_pBoids[i].r, min( g_pBoids[i].g, g_pBoids[i].b ) );
- FLOAT fMax = max( g_pBoids[i].r, min( g_pBoids[i].g, g_pBoids[i].b ) );
- g_pBoids[i].r = (g_pBoids[i].r - fMin) / (fMax-fMin);
- g_pBoids[i].g = (g_pBoids[i].g - fMin) / (fMax-fMin);
- g_pBoids[i].b = (g_pBoids[i].b - fMin) / (fMax-fMin);
- }
- // Position the obstacles
- g_vObstacleLocations[0] = D3DVECTOR( 100, 10, 0 );
- g_vObstacleLocations[1] = D3DVECTOR(-100, 10, 0 );
- g_vObstacleLocations[2] = D3DVECTOR( 0, 10, 100 );
- g_vObstacleLocations[3] = D3DVECTOR( 0, 10, -100 );
- for( i=4; i<NUM_OBSTACLES; i++ )
- g_vObstacleLocations[i] = D3DVECTOR( 100*(CDirectX::rnd()-CDirectX::rnd()), 10*CDirectX::rnd(), 100*(CDirectX::rnd()-CDirectX::rnd()) );
- FLOAT fSize = GRID_WIDTH/(NUM_GRID-1.0f);
- FLOAT fOffset = GRID_WIDTH/2.0f;
- for (i = 0; i < NUM_GRID; ++ i)
- {
- for (WORD j = 0; j < NUM_GRID; ++ j)
- {
- pvGridVertices[j+i*NUM_GRID] = D3DVERTEX(
- D3DVECTOR( i*fSize-fOffset, 0.0f, j*fSize-fOffset ),
- D3DVECTOR( 0.0f, 1.0f, 0.0f ), 0.0f, 0.0f );
- }
- }
- // Points and normals which make up a boid geometry
- D3DVECTOR p1 = D3DVECTOR( 0.00f, 0.00f, 0.50f );
- D3DVECTOR p2 = D3DVECTOR( 0.50f, 0.00f,-0.50f );
- D3DVECTOR p3 = D3DVECTOR( 0.15f, 0.15f,-0.35f );
- D3DVECTOR p4 = D3DVECTOR(-0.15f, 0.15f,-0.35f );
- D3DVECTOR p5 = D3DVECTOR( 0.15f,-0.15f,-0.35f );
- D3DVECTOR p6 = D3DVECTOR(-0.15f,-0.15f,-0.35f );
- D3DVECTOR p7 = D3DVECTOR(-0.50f, 0.00f,-0.50f );
- D3DVECTOR n1 = ::Normalize( D3DVECTOR( 0.2f, 1.0f, 0.0f ) );
- D3DVECTOR n2 = ::Normalize( D3DVECTOR( 0.1f, 1.0f, 0.0f ) );
- D3DVECTOR n3 = Normalize( D3DVECTOR( 0.0f, 1.0f, 0.0f ) );
- D3DVECTOR n4 = Normalize( D3DVECTOR(-0.1f, 1.0f, 0.0f ) );
- D3DVECTOR n5 = Normalize( D3DVECTOR(-0.2f, 1.0f, 0.0f ) );
- D3DVECTOR n6 = Normalize( D3DVECTOR(-0.4f, 0.0f, -1.0f ) );
- D3DVECTOR n7 = Normalize( D3DVECTOR(-0.2f, 0.0f, -1.0f ) );
- D3DVECTOR n8 = Normalize( D3DVECTOR( 0.2f, 0.0f, -1.0f ) );
- D3DVECTOR n9 = Normalize( D3DVECTOR( 0.4f, 0.0f, -1.0f ) );
- // Vertices for the top
- m_pvBoidVertices[ 0] = D3DVERTEX( p1, n1, 0.000f, 0.500f );
- m_pvBoidVertices[ 1] = D3DVERTEX( p2, n2, 0.500f, 1.000f );
- m_pvBoidVertices[ 2] = D3DVERTEX( p3, n3, 0.425f, 0.575f );
- m_pvBoidVertices[ 3] = D3DVERTEX( p4, n4, 0.425f, 0.425f );
- m_pvBoidVertices[ 4] = D3DVERTEX( p7, n5, 0.500f, 0.000f );
- // Vertices for the bottom
- m_pvBoidVertices[ 5] = D3DVERTEX( p1, -n5, 1.000f, 0.500f );
- m_pvBoidVertices[ 6] = D3DVERTEX( p2, -n4, 0.500f, 1.000f );
- m_pvBoidVertices[ 7] = D3DVERTEX( p5, -n3, 0.575f, 0.575f );
- m_pvBoidVertices[ 8] = D3DVERTEX( p6, -n2, 0.575f, 0.425f );
- m_pvBoidVertices[ 9] = D3DVERTEX( p7, -n1, 0.500f, 0.000f );
- // Vertices for the rear
- m_pvBoidVertices[10] = D3DVERTEX( p2, n6, 0.500f, 1.000f );
- m_pvBoidVertices[11] = D3DVERTEX( p3, n7, 0.425f, 0.575f );
- m_pvBoidVertices[12] = D3DVERTEX( p4, n8, 0.425f, 0.425f );
- m_pvBoidVertices[13] = D3DVERTEX( p7, n9, 0.500f, 0.000f );
- m_pvBoidVertices[14] = D3DVERTEX( p6, n8, 0.575f, 0.425f );
- m_pvBoidVertices[15] = D3DVERTEX( p5, n7, 0.575f, 0.575f );
- // Vertex inidices for the boid
- pwBoidIndices[ 0] = 0;
- pwBoidIndices[ 1] = 1;
- pwBoidIndices[ 2] = 2;
- pwBoidIndices[ 3] = 0;
- pwBoidIndices[ 4] = 2;
- pwBoidIndices[ 5] = 3;
- pwBoidIndices[ 6] = 0;
- pwBoidIndices[ 7] = 3;
- pwBoidIndices[ 8] = 4;
- pwBoidIndices[ 9] = 5;
- pwBoidIndices[10] = 7;
- pwBoidIndices[11] = 6;
- pwBoidIndices[12] = 5;
- pwBoidIndices[13] = 8;
- pwBoidIndices[14] = 7;
- pwBoidIndices[15] = 5;
- pwBoidIndices[16] = 9;
- pwBoidIndices[17] = 8;
- pwBoidIndices[18] = 10;
- pwBoidIndices[19] = 15;
- pwBoidIndices[20] = 11;
- pwBoidIndices[21] = 11;
- pwBoidIndices[22] = 15;
- pwBoidIndices[23] = 12;
- pwBoidIndices[24] = 12;
- pwBoidIndices[25] = 15;
- pwBoidIndices[26] = 14;
- pwBoidIndices[27] = 12;
- pwBoidIndices[28] = 14;
- pwBoidIndices[29] = 13;
- m_pD3DDevice->SetRenderState(D3DRENDERSTATE_AMBIENT, 0x0A0A0A0A);
- m_pTexture = new CD3DTexture;
- if (m_pTexture->Create(m_pD3DDevice, _T("EARTH.BMP"), m_pPackFileManager, 0, 0) == false)
- return false;
- m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
- m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
- m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
- m_pD3DDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTFN_LINEAR);
- m_pD3DDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTFG_LINEAR);
- m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DITHERENABLE, true);
- m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SPECULARENABLE, false);
- CD3DMatrix matProj;
- FLOAT fAspect = ((FLOAT)m_pD3DDevice->GetHeight()) / m_pD3DDevice->GetWidth();
- matProj.SetProjection(/*0.75f*/0.9f, fAspect, 1.0f, 1000.0f);
- m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_PROJECTION, &matProj);
- m_pLight1 = new CD3DDirectionalLight(-0.5f, -1.0f, -.3f);
- if (m_pLight1->Create(m_pD3DDevice) == false)
- return false;
- if (m_pLight1->Enable(true) == false)
- return false;
- D3DCOLORVALUE dcvLight2 = {0.5f, 0.5f, 0.5f, 0.0f};
- m_pLight2 = new CD3DDirectionalLight(
- D3DVECTOR(0.5f, 1.0f, .3f),
- dcvLight2);
- if (m_pLight2->Create(m_pD3DDevice) == false)
- return false;
- if (m_pLight2->Enable(true) == false)
- return false;
- ASSERT(m_pD3DDevice->GetActiveLightsNumber() == 2);
- return true;
- }
- // Called when the app is exitting, or the device is being changed,
- // this function deletes any device dependant objects.
- bool CBoidsApp::DestroyDXObjects(void)
- {
- // add your destroy code here !
- if (m_pTexture != NULL)
- {
- delete m_pTexture;
- m_pTexture = NULL;
- }
- if (m_pLight1 != NULL)
- {
- delete m_pLight1;
- m_pLight1 = NULL;
- }
- if (m_pLight2 != NULL)
- {
- delete m_pLight2;
- m_pLight2 = NULL;
- }
- m_pD3DMaterial = NULL;
- m_pD3DDevice = NULL;
- return CDirectXApp::DestroyDXObjects();
- }
- // Update posiiton of each boid in flock
- VOID UpdateFlock()
- {
- // first update the dist array 0.0..1.0 with 0.0 being furthest away
- for (WORD i = 0; i < NUM_BOIDS; ++ i)
- {
- for (WORD j = i + 1; j < NUM_BOIDS; ++ j)
- {
- FLOAT fDist = SquareMagnitude(g_pBoids[i].dvLoc - g_pBoids[j].dvLoc);
- fDist = InfluenceRadiusSquared - fDist;
- if( fDist < 0.0f )
- fDist = 0.0f;
- else
- fDist /= InfluenceRadiusSquared;
- g_pBoids[i].afDist[j] = g_pBoids[j].afDist[i] = fDist;
- }
- g_pBoids[i].afDist[i] = 0.0f;
- g_pBoids[i].dvDeltaDir = D3DVECTOR(0.0f, 0.0f, 0.0f);
- g_pBoids[i].dvDeltaPos = D3DVECTOR(0.0f, 0.0f, 0.0f);
- g_pBoids[i].wDeltaCnt = 0;
- }
- for (i = 0; i < NUM_BOIDS; ++ i)
- {
- for (WORD j = i + 1; j < NUM_BOIDS; ++ j)
- {
- // if i is near j have them influence each other
- if (g_pBoids[i].afDist[j] > 0.0f)
- {
- D3DVECTOR vDiff = Normalize(g_pBoids[i].dvLoc - g_pBoids[j].dvLoc);
- D3DVECTOR vDelta;
- FLOAT fCollWeight = 0.0f; // collision weighting
- // only do collision testing against the nearest ones
- if (g_pBoids[i].afDist[j] - CollisionFraction > 0.0f)
- fCollWeight = (g_pBoids[i].afDist[j] - CollisionFraction) * InvCollisionFraction;
- // add in a little flock centering
- if (g_pBoids[i].afDist[j] - (1.0f-CollisionFraction) > 0.0f)
- fCollWeight -= g_pBoids[i].afDist[j] * (1.0f - fCollWeight);
- vDelta = fCollWeight * vDiff;
- // add in the collision avoidance
- g_pBoids[i].dvDeltaPos += vDelta;
- g_pBoids[j].dvDeltaPos -= vDelta;
- // add in the velocity influences
- g_pBoids[i].dvDeltaDir += g_pBoids[j].dvDir * g_pBoids[i].afDist[j];
- g_pBoids[j].dvDeltaDir += g_pBoids[i].dvDir * g_pBoids[i].afDist[j];
- g_pBoids[i].wDeltaCnt++;
- g_pBoids[j].wDeltaCnt++;
- }
- }
- }
- // update the boids
- for (i = 0; i < NUM_BOIDS; ++ i)
- {
- if (g_pBoids[i].wDeltaCnt)
- {
- g_pBoids[i].dvDeltaDir /= (FLOAT)g_pBoids[i].wDeltaCnt;
- g_pBoids[i].dvDeltaDir -= g_pBoids[i].dvDir;
- g_pBoids[i].dvDeltaDir *= 1.5f;
- }
- D3DVECTOR vDelta = g_pBoids[i].dvDeltaDir + g_pBoids[i].dvDeltaPos;
- D3DVECTOR vOffset;
- // add in the influence of the global goal
- D3DVECTOR vGoal = 0.5 * Normalize(g_vGoal - g_pBoids[i].dvLoc);
- vDelta += vGoal;
- // add in any obstacles
- for (WORD j = 0; j < NUM_OBSTACLES; ++ j)
- {
- D3DVECTOR vOb = g_pBoids[i].dvLoc - g_vObstacleLocations[j];
- FLOAT fDist = Magnitude(vOb);
- if (fDist > 2 * OBSTACLE_RADIUS)
- continue;
- vOb /= fDist; // normalize
- fDist = 1.0f - fDist / (2 * OBSTACLE_RADIUS);
- vDelta += fDist * vOb * 5.0f;
- }
- // first deal with pitch changes
- if (vDelta.y > 0.01f)
- { // we're too low
- g_pBoids[i].fPitch += AngleTweak;
- if (g_pBoids[i].fPitch > 0.8f)
- g_pBoids[i].fPitch = 0.8f;
- }
- else if (vDelta.y < -0.01f)
- { // we're too high
- g_pBoids[i].fPitch -= AngleTweak;
- if (g_pBoids[i].fPitch < -0.8f)
- g_pBoids[i].fPitch = -0.8f;
- }
- else
- {
- // add damping
- g_pBoids[i].fPitch *= 0.98f;
- }
- // speed up or slow down depending on angle of attack
- g_pBoids[i].fSpeed -= g_pBoids[i].fPitch * PitchToSpeedRatio;
- // damp back to normal
- g_pBoids[i].fSpeed = (g_pBoids[i].fSpeed-NormalSpeed)*0.99f + NormalSpeed;
- if( g_pBoids[i].fSpeed < NormalSpeed/2 )
- g_pBoids[i].fSpeed = NormalSpeed/2;
- if( g_pBoids[i].fSpeed > NormalSpeed*5 )
- g_pBoids[i].fSpeed = NormalSpeed*5;
- // now figure out yaw changes
- vOffset = vDelta;
- vOffset.y = 0.0f;
- vDelta = g_pBoids[i].dvDir;
- vOffset = Normalize( vOffset );
- FLOAT fDot = DotProduct(vOffset, vDelta);
- // speed up slightly if not turning much
- if( fDot > 0.7f )
- {
- fDot -= 0.7f;
- g_pBoids[i].fSpeed += fDot * 0.005f;
- }
- vOffset = CrossProduct( vOffset, vDelta );
- fDot = (1.0f-fDot)/2.0f * 0.07f;
- if( vOffset.y > 0.05f )
- g_pBoids[i].fDYaw = (g_pBoids[i].fDYaw*19.0f + fDot) * 0.05f;
- else if( vOffset.y < -0.05f )
- g_pBoids[i].fDYaw = (g_pBoids[i].fDYaw*19.0f - fDot) * 0.05f;
- else
- g_pBoids[i].fDYaw *= 0.98f; // damp it
- g_pBoids[i].fYaw += g_pBoids[i].fDYaw;
- g_pBoids[i].fRoll = -g_pBoids[i].fDYaw * 20.0f;
- }
- }
- // Called once per frame, the call is the entry point for
- // animating the scene. This function sets up render states,
- // clears the viewport, and renders the scene.
- bool CBoidsApp::UpdateFrame(void)
- {
- // add your code here !
- static FLOAT tic = -200.0f * CDirectX::rnd();
- tic += 0.01f;
- CD3DMatrix matView;
- static D3DVECTOR vEyePt( 0.0f, 30.0f, 100.0f );
- static D3DVECTOR vLookatPt( 0.0f, 0.0f, 50.0f );
- static D3DVECTOR vUpVec( 0.0f, 1.0f, 0.0f );
- matView.SetView(vEyePt, vLookatPt, vUpVec);
- m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_VIEW, &matView);
- // Update grid matrix
- D3DVECTOR vOffset;
- vOffset.x = (FLOAT)floor(vLookatPt.x / 20) * 20.0f - 10.0f;
- vOffset.y = 0.0f;
- vOffset.z = (FLOAT)floor(vLookatPt.z / 20) * 20.0f - 10.0f;
- g_matGrid.SetTranslate(vOffset);
- UpdateFlock();
- vLookatPt = D3DVECTOR( 0.0f, 0.0f, 0.0f );
- // draw the boids
- for (WORD i = 0; i < NUM_BOIDS; ++ i)
- {
- // Build the world matrix for the boid. First translate into place,
- // then set orientation, then scale (if needed)
- CD3DMatrix matWorld;
- matWorld.SetTranslate(g_pBoids[i].dvLoc);
- CD3DMatrix matRotateY;
- matRotateY.SetRotateY(-g_pBoids[i].fYaw);
- CD3DMatrix matRotateX;
- matRotateX.SetRotateX(-g_pBoids[i].fPitch);
- CD3DMatrix matRotateZ;
- matRotateZ.SetRotateZ(-g_pBoids[i].fRoll);
- CD3DMatrix matTemp = matRotateX * matRotateY;
- matTemp = matRotateZ * matTemp;
- matWorld = matTemp * matWorld;
- g_pBoids[i].matLocal = matWorld;
- g_pBoids[i].dvDir.x = matWorld(2, 0);
- g_pBoids[i].dvDir.y = matWorld(2, 1);
- g_pBoids[i].dvDir.z = matWorld(2, 2);
- g_pBoids[i].dvLoc += g_pBoids[i].dvDir * g_pBoids[i].fSpeed;
- vLookatPt += g_pBoids[i].dvLoc;
- }
- vLookatPt /= NUM_BOIDS;
- vEyePt.x = vLookatPt.x + (FLOAT)(30.0f * ::sin(tic * 0.223));
- vEyePt.y = vLookatPt.y + (FLOAT)(21.0f + 20.0f * ::sin(tic * 0.33f));
- vEyePt.z = vLookatPt.z + (FLOAT)(30.0f * ::cos(tic * 0.31f));
- g_vGoal.x = 105.0f * (FLOAT)::sin(tic * 0.1f);
- g_vGoal.y = 10.0f;
- g_vGoal.z = 105.0f * (FLOAT)::cos(tic * 0.1f);
- m_pD3DDevice->Clear(RGBA_MAKE(0, 0, 0, 0));
- // Begin the scene
- if (SUCCEEDED(m_pD3DDevice->BeginScene()))
- {
- m_pD3DDevice->SetTexture(0, NULL);
- // Draw the north-south lines
- m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &g_matGrid);
- m_pD3DDevice->DrawPrimitive(D3DPT_LINELIST, D3DFVF_VERTEX,
- pvGridVertices, NUM_GRID * NUM_GRID, 0);
- // Draw the east-west lines
- CD3DMatrix matRotateY;
- matRotateY.SetRotateY(g_PI / 2.0f);
- // matRotateY = g_matGrid * matRotateY;
- matRotateY *= g_matGrid;
- m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &matRotateY);
- m_pD3DDevice->DrawPrimitive(D3DPT_LINELIST, D3DFVF_VERTEX,
- pvGridVertices, NUM_GRID*NUM_GRID, 0);
- // Draw the boids
- for (WORD i = 0; i < NUM_BOIDS; ++ i)
- {
- // Set the color for the boid
- m_pD3DMaterial->SetColor(g_pBoids[i].r, g_pBoids[i].g, g_pBoids[i].b, 0.0f);
- m_pD3DMaterial->Set();
- // Apply the boid's local matrix
- m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &g_pBoids[i].matLocal);
- // Draw the boid
- m_pD3DDevice->DrawIndexedPrimitive(
- D3DPT_TRIANGLELIST, D3DFVF_VERTEX,
- m_pvBoidVertices, 16,
- pwBoidIndices, 30, 0);
- }
- // Draw the obstacles
- m_pD3DMaterial->SetColor(1.0f, 1.0f, 1.0f, 0.0f);
- m_pD3DMaterial->Set();
- m_pTexture->SetAsCurrent();
- for (i = 0; i < NUM_OBSTACLES; ++ i)
- {
- CD3DMatrix matWorld;
- matWorld.SetTranslate(g_vObstacleLocations[i]);
- m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &matWorld);
- // Draw the obstacle
- m_pSphere->Render(m_pD3DDevice);
- }
- // End the scene.
- m_pD3DDevice->EndScene();
- }
- return CDirectXApp::UpdateFrame();
- }
- // ---- add your functions ! ----
- // ----
- #if _MSC_VER >= 1200 && _MSC_VER < 1400
- #ifdef _DEBUG
- #pragma comment(lib, "DXGuideD_VC6.lib")
- #else
- #pragma comment(lib, "DXGuide_VC6.lib")
- #endif // _DEBUG
- #endif // _MSC_VER
- #if _MSC_VER >= 1000 && _MSC_VER < 1200
- #ifdef _DEBUG
- #pragma comment(lib, "DXGuideD_VC5.lib")
- #else
- #pragma comment(lib, "DXGuide_VC5.lib")
- #endif // _DEBUG
- #endif // _MSC_VER
- BEGIN_MESSAGE_MAP(CBoidsApp, CDirectXApp)
- //{{AFX_MSG_MAP(CBoidsApp)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- CBoidsApp theApp;