1 IUnknown--> ID3DXBUFFER D3D泛型接口:

GetBufferPointer

Retrieves a pointer to the data in the buffer.

GetBufferSize

Retrieves the total size of the data in the buffer.

使用完之后需要进行释放:Release()

2XFILE

//从文件中加载xfile文件

 HRESULT  D3DXLoadMeshFromX(
__in LPCTSTR pFilename, //xfile文件名
__in DWORD Options, //加载可选项
__in LPDIRECT3DDEVICE9 pD3DDevice, //设备
__out LPD3DXBUFFER *ppAdjacency, //临街信息,DWORD数组
__out LPD3DXBUFFER *ppMaterials, //材质信息
__out LPD3DXBUFFER *ppEffectInstances, //
__out DWORD *pNumMaterials, //材质数目
__out LPD3DXMESH *ppMesh
);
//材质结构:ppMaterials结构中第I项 对应第I个子集
typedef struct D3DXMATERIAL {
D3DMATERIAL9 MatD3D;
LPSTR pTextureFilename;
} D3DXMATERIAL, *LPD3DXMATERIAL;
加载时,首先加载XFILE文件,然后遍历纹理数据,将纹理数据加载,在渲染的时候,先设置纹理和纹理
3生成顶点法线
方便使用光照,网格的法向量计算方法
 HRESULT  D3DXComputeNormals(
__inout LPD3DXBASEMESH pMesh, //输出的网格数据
__in const DWORD *pAdjacency //临街数据
);
pMesh参数中顶点格式必须包含D3DFVF_NORMAL,所以需要从原先的mesh数据copy一份过来
4.渐进网格
类似于渐进纹理数据,通过边折叠技术对网格进行简化,并且这种操作时可逆转的
渐进网格从已有的网格数据中产生:
    D3DXGeneratePMesh(
LPD3DXMESH pMesh,
CONST DWORD* pAdjacency, //邻接信息dword数组
CONST D3DXATTRIBUTEWEIGHTS *pVertexAttributeWeights, //指定相应顶点属性权值 涉及被移除的概率
CONST FLOAT *pVertexWeights, //指定顶点权值,涉及被移除的概率
DWORD MinValue, //面片简化的最少值,如果设置为1 表示尽可能的少
DWORD Options, //决定上面的参数为定点数或者面片数
LPD3DXPMESH* ppPMesh);
D3DXATTRIBUTEWEIGHTS 顶点属相权值结构,暂且使用默认值
D3DXPMesh提供的操作接口:
GetMaxFaces() //最多的面片数目上限
GetMinFaces() //最少的面片数目下限
GetMaxVertexs() //最多的顶点数目上限
GetMinVertexs() //最少的顶点数目下限
SetNumFaces() // 设置面片数目,
SetNumVertexs() //设置顶点数目
trimByFaces(newfacemin , newfacemax, --- ., --)  // 重新设置顶点的最大,最小限制
trimByVetex(newfacemin , newfacemax, --- ., --)  // 重新设置顶点的最大,最小限制
5外接体:可以用于检测碰撞以及可见性
//外接球
    D3DXComputeBoundingSphere(
CONST D3DXVECTOR3 *pFirstPosition, // pointer to first position 指向顶点的第一个位置
DWORD NumVertices, 数组中顶点数目
DWORD dwStride, // count in bytes to subsequent position vectors 每个顶点字节数
D3DXVECTOR3 *pCenter, 外界球中心
FLOAT *pRadius); 外接球半径
// 外界体
    D3DXComputeBoundingBox(
CONST D3DXVECTOR3 *pFirstPosition, // pointer to first position,指向顶点的第一个位置
DWORD NumVertices, //顶点数组中的顶点数目
DWORD dwStride, // count in bytes to subsequent position vectors 每个顶点的字节数
D3DXVECTOR3 *pMin, //返回的
D3DXVECTOR3 *pMax);
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// File: boundingvolumes.cpp
//
// Author: Frank Luna (C) All Rights Reserved
//
// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0
//
// Desc: Demonstrates how to use D3DXComputeBoundingSphere and D3DXComputeBoundingBox.
//
// -The spacebar key switches between rendering the mesh's bounding sphere and box.
//
////////////////////////////////////////////////////////////////////////////////////////////////// #include "d3dUtility.h"
#include <vector> //
// Globals
// IDirect3DDevice9* Device = ; const int Width = ;
const int Height = ; ID3DXMesh* Mesh = ;
ID3DXPMesh* pMesh = ;
std::vector<D3DMATERIAL9> Mtrls();
std::vector<IDirect3DTexture9*> Textures(); ID3DXMesh* SphereMesh = ;
ID3DXMesh* BoxMesh = ; bool RenderBoundingSphere = true; //
// Prototypes
// bool ComputeBoundingSphere(ID3DXMesh* mesh, d3d::BoundingSphere* sphere);
bool ComputeBoundingBox(ID3DXMesh* mesh, d3d::BoundingBox* box); //
// Framework functions
//
bool Setup()
{
HRESULT hr = ; //
// Load the XFile data.
//
ID3DXBuffer* adjBuffer = ;
ID3DXBuffer* mtrlBuffer = ;
DWORD numMtrls = ; hr = D3DXLoadMeshFromX(
"bigship1.x",
D3DXMESH_MANAGED,
Device,
&adjBuffer,
&mtrlBuffer,
,
&numMtrls,
&Mesh); if(FAILED(hr))
{
::MessageBox(, "D3DXLoadMeshFromX() - FAILED", , );
return false;
} //
// Extract the materials, load textures.
// if( mtrlBuffer != && numMtrls != )
{
D3DXMATERIAL* mtrls = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer(); for(int i = ; i < numMtrls; i++)
{
// the MatD3D property doesn't have an ambient value set
// when its loaded, so set it now:
mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse; // save the ith material
Mtrls.push_back( mtrls[i].MatD3D ); // check if the ith material has an associative texture
if( mtrls[i].pTextureFilename != )
{
// yes, load the texture for the ith subset
IDirect3DTexture9* tex = ;
D3DXCreateTextureFromFile(
Device,
mtrls[i].pTextureFilename,
&tex); // save the loaded texture
Textures.push_back( tex );
}
else
{
// no texture for the ith subset
Textures.push_back( );
}
}
}
d3d::Release<ID3DXBuffer*>(mtrlBuffer); // done w/ buffer //
// Optimize the mesh.
// hr = Mesh->OptimizeInplace(
D3DXMESHOPT_ATTRSORT |
D3DXMESHOPT_COMPACT |
D3DXMESHOPT_VERTEXCACHE,
(DWORD*)adjBuffer->GetBufferPointer(),
, , ); d3d::Release<ID3DXBuffer*>(adjBuffer); // done w/ buffer if(FAILED(hr))
{
::MessageBox(, "OptimizeInplace() - FAILED", , );
return false;
} //
// Compute Bounding Sphere and Bounding Box.
// d3d::BoundingSphere boundingSphere;
d3d::BoundingBox boundingBox; ComputeBoundingSphere(Mesh, &boundingSphere);
ComputeBoundingBox(Mesh, &boundingBox); D3DXCreateSphere(
Device,
boundingSphere._radius,
,
,
&SphereMesh,
); D3DXCreateBox(
Device,
boundingBox._max.x - boundingBox._min.x,
boundingBox._max.y - boundingBox._min.y,
boundingBox._max.z - boundingBox._min.z,
&BoxMesh,
); //
// Set texture filters.
// Device->SetSamplerState(, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(, D3DSAMP_MIPFILTER, D3DTEXF_POINT); //
// Set Lights.
// D3DXVECTOR3 dir(1.0f, -1.0f, 1.0f);
D3DXCOLOR col(1.0f, 1.0f, 1.0f, 1.0f);
D3DLIGHT9 light = d3d::InitDirectionalLight(&dir, &col); Device->SetLight(, &light);
Device->LightEnable(, true);
Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
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 V;
D3DXMatrixLookAtLH(
&V,
&pos,
&target,
&up); Device->SetTransform(D3DTS_VIEW, &V); //
// Set projection matrix.
// D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)Width / (float)Height,
1.0f,
1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj); return true;
} void Cleanup()
{
d3d::Release<ID3DXMesh*>(Mesh); for(int i = ; i < Textures.size(); i++)
d3d::Release<IDirect3DTexture9*>( Textures[i] ); d3d::Release<ID3DXMesh*>(SphereMesh);
d3d::Release<ID3DXMesh*>(BoxMesh);
} bool Display(float timeDelta)
{
if( Device )
{
//
// Update: Rotate the mesh.
// static float y = 0.0f;
D3DXMATRIX yRot;
D3DXMatrixRotationY(&yRot, y);
y += timeDelta; if( y >= 6.28f )
y = 0.0f; D3DXMATRIX World = yRot; Device->SetTransform(D3DTS_WORLD, &World); //
// Render
// Device->Clear(, , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, );
Device->BeginScene(); // draw the mesh
for(int i = ; i < Mtrls.size(); i++)
{
Device->SetMaterial( &Mtrls[i] );
Device->SetTexture(, Textures[i]);
Mesh->DrawSubset(i);
} //
// Draw bounding volume in blue and at 10% opacity
D3DMATERIAL9 blue = d3d::BLUE_MTRL;
blue.Diffuse.a = 0.10f; // 10% opacity Device->SetMaterial(&blue);
Device->SetTexture(, ); // disable texture Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); if( RenderBoundingSphere )
SphereMesh->DrawSubset();
else
BoxMesh->DrawSubset(); Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false); Device->EndScene();
Device->Present(, , , );
}
return true;
} //
// WndProc
//
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch( msg )
{
case WM_DESTROY:
::PostQuitMessage();
break; case WM_KEYDOWN:
if( wParam == VK_ESCAPE )
::DestroyWindow(hwnd); if( wParam == VK_SPACE )
RenderBoundingSphere = !RenderBoundingSphere; break;
}
return ::DefWindowProc(hwnd, msg, wParam, lParam);
} //
// WinMain
//
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE prevInstance,
PSTR cmdLine,
int showCmd)
{
if(!d3d::InitD3D(hinstance,
Width, Height, true, D3DDEVTYPE_HAL, &Device))
{
::MessageBox(, "InitD3D() - FAILED", , );
return ;
} if(!Setup())
{
::MessageBox(, "Setup() - FAILED", , );
return ;
} d3d::EnterMsgLoop( Display ); Cleanup(); Device->Release(); return ;
} bool ComputeBoundingSphere(ID3DXMesh* mesh, d3d::BoundingSphere* sphere)
{
HRESULT hr = ; BYTE* v = ;
mesh->LockVertexBuffer(, (void**)&v); hr = D3DXComputeBoundingSphere(
(D3DXVECTOR3*)v,
mesh->GetNumVertices(),
D3DXGetFVFVertexSize(mesh->GetFVF()),
&sphere->_center,
&sphere->_radius); mesh->UnlockVertexBuffer(); if( FAILED(hr) )
return false; return true;
} bool ComputeBoundingBox(ID3DXMesh* mesh, d3d::BoundingBox* box)
{
HRESULT hr = ; BYTE* v = ;
mesh->LockVertexBuffer(, (void**)&v); hr = D3DXComputeBoundingBox(
(D3DXVECTOR3*)v,
mesh->GetNumVertices(),
D3DXGetFVFVertexSize(mesh->GetFVF()),
&box->_min,
&box->_max); mesh->UnlockVertexBuffer(); if( FAILED(hr) )
return false; return true;
}

DirectX基础学习系列8 渐进网格以及外接体的更多相关文章

  1. directX基础学习系列7 网格(自己创建)

    D3DXMesh 以及 D3DXPMesh都是从ID3DXBaseMesh类中集成,mesh基本是对三角单元进行操作 ID3DXBaseMesh主要函数: HRESULT DrawSubset( DW ...

  2. DirectX 基础学习系列5 纹理映射

    1 纹理坐标 类似BMP图像坐标系,左上为原点 纹理坐标为了规范化,范围限定在[0,1]之间,使用纹理的时候,需要修改顶点结构 struct ColorVetex { float x, y,z; fl ...

  3. DirectX基础学习系列1

    1.3 基础 1.3.1表面 表面接口:     IDirect3DSurface9 获得表面信息:GetDesc(D3DSURFACE_DESC) 获得表面接口指针 :LockRect( D3DLO ...

  4. DirectX 基础学习系列6 字体

    DIRECTX9自带ID3DXFONT类 内部调用GDI的接口,效率一般,但能够处理一些复杂的字体 HRESULT D3DXCreateFontIndirect( LPDIRECT3DDEVICE9 ...

  5. DirectX基础学习系列5 融合技术

    7.1融合方程 1概念 融合技术将当前光栅化像素的颜色与以前已光栅化并处于同一个位置的像素颜色进行合成,即将当前要进行光栅化的三角形单元与已写入后台的像素进行融合 2需要遵循的原则: (1)先绘制不需 ...

  6. DirectX基础学习系列4 颜色和光照

    4.1颜色表示 RGB颜色:D3DCOLOR  可以用宏D3DCOLOR_ARGB(a,r,g,b)  D3DCOLOR_XRGB(255,r,g,b) 另外一种浮点表示:D3DCOLORVALUE, ...

  7. DirectX基础学习系列2

    补充第一章矩阵内容 向量 1 3D空间向量,包含浮点数类型坐标 D3DXVECTOR-->D3DXVECTOR3 2向量的长度 D3DXVector3Length(const D3DXVECTO ...

  8. Linux基础学习系列目录导航

    Linux基础学习-通过VM安装RHEL7.4 Linux基础学习-命令行与图形界面切换 Linux基础学习-基本命令 Linux基础学习-RHEL7.4之YUM更换CentOS源 Linux基础学习 ...

  9. Bootstrap基础学习 ---- 系列文章

    [Bootstrap基础学习]05 Bootstrap学习总结 [Bootstrap基础学习]04 Bootstrap的HTML和CSS编码规范 [Bootstrap基础学习]03 Bootstrap ...

随机推荐

  1. hrbustoj 1551:基础数据结构——字符串2 病毒II(字符串匹配,BM算法练习)

    基础数据结构——字符串2 病毒IITime Limit: 1000 MS Memory Limit: 10240 KTotal Submit: 284(138 users) Total Accepte ...

  2. 16、C++获取磁盘空间的方法

    使用 C# 获取磁盘空间的方法: public async static Task<int> GetFreeSpace() { StorageFolder localFolder = Ap ...

  3. POJ1236 - Network of Schools tarjan

                                                     Network of Schools Time Limit: 1000MS   Memory Limi ...

  4. SQLServer2005利用维护计划自动备份数据库

    经常性忘了给数据库备份,结果当数据库发生问题的时候,才发现备份是1个月以前的,那个后悔与懊恼还加惭愧啊,别提有对难受了.要认为的记住去备份比较难,每天事情又那么多,所以有了这个自动备份就不用愁了.先拷 ...

  5. Professional Android Application Development

    Professional Android Application Development 访问地址 http://docs.google.com/fileview?id=0ByVHV5sjM4fNNj ...

  6. centOS下安装node4.x

    在以前 Node 0.x 的时候,只需要通过 yum install -y nodejs 就可以把 Node 安装好了.但是 yum 安装的 Node 只能安装到 0.10.40 版的,并不能安装到 ...

  7. 17111 Football team

    时间限制:1000MS  内存限制:65535K 提交次数:0 通过次数:0 题型: 编程题   语言: C++;C Description As every one known, a footbal ...

  8. ISODATA算法

    ISODATA算法是在k-均值算法的基础上,增加对聚类结果的'合并'和'分裂'两个操作,并 设定算法运行控制参数的一种聚类算法. 全称:Iterative Selforganizing Data An ...

  9. MathType 插入定义的chapter and section break后无法隐藏

    每一章标题后面插入一个“Next Section Break”,这样定稿后各章文件组合为总文件后,方程编号会自动递增,如果已经插入了默认的“Equation Chapter 1 Section 1”, ...

  10. js对象数组按属性快速排序

    前一篇<关于selector性能比赛>中提到,目测觉得在$("div,p,a")这样有逗号时,sizzle耗时异常(600多个元素,花了200ms),说是它可能没有优化 ...