.x类型的3D小东西是怎么做的?(100分)

  • 主题发起人 主题发起人 tigtan
  • 开始时间 开始时间
T

tigtan

Unregistered / Unconfirmed
GUEST, unregistred user!
请各位多多帮助,急用!
 
你用3D建模工具把模型转成*.x就成啦。

Introduction (序)
这一章我们将会看到怎样创建模型和怎样在DX中应用模型。至今为止,我们已经可以创建一些简单的对象了,像立方体、球体、圆柱体等等。但是一个游戏需要的可远远不只这些,它需要比如枪、人物、建筑物或是宇宙飞船等等这样的模型。你可能会在一张纸上创建出这些并且计算出了所有顶点的准确位置,或者你会用一种类似“3D modeller”的软件,但是我的建议是使用立体建模软件(如3DSMAX、Maya),它更方便。

3D Modelling Software (立体建模软件)
你需要用立体建模软件来创建你的游戏中的对象,下面是一个建模软件列表(按价格排序),应该会有你喜欢的。立体建模软件其实有很多种,但是根据我的调查,这些是更受欢迎的。

想要配合DX,软件得能导出模型为 .x 文件。这得需要一个有此功能的插件或是转化程序。

Package Website Price*
3D Studio Max www.discreet.com $3495
Maya www.aliaswavefront.com $1999
Cinema 4D www.cinema4d.com $1695
TrueSpace www.caligari.com $595
3D Canvas LP www.amabilis.com $34.95
MilkShape 3D www.milkshape3d.com $20
OpenFX www.openfx.org Free

* 列表中的价格为发布价格,想要了解准确的当前的价格请到它的主页上。你可以到www.oanda.com来把价格换算成你的本国货币单位。

你会选择哪一个呢?那是你的事了。我选择了3D Canvas (trial version)、MilkShape 3D (trial version) 和OpenFX (full version),因为他们很便宜,对于业余爱好者很适用。

译者:下面的几段原著简要地介绍了他选择的三个小软件,接着又稍微重点地介绍了一下MilkShape 3D。我认为我们能使用这些软件的机率很小,所以偷懒不翻译了。

3D Canvas is a good piece of software but it has one major downside. If you are using Windows XP or Windows 2000 (like me) it can reboot your machine without warning! The help advised me that updating my display driver to the latest version would fix the problem... but I have the latest version already! So, I'm afraid 3D Canvas is no good for me.

MilkShape 3D is an excellent package for creating low polygon models. It has most of the features that you need to create a 3D model, and is pretty easy to use. There are loads of tutorials on the web and a forum on the MilkShape site that is really useful.

OpenFX is a great all round 3D modeller. I found it easy to use and very powerful. The main problem I had was exporting my model into a .x file. The only way I found was to export to a .3ds file and then use the conv3ds.exe tool that comes with the DirectX SDK to convert it to a .x file. The only problem was that it lost the textures when exporting to a .3ds file. There is also very limited help. So, OpenFX isn't the right tool for me.

So, I have decided to use MilkShape 3D because it is cheap, it can export to a .x file (via a free plug-in) and it is easy to use.


Creating a .x file with MilkShape 3D (用MilkShape 3D创建.X文件)
Once you have your 3D modelling package, the next thing to do is create a model. For this tutorial have created a simple spaceship. I used the tutorials here to get me started with MilkShape. Fig 10.1 below, shows a screenshot of MilkShape 3D with my completed spaceship model.

Fig 10.1

Once your model is complete, you need to export it to a .x file. To do this with MilkShape, download and install the "DirectX 8.1 Exporter" plug-in by John Thompson from the MilkShape website. Then open your model in MilkShape and go to "File" > "Export" > "DirectX (JT)...". Select a location for your .x file, then select the options you require (normally the defaults) and press "OK". You are now ready to load your model into your DirectX program.

译者:其实商业游戏软件是不用.x这种文件格式的,不信的话你可以随便找来一款,到它的目录下看一看。一般来说,商业游戏无论用什么软件来建模,最终都会把模型文件转换成自己的格式,而且一般都会将文件打包,以节省空间。因为现在是学习阶段,所以我们暂时就用.x这种格式。

Loading a .x file into your program (在程序中载入.x文件)
这一章我们会增加一个新的类CMesh,它的功能是渲染从.x文件中读取的网格(Mesh)。下面有CMesh类的源代码,它的构造函数将展示出怎样从.x文件中读取网格(Mesh)并且将它们保存至内存,它的析构函数将展示出怎样释放网格所占有的内存。它的渲染模块会告诉你怎样渲染网格(Mesh)。

