星期五, 五月 11, 2007

XNA GSE beta中的载入3D模型

今天我在查看XNA GSE beta文档是发现最开始的FAQ有这样一段话

How do I import complex 3D objects into my XNA Game?

The XNA Framework will provide a content pipeline to make it easy to get rich content (3D, 2D, sound, etc.) from content creation sources into a game. The XNA Framework Content Pipeline is a large feature; in an effort to make sure we provide a usable end-to-end experience, we have decided to hold back this feature from the beta release.

顿时觉得有不详预感,作为XNA大势鼓吹的Content Pipeline在我们现在手上的beta版本中还是hold back的。也就是说,XNA对于资源管理的很大一部分功能,我们现在还无法接触到,其中就包括3D模型的载入。我当时作一个最坏的猜测,基于directX的XNA,总应该可以载入XFile的吧。


在文档关于3d图形部分,我们看不到任何关于3D模型载入的部分,在API的文档里面,我也没有发现任何关于mesh管理的参考。

打开昨天编译的SpaceWar游戏,其中管理3D模型的类是工程里面的SimpleMesh,也就是说,这个并不属于XNA的一部分。到此,基本可以认为XNA根本还没有提供如何载入3D模型的方法。

我转到XNA GSE beta的官方论坛,里面有位老兄说到

Those are big words but it does mean nothing else than having to program all the mesh functionality and animated mesh functionality Managed Directx supported all by myself now?It confuses me a little because it was said that XNA takes away all that overhead programming stuff and gets you started writing your game without worrying about anything else.

So in the current version of the framework, is the only way to use “mesh” functionality to define a 3d file format (or use the already existing .x format for example) and then write an a tool that loads that data into a vertexbuffer and material- and texturelist which i then can use to render the whole thing?

原贴地址

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=686368&SiteID=1

他也发现,除非自己写3D文件的载入方法,XNA是没有办法读取任何3D文件的,就是XFile也不行。晕倒……

为了验证他的说法和我的猜测是否正确,我又仔细查看了SpaceWar游戏里面的模型载入方法,果然,在SimpleMesh类里面有载入模型的代码

public static SimpleMesh Load(string fileName)
{
Stream stream = File.OpenRead(fileName);
BinaryReader reader = new BinaryReader(stream);
SimpleMesh mesh = new SimpleMesh();
// Version
Version meshVersion = new Version(reader.ReadString());
if (meshVersion != SimpleMesh.Version)
{
throw new InvalidOperationException(”Mesh ‘” + fileName + “‘ has version ” + meshVersion.ToString() + “. Expected version ” + SimpleMesh.Version.ToString() + “.”);
}
// Source File mesh.SourceFile = reader.ReadString();
// TriangleCount mesh.TriangleCount = reader.ReadInt32();
// VertexSizeInBytes mesh.VertexSizeInBytes = reader.ReadInt32();
……
}
在SpaceWar中用到的模型文件为*.swm。很明显就是Space War Mesh的缩写,具体的文件结构是这样的

数据类型
描叙

string
1.0.0.0v,文件的版本号

string
源文件地址,保存了转换成*.swm文件前的全文件名

int32
文件中三角形的数量

int32
顶点的大小,以字节记

int32
顶点数量

string
顶点格式

int32
顶点集合数量


顶点集合数据
(我自己分析的,可能有纰漏)
Space War这个例子就完全是自己定义的3D数据文件格式,如果需要给这个游戏添加新的模型,需要参考上面的文件格式,并做一个工具将3d建模软件生成的模型文件转为*.swm文件。Space War是不能直接使用XFile文件的,当然,如果你打算自己写XFile文件的分析器,另当别论。
基本上这次算是大家被ms忽悠一下,就凭我们现在手头的XNA,开发一个3d游戏,比我们直接用directX SDK开发更麻烦。不过2d游戏方面,还是值得参考一下的,下篇我将对2d部分做一下介绍。

没有评论: