DirectX基础 常用函数语句

 

DirectX常用函数语句

 

常用数学类函数:

  计算向量的长度(模): 

FLOAT D3DXVec3Length(CONST D3DXVECTOR3* pV);

   向量的规范化: 

D3DXVECTOR3 *D3DXVec3Normalize(D3DXVECTOR3* pOut, CONST D3DXVECTOR3* pV);

  点积: 

FLOAT D3DXVec3Dot(CONST D3DXVECTOR3* pV1, CONST D3DXVECTOR3* pV2);

  叉积:

D3DXVECTOR3 *  D3DXVec3Cross(D3DXVECTOR3* pOut,
CONST D3DXVECTOR3* pV1, CONST D3DXVECTOR3* pV2);

 

  创建镜像变换矩阵:

D3DXMATRIX * D3DXMatrixReflect(
D3DXMATRIX *pOut,CONST D3DXPLANE *pPlane);

 

  创建阴影矩阵:

D3DXMATRIX * D3DXMatrixShadow(
D3DXMATRIX *pOut,
CONST D3DXVECTOR4 *pLight,
CONST D3DXPLANE *pPlane);

 

  绕任意轴旋转的矩阵:

D3DXMATRIX *D3DXMatrixRotationAxis(
D3DXMATRIX *pOut,  //返回旋转矩阵
CONST D3DXVECTOR3 *pV,
FLOAT Angle)

 

  变换点:

// Transform (x, y, z, 1) by matrix, project result back into w=1.

D3DXVECTOR3* WINAPI D3DXVec3TransformCoord
    ( D3DXVECTOR3 *pOut, CONST D3DXVECTOR3 *pV, CONST D3DXMATRIX *pM );

 

  变换向量:

// Transform (x, y, z, 0) by matrix.  If you transforming a normal by a
// non-affine matrix, the matrix you pass to this function should be the
// transpose of the inverse of the matrix you would use to transform a coord.

D3DXVECTOR3* WINAPI D3DXVec3TransformNormal
    ( D3DXVECTOR3 *pOut, CONST D3DXVECTOR3 *pV, CONST D3DXMATRIX *pM );

 

 

常用相机相关函数:

  创建视矩阵:

D3DXMATRIX *D3DXMatrixLookAtLH(
D3DXMATRIX* pOut,
CONST D3DXVECTOR3* pEye,
CONST D3DXVECTOR3* pAt,
CONST D3DXVECTOR3* pUp)

  设定视矩阵:

Device->SetTransform(D3DTS_VIEW,&V);

 

  创建投影矩阵:

D3DXMATRIX *D3DXMatrixPerspectiveFovLH(
D3DXMATRIX* pOut,
FLOAT fovY,  //垂直方向的视场角(单位为弧度)
FLOAT Aspect,
FLOAT zn,  //到近平面的距离
FLOAT zf);

  设定投影矩阵:

Device->SetTransform(D3DTS_PROJECTION,&proj);

 

 

渲染状态设置:

  背面消隐:

Device->SetRenderState(D3DRS_CULLMODE,Value);

  其中Value可以取值:D3DCULL_NONED3DCULL_CWD3DCULL_CCW

 

光照类函数:

  镜面反射分量开启:  

Device->SetRenderState(D3DRS_SPECULARENABLE, true);

  所有法向量重新规范化:

Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);

  对光源进行注册:

Device->SetLight(0,&light);

  使能光源:

Device->LightEnable(0,true);

  启用光照:

Device->SetRenderState(D3DRS_LIGHTING, true);  //默认状态下光照是启用的,但是显示指定并无大碍

  设定材质:

D3DMATERIAL9 mtrl;
//。。。。设置材质参数
Device->SetMaterial(&mtrl);

 

纹理:

  从图像文件中读入纹理数据,然后加载到IDirect3DTexture9对象中:

HRESULT D3DXCreateTextureFromFile(
LPDIRECT3DDEVICE9 pDevice,
LPCTSTR pSrcFile,
LPDIRECT3DTEXTURE9 *ppTexture);

 

  设置当前纹理:

HRESULT IDirect3DDevice9::SetTexture(
DWORD Stage,
IDirect3DBaseTexture9 *pTexture);

 


  设置纹理过滤方式:

  放大过滤器:

Device->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_POINT);

  缩小过滤器:

Device->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_POINT);

 

  放大过滤器:

Device->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);

  缩小过滤器:

Device->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);

 

  放大过滤器:

Device->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_ANISOTROPIC);

  缩小过滤器:

Device->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_ANISOTROPIC);

 

  使用各向异性纹理过滤时,必须对D3DSAMP_MAXANISOTROPIC水平值进行设定,该值决定了各向异性过滤的质量水平。

Device->SetSamplerState(0,D3DSAMP_MAXANISOTROPIC,4);

 

  多级渐进纹理过滤器设置:

Device->SetSamplerState(0,D3DSAMP_MIPFILTER,Filter);

  Filter可取以下值:

  D3DTEXF_NONE, D3DTEXF_POINT, D3DTEXF_LINEAR

 

  寻址模式:

  重复寻址模式Wrap:

Device->SetSamplerState(0,D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
Device->SetSamplerState(0,D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);

 

  边界颜色(border color)寻址模式:

Device->SetSamplerState(0,D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
Device->SetSamplerState(0,D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
Device->SetSamplerState(0,D3DSAMP_BORDERCOLOR, 0x000000ff);

 

  箝位(Clamp)寻址模式:

Device->SetSamplerState(0,D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
Device->SetSamplerState(0,D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);

 

  镜像(Mirror)寻址模式:

Device->SetSamplerState(0,D3DSAMP_ADDRESSU, D3DTADDRESS_MIRROR);
Device->SetSamplerState(0,D3DSAMP_ADDRESSV, D3DTADDRESS_MIRROR);

 

 

融合技术:

  启用融合运算:(默认禁止)

Device->SetRenderState(D3DRS_ALPHABLENDENABLE,true);

 

  设定融合因子:

Device->SetRenderState(D3DRS_SRCBLEND,Souce);
Device->SetRenderState(D3DRS_DESTBLEND,Destination);

  其中,Source和Destination可取下列融合因子

  源融合因子和目标融合因子的默认值分别是D3DBLEND_SRCALPHA  D3DBLEND_INVSRCALPHA

 

D3DBLEND_ZERO
D3DBLEND_ONE
D3DBLEND_SRCCOLOR
D3DBLEND_INVSRCCOLOR
D3DBLEND_SRCALPHA
D3DBLEND_INVSRCALPHA
D3DBLEND_DESTALPHA
D3DBLEND_INDESTALPHA
D3DBLEND_DESTCOLOR
D3DBLEND_INVDESTCOLOR
D3DBLEND_SRCALPHASAT
D3DBLEND_BOTHINVSRCALPHA(该模式仅对D3DRS_SRCBLEND有效)

 

  指定ALPHA来源:

//根据漫反射颜色计算Alpha
Device->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_DIFFUSE);
Device->SetTextureStageState(0,D3DTSS_ALPHAAOP,D3DTOP_SELECTARG1);

 

//从Alpha通道获取Alpha
Device->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);
Device->SetTextureStageState(0,D3DTSS_ALPHAAOP,D3DTOP_SELECTARG1);

 

 

模板:

  启用和禁用模板缓存:

Device->SetRenderState(D3DRS_STENCILENABLE,true);
Device->SetRenderState(D3DRS_STENCILENABLE,false);

 

  模板测试:

(ref & mask) ComparisonOperation(value & mask)

 

  模板参考值ref默认值为0,改变该值:

Device->SetRenderState(D3DRS_STENCILREF,0x1);

 

  模板掩码mask默认值为0xffffffff,改变该值:

Device->SetRenderState(D3DRS_STENCILMASK,0x0000ffff);

 

 

  通过绘制状态D3DRS_STENCILFUNC来设置比较运算函数:

  该比较运算函数可以取自枚举类型D3DCMPFUNC

  

  

  模板缓存的更新:

Device->SetRenderState(D3DRS_STENCILFAIL,StencilOperation);
Device->SetRenderState(D3DRS_STENCILZFAIL, StencilOperation);
Device->SetRenderState(D3DRS_STENCILPASS, StencilOperation);

 

  StencilOperation可取以下预定义常量:

D3DSTENCILOP_KEEP 不更新模板缓存的值,保留当前值
D3DSTENCILOP_ZERO
D3DSTENCILOP_REPLACE
D3DSTENCILOP_INCRSAT
D3DSTENCILOP_DECRSAT
D3DSTENCILOP_INVERT
D3DSTENCILOP_INCR
D3DSTENCILOP_DECR

 

  模板写掩码:

Device->SetRenderState(D3DRS_STENCILWRITEMASK,0x0000ffff);

 

网格:

  得到指向顶点缓存和索引缓存接口的指针:

HRESULT ID3DXMesh :: GetVertexBuffer(LPDIRECT3DVERTEXBUFFER9* ppVB);
HRESULT ID3DXMesh :: GetIndexBuffer(LPDIRECT3DINDEXBUFFER9* ppIB);

 

  锁定顶点缓存和索引缓存:

HRESULT ID3DXMesh :: LockVertexBuffer(DWORD Flags,BYTE** ppData);
HRESULT ID3DXMesh :: LockIndexBuffer(DWORD Flags,BYTE** ppData);

  当该函数返回时,ppData返回指向被锁定的内存的指针的地址。

  当对被锁定内存完成操作后,务必调用相应的解锁方法:

HRESULT ID3DXMesh :: UnlockVertexBuffer();
HRESULT ID3DXMesh :: UnlockIndexBuffer();

 

  ID3DXMesh接口用于获取几何信息的另外一些方法:

DWORD GetFVF();
DWORD GetNumVertices();
DWORD GetNumBytesPerVertex();
DWORD GetNumFaces();

 

  访问属性缓存,必须先将其锁定:

DWORD* buffer=0;
Mesh->LockAttributeBuffer(lockingFlags, &buffer);
//Read or write to attribute buffer…..
Mesh->UnlockAttributeBuffer();

 


  绘制:

Mesh->DrawSubset(0);
Mesh->DrawSubset(i);

 

  网格优化:

HRESULT ID3DXMesh :: OptimizeInplace(
DWORD Flags,
CONST DWORD* pAdjacencyIn,
DWORD* pAdjacencyOut,
DWORD* pFaceRemap,
LPD3DXBUFFER* ppVertexRemap);

 

 

  访问一个网格面的属性表:

HRESULT ID3DXMesh :: GetAttributeTable(
D3DXATTRIBUTERANGE *pAttribTable,
DWORD *pAttribTableSize);

  对属性表进行设置:

D3DXATTRIBUTERANGE attributeTable[12];
//…fill attributeTable array with data
Mesh->SetAttributeTable(attributeTable,12);

 

  输出邻接信息:

HRESULT ID3DXMesh :: GenerateAdjacency(
FLOAT fEpsilon,
DWORD* pAdjacency);

 

  生成网格数据的一个副本:

HRESULT ID3DXMesh :: CloneMeshFVF(
DWORD Options, 创建标记或标记组合
DWORD FVF, 所要创建的克隆网格的灵活顶点格式
LPDIRECT3DDEVICE9 pDevice,
LPD3DXMESH *ppCloneMesh)

 

  创建空网格:

HRESULT WINAPI D3DXCreateMeshFVF(
DWORD NumFaces,
DWORD NumVertices,
DWORD Options,
DWORD FVF,
LPDIRECT3DDEVICE9 pDevice,
LPD3DXMESH *ppMesh)

 

  创建空网格2:

HRESULT D3DXCreateMesh (
DWORD NumFaces,
DWORD NumVertices,
DWORD Options,
CONST LPD3DVERTEXELEMENT9* pDeclaration, 数组,描述顶点数据的布局方式
LPDIRECT3DDEVICE9 pDevice,
LPD3DXMESH *ppMesh)

 


  格式相关函数:

HRESULT D3DXDeclaratorFromFVF(
DWORD FVF,
D3DVERTEXELEMENT9 Declaration[MAX_FVF_DECL_SIZE])

 

  加载XFile文件:

HRESULT D3DXLoadMeshFromX(
LPCSTR pFilename,
DWORD Options,
LPDIRECT3DDEVICE9 pDevice,
LPD3DXBUFFER *ppAdjacency,
LPD3DXBUFFER *ppMaterials,
LPD3DXBUFFER *ppEffectInstances, 该参数返回一个ID3DXBuffer对象,该对象包含了一个D3DXEFFECTINSTANCE结构。可以通过指定该参数为0而将其忽略。
PDWORD pNumMaterials,
LPD3DXMESH *ppMesh);

 

  产生任意网格的顶点法向量

HRESULT D3DXComputeNormals(
LPD3DXBASEMESH pMesh,
CONST DWORD *pAdjacency)

 

  注意:必须包含标记D3DFVF_NORMAL

 

  生成渐进网格:

HRESULT D3DXGeneratePMesh(
LPD3DXMESH pMesh,
CONST DWORD *pAdjacency,
CONST D3DXATTRIBUTEWEIGHTS *pVertexAttributeWeights,
CONST FLOAT *pVertexWeights,
DWORD MinValue,
DWORD Options,
LPD3DXPMESH *ppPMesh)

 

  设置网格面片数被简化到的个数:

HRESULT SetNumFaces(DWORD Faces)

 

  设置网格的顶点数被简化到的个数:

HRESULT SetNumVertices(DWORD Vertices)

 


  计算网格的外接球:

HRESULT WINAPI D3DXComputeBoundingSphere(
CONST D3DXVECTOR3 *pFirstPosition,
DWORD NumVertices,
DWORD dwStride,
D3DXVECTOR3 *pCenter,
FLOAT *pRadius)

 

  计算外接体:

HRESULT WINAPI D3DXComputeBoundingBox(
CONST D3DXVECTOR3 *pFirstPosition,
DWORD NumVertices,
DWORD dwStride,
D3DXVECTOR3 *pMin,
D3DXVECTOR3 *pMax)

 

 

粒子系统:

  点精灵的行为由绘制状态来控制:

D3DRS_POINTSPRITEENABLE默认为false
D3DRS_POINTSALEENABLE 默认为false,规定点的尺寸用屏幕坐标系的单位(即像素)来度量。若指定为true,则规定点的尺寸将用观察坐标系的单位来度量。
D3DRS_POINTSIZE用于指定点精灵的尺寸。
D3DRS_POINTSIZE_MIN
D3DRS_POINTSIZE_MAX
D3DRS_POINTSCALE_A, D3DRS_POINTSCALE_B,D3DRS_POINTSCALE_C,这三个常量控制了点精灵的尺寸如何随距离发生变化。

                      

  创建顶点缓存:

Device->CreateVertexBuffer(
-vbSize*sizeof(Particle),
D3DUSAGE_DYNAMIC|D3DUSAGE_POINTS|D3DUSAGE_WRITEONLY,
Particle::FVF,
D3DPOOL_DEFAULT,
&_vb,
0)

 

 

 

posted @ 2013-08-05 10:39  圣骑士wind  阅读(2868)  评论(0编辑  收藏  举报