Directx中的拾取方法

一、下面来自: http://blog.csdn.net/z18_28_19/article/details/9140559

               今天来看一下,DirectX3D的拾取,拾取技术在许多游戏和3D程序中都得到了很广泛的应用。例如玩家需要通过单击鼠标与场景中各种各样的物体进行交互。玩家可以通过单击敌人而向其开火,也可以单击某些道具将其捡起。我们一般将拾取分解为以下4个步骤:

 

              给定所单击的屏幕点S,求出它在投影窗口中的对应点p.

              计算拾取射线。即自坐标原点发出且通过点p的那条射线。

             将拾取射线和物体模型变换至同一个坐标系中。

             进行物体/射线的相交判断。相交的物体即为用户所拾取的屏幕对象。

 

我们先来看DirectX3D.h的代码:

[plain]  view plain copy print ?
  1. #ifndef __DirectX3DH__  
  2. #define __DirectX3DH__  
  3.   
  4. #include <d3dx9.h>  
  5. #include <string>  
  6.   
  7. namespace d3d  
  8. {  
  9.     bool InitD3D(  
  10.         HINSTANCE hInstance,       // [in] Application instance.  
  11.         int width, int height,     // [in] Backbuffer dimensions.  
  12.         bool windowed,             // [in] Windowed (true)or full screen (false).  
  13.         D3DDEVTYPE deviceType,     // [in] HAL or REF  
  14.         IDirect3DDevice9** device);// [out]The created device.  
  15.       
  16.     int EnterMsgLoop(   
  17.         bool (*ptr_display)(float timeDelta));  
  18.       
  19.     LRESULT CALLBACK WndProc(  
  20.         HWND hwnd,  
  21.         UINT msg,   
  22.         WPARAM wParam,  
  23.         LPARAM lParam);  
  24.       
  25.     template<class T> void Release(T t)  
  26.     {  
  27.         if( t )  
  28.         {  
  29.             t->Release();  
  30.             t = 0;  
  31.         }  
  32.     }  
  33.       
  34.     template<class T> void Delete(T t)  
  35.     {  
  36.         if( t )  
  37.         {  
  38.             delete t;  
  39.             t = 0;  
  40.         }  
  41.     }  
  42.       
  43.     const D3DXCOLOR      WHITE( D3DCOLOR_XRGB(255, 255, 255) );  
  44.     const D3DXCOLOR      BLACK( D3DCOLOR_XRGB(  0,   0,   0) );  
  45.     const D3DXCOLOR        RED( D3DCOLOR_XRGB(255,   0,   0) );  
  46.     const D3DXCOLOR      GREEN( D3DCOLOR_XRGB(  0, 255,   0) );  
  47.     const D3DXCOLOR       BLUE( D3DCOLOR_XRGB(  0,   0, 255) );  
  48.     const D3DXCOLOR     YELLOW( D3DCOLOR_XRGB(255, 255,   0) );  
  49.     const D3DXCOLOR       CYAN( D3DCOLOR_XRGB(  0, 255, 255) );  
  50.     const D3DXCOLOR    MAGENTA( D3DCOLOR_XRGB(255,   0, 255) );  
  51.       
  52.     //  
  53.     // Lights  
  54.     //  
  55.       
  56.     D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3* direction, D3DXCOLOR* color);  
  57.     D3DLIGHT9 InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color);  
  58.     D3DLIGHT9 InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color);  
  59.       
  60.     //  
  61.     // Materials  
  62.     //  
  63.       
  64.     D3DMATERIAL9 InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p);  
  65.       
  66.     const D3DMATERIAL9 WHITE_MTRL  = InitMtrl(WHITE, WHITE, WHITE, BLACK, 2.0f);  
  67.     const D3DMATERIAL9 RED_MTRL    = InitMtrl(RED, RED, RED, BLACK, 2.0f);  
  68.     const D3DMATERIAL9 GREEN_MTRL  = InitMtrl(GREEN, GREEN, GREEN, BLACK, 2.0f);  
  69.     const D3DMATERIAL9 BLUE_MTRL   = InitMtrl(BLUE, BLUE, BLUE, BLACK, 2.0f);  
  70.     const D3DMATERIAL9 YELLOW_MTRL = InitMtrl(YELLOW, YELLOW, YELLOW, BLACK, 2.0f);   
  71.   
  72.     //  
  73.     // 外接体对象  
  74.     //  
  75.       
  76.     struct BoundingBox  
  77.     {  
  78.         BoundingBox();  
  79.           
  80.         bool isPointInside(D3DXVECTOR3& p);  
  81.           
  82.         D3DXVECTOR3 _min;  
  83.         D3DXVECTOR3 _max;  
  84.     };  
  85.       
  86.     struct BoundingSphere  
  87.     {  
  88.         BoundingSphere();  
  89.           
  90.         D3DXVECTOR3 _center;  
  91.         float       _radius;  
  92.     };  
  93.     struct Ray  
  94.     {  
  95.         D3DXVECTOR3 _origin;  
  96.         D3DXVECTOR3 _direction;  
  97.     };  
  98.       
  99.     //  
  100.     // 常量  
  101.     //  
  102.     /*  
  103.     常量INFINITY用来表示float类型所能存储的最大浮点数。由于我们不可能取得一个比FLT_MAX更大的浮点数,我们可将  
  104.     该值概念化为无穷大,这样可使表达了无穷大概念的代码更具可读性。常量EPSLION是我们定义的一个很小的数,如果某个  
  105.     数小于该值,我们就可认为该数为0.这样做是很有必要的,因为浮点数运算具有不精确性,一个本应为0的数在计算机中表示  
  106.     可能出现微小的偏差。这样,就会得出该数与0不相等的结果。这样,我们就将判断某个数是否等于0转化为判断某个数是否小于  
  107.     EPSILON.  
  108.       
  109.       
  110.     */  
  111.       
  112.     const float INFINITY = FLT_MAX;  
  113.     const float EPSILON  = 0.001f;  
  114.   
  115.     bool DrawBasicScene(  
  116.         IDirect3DDevice9* device,// Pass in 0 for cleanup.  
  117.         float scale);            // uniform scale   
  118.       
  119.     //  
  120.     // Vertex Structures  
  121.     //  
  122.       
  123.     struct Vertex  
  124.     {  
  125.         Vertex(){}  
  126.         Vertex(float x, float y, float z,   
  127.             float nx, float ny, float nz,  
  128.             float u, float v)  
  129.         {  
  130.             _x  = x;  _y  = y;  _z  = z;  
  131.             _nx = nx; _ny = ny; _nz = nz;  
  132.             _u  = u;  _v  = v;  
  133.         }  
  134.         float _x, _y, _z;  
  135.         float _nx, _ny, _nz;  
  136.         float _u, _v;  
  137.           
  138.         static const DWORD FVF;  
  139.     };  
  140.   
  141. }  
  142.   
  143.   
  144.   
  145.   
  146. #endif   


再来看DirectX3D.cpp的代码:

