DX学习笔记(旋转的立方体)

这篇博客主要记录了使用DirectX(DX)实现旋转立方体的过程。内容包括d3dUtility.cpp和d3dUtility.h文件的使用,它们与之前Demo相同,而cube.cpp是重点,初始化VB和IB,设置立方体顶点和索引数据,定义窗口大小。通过不断改变旋转角度,实现了立方体的转动效果。同时,解释了IDirect3DDevice9::DrawIndexPrimitive()函数的作用。
摘要由CSDN通过智能技术生成

里面主要包含d3dUtility.cpp,d3dUtility.h,cube.cpp。

d3dUtility.cpp,d3dUtility.h与前面demo的是完全一致的,参考http://blog.csdn.net/zero_witty/article/details/51621908

这里主要是cube.cpp,

首先初始化VB,IB全局变量,保存立方体的顶点和索引数据,并初始化全局变量Width,Height,定义窗口的大小

IDirect3DDevice9* Device = 0;

const int Width = 640;
const int Height = 480;

IDirect3DVertexBuffer9* VB = 0;
IDirect3DIndexBuffer9* IB = 0;

接着定义顶点结构,和该结构的灵活顶点类型

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

const DWORD Vertex::FVF = D3DFVF_XYZ;

再接着便是框架函数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 = 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] =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值