关于Direct 9 绘制立方体的问题

只负责写Bug 只负责写Bug 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

#include "d3dUtility.h"

#define WIDTH 800 //屏幕宽
#define HEIGHT 600 //屏幕高

IDirect3DDevice9*device = 0;
D3DPRESENT_PARAMETERS d3dpp;

IDirect3DVertexBuffer9 *VB; //定义顶点缓存
IDirect3DIndexBuffer9 *IB; //定义索引缓存

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

bool D3D::Setup()
{
device->CreateVertexBuffer(8 * sizeof(Vertex), D3DUSAGE_WRITEONLY, Vertex::FVF, D3DPOOL_MANAGED, &VB, 0);
device->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.0f, -1.0f, -1.0f);
vertices[1] = Vertex(-1.0f, 1.0f, -1.0f);
vertices[2] = Vertex(1.0f, 1.0f, -1.0f);
vertices[3] = Vertex(1.0f, -1.0f, -1.0f);
vertices[4] = Vertex(-1.0f, -1.0f, 1.0f);
vertices[5] = Vertex(-1.0f, 1.0f, 1.0f);
vertices[6] = Vertex(1.0f, 1.0f, 1.0f);
vertices[7] = Vertex(1.0f, -1.0f, 1.0f);
VB->Unlock();

WORD *indices;
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();

D3DXVECTOR3 position(0.0f, 0.0f, -5.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &position, &target, &up);
device->SetTransform(D3DTS_VIEW, &V);
// 投影矩阵
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)WIDTH / (float)HEIGHT,
1.0f,
1000.0f);
device->SetTransform(D3DTS_PROJECTION, &proj);
device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);

device->BeginScene();// 开始绘制场景
device->SetStreamSource(0, VB, 0, sizeof(Vertex));// 设置资源流
device->SetIndices(IB); // 设置索引缓存
device->SetFVF(Vertex::FVF); // 设置顶点格式
// 利用索引缓存绘制
device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
device->EndScene();// 结束绘制场景

return true;
}



我直接调用这个函数窗口没东西,老哥们帮我看看缺少了什么步骤,谢谢
...全文

相关推荐

评论 15
收藏 已收藏
分享
返回顶部