directx初第五卷

索引缓存

声明变量

IDirect3DVertexBuffer9* vb = 0;
IDirect3DIndexBuffer9* ib = 0;


设计顶点结构

struct vertex 
{
    float _x, _y,_z;
    vertex(){}
    vertex(float x, float y, float z)
    {
        _x = x; _y = y; _z = z;
    }
    static const DWORD fvf;
};
const DWORD vertex::fvf = D3DFVF_XYZ;

创建顶点缓存&索引缓存

g_pd3dDevice->CreateVertexBuffer(8 * sizeof(vertex), D3DUSAGE_WRITEONLY, vertex::fvf, D3DPOOL_MANAGED, &vb, 0);
//创建索引缓存(索引缓存大小,只写,16位索引,由d3d自动选择顶点缓冲区的位置(显存/缓存),把结果存到索引缓存指针,基本为null)
g_pd3dDevice->CreateIndexBuffer(36 * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &ib, 0);

访问和填充顶点缓存&索引缓存

vertex* vertices;
vb->Lock(0, 0, (void**)&vertices,0);
vertices[0] = vertex{ -1, -1, -1 };
vertices[1] = vertex{ -1, 1, -1 };
vertices[2] = vertex{ 1, 1, -1 };
vertices[3] = vertex{ 1, -1, -1 };
vertices[4] = vertex{ -1, -1, 1 };
vertices[5] = vertex{ -1, 1, 1 };
vertices[6] = vertex{ 1, 1, 1 };
vertices[7] = vertex{ 1, -1, 1 };
vb->Unlock();

WORD* indices = 0;
ib->Lock(0, 0, (void**)&indices, 0);
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 0;
indices[4] = 2;
indices[5] = 3;

indices[6] = 4;
indices[7] = 6;
indices[8] = 5;
indices[9] = 4;
indices[10] = 7;
indices[11] = 6;

indices[12] = 4;
indices[13] = 5;
indices[14] = 1;
indices[15] = 4;
indices[16] = 1;
indices[17] = 0;

indices[18] = 3;
indices[19] = 2;
indices[20] = 6;
indices[21] = 3;
indices[22] = 6;
indices[23] = 7;

indices[24] = 1;
indices[25] = 5;
indices[26] = 6;
indices[27] = 1;
indices[28] = 6;
indices[29] = 2;

indices[30] = 4;
indices[31] = 0;
indices[32] = 3;
indices[33] = 4;
indices[34] = 3;
indices[35] = 7;
ib->Unlock();

绘制顶点&索引

g_pd3dDevice->SetStreamSource(0, vb, 0, sizeof(vertex));
//设置索引缓存
g_pd3dDevice->SetIndices(ib);
g_pd3dDevice->SetFVF(vertex::fvf);
//图形索引绘制(三角形列模式,从索引0开始读取索引缓存,索引中最小值为0,为8个顶点做索引,从索引0开始绘制图元,绘制12个图元)
g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);

目前写完可是运行看不见效果?加上下面代码(以后再详细说明)

D3DXVECTOR3 position(0, 0, -5);
D3DXVECTOR3 target(0, 0, 0);
D3DXVECTOR3 up(0, 1, 0);
D3DXMATRIX v;
D3DXMatrixLookAtLH(&v, &position, &target, &up);
g_pd3dDevice->SetTransform(D3DTS_VIEW, &v);

D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI*.5f, (float)640 / (float)480, 1, 1000);
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj);
g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);

运行结果:

注:渲染效果不一样?
加上以下代码(以后再详细说明)

g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值