home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Utilities / MView / gxu / showarcball.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-30  |  3.6 KB  |  146 lines

  1. /*//////////////////////////////////////////////////////////////////////////////
  2. //
  3. // File: stripoutline.cpp
  4. //
  5. // Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  6. //
  7. //
  8. //////////////////////////////////////////////////////////////////////////////*/
  9.  
  10. #include "pchgxu.h"
  11.  
  12. #include "ShowArcball.h"
  13.  
  14. #define PI 3.141592f
  15.  
  16. CShowArcball::CShowArcball()
  17.   :m_pDevice(NULL),
  18.     m_pVertexBuffer(NULL),
  19.     m_cSegments(0)
  20. {
  21. }
  22.  
  23. CShowArcball::~CShowArcball()
  24. {
  25.     GXRELEASE(m_pVertexBuffer);
  26.     GXRELEASE(m_pDevice);
  27. }
  28.  
  29. HRESULT
  30. CShowArcball::Init
  31.     (
  32.     LPDIRECT3DDEVICE9 pDevice,
  33.     DWORD cSegments
  34.     )
  35. {
  36.     HRESULT hr;
  37.     DWORD iSegment;
  38.     D3DXVECTOR3 *rgvDest = NULL;
  39.  
  40.     hr = CreateEmptyOutline();
  41.     if (FAILED(hr))
  42.         goto e_Exit;
  43.  
  44.     m_pDevice = pDevice;
  45.     m_pDevice->AddRef();
  46.  
  47.     m_cSegments = cSegments;
  48.  
  49.     hr = m_pDevice->CreateVertexBuffer(sizeof(D3DXVECTOR3) * (cSegments+1) * 3, 
  50.                         D3DUSAGE_WRITEONLY, 
  51.                         D3DFVF_XYZ, D3DPOOL_MANAGED, &m_pVertexBuffer, NULL);
  52.     if (FAILED(hr))
  53.         goto e_Exit;
  54.  
  55.  
  56.     hr = m_pVertexBuffer->Lock(0,0, (PVOID*)&rgvDest, D3DLOCK_NOSYSLOCK);
  57.     if (FAILED(hr))
  58.         goto e_Exit;
  59.     
  60.     for (iSegment = 0; iSegment < cSegments+1; iSegment++)
  61.     {
  62.         rgvDest[iSegment].x = cosf(PI*2 * ((float)iSegment/(float)cSegments));
  63.         rgvDest[iSegment].y = sinf(PI*2 * ((float)iSegment/(float)cSegments));
  64.         rgvDest[iSegment].z = 0.0f;
  65.     }
  66.  
  67.     rgvDest += cSegments+1;
  68.     for (iSegment = 0; iSegment < cSegments+1; iSegment++)
  69.     {
  70.         rgvDest[iSegment].x = 0.0f;
  71.         rgvDest[iSegment].y = cosf(PI*2 * ((float)iSegment/(float)cSegments));
  72.         rgvDest[iSegment].z = sinf(PI*2 * ((float)iSegment/(float)cSegments));
  73.     }
  74.  
  75.     rgvDest += cSegments+1;
  76.     for (iSegment = 0; iSegment < cSegments+1; iSegment++)
  77.     {
  78.         rgvDest[iSegment].x = cosf(PI*2 * ((float)iSegment/(float)cSegments));
  79.         rgvDest[iSegment].y = 0.0f;
  80.         rgvDest[iSegment].z = sinf(PI*2 * ((float)iSegment/(float)cSegments));
  81.     }
  82.  
  83. e_Exit:
  84.     if (rgvDest != NULL)
  85.     {
  86.         m_pVertexBuffer->Unlock();
  87.     }
  88.  
  89.     return hr;
  90. }
  91.  
  92. HRESULT
  93. CShowArcball::Draw
  94.     (
  95.     LPD3DXEFFECT pfxColor, 
  96.     DWORD dwColor
  97.     )
  98. {
  99.     HRESULT hr;
  100.     UINT iPass;
  101.     UINT cPasses;
  102.     D3DXCOLOR vClr(dwColor);
  103.  
  104.     pfxColor->Begin(&cPasses, 0);
  105.  
  106.     for (iPass = 0; iPass < cPasses; iPass++)
  107.     {
  108.         m_pDevice->SetFVF(D3DFVF_XYZ);
  109.  
  110.         pfxColor->BeginPass(iPass);
  111.  
  112.         m_pDevice->SetStreamSource(0, m_pVertexBuffer, 0, sizeof(D3DXVECTOR3));
  113.  
  114.         vClr = D3DXCOLOR(1.0f, 0.2f, 0.2f, 1.0f);
  115.         pfxColor->SetVector("vClr", (D3DXVECTOR4*)&vClr);                       
  116.         hr = m_pDevice->DrawPrimitive(D3DPT_LINESTRIP, 
  117.                                      0, m_cSegments);
  118.  
  119.         vClr = D3DXCOLOR(0.2f, 1.0f, 0.2f, 1.0f);
  120.         pfxColor->SetVector("vClr", (D3DXVECTOR4*)&vClr);                       
  121.         hr = m_pDevice->DrawPrimitive(D3DPT_LINESTRIP, 
  122.                                      m_cSegments + 1, m_cSegments);
  123.  
  124.         vClr = D3DXCOLOR(0.2f, 0.2f, 1.0f, 1.0f);
  125.         pfxColor->SetVector("vClr", (D3DXVECTOR4*)&vClr);                       
  126.         hr = m_pDevice->DrawPrimitive(D3DPT_LINESTRIP, 
  127.                                      m_cSegments * 2 + 2, m_cSegments);
  128.         pfxColor->EndPass();
  129.     }
  130.  
  131.     pfxColor->End();
  132.  
  133.     return S_OK;
  134. }
  135.  
  136. HRESULT
  137. CShowArcball::CreateEmptyOutline()
  138. {
  139.     m_cSegments = 0;
  140.  
  141.     GXRELEASE(m_pVertexBuffer);
  142.     GXRELEASE(m_pDevice);
  143.  
  144.     return S_OK;
  145. }
  146.