home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Direct3D / EnvMapping / FishEye / fisheye.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  19.3 KB  |  560 lines

  1. //-----------------------------------------------------------------------------
  2. // File: FishEye.cpp
  3. //
  4. // Desc: Example code showing how to do a fisheye lens effect with cubemapping.
  5. //       The scene is rendering into a cubemap each frame, and then a
  6. //       funky-shaped object is rendered using the cubemap and an environment
  7. //       map.
  8. //
  9. // Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
  10. //-----------------------------------------------------------------------------
  11. #define STRICT
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include <D3DX8.h>
  15. #include "D3DApp.h"
  16. #include "D3DFile.h"
  17. #include "D3DFont.h"
  18. #include "D3DUtil.h"
  19. #include "DXUtil.h"
  20.  
  21.  
  22.  
  23.  
  24. //-----------------------------------------------------------------------------
  25. // Name: struct ENVMAPPEDVERTEX
  26. // Desc: D3D vertex type for environment-mapped objects
  27. //-----------------------------------------------------------------------------
  28. struct ENVMAPPEDVERTEX
  29. {
  30.     D3DXVECTOR3 p; // Position
  31.     D3DXVECTOR3 n; // Normal
  32. };
  33.  
  34. #define D3DFVF_ENVMAPVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL)
  35.  
  36.  
  37.  
  38.  
  39. //-----------------------------------------------------------------------------
  40. // Name: class CMyD3DApplication
  41. // Desc: Application class. The base class (CD3DApplication) provides the 
  42. //       generic functionality needed in all Direct3D samples. CMyD3DApplication 
  43. //       adds functionality specific to this sample program.
  44. //-----------------------------------------------------------------------------
  45. class CMyD3DApplication : public CD3DApplication
  46. {
  47.     // Font for drawing text
  48.     CD3DFont* m_pFont;
  49.  
  50.     CD3DMesh*               m_pSkyBoxObject;
  51.     LPDIRECT3DCUBETEXTURE8  m_pCubeMap;
  52.  
  53.     LPDIRECT3DVERTEXBUFFER8 m_pFishEyeLensVB;
  54.     LPDIRECT3DINDEXBUFFER8  m_pFishEyeLensIB;
  55.     DWORD                   m_dwNumFishEyeLensVertices;
  56.     DWORD                   m_dwNumFishEyeLensFaces;
  57.  
  58.     HRESULT RenderScene();
  59.     HRESULT RenderSceneIntoCubeMap();
  60.     HRESULT GenerateFishEyeLens( DWORD, DWORD, FLOAT );
  61.  
  62. protected:
  63.     HRESULT OneTimeSceneInit();
  64.     HRESULT InitDeviceObjects();
  65.     HRESULT RestoreDeviceObjects();
  66.     HRESULT InvalidateDeviceObjects();
  67.     HRESULT DeleteDeviceObjects();
  68.     HRESULT Render();
  69.     HRESULT FrameMove();
  70.     HRESULT FinalCleanup();
  71.     HRESULT ConfirmDevice( D3DCAPS8*, DWORD, D3DFORMAT );
  72.  
  73. public:
  74.     CMyD3DApplication();
  75. };
  76.  
  77.  
  78.  
  79.  
  80. //-----------------------------------------------------------------------------
  81. // Name: WinMain()
  82. // Desc: Entry point to the program. Initializes everything, and goes into a
  83. //       message-processing loop. Idle time is used to render the scene.
  84. //-----------------------------------------------------------------------------
  85. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  86. {
  87.     CMyD3DApplication d3dApp;
  88.  
  89.     if( FAILED( d3dApp.Create( hInst ) ) )
  90.         return 0;
  91.  
  92.     return d3dApp.Run();
  93. }
  94.  
  95.  
  96.  
  97.  
  98. //-----------------------------------------------------------------------------
  99. // Name: CMyD3DApplication()
  100. // Desc: Application constructor. Sets attributes for the app.
  101. //-----------------------------------------------------------------------------
  102. CMyD3DApplication::CMyD3DApplication()
  103. {
  104.     m_strWindowTitle    = _T("FishEye: Environment mapping");
  105.     m_bUseDepthBuffer   = TRUE;
  106.  
  107.     m_pFont             = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  108.     m_pSkyBoxObject     = new CD3DMesh();
  109.     m_pCubeMap          = NULL;
  110.     m_pFishEyeLensVB    = NULL;
  111.     m_pFishEyeLensIB    = NULL;
  112. }
  113.  
  114.  
  115.  
  116.  
  117. //-----------------------------------------------------------------------------
  118. // Name: OneTimeSceneInit()
  119. // Desc: Called during initial app startup, this function performs all the
  120. //       permanent initialization.
  121. //-----------------------------------------------------------------------------
  122. HRESULT CMyD3DApplication::OneTimeSceneInit()
  123. {
  124.     return S_OK;
  125. }
  126.  
  127.  
  128.  
  129.  
  130. //-----------------------------------------------------------------------------
  131. // Name: FrameMove()
  132. // Desc: Called once per frame, the call is the entry point for animating
  133. //       the scene.
  134. //-----------------------------------------------------------------------------
  135. HRESULT CMyD3DApplication::FrameMove()
  136. {
  137.     // When the window has focus, let the mouse adjust the scene
  138.     if( GetFocus() )
  139.     {
  140.         D3DXQUATERNION quat = D3DUtil_GetRotationFromCursor( m_hWnd );
  141.         D3DXMATRIX matTrackBall;
  142.         D3DXMatrixRotationQuaternion( &matTrackBall, &quat );
  143.         m_pd3dDevice->SetTransform( D3DTS_WORLD, &matTrackBall );
  144.  
  145.         // Render the scene into the surfaces of the cubemap
  146.         if( FAILED( RenderSceneIntoCubeMap() ) )
  147.             return E_FAIL;
  148.     }
  149.  
  150.     return S_OK;
  151. }
  152.  
  153.  
  154.  
  155.  
  156. //-----------------------------------------------------------------------------
  157. // Name: RenderScene()
  158. // Desc: Renders all visual elements in the scene. This is called by the main
  159. //       Render() function, and also by the RenderIntoCubeMap() function.
  160. //-----------------------------------------------------------------------------
  161. HRESULT CMyD3DApplication::RenderScene()
  162. {
  163.     // Render the skybox
  164.     {
  165.         // Save current state
  166.         D3DXMATRIX matViewSave, matProjSave;
  167.         m_pd3dDevice->GetTransform( D3DTS_VIEW,       &matViewSave );
  168.         m_pd3dDevice->GetTransform( D3DTS_PROJECTION, &matProjSave );
  169.  
  170.         // Disable zbuffer, center view matrix, and set FOV to 90 degrees
  171.         D3DXMATRIX matView = matViewSave;
  172.         D3DXMATRIX matProj = matViewSave;
  173.         matView._41 = matView._42 = matView._43 = 0.0f;
  174.         D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/2, 1.0f, 0.5f, 10000.0f );
  175.         m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  176.         m_pd3dDevice->SetTransform( D3DTS_VIEW,       &matView );
  177.         m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,  FALSE );
  178.  
  179.         // Render the skybox
  180.         m_pSkyBoxObject->Render( m_pd3dDevice );
  181.  
  182.         // Restore the render states
  183.         m_pd3dDevice->SetTransform( D3DTS_VIEW,       &matViewSave );
  184.         m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProjSave );
  185.         m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,  TRUE );
  186.     }
  187.  
  188.     // Render any other elements of the scene here. In this sample, only a
  189.     // skybox is render, but a much more interesting scene could be rendered
  190.     // instead.
  191.  
  192.     return S_OK;
  193. }
  194.  
  195.  
  196.  
  197.  
  198. //-----------------------------------------------------------------------------
  199. // Name: RenderSceneIntoCubeMap()
  200. // Desc: Renders the scene to each of the 6 faces of the cube map
  201. //-----------------------------------------------------------------------------
  202. HRESULT CMyD3DApplication::RenderSceneIntoCubeMap()
  203. {
  204.     // Save transformation matrices of the device
  205.     D3DXMATRIX   matProjSave, matViewSave;
  206.     m_pd3dDevice->GetTransform( D3DTS_VIEW,       &matViewSave );
  207.     m_pd3dDevice->GetTransform( D3DTS_PROJECTION, &matProjSave );
  208.  
  209.     // Set the projection matrix for a field of view of 90 degrees
  210.     D3DXMATRIX matProj;
  211.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/2, 1.0f, 0.5f, 100.0f );
  212.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  213.  
  214.     // Get the current view matrix, to concat it with the cubemap view vectors
  215.     D3DXMATRIX matViewDir;
  216.     m_pd3dDevice->GetTransform( D3DTS_VIEW, &matViewDir );
  217.     matViewDir._41 = 0.0f; matViewDir._42 = 0.0f; matViewDir._43 = 0.0f;
  218.  
  219.     // Store the current backbuffer and zbuffer
  220.     LPDIRECT3DSURFACE8 pBackBuffer, pZBuffer;
  221.     m_pd3dDevice->GetRenderTarget( &pBackBuffer );
  222.     m_pd3dDevice->GetDepthStencilSurface( &pZBuffer );
  223.  
  224.     // Render to the six faces of the cube map
  225.     for( DWORD i=0; i<6; i++ )
  226.     {
  227.         // Set the view transform for this cubemap surface
  228.         D3DXMATRIX matView;
  229.         matView = D3DUtil_GetCubeMapViewMatrix( (D3DCUBEMAP_FACES)i );
  230.         D3DXMatrixMultiply( &matView, &matViewDir, &matView );
  231.         m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
  232.  
  233.         // Set the rendertarget to the i'th cubemap surface
  234.         LPDIRECT3DSURFACE8 pCubeMapFace;
  235.         m_pCubeMap->GetCubeMapSurface( (D3DCUBEMAP_FACES)i, 0, &pCubeMapFace );
  236.         m_pd3dDevice->SetRenderTarget( pCubeMapFace, NULL );
  237.         pCubeMapFace->Release();
  238.  
  239.         // Render the scene
  240.         m_pd3dDevice->BeginScene();
  241.         RenderScene();
  242.         m_pd3dDevice->EndScene();
  243.     }
  244.  
  245.     // Change the rendertarget back to the main backbuffer
  246.     m_pd3dDevice->SetRenderTarget( pBackBuffer, pZBuffer );
  247.     pBackBuffer->Release();
  248.     pZBuffer->Release();
  249.  
  250.     // Restore the original transformation matrices
  251.     m_pd3dDevice->SetTransform( D3DTS_VIEW,       &matViewSave );
  252.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProjSave );
  253.  
  254.     return S_OK;
  255. }
  256.  
  257.  
  258.  
  259.  
  260. //-----------------------------------------------------------------------------
  261. // Name: Render()
  262. // Desc: Called once per frame, the call is the entry point for 3d
  263. //       rendering. This function sets up render states, clears the
  264. //       viewport, and renders the scene.
  265. //-----------------------------------------------------------------------------
  266. HRESULT CMyD3DApplication::Render()
  267. {
  268.     // Begin the scene
  269.     if( FAILED( m_pd3dDevice->BeginScene() ) )
  270.         return S_OK;
  271.  
  272.     // Set the states we want: identity matrix, no z-buffer, and cubemap texture
  273.     // coordinate generation
  274.     D3DXMATRIX matWorld;
  275.     D3DXMatrixIdentity( &matWorld );
  276.     m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  277.     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
  278.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR );
  279.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3 );
  280.  
  281.     // Render the fisheye lens object with the environment-mapped body.
  282.     m_pd3dDevice->SetTexture( 0, m_pCubeMap );
  283.     m_pd3dDevice->SetVertexShader( D3DFVF_ENVMAPVERTEX );
  284.     m_pd3dDevice->SetStreamSource( 0, m_pFishEyeLensVB, sizeof(ENVMAPPEDVERTEX) );
  285.     m_pd3dDevice->SetIndices( m_pFishEyeLensIB, 0 );
  286.     m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
  287.                                         0, m_dwNumFishEyeLensVertices,
  288.                                         0, m_dwNumFishEyeLensFaces );
  289.  
  290.     // Restore the render states
  291.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU );
  292.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
  293.     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
  294.  
  295.     // Output statistics
  296.     m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  297.     m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  298.  
  299.     // End the scene.
  300.     m_pd3dDevice->EndScene();
  301.  
  302.     return S_OK;
  303. }
  304.  
  305.  
  306.  
  307.  
  308. //-----------------------------------------------------------------------------
  309. // Name: InitDeviceObjects()
  310. // Desc: Initialize scene objects.
  311. //-----------------------------------------------------------------------------
  312. HRESULT CMyD3DApplication::InitDeviceObjects()
  313. {
  314.     HRESULT hr;
  315.  
  316.     // Initialize the font's internal textures
  317.     m_pFont->InitDeviceObjects( m_pd3dDevice );
  318.  
  319.     // Load the file objects
  320.     if( FAILED( hr = m_pSkyBoxObject->Create( m_pd3dDevice, _T("Lobby_skybox.x") ) ) )
  321.         return D3DAPPERR_MEDIANOTFOUND;
  322.  
  323.     // Create the fisheye lens
  324.     if( FAILED( hr = GenerateFishEyeLens( 20, 20, 1.0f ) ) )
  325.         return hr;
  326.  
  327.     return S_OK;
  328. }
  329.  
  330.  
  331.  
  332.  
  333. //-----------------------------------------------------------------------------
  334. // Name: RestoreDeviceObjects()
  335. // Desc: Restore device-memory objects and state after a device is created or
  336. //       resized.
  337. //-----------------------------------------------------------------------------
  338. HRESULT CMyD3DApplication::RestoreDeviceObjects()
  339. {
  340.     HRESULT hr;
  341.  
  342.     m_pFont->RestoreDeviceObjects();
  343.  
  344.     // Restore device objects for the skybox
  345.     m_pSkyBoxObject->RestoreDeviceObjects( m_pd3dDevice );
  346.  
  347.     // Create the cubemap
  348.     if( FAILED( hr = m_pd3dDevice->CreateCubeTexture( 256, 1, D3DUSAGE_RENDERTARGET,
  349.                                                       m_d3dsdBackBuffer.Format,
  350.                                                       D3DPOOL_DEFAULT, &m_pCubeMap ) ) )
  351.         return hr;
  352.  
  353.     // Set default render states
  354.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  355.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  356.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
  357.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
  358.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU,  D3DTADDRESS_MIRROR );
  359.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV,  D3DTADDRESS_MIRROR );
  360.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  361.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  362.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MIPFILTER, D3DTEXF_NONE );
  363.     m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0xffffffff );
  364.  
  365.     // Set the transforms
  366.     D3DXVECTOR3 m_vEyePt    = D3DXVECTOR3( 0.0f, 0.0f,-5.0f );
  367.     D3DXVECTOR3 m_vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
  368.     D3DXVECTOR3 m_vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
  369.  
  370.     D3DXMATRIX matWorld, matView, matProj;
  371.     D3DXMatrixIdentity( &matWorld );
  372.     D3DXMatrixLookAtLH( &matView, &m_vEyePt, &m_vLookatPt, &m_vUpVec );
  373.     D3DXMatrixOrthoLH( &matProj, 2.0f, 2.0f, 0.5f, 100.0f );
  374.     m_pd3dDevice->SetTransform( D3DTS_WORLD,      &matWorld );
  375.     m_pd3dDevice->SetTransform( D3DTS_VIEW,       &matView );
  376.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  377.  
  378.     return S_OK;
  379. }
  380.  
  381.  
  382.  
  383.  
  384. //-----------------------------------------------------------------------------
  385. // Name: InvalidateDeviceObjects()
  386. // Desc: Called when the device-dependent objects are about to be lost.
  387. //-----------------------------------------------------------------------------
  388. HRESULT CMyD3DApplication::InvalidateDeviceObjects()
  389. {
  390.     m_pFont->InvalidateDeviceObjects();
  391.     m_pSkyBoxObject->InvalidateDeviceObjects();
  392.  
  393.     SAFE_RELEASE( m_pCubeMap );
  394.  
  395.     return S_OK;
  396. }
  397.  
  398.  
  399.  
  400.  
  401. //-----------------------------------------------------------------------------
  402. // Name: DeleteDeviceObjects()
  403. // Desc: Called when the app is exiting, or the device is being changed,
  404. //       this function deletes any device dependent objects.
  405. //-----------------------------------------------------------------------------
  406. HRESULT CMyD3DApplication::DeleteDeviceObjects()
  407. {
  408.     m_pFont->DeleteDeviceObjects();
  409.     m_pSkyBoxObject->Destroy();
  410.  
  411.     SAFE_RELEASE( m_pFishEyeLensVB );
  412.     SAFE_RELEASE( m_pFishEyeLensIB );
  413.  
  414.     return S_OK;
  415. }
  416.  
  417.  
  418.  
  419.  
  420. //-----------------------------------------------------------------------------
  421. // Name: FinalCleanup()
  422. // Desc: Called before the app exits, this function gives the app the chance
  423. //       to cleanup after itself.
  424. //-----------------------------------------------------------------------------
  425. HRESULT CMyD3DApplication::FinalCleanup()
  426. {
  427.     SAFE_DELETE( m_pFont );
  428.     SAFE_DELETE( m_pSkyBoxObject );
  429.  
  430.     return S_OK;
  431. }
  432.  
  433.  
  434.  
  435.  
  436. //-----------------------------------------------------------------------------
  437. // Name: ConfirmDevice()
  438. // Desc: Called during device intialization, this code checks the device
  439. //       for some minimum set of capabilities
  440. //-----------------------------------------------------------------------------
  441. HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
  442.                                           D3DFORMAT Format )
  443. {
  444.     if( dwBehavior & D3DCREATE_PUREDEVICE )
  445.         return E_FAIL; // GetTransform doesn't work on PUREDEVICE
  446.  
  447.     // Check for cubemapping devices
  448.     if( 0 == ( pCaps->TextureCaps & D3DPTEXTURECAPS_CUBEMAP ) )
  449.         return E_FAIL;
  450.  
  451.     // Check that we can create a cube texture that we can render into
  452.     if( FAILED( m_pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, 
  453.         pCaps->DeviceType, Format, D3DUSAGE_RENDERTARGET,
  454.         D3DRTYPE_CUBETEXTURE, Format ) ) )
  455.     {
  456.        return E_FAIL;
  457.     }
  458.  
  459.     return S_OK;
  460. }
  461.  
  462.  
  463.  
  464.  
  465. //-----------------------------------------------------------------------------
  466. // Name: GenerateFishEyeLens()
  467. // Desc: Makes vertex and index data for a fish eye lens
  468. //-----------------------------------------------------------------------------
  469. HRESULT CMyD3DApplication::GenerateFishEyeLens( DWORD dwNumRings, DWORD dwNumSections,
  470.                                                 FLOAT fScale )
  471. {
  472.     ENVMAPPEDVERTEX* pVertices;
  473.     WORD*            pIndices;
  474.     DWORD            dwNumTriangles = (dwNumRings+1) * dwNumSections * 2;
  475.     DWORD            dwNumVertices  = (dwNumRings+1) * dwNumSections + 2;
  476.     DWORD            i, j, m;
  477.  
  478.     // Generate space for the required triangles and vertices.
  479.     if( FAILED( m_pd3dDevice->CreateVertexBuffer( dwNumVertices*sizeof(ENVMAPPEDVERTEX),
  480.                                                   D3DUSAGE_WRITEONLY, D3DFVF_ENVMAPVERTEX,
  481.                                                   D3DPOOL_MANAGED, &m_pFishEyeLensVB ) ) )
  482.         return E_FAIL;
  483.  
  484.     if( FAILED( m_pd3dDevice->CreateIndexBuffer( dwNumTriangles*3*sizeof(WORD),
  485.                                                  D3DUSAGE_WRITEONLY, D3DFMT_INDEX16,
  486.                                                  D3DPOOL_MANAGED, &m_pFishEyeLensIB ) ) )
  487.         return E_FAIL;
  488.  
  489.     m_pFishEyeLensVB->Lock( 0, 0, (BYTE**)&pVertices, 0 );
  490.     m_pFishEyeLensIB->Lock( 0, 0, (BYTE**)&pIndices, 0 );
  491.  
  492.     // Generate vertices at the end points.
  493.     pVertices->p = D3DXVECTOR3( 0.0f, 0.0f, fScale );
  494.     pVertices->n = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
  495.     pVertices++;
  496.  
  497.     // Generate vertex points for rings
  498.     FLOAT r = 0.0f;
  499.     for( i = 0; i < (dwNumRings+1); i++ )
  500.     {
  501.         FLOAT phi = 0.0f;
  502.  
  503.         for( j = 0; j < dwNumSections; j++ )
  504.         {
  505.             FLOAT x  =  r * sinf(phi);
  506.             FLOAT y  =  r * cosf(phi);
  507.             FLOAT z  = 0.5f - 0.5f * ( x*x + y*y );
  508.  
  509.             FLOAT nx = -x;
  510.             FLOAT ny = -y;
  511.             FLOAT nz = 1.0f;
  512.  
  513.             pVertices->p = D3DXVECTOR3( x, y, z );
  514.             pVertices->n = D3DXVECTOR3( nx, ny, nz );
  515.             pVertices++;
  516.  
  517.             phi += (FLOAT)(2*D3DX_PI / dwNumSections);
  518.         }
  519.  
  520.         r += 1.5f/dwNumRings;
  521.     }
  522.  
  523.     // Generate triangles for the centerpiece
  524.     for( i = 0; i < 2*dwNumSections; i++ )
  525.     {
  526.         *pIndices++ = (WORD)(0);
  527.         *pIndices++ = (WORD)(i + 1);
  528.         *pIndices++ = (WORD)(1 + ((i + 1) % dwNumSections));
  529.     }
  530.  
  531.     // Generate triangles for the rings
  532.     m = 1;  // 1st vertex begins at 1 to skip top point
  533.  
  534.     for( i = 0; i < dwNumRings; i++ )
  535.     {
  536.         for( j = 0; j < dwNumSections; j++ )
  537.         {
  538.             *pIndices++ = (WORD)(m + j);
  539.             *pIndices++ = (WORD)(m + dwNumSections + j);
  540.             *pIndices++ = (WORD)(m + dwNumSections + ((j + 1) % dwNumSections));
  541.  
  542.             *pIndices++ = (WORD)(m + j);
  543.             *pIndices++ = (WORD)(m + dwNumSections + ((j + 1) % dwNumSections));
  544.             *pIndices++ = (WORD)(m + ((j + 1) % dwNumSections));
  545.         }
  546.         m += dwNumSections;
  547.     }
  548.  
  549.     m_pFishEyeLensVB->Unlock();
  550.     m_pFishEyeLensIB->Unlock();
  551.     m_dwNumFishEyeLensVertices = dwNumVertices;
  552.     m_dwNumFishEyeLensFaces    = dwNumTriangles;
  553.  
  554.     return S_OK;
  555. }
  556.  
  557.  
  558.  
  559.  
  560.