d3dpool_default,
&pindexbuffer);
第二步是用顶点填充这个索引缓冲:
word *pindex;
pindexbuffer->lock(0, 0, (byte **)&pindex, 0);
for(ii = 0; ii < num_elems; ii++)
{
pindex[ii] = indices[ii];
}
pindexbuffer->unlock();
设定索引缓冲:
pid3ddevice->setindices(pindexbuffer, 0);
把drawscene()的相应的pid3ddevice->drawprimitive(...)换成:
pid3ddevice->drawindexedprimitive(
d3dpt_trianglelist,
0,
sizeof(indices) / sizeof(indices[0]),
0,
sizeof(indices) / sizeof(indices[0]) / 3);
运行程序的到的还是一个三角形。
第五章 加入帖图
(dx8mfc4)
首先,在myvertex结构中加入帖图坐标系tu和tv,并给顶点阵列赋以适当的值。
下一步,设置你的帖图:
d3dxcreatetexturefromfile(pid3ddevice,
"dx5_logo.bmp",
&ptexture);
pid3ddevice->settexture(0, ptexture);
其中的"dx5_logo.bmp"指的是帖图文件,你可以用其他的文件代替它,运行程序你会看到一个带帖图的三角形。
第六章 帖图立方体
(dx8mfc5)
现在样我们来进入三维的世界吧!这里你要启用z缓冲(z-buffer),设置立方体的材质,世界坐标系和投影坐标系。
启用z缓冲(z-buffer),你要在d3dpresent_parameters结构中加入:
present.enableautodepthstencil = true;
present.autodepthstencilformat = d3dfmt_d16;
这里告诉directx8使用16位的z缓冲,下一步:
pid3ddevice->setrenderstate(d3drs_zenable, true);
到这里z缓冲已经设置完成。最后你还要在drawscene()中调用清除z缓冲内容的代码:
pid3ddevice->clear(0,
null,
d3dclear_target | d3dclear_zbuffer,
d3dcolor_rgba(0,63,0,0),
1.0,
0);
myvertex结构改成:
struct myvertex
{
float x, y, z; // the transformed position
dword color; // the vertex color
float tu, tv; // texture coordinates
};
在initdirect3d()也作了相应改动,具体可见源代码。
direct3d中有多种矩阵,在这里只使用其中的三个:世界、视图和投影矩阵。世界矩阵变换会把正方体放在世界坐标系中,视图矩阵变换把正方体放在可视空间内,投影矩阵使正方体看起来有深度感。
buildmatrices()函数将建立这三个矩阵:
void cframewin::buildmatrices()
{
d3dxmatrix matrix;
d3dxmatrixrotationy(&matrix, timegettime() / 1000.0f);
pid3ddevice->settransform(d3dts_world, &matrix);
d3dxmatrixlookatlh(&matrix, &d3dxvector3(0.0f, 3.0f, -5.0f), // 摄像机的空间位置
&d3dxvector3(0.0f, 0.0f, 0.0f), // 摄象机观察点
&d3dxvector3(0.0f, 1.0f, 0.0f)); // 摄象机向上方向矢量
pid3ddevice->settransform(d3dts_view, &matrix);
// 设置我们的平截面为45度角
d3dxmatrixperspectivefovlh(&matrix, d3dx_pi / 4, 4.0f / 3.0f, 1.0f, 100.0f);
pid3ddevice->settransform(d3dts_projection, &matrix);
}
运行本章的例子你将看到一个旋转的正方体。
声明:
本文的sourcecode可以从http://gamedev.363.net得到
欢迎您光临我的主页:http://gamedev.363.net
作者:陈伟凡
e-mail: laical@21cn.com
2001/1/12