[plain]  view plain copy print ?
  1. #include "DirectX3D.h"  
  2.   
  3.   
  4. // vertex formats  
  5. const DWORD d3d::Vertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;  
  6.   
  7.   
  8.   
  9. bool d3d::InitD3D(  
  10.     HINSTANCE hInstance,  
  11.     int width, int height,  
  12.     bool windowed,  
  13.     D3DDEVTYPE deviceType,  
  14.     IDirect3DDevice9** device)  
  15. {  
  16.     //  
  17.     // Create the main application window.  
  18.     //  
  19.   
  20.     WNDCLASS wc;  
  21.   
  22.     wc.style         = CS_HREDRAW | CS_VREDRAW;  
  23.     wc.lpfnWndProc   = (WNDPROC)d3d::WndProc;   
  24.     wc.cbClsExtra    = 0;  
  25.     wc.cbWndExtra    = 0;  
  26.     wc.hInstance     = hInstance;  
  27.     wc.hIcon         = LoadIcon(0, IDI_APPLICATION);  
  28.     wc.hCursor       = LoadCursor(0, IDC_ARROW);  
  29.     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);  
  30.     wc.lpszMenuName  = 0;  
  31.     wc.lpszClassName = "Direct3D9App";  
  32.   
  33.     if( !RegisterClass(&wc) )   
  34.     {  
  35.         ::MessageBox(0, "RegisterClass() - FAILED", 0, 0);  
  36.         return false;  
  37.     }  
  38.           
  39.     HWND hwnd = 0;  
  40.     hwnd = ::CreateWindow("Direct3D9App", "Direct3D9App",   
  41.         WS_EX_TOPMOST,  
  42.         0, 0, width, height,  
  43.         0 /*parent hwnd*/, 0 /* menu */, hInstance, 0 /*extra*/);   
  44.   
  45.     if( !hwnd )  
  46.     {  
  47.         ::MessageBox(0, "CreateWindow() - FAILED", 0, 0);  
  48.         return false;  
  49.     }  
  50.   
  51.     ::ShowWindow(hwnd, SW_SHOW);  
  52.     ::UpdateWindow(hwnd);  
  53.   
  54.     //  
  55.     // Init D3D:   
  56.     //  
  57.   
  58.     //第一步  
  59.     //要初始化IDirect3D 首先必须获取IDirect3D9的指针,使用一个专门的Direct3D函数就可以很容易做到  
  60.      IDirect3D9 * _d3d9;  
  61.     
  62.   //这个对象的主要有两个用途:设备枚举以及创建IDirect3DDevice9类型的对象。设备枚举是指获取系统中可用的的每块图形卡的  
  63.   //性能,显示模型,格式以及其他信息。这个函数调用失败会返回一个NULL指针。  
  64.   if(NULL == (_d3d9 = Direct3DCreate9(D3D_SDK_VERSION))){  
  65.   
  66.   
  67.     return FALSE;  
  68.   
  69.   }  
  70.   
  71.   //第二步  
  72.   //创建一个代表主显卡的IDirect3DDevice9类型对象时,必须指定使用该对象进行顶点运算的类型。如果可以,我们希望使用硬件顶点运算  
  73.   //但是由于并非所有的显卡都支持硬件顶点运算,我们必须首先检查图形卡是否支持该类型的运算。  
  74.   
  75.   //要进行检查,必须先根据主显卡的性能参数初始化一个IDirect3DDevice9类型的对象。我们使用如下方法来完成初始化:  
  76.      /*  
  77.   HRESULT IDirect3D9:GetDeviceCaps(  
  78.       UINT Adapter,  
  79.       D3DDEVTYPE DeviceType,  
  80.       D3DCAPS9 * pCaps;  
  81.      
  82.   )  
  83.   
  84.         Adapter : 指定物理显卡的序号。  
  85.   DeviceType:指定设备类(例如硬件设备(D3DDEVTYPE_HAL)或软件设备(D3DDEVTYPE_REF));  
  86.   pCaps 返回已初始化的设备性能结构实例。  
  87.         */  
  88.          D3DCAPS9 caps;   
  89.          int vp = 0; //代表顶点如何操作  
  90.   
  91.         _d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);  
  92.   
  93.           if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT){  
  94.   
  95.                         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;  
  96.            }  
  97.           else  
  98.           {  
  99.                         vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;  
  100.           }  
  101.   
  102.           //第三步是填充D3DPRESENT_PARAMETER结构  
  103.           //该结构用于指定所要创建的IDirect3DDevice9类型对象的一些特性,该结构定义如下:  
  104.           /*  
  105.           typedef struct _D3DPRESENT_PARAMETERS_{  
  106.                         UINT BackBufferWidth;  
  107.                         UINT BackBufferHeight;  
  108.                         UINT BackBufferFormat;  
  109.                         UINT BackBufferCount;  
  110.                         D3DMULTISAMPLE_TYPE MultiSampleType;  
  111.                         DWORD MultiSampleQuality;  
  112.                         D3DSWAPEFFECT SwapEffect;  
  113.                         HWND  hDeviceWindow;  
  114.                         BOOL  Windowed;  
  115.                         BOOL  EnableAutoDepthStencil;  
  116.                         D3DFORMAT AutoDepthStencilFormat;  
  117.                         DWORD  Flags;  
  118.                         UINT   FullScreen_RefreshRateInHz;  
  119.                         UINT   PresentationInterval;  
  120.           };   
  121.          */  
  122.           
  123.         /*   
  124.          BackBufferWidth: 后台缓存中表面的宽度,单位为像素。  
  125.         BackBufferHeight:后台缓存中表面的高度,单位为像素。  
  126.         BackBufferFormat:后台缓存的像素格式(如32位像素格式:D3DFMT_A8R8G8B8);  
  127.         BackBufferCount: 所需使用的后台缓存的个数,通常指定为1,表明我们仅需要一个后台缓存。  
  128.         MultiSampleType: 后台缓存所使用的多重采样类型。  
  129.         MultiSampleQuality:多重采样的质量水平。  
  130.         SwapEffect:D3DSWAPEFFECT 枚举类型的一个成员。该枚举类型指定了交换链中的缓存的页面置换方式。指定D3DSWAPEFFECT_DISCARD时效率最高。  
  131.         hDeviceWindow:与设备相关的窗口句柄。指定了所要进行绘制的应用程序窗口。  
  132.         Windowed:为true时表示窗口模式,false时为全屏模式  
  133.         EnableAutoDepthStencil:设为true,则Direct3D自动创建并维护深度缓存或模板缓存。  
  134.         AutoDepthStencilFormat:深度缓存或模板缓存的像素格式(例如,用24位表示深度并将8位保留供模板缓存使用,D3DFMT_D24S8).  
  135.         Flags:一些附加的特性。可以指定为0,表示无标记,或D3DPRESENTFLAG集合中的一个成员,其中两个成员较常用。  
  136.                  D3DPRESENTFLAG_LOCKABLE_DEPTHBUFFER 指定为可锁定的后台缓存。注意,使用一个可锁定的后台缓存会降低性能。  
  137.                  D3DPRESENTFLAG_DISCARD_DEPTHBUFFER 指定当下一个后台缓存提交时,哪个深度或模块缓存将被丢弃。丢弃的意思是深度或模板缓存存储区  
  138.                                                     中的内容别丢弃或无效。这样可以提升性能。  
  139.         FullScreen_RefreshRateInHz: 刷新频率,如果想使用默认的刷新频率,则可将该参数指定为D3DPRESENT_RATE_DEFAULT;  
  140.         PresentationInterval:D3DPRESENT集合的一个成员,其中有两个比较常用。  
  141.                       D3DPRESENT_INTERVAL_IMMEDIATE 立即提交。  
  142.                       D3DPRESENT_INTERVAL_DEFAULT 由Direct3D来选择提交频率,通常该值等于刷新频率。  
  143.        */   
  144.   
  145.                   D3DPRESENT_PARAMETERS d3dpp;    
  146.                   ZeroMemory(&d3dpp, sizeof(d3dpp));    
  147.                  d3dpp.BackBufferWidth            = 800;    
  148.                  d3dpp.BackBufferHeight           = 600;    
  149.                  d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;    
  150.                  d3dpp.BackBufferCount            = 1;    
  151.                  d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;    
  152.                  d3dpp.MultiSampleQuality         = 0;    
  153.                  d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD;     
  154.                  d3dpp.hDeviceWindow              = hwnd;    
  155.                  d3dpp.Windowed                   = true;    
  156.                  d3dpp.EnableAutoDepthStencil     = true;     
  157.                  d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;    
  158.                  d3dpp.Flags                      = 0;    
  159.                  d3dpp.FullScreen_RefreshRateInHz = 0;    
  160.                  d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;   
  161.   
  162.               //第四步 创建IDirectDevice9类型的对象  
  163.                /*    
  164.                        HRESULT IDirect3D9::CreateDevice(  
  165.                                UINT Adapter,  
  166.                                D3DDEVTYPE DeviceType,  
  167.                                HWND hFocusWindow,  
  168.                                DWORD BehaviorFlags,  
  169.                                D3DPRESENT_PARAMETERS *pPresentationParameters,  
  170.                                IDirect3DDevice9 ** ppReturnedDeviceInterface  
  171.      
  172.      
  173.                        );  
  174.   
  175.        Adapter:指定我们希望用已创建的IDirect3DDevice9对象代表哪块物理显卡。  
  176.        DeviceType:指定需要使用的设备类型()如,硬件设备用D3DDEVTYPE_HAL,或D3DDEVTYPE_REF代表软件设备。  
  177.        hFocusWindow:与设备相关的窗口句柄。通常情况下是指设备所要进行绘制的目标窗口。  
  178.        为了达到预期的目的,该句柄与D3DPRESENT_PARAMETER结构的数据成员hDeviceWindow应为同一个句柄。  
  179.        BehaviorFlags:该参数可为D3DCREATE_HARDWARE_VERTEXPROCESSING或D3DCREATE_SOFTWARE_VERTEXPROCESSING.  
  180.        pPresentationParameters:一个已经完成初始化的D3DPRESENT_PARAMETERS类型的实例,该实例定义了设备的一些特性。  
  181.        ppReturnedDeviceInterface:返回所创建的设备。  
  182. */  
  183.   if(FAILED(_d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,     
  184.    hwnd, vp, &d3dpp, device)))    
  185.            return FALSE;    
  186.   
  187.       _d3d9->Release();  
  188.   
  189.       return TRUE;  
  190.   
  191.   
  192. }  
  193.   
  194. int d3d::EnterMsgLoop( bool (*ptr_display)(float timeDelta) )  
  195. {  
  196.     MSG msg;  
  197.     ::ZeroMemory(&msg, sizeof(MSG));  
  198.   
  199.     static float lastTime = (float)timeGetTime();   
  200.   
  201.     while(msg.message != WM_QUIT)  
  202.     {  
  203.         if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))  
  204.         {  
  205.             ::TranslateMessage(&msg);  
  206.             ::DispatchMessage(&msg);  
  207.         }  
  208.         else  
  209.         {     
  210.             float currTime  = (float)timeGetTime();  
  211.             float timeDelta = (currTime - lastTime)*0.001f;  
  212.   
  213.             ptr_display(timeDelta);  
  214.   
  215.             lastTime = currTime;  
  216.         }  
  217.     }  
  218.     return msg.wParam;  
  219. }  
  220.   
  221.   
  222. D3DLIGHT9 d3d::InitDirectionalLight(D3DXVECTOR3* direction, D3DXCOLOR* color)  
  223. {  
  224.     D3DLIGHT9 light;  
  225.     ::ZeroMemory(&light, sizeof(light));  
  226.       
  227.     light.Type      = D3DLIGHT_DIRECTIONAL;  
  228.     light.Ambient   = *color * 0.4f;  
  229.     light.Diffuse   = *color;  
  230.     light.Specular  = *color * 0.6f;  
  231.     light.Direction = *direction;  
  232.       
  233.     return light;  
  234. }  
  235.   
  236. D3DLIGHT9 d3d::InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color)  
  237. {  
  238.     D3DLIGHT9 light;  
  239.     ::ZeroMemory(&light, sizeof(light));  
  240.       
  241.     light.Type      = D3DLIGHT_POINT;  
  242.     light.Ambient   = *color * 0.4f;  
  243.     light.Diffuse   = *color;  
  244.     light.Specular  = *color * 0.6f;  
  245.     light.Position  = *position;  
  246.     light.Range        = 1000.0f;  
  247.     light.Falloff      = 1.0f;  
  248.     light.Attenuation0 = 1.0f;  
  249.     light.Attenuation1 = 0.0f;  
  250.     light.Attenuation2 = 0.0f;  
  251.       
  252.     return light;  
  253. }  
  254.   
  255. D3DLIGHT9 d3d::InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color)  
  256. {  
  257.     D3DLIGHT9 light;  
  258.     ::ZeroMemory(&light, sizeof(light));  
  259.       
  260.     light.Type      = D3DLIGHT_SPOT;  
  261.     light.Ambient   = *color * 0.4f;  
  262.     light.Diffuse   = *color;  
  263.     light.Specular  = *color * 0.6f;  
  264.     light.Position  = *position;  
  265.     light.Direction = *direction;  
  266.     light.Range        = 1000.0f;  
  267.     light.Falloff      = 1.0f;  
  268.     light.Attenuation0 = 1.0f;  
  269.     light.Attenuation1 = 0.0f;  
  270.     light.Attenuation2 = 0.0f;  
  271.     light.Theta        = 0.5f;  
  272.     light.Phi          = 0.7f;  
  273.       
  274.     return light;  
  275. }  
  276.   
  277. D3DMATERIAL9 d3d::InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p)  
  278. {  
  279.     D3DMATERIAL9 mtrl;  
  280.     mtrl.Ambient  = a;  
  281.     mtrl.Diffuse  = d;  
  282.     mtrl.Specular = s;  
  283.     mtrl.Emissive = e;  
  284.     mtrl.Power    = p;  
  285.     return mtrl;  
  286. }  
  287.   
  288. d3d::BoundingBox::BoundingBox()  
  289. {  
  290.     // infinite small   
  291.     _min.x = d3d::INFINITY;  
  292.     _min.y = d3d::INFINITY;  
  293.     _min.z = d3d::INFINITY;  
  294.       
  295.     _max.x = -d3d::INFINITY;  
  296.     _max.y = -d3d::INFINITY;  
  297.     _max.z = -d3d::INFINITY;  
  298. }  
  299.   
  300. bool d3d::BoundingBox::isPointInside(D3DXVECTOR3& p)  
  301. {  
  302.     if( p.x >= _min.x && p.y >= _min.y && p.z >= _min.z &&  
  303.         p.x <= _max.x && p.y <= _max.y && p.z <= _max.z )  
  304.     {  
  305.         return true;  
  306.     }  
  307.     else  
  308.     {  
  309.         return false;  
  310.     }  
  311. }  
  312.   
  313. d3d::BoundingSphere::BoundingSphere()  
  314. {  
  315.     _radius = 0.0f;  
  316. }  
  317.   
  318.   
  319. bool d3d::DrawBasicScene(IDirect3DDevice9* device, float scale)  
  320. {  
  321.     static IDirect3DVertexBuffer9* floor  = 0;  
  322.     static IDirect3DTexture9*      tex    = 0;  
  323.     static ID3DXMesh*              pillar = 0;  
  324.   
  325.     HRESULT hr = 0;  
  326.   
  327.     if( device == 0 )  
  328.     {  
  329.         if( floor && tex && pillar )  
  330.         {  
  331.             // they already exist, destroy them  
  332.             d3d::Release<IDirect3DVertexBuffer9*>(floor);  
  333.             d3d::Release<IDirect3DTexture9*>(tex);  
  334.             d3d::Release<ID3DXMesh*>(pillar);  
  335.         }  
  336.     }  
  337.     else if( !floor && !tex && !pillar )  
  338.     {  
  339.         // they don't exist, create them  
  340.         device->CreateVertexBuffer(  
  341.             6 * sizeof(d3d::Vertex),  
  342.             0,   
  343.             d3d::Vertex::FVF,  
  344.             D3DPOOL_MANAGED,  
  345.             &floor,  
  346.             0);  
  347.   
  348.         Vertex* v = 0;  
  349.         floor->Lock(0, 0, (void**)&v, 0);  
  350.   
  351.         v[0] = Vertex(-20.0f, -2.5f, -20.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);  
  352.         v[1] = Vertex(-20.0f, -2.5f,  20.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);  
  353.         v[2] = Vertex( 20.0f, -2.5f,  20.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);  
  354.   
  355.         v[3] = Vertex(-20.0f, -2.5f, -20.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);  
  356.         v[4] = Vertex( 20.0f, -2.5f,  20.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);  
  357.         v[5] = Vertex( 20.0f, -2.5f, -20.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f);  
  358.   
  359.         floor->Unlock();  
  360.   
  361.         D3DXCreateCylinder(device, 0.5f, 0.5f, 5.0f, 20, 20, &pillar, 0);  
  362.   
  363.         D3DXCreateTextureFromFile(  
  364.             device,  
  365.             "desert.bmp",  
  366.             &tex);  
  367.     }  
  368.     else  
  369.     {  
  370.         //  
  371.         // Pre-Render Setup  
  372.         //  
  373.         device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);  
  374.         device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);  
  375.         device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);  
  376.   
  377.         D3DXVECTOR3 dir(0.707f, -0.707f, 0.707f);  
  378.         D3DXCOLOR col(1.0f, 1.0f, 1.0f, 1.0f);  
  379.         D3DLIGHT9 light = d3d::InitDirectionalLight(&dir, &col);  
  380.   
  381.         device->SetLight(0, &light);  
  382.         device->LightEnable(0, true);  
  383.         device->SetRenderState(D3DRS_NORMALIZENORMALS, true);  
  384.         device->SetRenderState(D3DRS_SPECULARENABLE, true);  
  385.   
  386.         //  
  387.         // Render  
  388.         //  
  389.   
  390.         D3DXMATRIX T, R, P, S;  
  391.   
  392.         D3DXMatrixScaling(&S, scale, scale, scale);  
  393.   
  394.         // used to rotate cylinders to be parallel with world's y-axis  
  395.         D3DXMatrixRotationX(&R, -D3DX_PI * 0.5f);  
  396.   
  397.         // draw floor  
  398.         D3DXMatrixIdentity(&T);  
  399.         T = T * S;  
  400.         device->SetTransform(D3DTS_WORLD, &T);  
  401.         device->SetMaterial(&d3d::WHITE_MTRL);  
  402.         device->SetTexture(0, tex);  
  403.         device->SetStreamSource(0, floor, 0, sizeof(Vertex));  
  404.         device->SetFVF(Vertex::FVF);  
  405.         device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);  
  406.           
  407.         // draw pillars  
  408.         device->SetMaterial(&d3d::BLUE_MTRL);  
  409.         device->SetTexture(0, 0);  
  410.         for(int i = 0; i < 5; i++)  
  411.         {  
  412.             D3DXMatrixTranslation(&T, -5.0f, 0.0f, -15.0f + (i * 7.5f));  
  413.             P = R * T * S;  
  414.             device->SetTransform(D3DTS_WORLD, &P);  
  415.             pillar->DrawSubset(0);  
  416.   
  417.             D3DXMatrixTranslation(&T, 5.0f, 0.0f, -15.0f + (i * 7.5f));  
  418.             P = R * T * S;  
  419.             device->SetTransform(D3DTS_WORLD, &P);  
  420.             pillar->DrawSubset(0);  
  421.         }  
  422.     }  
  423.     return true;  
  424. }  


