11.4 外接體(Bounding Volumes)
有時(shí)我們需要計(jì)算mesh的外接體(邊界范圍),常用的有兩種類型:立方體和球。也有使用其它方法的,如圓柱體,橢球體,菱形體,膠囊形。圖11.4演示了對(duì)同一個(gè)mesh分別使用立方體和球體類型。

邊界盒/球常常被用來(lái)加速可見(jiàn)性測(cè)試,碰撞檢測(cè)等。假如一個(gè)mesh的邊界盒/球不可見(jiàn),那么我們就說(shuō)mesh不可見(jiàn)。一個(gè)盒/球可見(jiàn)性測(cè)試是比分別測(cè)試 mesh中的每個(gè)三角形要廉價(jià)的多。對(duì)于一個(gè)碰撞檢測(cè)例子,如果一枚導(dǎo)彈點(diǎn)火起飛,我們需要檢測(cè)它是否擊中了同一場(chǎng)景中的目標(biāo)。由于這些物體都是由大量三角形構(gòu)成,我們可以依次檢測(cè)每個(gè)對(duì)象的每個(gè)三角形,來(lái)測(cè)試導(dǎo)彈(可以用射線數(shù)學(xué)模型)是否碰撞到了這些三角形。這個(gè)方法需要進(jìn)行多次的射線/三角形交點(diǎn)的運(yùn)算。一個(gè)更好的方法是使用邊界盒或邊界球,計(jì)算射線與場(chǎng)景中的每個(gè)對(duì)象的邊界盒/邊界球的交點(diǎn)。如果射線與對(duì)象的邊界范圍相交,可以認(rèn)為該對(duì)象被擊中了。這是一個(gè)公平的近似方法,如果需要更高的精度,可以用邊界范圍法先去除那些明顯不會(huì)相撞的對(duì)象,然后用更精確地方法檢測(cè)很可能相撞的對(duì)象。如果邊界范圍檢測(cè)發(fā)現(xiàn)相撞,則該對(duì)象就很有可能相撞。
D3DX庫(kù)提供了計(jì)算mesh的邊界盒和邊界球的函數(shù)。這些函數(shù)使用頂點(diǎn)數(shù)組作為輸入計(jì)算邊界盒/球。這些函數(shù)本來(lái)就是設(shè)計(jì)的很靈活的,它們可以使用各種頂點(diǎn)格式:
HRESULT D3DXComputeBoundingSphere(
LPD3DXVECTOR3 pFirstPosition,
DWORD NumVertices,
DWORD dwStride,
D3DXVECTOR3* pCenter,
FLOAT* pRadius
);
|
pFirstPosition——指向在頂點(diǎn)數(shù)組中第一個(gè)頂點(diǎn)的向量,描述頂點(diǎn)位置。
NumVertices——在頂點(diǎn)數(shù)組中的的頂點(diǎn)數(shù)。
dwStride——每個(gè)頂點(diǎn)的字節(jié)大小。這很重要,因?yàn)轫旤c(diǎn)結(jié)構(gòu)可能有一些額外信息如法向量和紋理坐標(biāo),函數(shù)需要知道應(yīng)該跳過(guò)多少字節(jié)來(lái)得到下一個(gè)頂點(diǎn)的位置。
pCenter——返回邊界球的中心。
pRadius——返回邊界球的半徑。
HRESULT D3DXComputeBoundingBox(
LPD3DXVECTOR3 pFirstPosition,
DWORD NumVertices,
DWORD dwStride,
D3DXVECTOR3* pMin,
D3DXVECTOR3* pMax
);
|
前三個(gè)參數(shù)和D3DXComputeBoundingSphere的前三個(gè)參數(shù)是完全一樣的。最后兩個(gè)參數(shù)分別用來(lái)返回邊界盒的最小和最大點(diǎn)。< /p>
11.4.1一些新的特殊常量
const float INFINITY = FLT_MAX;
const float EPSILON = 0.001f;
常量INFINITY是用來(lái)表示一個(gè)浮點(diǎn)數(shù)所能存儲(chǔ)的最大數(shù)。因?yàn)槲覀冋也坏揭粋€(gè)比FLT_MAX還要大的浮點(diǎn)數(shù),我們可以將它視為無(wú)窮大。常量 EPSILON是一個(gè)很小的值,我們這樣定義它,凡是比它小的數(shù)就視為0。這也是很有必要的,因?yàn)榈玫降母↑c(diǎn)是不精確的。因此,讓它和0比較相等肯定會(huì)失敗。我們因此可以通過(guò)把該值與0的差值與EPSILON比較來(lái)確定是否相等:
bool Equals(float lhs, float rhs)
{
// if lhs == rhs their difference should be zero
return fabs(lhs - rhs) < EPSILON ? true : false;
}
|
11.4.2外接體類型
為了更容易的使用邊界盒和邊界球,我們將它們分別封裝到兩個(gè)類中。
class cBoundingBox
{
public:
D3DXVECTOR3 m_min, m_max;
cBoundingBox();
bool is_point_inside(D3DXVECTOR3& point);
};
class cBoundingSphere
{
public:
D3DXVECTOR3 m_center;
float m_radius;
cBoundingSphere();
};
cBoundingBox::cBoundingBox()
{
m_min.x = INFINITY;
m_min.y = INFINITY;
m_min.z = INFINITY;
m_max.x = -INFINITY;
m_max.y = -INFINITY;
m_max.z = -INFINITY;
}
bool cBoundingBox::is_point_inside(D3DXVECTOR3 &point)
{
return (point.x >= m_min.x && point.y >= m_min.y && point.z >= m_min.z &&
point.x <= m_max.x && point.y <= m_max.y && point.z <= m_max.z);
}
cBoundingSphere::cBoundingSphere()
{
m_radius = 0.0f;
}
11.4.3實(shí)例程序:外接體
該實(shí)例程序主要演示使用D3DXComputeBoundingSphere和 D3DXComputeBoundingBox。程序讀取一個(gè)X文件并且計(jì)算該mesh的邊界球,它創(chuàng)建兩個(gè)ID3DXMesh對(duì)象,一個(gè)用來(lái)作為邊界球模型一個(gè)用來(lái)作為邊界盒模型(如下圖所示)。你能夠通過(guò)敲空格鍵在邊界球和邊界盒之間切換。


主程序:
/**************************************************************************************
Demonstrates how to use D3DXComputeBoundingSphere and D3DXComputeBoundingBox.
The spacebar key switches between rendering the mesh's bounding sphere and box.
**************************************************************************************/
#include <vector>
#include "d3dUtility.h"
#pragma warning(disable : 4100)
using namespace std;
const int WIDTH = 640;
const int HEIGHT = 480;
IDirect3DDevice9* g_device;
ID3DXMesh* g_mesh;
ID3DXMesh* g_sphere_mesh;
ID3DXMesh* g_box_mesh;
vector<D3DMATERIAL9> g_materials;
vector<IDirect3DTexture9*> g_textures;
bool g_is_render_bounding_sphere = true;
bool compute_bounding_sphere(ID3DXMesh* mesh, cBoundingSphere* sphere);
bool compute_bounding_box(ID3DXMesh* mesh, cBoundingBox* box);
////////////////////////////////////////////////////////////////////////////////////////////////////
bool setup()
{
// load the XFile data
ID3DXBuffer* adjacency_buffer = NULL;
ID3DXBuffer* material_buffer = NULL;
DWORD num_material = 0;
HRESULT hr = D3DXLoadMeshFromX("bigship1.x", D3DXMESH_MANAGED, g_device, &adjacency_buffer, &material_buffer,
NULL, &num_material, &g_mesh);
if(FAILED(hr))
{
MessageBox(NULL, "D3DXLoadMeshFromX() - FAILED", "ERROR", MB_OK);
return false;
}
// extract the materials, and load textures.
if(material_buffer != NULL && num_material != 0)
{
D3DXMATERIAL* materials = (D3DXMATERIAL*) material_buffer->GetBufferPointer();
for(DWORD i = 0; i < num_material; i++)
{
// the MatD3D property doesn't have an ambient value set when it load, so set it now.
materials[i].MatD3D.Ambient = materials[i].MatD3D.Diffuse;
// save the ith material
g_materials.push_back(materials[i].MatD3D);
// check if the ith material has an associative texture
if(materials[i].pTextureFilename != NULL)
{
// yes, load the texture for the ith subset.
IDirect3DTexture9* texture;
D3DXCreateTextureFromFile(g_device, materials[i].pTextureFilename, &texture);
// save the loaded texture
g_textures.push_back(texture);
}
else
{
// no texture for the ith subset
g_textures.push_back(NULL);
}
}
}
safe_release<ID3DXBuffer*>(material_buffer);
// optimize the mesh
hr = g_mesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_COMPACT | D3DXMESHOPT_VERTEXCACHE,
(DWORD*) adjacency_buffer->GetBufferPointer(), NULL, NULL, NULL);
safe_release<ID3DXBuffer*>(adjacency_buffer);
if(FAILED(hr))
{
MessageBox(NULL, "OptimizeInplace() - FAILED", "ERROR", MB_OK);
return false;
}
// compute bounding sphere and bounding box
cBoundingSphere bounding_sphere;
cBoundingBox bounding_box;
compute_bounding_sphere(g_mesh, &bounding_sphere);
compute_bounding_box(g_mesh, &bounding_box);
D3DXCreateSphere(g_device, bounding_sphere.m_radius, 20, 20, &g_sphere_mesh, NULL);
float width = bounding_box.m_max.x - bounding_box.m_min.x;
float height = bounding_box.m_max.y - bounding_box.m_min.y;
float depth = bounding_box.m_max.z - bounding_box.m_min.z;
D3DXCreateBox(g_device, width, height, depth, &g_box_mesh, NULL);
// set texture filters
g_device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
g_device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
g_device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
// set lights
D3DXVECTOR3 dir(1.0f, -1.0f, 1.0f);
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, TRUE);
// set camera
D3DXVECTOR3 pos(4.0f, 12.0f, -20.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 * 0.5f, (float)WIDTH/HEIGHT, 1.0f, 1000.0f);
g_device->SetTransform(D3DTS_PROJECTION, &proj);
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
void cleanup()
{
safe_release<ID3DXMesh*>(g_mesh);
safe_release<ID3DXMesh*>(g_sphere_mesh);
safe_release<ID3DXMesh*>(g_box_mesh);
for(DWORD i = 0; i < g_textures.size(); i++)
safe_release<IDirect3DTexture9*>(g_textures[i]);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
bool display(float time_delta)
{
// update: rotate the mesh
static float y = 0.0f;
D3DXMATRIX y_rot_matrix;
D3DXMatrixRotationY(&y_rot_matrix, y);
g_device->SetTransform(D3DTS_WORLD, &y_rot_matrix);
y += time_delta;
if(y >= 6.28f)
y = 0.0f;
// render now
g_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
g_device->BeginScene();
for(DWORD i = 0; i < g_materials.size(); i++)
{
g_device->SetMaterial(&g_materials[i]);
g_device->SetTexture(0, g_textures[i]);
g_mesh->DrawSubset(i);
}
// draw bounding volume in yellow and at 30% opacity
D3DMATERIAL9 blue_material = YELLOW_MATERIAL;
blue_material.Diffuse.a = 0.30f; // 30% opacity
g_device->SetMaterial(&blue_material);
g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
g_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
if(g_is_render_bounding_sphere)
g_sphere_mesh->DrawSubset(0);
else
g_box_mesh->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);
if(word_param == VK_SPACE)
g_is_render_bounding_sphere = !g_is_render_bounding_sphere;
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;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
bool compute_bounding_sphere(ID3DXMesh* mesh, cBoundingSphere* sphere)
{
BYTE* v;
mesh->LockVertexBuffer(0, (void**)&v);
HRESULT hr = D3DXComputeBoundingSphere((D3DXVECTOR3*)v, mesh->GetNumVertices(),
D3DXGetFVFVertexSize(mesh->GetFVF()), &sphere->m_center, &sphere->m_radius);
mesh->UnlockVertexBuffer();
if(FAILED(hr))
return false;
return true;
}
bool compute_bounding_box(ID3DXMesh* mesh, cBoundingBox* box)
{
BYTE* v;
mesh->LockVertexBuffer(0, (void**)&v);
HRESULT hr = D3DXComputeBoundingBox((D3DXVECTOR3*)v, mesh->GetNumVertices(),
D3DXGetFVFVertexSize(mesh->GetFVF()), &box->m_min, &box->m_max);
mesh->UnlockVertexBuffer();
if(FAILED(hr))
return false;
return true;
}
下載源代碼