CMesh类的构造函数有两个输入参数:第一个是DX设备,我们早就接触过了;第二个是要读取的.x文件的文件名,这个很好理解。我们用D3DXLoadMeshFromX函数来完成把网格载入内存的工作。然后,我们会创建两个数组,分别用于保存模型的材质和纹理。然后我们会有一个循环,其实它的功能就是从文件(由.x文件给定)中读取纹理并保存到数组中。最后,我们克隆了网格并且调用了D3DXComputeNormals函数来设置法线。
(译者:其实原著这里说的也不是很详细,不明白就研究源代码吧)

在析构函数中我们释放了网格和纹理占用的内存,还有前面创建的两个数组,都通通释放了。

渲染模块是很简单的,我们会循环的处理网格的每个子集,并且用适当的纹理和材质渲染它们。

CMesh::CMesh(LPDIRECT3DDEVICE8 pD3DDevice, LPSTR pFilename)
{
LPD3DXBUFFER pMaterialsBuffer = NULL;
LPD3DXMESH pMesh = NULL;

m_pD3DDevice = pD3DDevice;

if(FAILED(D3DXLoadMeshFromX(pFilename, D3DXMESH_SYSTEMMEM, m_pD3DDevice, NULL,
&pMaterialsBuffer, &m_dwNumMaterials, &pMesh)))
{
m_pMesh = NULL;
m_pMeshMaterials = NULL;
m_pMeshTextures = NULL;

LogError("<li>Mesh '%s' failed to load", pFilename);
return;
}

D3DXMATERIAL* matMaterials = (D3DXMATERIAL*)pMaterialsBuffer->GetBufferPointer();

//Create two arrays. One to hold the materials and only to hold the textures
m_pMeshMaterials = new D3DMATERIAL8[m_dwNumMaterials];
m_pMeshTextures = new LPDIRECT3DTEXTURE8[m_dwNumMaterials];

for(DWORD i = 0; i < m_dwNumMaterials; i++)
{
//Copy the material
m_pMeshMaterials = matMaterials.MatD3D;

//Set the ambient color for the material (D3DX does not do this)
m_pMeshMaterials.Ambient = m_pMeshMaterials.Diffuse;

//Create the texture
if(FAILED(D3DXCreateTextureFromFile(m_pD3DDevice,
matMaterials.pTextureFilename,
&amp;m_pMeshTextures)))
{
m_pMeshTextures = NULL;
}
}

//We've finished with the material buffer, so release it
SafeRelease(pMaterialsBuffer);


//Make sure that the normals are setup for our mesh
pMesh->CloneMeshFVF(D3DXMESH_MANAGED, MESH_D3DFVF_CUSTOMVERTEX,
m_pD3DDevice, &amp;m_pMesh);
SafeRelease(pMesh);

D3DXComputeNormals(m_pMesh);


LogInfo("<li>Mesh '%s' loaded OK", pFilename);
}

CMesh::~CMesh()
{
SafeDelete(m_pMeshMaterials);

if(m_pMeshTextures != NULL)
{
for(DWORD i = 0; i < m_dwNumMaterials; i++)
{
if(m_pMeshTextures)
{
SafeRelease(m_pMeshTextures);
}
}
}

SafeDelete(m_pMeshTextures);
SafeRelease(m_pMesh);

LogInfo("<li>Mesh destroyed OK");
}

DWORD CMesh::Render()
{
if(m_pMesh != NULL)
{
for(DWORD i = 0; i < m_dwNumMaterials; i++)
{
m_pD3DDevice->SetMaterial(&amp;m_pMeshMaterials);
m_pD3DDevice->SetTexture(0, m_pMeshTextures);

m_pMesh->DrawSubset(i);
}

return m_pMesh->GetNumFaces();
}
else
{
return 0;
}
}

Scaling makes your object darker? (缩放使你的对象变暗了?)
还有一点需要注意的是,如果你缩放了网格,同时你也缩放了法线。这可能会使你的对象看起来发暗,解决的办法是激活D3DRS_NORMALIZENORMALS渲染状态:

m_pD3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);

这一章我们的例子是创建三个太空船(译者:即使看起来不太像),它们会做不同的旋转。运行例子你会得到像下面这样的屏幕:
 
从哪摘的这些东西,你用过3DMax吗,是不是用它就行了
 
我没用过3d Max,但我知道它绝对可导出*x.
 
接受答案了.
 
后退
顶部