最后来看wmain.cpp的代码:

[plain]  view plain copy print ?
  1. #include "DirectX3D.h"  
  2. #include <fstream>  
  3. #include <vector>  
  4. //  
  5. // Globals  
  6. //  
  7.   
  8. IDirect3DDevice9* Device = 0;   
  9.   
  10. const int Width  = 640;  
  11. const int Height = 480;  
  12.   
  13. ID3DXMesh* Teapot = 0;  
  14. ID3DXMesh* Sphere = 0;  
  15.   
  16. D3DXMATRIX World;  
  17. d3d::BoundingSphere BSphere;  
  18.   
  19. //  
  20. // Framework Functions  
  21. //  
  22. //  
  23. // Functions  
  24. //  
  25. d3d::Ray CalcPickingRay(int x, int y)  
  26. {  
  27.     float px = 0.0f;  
  28.     float py = 0.0f;  
  29.       
  30.     //得到视口的指针  
  31.     D3DVIEWPORT9 vp;  
  32.     Device->GetViewport(&vp);  
  33.       
  34.     //进行投影变换  
  35.     D3DXMATRIX proj;  
  36.     Device->GetTransform(D3DTS_PROJECTION, &proj);  
  37.       
  38.     px = ((( 2.0f*x) / vp.Width)  - 1.0f) / proj(0, 0);  
  39.     py = (((-2.0f*y) / vp.Height) + 1.0f) / proj(1, 1);  
  40.       
  41.     d3d::Ray ray;  
  42.     ray._origin    = D3DXVECTOR3(0.0f, 0.0f, 0.0f);  
  43.     ray._direction = D3DXVECTOR3(px, py, 1.0f);  
  44.       
  45.     return ray;  
  46. }  
  47.   
  48. void TransformRay(d3d::Ray* ray, D3DXMATRIX* T)  
  49. {  
  50.     // transform the ray's origin, w = 1.  
  51.     /*函数D3DXVec3TransformCoord,和函数D3DXVec3TransformNormal均已3D向量作为其参数,但要注意,使用D3DXVec3TransformCoord  
  52.     时,向量参数的第4个分量应理解为1,D3DXVec3TransformNormal时,第4个参数应理解为0,所以我们用D3DXVec3TransformCoord来实现  
  53.     点的变换,D3DXVec3TransformNormal来实现向量的变换*/  
  54.     D3DXVec3TransformCoord(  
  55.         &ray->_origin,  
  56.         &ray->_origin,  
  57.         T);  
  58.       
  59.     // transform the ray's direction, w = 0.  
  60.     D3DXVec3TransformNormal(  
  61.         &ray->_direction,  
  62.         &ray->_direction,  
  63.         T);  
  64.       
  65.     // normalize the direction  
  66.     D3DXVec3Normalize(&ray->_direction, &ray->_direction);  
  67. }  
  68.   
  69. bool RaySphereIntTest(d3d::Ray* ray, d3d::BoundingSphere* sphere)  
  70. {  
  71.     D3DXVECTOR3 v = ray->_origin - sphere->_center;  
  72.       
  73.     float b = 2.0f * D3DXVec3Dot(&ray->_direction, &v);  
  74.     float c = D3DXVec3Dot(&v, &v) - (sphere->_radius * sphere->_radius);  
  75.       
  76.     // find the discriminant  
  77.     float discriminant = (b * b) - (4.0f * c);  
  78.       
  79.     // test for imaginary number  
  80.     if( discriminant < 0.0f )  
  81.         return false;  
  82.       
  83.     discriminant = sqrtf(discriminant);  
  84.       
  85.     float s0 = (-b + discriminant) / 2.0f;  
  86.     float s1 = (-b - discriminant) / 2.0f;  
  87.       
  88.     // if a solution is >= 0, then we intersected the sphere  
  89.     if( s0 >= 0.0f || s1 >= 0.0f )  
  90.         return true;  
  91.       
  92.     return false;  
  93. }  
  94.   
  95.   
  96. bool Setup()  
  97. {  
  98.     //  
  99.     // 创建茶壶  
  100.     //  
  101.   
  102.     D3DXCreateTeapot(Device, &Teapot, 0);  
  103.   
  104.     //  
  105.     //通过网格的顶点得到外接球的尺寸  
  106.     //  
  107.   
  108.     BYTE* v = 0;  
  109.     Teapot->LockVertexBuffer(0, (void**)&v);  
  110.       
  111.     D3DXComputeBoundingSphere(  
  112.         (D3DXVECTOR3*)v,  
  113.         Teapot->GetNumVertices(),  
  114.         D3DXGetFVFVertexSize(Teapot->GetFVF()),  
  115.         &BSphere._center,  
  116.         &BSphere._radius);  
  117.       
  118.     Teapot->UnlockVertexBuffer();  
  119.   
  120.     //  
  121.     // 创建外接球  
  122.     //  
  123.       
  124.     D3DXCreateSphere(Device, BSphere._radius, 20, 20, &Sphere, 0);  
  125.       
  126.     //  
  127.     // 设置光照  
  128.     //  
  129.       
  130.     D3DXVECTOR3 dir(0.707f, -0.0f, 0.707f);  
  131.     D3DXCOLOR col(1.0f, 1.0f, 1.0f, 1.0f);  
  132.     D3DLIGHT9 light = d3d::InitDirectionalLight(&dir, &col);  
  133.       
  134.     Device->SetLight(0, &light);  
  135.     Device->LightEnable(0, true);  
  136.     Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);  
  137.     Device->SetRenderState(D3DRS_SPECULARENABLE, false);   
  138.       
  139.     //  
  140.     // 设置观察矩阵,进行取景变换  
  141.     //  
  142.       
  143.     D3DXVECTOR3 pos(0.0f, 0.0f, -10.0f);  
  144.     D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);  
  145.     D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);  
  146.       
  147.     D3DXMATRIX V;  
  148.     D3DXMatrixLookAtLH(&V, &pos, &target, &up);  
  149.     Device->SetTransform(D3DTS_VIEW, &V);  
  150.       
  151.     //  
  152.     // 设置投影矩阵  
  153.     //  
  154.       
  155.     D3DXMATRIX proj;  
  156.     D3DXMatrixPerspectiveFovLH(  
  157.         &proj,  
  158.         D3DX_PI * 0.25f, // 45 - degree  
  159.         (float)Width / (float)Height,  
  160.         1.0f,  
  161.         1000.0f);  
  162.     Device->SetTransform(D3DTS_PROJECTION, &proj);  
  163.     return true;  
  164. }  
  165. //将之前分配的内存进行清理,也就是顶点缓存和索引缓存  
  166. void Cleanup()  
  167. {  
  168.   
  169. d3d::Release<ID3DXMesh*>(Teapot);  
  170. d3d::Release<ID3DXMesh*>(Sphere);  
  171. }  
  172.   
  173.   
  174. bool Display(float timeDelta)  
  175. {  
  176.     if( Device )  
  177.     {  
  178.           
  179.   
  180.         //  
  181.         // Update: Update Teapot.  
  182.         //  
  183.           
  184.         static float r     = 0.0f;  
  185.         static float v     = 1.0f;  
  186.         static float angle = 0.0f;  
  187.           
  188.         D3DXMatrixTranslation(&World, cosf(angle) * r, sinf(angle) * r, 10.0f);  
  189.           
  190.         // 不断的修改,外接球的球心位置  
  191.         //   
  192.         BSphere._center = D3DXVECTOR3(cosf(angle)*r, sinf(angle)*r, 10.0f);  
  193.           
  194.         r += v * timeDelta;  
  195.           
  196.         if( r >= 8.0f )  
  197.             v = -v; // reverse direction  
  198.           
  199.         if( r <= 0.0f )  
  200.             v = -v; // reverse direction  
  201.           
  202.         angle += 1.0f * D3DX_PI * timeDelta;  
  203.         if( angle >= D3DX_PI * 2.0f )  
  204.             angle = 0.0f;  
  205.           
  206.         //  
  207.         // Render  
  208.         //  
  209.           
  210.         Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);  
  211.         Device->BeginScene();  
  212.           
  213.         //设置茶壶的材质,并绘制茶壶  
  214.         Device->SetTransform(D3DTS_WORLD, &World);  
  215.         Device->SetMaterial(&d3d::YELLOW_MTRL);  
  216.         Teapot->DrawSubset(0);  
  217.           
  218.         // 将外接球和茶壶进行融合  
  219.         Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);  
  220.         Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);  
  221.         Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);  
  222.           
  223.         //设置融合后的物体的材质,并进行绘制  
  224.         D3DMATERIAL9 blue = d3d::BLUE_MTRL;  
  225.         blue.Diffuse.a = 0.25f; // 25% opacity  
  226.         Device->SetMaterial(&blue);  
  227.         Sphere->DrawSubset(0);  
  228.           
  229.         Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);  
  230.           
  231.         Device->EndScene();  
  232.         Device->Present(0, 0, 0, 0);  
  233.     }  
  234.     return true;  
  235. }     
  236.   
  237. //  
  238. // WndProc  
  239. //  
  240. LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)  
  241. {  
  242.     switch( msg )  
  243.     {  
  244.     case WM_DESTROY:  
  245.         ::PostQuitMessage(0);  
  246.         break;  
  247.           
  248.     case WM_KEYDOWN:  
  249.         if( wParam == VK_ESCAPE )  
  250.             ::DestroyWindow(hwnd);  
  251. case WM_LBUTTONDOWN:  
  252.       
  253.     // 通过鼠标按下时,点的坐标,计算出射线,我们用世界坐标系中的原点为射线的起点  
  254.     d3d::Ray ray = CalcPickingRay(LOWORD(lParam), HIWORD(lParam));  
  255.       
  256.     // transform the ray to world space  
  257.     D3DXMATRIX view;  
  258.     Device->GetTransform(D3DTS_VIEW, &view);  
  259.       
  260.     D3DXMATRIX viewInverse;  
  261.     D3DXMatrixInverse(&viewInverse, 0, &view);  
  262.       
  263.     TransformRay(&ray, &viewInverse);  
  264.       
  265.     // test for a hit  
  266.     if( RaySphereIntTest(&ray, &BSphere) )  
  267.         ::MessageBox(0, "Hit!", "HIT", 0);  
  268.       
  269.         break;  
  270.     }  
  271.     return ::DefWindowProc(hwnd, msg, wParam, lParam);  
  272. }  
  273.   
  274. //  
  275. // WinMain  
  276. //  
  277. int WINAPI WinMain(HINSTANCE hinstance,  
  278.                    HINSTANCE prevInstance,   
  279.                    PSTR cmdLine,  
  280.                    int showCmd)  
  281. {  
  282.     if(!d3d::InitD3D(hinstance,  
  283.         640, 480, true, D3DDEVTYPE_HAL, &Device))  
  284.     {  
  285.         ::MessageBox(0, "InitD3D() - FAILED", 0, 0);  
  286.         return 0;  
  287.     }  
  288.       
  289.     if(!Setup())  
  290.     {  
  291.         ::MessageBox(0, "Setup() - FAILED", 0, 0);  
  292.         return 0;  
  293.     }  
  294.       
  295.     d3d::EnterMsgLoop( Display );  
  296.       
  297.     Cleanup();  
  298.       
  299.     Device->Release();  
  300.       
  301.     return 0;  
  302. }  
  303. bool ComputeBoundingSphere(ID3DXMesh* mesh, d3d::BoundingSphere* sphere)  
  304. {  
  305.     HRESULT hr = 0;  
  306.       
  307.     BYTE* v = 0;  
  308.     mesh->LockVertexBuffer(0, (void**)&v);//得到网格的顶点缓存  
  309.   
  310.     /*  
  311.     我们来看一下D3DX库提供的用来计算一个网格的外接球的函数  
  312.     HRESULT  D3DXComputeBoundingSphere(  
  313.     __in  const D3DXVECTOR3 *pFirstPosition,  
  314.     __in  DWORD NumVertices,  
  315.     __in  DWORD dwStride,  
  316.     __in  D3DXVECTOR3 *pCenter,  
  317.     __in  FLOAT *pRadius  
  318.     );  
  319.       
  320.     pFirstPosition:指向顶点数组(该数组的每个元素都描述了对应顶点)中第一个顶点的位置向量的指针。我们可以通过网格对象得到顶点缓存的指针,最后  
  321.                     可转化为该值。  
  322.     NumVertices:该网格中顶点数组中顶点的个数。可通过网格mesh->GetNumVertices()得到  
  323.     dsStride:每个顶点的大小,单位为字节。该值很重要,因为一种顶点结构可能包含了许多该函数所不需要的附加信息,如法向量和纹理坐标等。这样该函数  
  324.              就需要知道应跳过多少字节才能找到下一个顶点的位置。mesh->GetFVF()可以返回一个描述了顶点格式的DWORD类型值。D3DXGetFVFVertexSize这个  
  325.              函数可以得到该顶点占用多少个字节。  
  326.                
  327.     pCenter:返回的外接球的球心位置。  
  328.     pRadius: 返回外接球的半径。  
  329.       
  330.     */  
  331.   
  332.       
  333.     hr = D3DXComputeBoundingSphere(  
  334.         (D3DXVECTOR3*)v,  
  335.         mesh->GetNumVertices(),  
  336.         D3DXGetFVFVertexSize(mesh->GetFVF()),  
  337.         &sphere->_center,  
  338.         &sphere->_radius);  
  339.       
  340.     mesh->UnlockVertexBuffer();  
  341.       
  342.     if( FAILED(hr) )  
  343.         return false;  
  344.       
  345.     return true;  
  346. }  
  347.   
  348. bool ComputeBoundingBox(ID3DXMesh* mesh, d3d::BoundingBox* box)  
  349. {  
  350.     HRESULT hr = 0;  
  351.       
  352.     BYTE* v = 0;  
  353.     mesh->LockVertexBuffer(0, (void**)&v);  
  354.   
  355.     //这里是D3DX中计算一个网格外接体的函数,跟外接球的函数很类似,只是最后两个参数,返回外接体的最大点和最小点。  
  356.       
  357.     hr = D3DXComputeBoundingBox(  
  358.         (D3DXVECTOR3*)v,  
  359.         mesh->GetNumVertices(),  
  360.         D3DXGetFVFVertexSize(mesh->GetFVF()),  
  361.         &box->_min,  
  362.         &box->_max);  
  363.       
  364.     mesh->UnlockVertexBuffer();  
  365.       
  366.     if( FAILED(hr) )  
  367.         return false;  
  368.       
  369.     return true;  
  370. }  


最后来看程序运行时的截图:

 

每日总结:

拾取是一项根据用户在屏幕上单击的位置(3D物体在屏幕上的投影)来确定用户是否选中以及选中哪个3D物体的技术。

如果一条射线的起点与观察坐标系的原点重合,且经过用户所单击的屏幕点,则该射线即对应一条拾取射线。

要判断射线与某物体是否相交,可测试射线是否与构成物体的某一面片相交或射线是否与该物体的外接体相交。


二、下面来自:http://www.cppblog.com/lovedday/archive/2008/04/04/46264.html

假设用户点击了屏幕上的点 s (x, y)。 从图15.1我们能看到用户选取了茶壶。无论如何,应用程序无法根据给定的s点就立即确定茶壶是被选取。

Directx中的拾取方法_第1张图片

我们知道一些知识:关于茶壶和它的关联点s,茶壶投影在围绕s点的区域,更准确的说是:它投影到投影窗口上围绕p点的区域,与它对应的屏幕点是s。因为这个问题依赖于3D物体与它的投影之间的关系,我们看图15.2就可以了解。

Directx中的拾取方法_第2张图片

图15.2我们看到如果我们发射一条选取射线,从原点发出,经过点p,会与围绕p点投影的对象相交,即茶壶。所以一旦我们计算选取射线,我们可以遍例场景中的每个对象并测试,看射线是否与它相交。与射线相交的对象即是用户选择的对象,在这个例子中用户选取的对象是茶壶。

上面的例子讲解了点s与茶壶的关系。通常我们任意点击屏幕上的点,我们遍例场景中的每个对象,如果对象与射线相交,那么这个对象就是用户选取的对象。例如,图15.1中,如果用户没有点击5个对象中的一个,而是点击了白色的背景区域,射线将不能相交任何对象。因此,结论是:如果射线没有与场景中的任何对象相交,则用户没有点击任何一个对象,其它的我们不关心。

“选取”适用于所有种类的游戏和3D程序。例如,玩家通过用鼠标点击来影响3D世界中的不同对象,玩家可能点击向敌人射击,或点击拾取物品。好的程序会适当做出反应,程序需要知道哪个对象被选取(是敌人还是物品),和在3D空间中的位置(开枪会击中哪?或玩家将要移动到哪去拾取物品?)。选取回答了我们这些问题。

我们将选取分解成四步:

1)       给一个屏幕点s,找出它在投影窗口上相交的点,即p

2)       计算射线,它是从原点出发并经过点p

3)       转换射线与模型到同一空间。

4)       测试与射线相交的对象,相交的对象即是屏幕上点击的对象。


15.1屏幕到投影窗口的转换

首先,转换屏幕点到投影窗口,视口变换矩阵是:

Directx中的拾取方法_第3张图片

因为前面的定义,投影窗口就是z=1的平面,所以pz = 1。

投影矩阵缩放投影窗口上的点,来模拟不同的视角。为了返回缩放前的点值,我们必须用与缩放相反的操作来转换点。P是投影矩阵,因为P00和 P11转换距阵缩放点的x和y坐标,我们得到:

Directx中的拾取方法_第4张图片


15.2计算射线

回忆一下,射线能够描述参数方程:p(t) = p0 + tu。其中p0是射线的起点,用来描述它的位置,u是向量,用来描述它的方向。

如图15.2,我们知道射线的起点总是视图空间的原点,所以p0 = (0, 0, 0),如果p是射线穿过投影窗口上的点,方向向量u给出:u = p - p0 = (px, py, 1) - (0, 0, 0) = p

下面的方法用来计算选取射线(从屏幕空间点击的点所对应的视图空间的点x、y坐标):

struct sRay
{
    D3DXVECTOR3 origin;
    D3DXVECTOR3 direction;
};

sRay calculate_picking_ray(int x, int y)
{
        D3DVIEWPORT9 viewport;
        g_device->GetViewport(&viewport);
    
        D3DXMATRIX proj_matrix;
        g_device->GetTransform(D3DTS_PROJECTION, &proj_matrix);
    
        
float px = ((( 2.0f * x) / viewport.Width)  - 1.0f) / proj_matrix(0, 0);
        
float py = (((-2.0f * y) / viewport.Height) + 1.0f) / proj_matrix(1, 1);
    
        sRay ray;
        ray.origin      = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
        ray.direction = D3DXVECTOR3(px, py, 1.0f);
    
        
return ray;

}

15.3变换射线

选取射线的计算被描述在视图空间,为了完成射线的相交的测试,射线和对象必须在同一个坐标系统。通常转换射线到世界空间(甚至对象在本地空间)要好于将所有对象转换到视图空间。

我们能够将一个变换矩阵转换为一条原点为p0,方向为u的射线r(t) = p0 + tu,注意:原点转换为一个点,方向转换为一个向量,下列函数转换一条射线:

     void  transform_ray(sRay* ray, D3DXMATRIX* trans_matrix)
    {
        
// transform the ray's origin, w = 1.
    
        D3DXVec3TransformCoord(&ray->origin, &ray->origin, trans_matrix);
    
        
// transform the ray's direction, w = 0.
    
        D3DXVec3TransformNormal(&ray->direction, &ray->direction, trans_matrix);
    
        
// normalize the direction
    
        D3DXVec3Normalize(&ray->direction, &ray->direction);
    }

D3DXVec3TransformCoord和D3DXVec3TransformNormal接受一个Ray类型参数(包含二个3D向量成员)。 D3DXVec3TransformCoord函数中,射线的原点(origin)向量的第四部分w = 1。相反,函数D3DXVec3TransformNormal中,射线的方向(direction)向量的第四部分w = 0。

这样,当我们向世界空间转换时,能够用D3DXVec3TransformCoord转换一个点,用D3DXVec3TransformNormal转换一个向量。


15.4射线-对象 交点

我们将射线和对象转换到同一坐标系统后,准备测试哪个对象与射线相交。因为我们将对象描述为三角形组成的网络,下面详细说明这种方法。遍例场景中每个对象的三角形列表并测试,如果射线相交于一个三角形,它就与三角形所在的对象相交。然而,通过遍例场景中的每个三角形来实现射线相交在计算上会增加时间,一种比较快的方法,虽然准确性会差一点。它将每个对象围成一个近似的球形(边界球),这样我们就能通过遍例每个边界球来测试射线相交。用边界球来描述相交的对象。

注意:射线可能相交多个对象,然而离照相机近的对象会被选取。因为近距离对象遮挡了后面的对象

给出一个边界球的圆心c和半径r,使用下列恒等式能够测试点p是否在边界球上:

||p-c||-r = 0

如果恒等式满足,则点p在边界球上。如图15.3

Directx中的拾取方法_第5张图片

假定射线p(t) = p0 + tu相交于边界球,我们将射线代入球的恒等式中,使参数t满足了球的恒等式。

将射线p(t) = p0 + tu代入球的恒等式:

||p(t) - c|| r = 0   à  ||p0 + tu - c|| r = 0

通过以上推导,我们得到二次方程:

At2 + Bt + C = 0

其中A = · uB = 2(u · (p0 - c)),而C = (p0 - c) . (p0 - c) – 2

如果u是标准化的,那么A = 1。

因为u是标准化的,我们解t0和 t1

图15.4显示可能返回的t0和 t1,并显示了一些返回值的几何意义:

Directx中的拾取方法_第6张图片

下列函数测试如果射线与边界球相交,返回true;射线错过边界球,返回false。

     bool  ray_sphere_intersect(sRay* ray, cBoundingSphere* sphere)
    {
        D3DXVECTOR3 v = ray->origin - sphere->m_center;
    
        
float  b = 2.0f * D3DXVec3Dot(&ray->direction, &v);
        
float  c = D3DXVec3Dot(&v, &v) - (sphere->m_radius * sphere->m_radius);
    
        
float  discriminant = (b * b) - (4.0f * c);
    
        
if (discriminant < 0.0f)
            
return   false ;
    
        discriminant = sqrt(discriminant);
    
        
float  s0 = (-b + discriminant) / 2.0f;
        
float  s1 = (-b - discriminant) / 2.0f;
    
        
// if one solution is >= 0, then we intersected the sphere.
    
     return  (s0 >= 0.0f || s1 >= 0.0f);
    }

15.5例子程序:选取

下图显示了该示例的屏幕截图,茶壶绕着屏幕移动,你可以用鼠标试着点击它。如果你点击到茶壶的边界球上,一个消息框将弹出,表示你点中了。我们通过测试WM_LBUTTONDOWN消息来处理鼠标点击事件。

Directx中的拾取方法_第7张图片

执行程序:

     #include "d3dUtility.h"
    
    #pragma warning(disable : 4100)
    
    
const   int  WIDTH  = 640;
    
const   int  HEIGHT = 480;
    
    IDirect3DDevice9*    g_device;
    ID3DXMesh*            g_teapot;
    ID3DXMesh*            g_sphere;
    
    D3DXMATRIX            g_world_matrix;
    cBoundingSphere        g_bounding_sphere;
    
    
    /////////////////////////////////////////////////////////////////////////////////////////////////// /
    

    sRay calculate_picking_ray(
int  x,  int  y)
    {
        D3DVIEWPORT9 viewport;
        g_device->GetViewport(&viewport);
    
        D3DXMATRIX proj_matrix;
        g_device->GetTransform(D3DTS_PROJECTION, &proj_matrix);
    
        
float  px = ((( 2.0f * x) / viewport.Width)  - 1.0f) / proj_matrix(0, 0);
        
float  py = (((-2.0f * y) / viewport.Height) + 1.0f) / proj_matrix(1, 1);
    
        sRay ray;
        ray.origin      = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
        ray.direction = D3DXVECTOR3(px, py, 1.0f);
    
        
return  ray;
    }
    
    
void  transform_ray(sRay* ray, D3DXMATRIX* trans_matrix)
    {
        
// transform the ray's origin, w = 1.
    
        D3DXVec3TransformCoord(&ray->origin, &ray->origin, trans_matrix);
    
        
// transform the ray's direction, w = 0.
    
        D3DXVec3TransformNormal(&ray->direction, &ray->direction, trans_matrix);
    
        
// normalize the direction
    
        D3DXVec3Normalize(&ray->direction, &ray->direction);
    }
    
    
bool  ray_sphere_intersect(sRay* ray, cBoundingSphere* sphere)
    {
        D3DXVECTOR3 v = ray->origin - sphere->m_center;
    
        
float  b = 2.0f * D3DXVec3Dot(&ray->direction, &v);
        
float  c = D3DXVec3Dot(&v, &v) - (sphere->m_radius * sphere->m_radius);
    
        
float  discriminant = (b * b) - (4.0f * c);
    
        
if (discriminant < 0.0f)
            
return   false ;
    
        discriminant = sqrt(discriminant);
    
        
float  s0 = (-b + discriminant) / 2.0f;
        
float  s1 = (-b - discriminant) / 2.0f;
    
        
// if one solution is >= 0, then we intersected the sphere.
    
     return  (s0 >= 0.0f || s1 >= 0.0f);
    }
    
    
bool  setup()
    {    
        D3DXCreateTeapot(g_device, &g_teapot, NULL);
    
        
// compute the bounding sphere
    

        BYTE* v;
    
        g_teapot->LockVertexBuffer(0, (
void **)&v);
    
        D3DXComputeBoundingSphere(
            (D3DXVECTOR3*)v,
            g_teapot->GetNumVertices(),
            D3DXGetFVFVertexSize(g_teapot->GetFVF()),
            &g_bounding_sphere.m_center,
            &g_bounding_sphere.m_radius);
    
        g_teapot->UnlockVertexBuffer();
    
        
// build a sphere mesh that describes the teapot's bounding sphere
    
        D3DXCreateSphere(g_device, g_bounding_sphere.m_radius, 20, 20, &g_sphere, NULL);
    
        
// set light
    

        D3DXVECTOR3 dir(0.707f, -0.0f, 0.707f);
        D3DXCOLOR color(1.0f, 1.0f, 1.0f, 1.0f);
        D3DLIGHT9 light = init_directional_light(&dir, &color);
    
        g_device->SetLight(0, &light);
        g_device->LightEnable(0, TRUE);
        g_device->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
        g_device->SetRenderState(D3DRS_SPECULARENABLE,   FALSE);
    
        
// Set view matrix
    

        D3DXVECTOR3 pos(0.0f, 0.0f, -10.0f);
        D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
        D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
    
        D3DXMATRIX view_matrix;
        D3DXMatrixLookAtLH(&view_matrix, &pos, &target, &up);
        g_device->SetTransform(D3DTS_VIEW, &view_matrix);
    
        
// set the projection matrix
    
    D3DXMATRIX proj;
        D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI/4.0f, (
float )WIDTH/HEIGHT, 1.0f, 1000.0f);
        g_device->SetTransform(D3DTS_PROJECTION, &proj);
        
        
return   true ;
    }
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////// /
    

    
void  cleanup()
    {    
        safe_release<ID3DXMesh*>(g_teapot);
        safe_release<ID3DXMesh*>(g_sphere);
    }
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////// /
    

    
bool  display( float  time_delta)
    {
        
// update teapot
    

        
static   float  radius = 0.0f;
        
static   float  angle  = 0.0f;
    
        D3DXMatrixTranslation(&g_world_matrix, cos(angle) * radius, sin(angle) * radius, 10.0f);
    
        
// transform the bounding sphere to match the teapot's position in the world
    
        g_bounding_sphere.m_center = D3DXVECTOR3(cos(angle) * radius, sin(angle) * radius, 10.0f);
    
        
static   float  velocity = 1.0f;
    
        radius += velocity * time_delta;
        
if (radius >= 8.0f || radius <= 0.0f)
            velocity = -velocity;    
// reverse direction
    

        angle += D3DX_PI * time_delta;
        
if (angle >= D3DX_PI * 2.0f)
            angle = 0.0f;
        
        
// render now
    

        g_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
    
        g_device->BeginScene();
    
        g_device->SetTransform(D3DTS_WORLD, &g_world_matrix);
        g_device->SetMaterial(&RED_MATERIAL);
        g_teapot->DrawSubset(0);
    
        
// render the bounding sphere with alpha blending so we can see through it
    
        g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
        g_device->SetRenderState(D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA);
        g_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
    
        D3DMATERIAL9 yellow_material = YELLOW_MATERIAL;
        yellow_material.Diffuse.a = 0.25f;    
// 25% opacity
    
        g_device->SetMaterial(&yellow_material);
        g_sphere->DrawSubset(0);
    
        g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
    
        g_device->EndScene();
    
        g_device->Present(NULL, NULL, NULL, NULL);
    
        
return   true ;
    }
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////// /
    

    LRESULT CALLBACK wnd_proc(HWND hwnd, UINT msg, WPARAM word_param, LPARAM long_param)
    {
        
switch (msg)
        {
        
case  WM_DESTROY:
            PostQuitMessage(0);
            
break ;
    
        
case  WM_KEYDOWN:
            
if (word_param == VK_ESCAPE)
                DestroyWindow(hwnd);
    
            
break ;
    
        
case  WM_LBUTTONDOWN:
            
// compute the ray in view space given by the clicked screen point
    
            sRay ray = calculate_picking_ray(LOWORD(long_param), HIWORD(long_param));
    
            
// transform the ray to world space
    
            D3DXMATRIX view_matrix, view_inverse_matrix;
            g_device->GetTransform(D3DTS_VIEW, &view_matrix);
            D3DXMatrixInverse(&view_inverse_matrix, NULL, &view_matrix);
    
            transform_ray(&ray, &view_inverse_matrix);
    
            
if (ray_sphere_intersect(&ray, &g_bounding_sphere))
                MessageBox(NULL, "Hit teapot's bounding sphere!", "HIT", MB_OK);
    
            
break ;
        }
    
        
return  DefWindowProc(hwnd, msg, word_param, long_param);
    }
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////// /
    

    
int  WINAPI WinMain(HINSTANCE inst, HINSTANCE, PSTR cmd_line,  int  cmd_show)
    {
        
if (! init_d3d(inst, WIDTH, HEIGHT,  true , D3DDEVTYPE_HAL, &g_device))
        {
            MessageBox(NULL, "init_d3d() - failed.", 0, MB_OK);
            
return  0;
        }
    
        
if (! setup())
        {
            MessageBox(NULL, "Steup() - failed.", 0, MB_OK);
            
return  0;
        }
    
        enter_msg_loop(display);
    
        cleanup();
        g_device->Release();
    
        
return  0;
    }

三、下面来自:http://www.cnblogs.com/lancidie/archive/2010/10/08/1845772.html

简单总结一下D3D中的拾取问题,所谓拾取就是3D程序中当用户使用鼠标同3D世界内的物体进行交互的时候,如何能正确的实现从用户的鼠标到3D世界中的变换。 
    呵呵,如果要是推及到原理的话比较复杂,需要好好总结,先从简单的入手,D3D中提供了很多易用的API,使用这些API的话就可以绕过复杂的数学原理,所以呢,我们先来看实际应用中是怎样实现它的。 
    //首先获取世界、视角、投影矩阵 
    D3DXMATRIX matWorld,matView,matProj; 
    pd3dDevice->GetTransform(D3DTS_WORLD,&matWorld); 
    pd3dDevice->GetTransform(D3DTS_VIEW,&matView); 
    pd3dDevice->GetTransform(D3DTS_PROJECTION,&matProj); 
    //获取平面坐标 
    POINT ptCursor; 
    GetCursorPos(&ptCursor); 
    ScreenToClient(DXUTGetHWND(),&ptCursor); 
    D3DXVECTOR3 vScreen((float)ptCursor.x,(float)ptCursor.y,0.0f),vOut; 
    //创建视窗接口 
    D3DVIEWPORT9 viewPort; 
    pd3dDevice->GetViewport( &viewPort ); 
    //从屏幕空间投影到3D空间 
    D3DXVec3Unproject(&vOut,&vScreen,&viewPort,&matProj,&matView,&matWorld); 
    D3DXVECTOR3 vMousePt; 
    D3DXPLANE plane; 
    D3DXVECTOR3 v1(1.0f,1.0f,0.0f); 
    D3DXVECTOR3 v2(1.0f,-1.0f,0.0f); 
    D3DXVECTOR3 v3(-1.0f,1.0f,0.0f); 
    D3DXPlaneFromPoints( &plane,&v1,&v2,&v3); 
    D3DXPlaneIntersectLine(&vMousePt,&plane,pCamera->GetEyePt(),&vOut); 
    //vMousePt.x,vMousePt.y和vMousePt.z就是鼠标根据视角所投射线同所指定平面的交点 
    很简单的一段代码,为了确保它的正确性,测试倒是费了半天劲-。-,全部都是用API来实现的,看程序不难理解,这段程序重点是D3DXVec3Unproject()函数,这个函数的作用便是进行屏幕到空间的投影(还有一个D3DXVec3project()函数是用来进行空间到屏幕的投影的^_^),这个函数需要的是设备的Viewport、决定3D空间的三个矩阵和屏幕的坐标,函数可以算出结果向量,不过测试的时候我发现这个向量好像既不是近裁剪面的投影点也不是远裁剪面的,这个地方确实还是存在疑惑的,不过不影响使用,呵呵。 
    其实有了这个向量后面的怎么用就看大家了,上面是通过D3DXPlaneIntersectLine()函数(计算直线与平面交点用函数)来实现的,还可以用D3DXIntersect()来做与三角面片的相交。 

你可能感兴趣的:(游戏,3D,图形,DirectX,Direct3D)