XNA Billiards Visual Demo:XNA内容处理和3D渲染的示例(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/xna-billiards-visual-demo-an-example-of-xna-conten-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 9 分钟阅读 - 4398 个词 阅读量 0XNA Billiards Visual Demo:XNA内容处理和3D渲染的示例(译文)
原文地址:https://www.codeproject.com/Articles/21365/XNA-Billiards-Visual-Demo-An-example-of-XNA-conten
原文作者:Inaki Ayucar
译文由本站 robot-v1.0 翻译
前言
This article is an example of visual rendering and content processing with XNA, and a good start for those who want to start learning XNA.
本文是使用XNA进行视觉呈现和内容处理的示例,对于那些想开始学习XNA的人来说是一个不错的开始.
- 下载演示-18.4 KB(Download demo - 18.4 KB)
- 下载演示内容1-2.23 MB(Download demo content 1 - 2.23 MB)
- 下载演示内容2-1.86 MB(Download demo content 2 - 1.86 MB)
- 下载演示内容3-2.86 MB(Download demo content 3 - 2.86 MB)
- 下载源包1-968 KB(Download source pack 1 - 968 KB)
- 下载源包2-2.08 MB(Download source pack 2 - 2.08 MB)
- 下载源包3-3.19 MB(Download source pack 3 - 3.19 MB)
介绍(Introduction)
本文是使用XNA进行3D渲染和内容处理的示例,对于那些想开始学习XNA的人来说是一个不错的开始.尽管这不是一个完整的台球游戏(这将是另一篇文章),但它对于一个人来说也可能是一个很好的起点.(This article is an example of 3D rendering and content processing with XNA, and a good start for those of you who want to start learning XNA. Although it’s not a full working Billiards game (that will be another article), it can be a good starting point for one too.) 它并不是要显示执行任何操作的最佳方法,这只是我在四,五个小时内做出的一个简单示例.任何改进都非常欢迎!(It’s not intended to show the best way of doing anything, it’s just a quick example I’ve made in four or five hours. Any improvement is really welcome!) 它涵盖以下主题:(It covers the following topics:)
- 内容处理器开发(Content processor development)
- 从3DSMax导出的内容(包括Max Shaders)可以与XNA一起使用(Content exporting from 3DSMax, including Max Shaders, ready to use with XNA)
- 内容加载(Content loading)
- 带有和不带有Max Shaders的3D渲染(3D rendering, with and without Max Shaders)
- 基本相机开发(Basic camera development)
- 基本的精灵管理(Basic Sprite management)
- 基本的自定义用户界面开发(Basic custom user interface development)
- 鼠标阅读(Mouse reading)
- 场景和3D模型管理(Scene and 3D models management)
- 使用XML设置序列化(Settings serialization with XML)
- 附加模型属性实现(Additional model properties implementation)
下载说明(Download instructions)
拉链分为两组:(The zips are divided into two groups:)
- Article_demo_XXX(Article_demo_XXX):仅适用于二进制文件. (每个(: for the binaries only. (Every)**demo_XXX(demo_XXX)**压缩文件应解压缩到同一文件夹.)(Zip file should be uncompressed to the same folder.))
- Article_src_XXX(Article_src_XXX):对于完整的Visual Studio项目(每个(: for the full Visual Studio project (every)**src_XXX(src_XXX)**压缩文件应解压缩到同一文件夹)(Zip file should be uncompressed to the same folder)) 您将在其中找到的项目(The project you will find in)**Article_src(Article_src)**是一个Visual C#Express项目.因此,您将需要Visual C#Express(带有Service Pack 1)和(is a Visual C# Express project. So, you’ll need Visual C# Express (with Service Pack 1) and the) XNA游戏工作室(XNA Game Studio) (1.0版刷新)进行编译和运行.((version 1.0 Refresh) to compile and run it.)
背景(Background)
该项目可以用作学习一些中间3D渲染和场景管理概念及其在XNA中的实现的起点.(This project may be used as a starting point to learn some intermediate 3D rendering and scene management concepts, and their implementation in XNA.)
如果您不知道相机是什么,如何渲染3D对象(由多边形组成),或者没有一些基本的代数知识,建议您先阅读一些入门教程.在Code Project撰写有关游戏开发的更多文章之前,一个不错的起点是(If you don’t know what a Camera is, how a 3D object (made up of polygons) is rendered, or don’t have some basic algebraic knowledge, I’d suggest you to first read some introductory tutorial. Until The Code Project has more articles on game development, a good place to start looking is) 游戏开发(GameDev) .(.)
如果您已经具备所有这些基本知识,请继续阅读:(If you already have all this basic knowledge, keep reading:)
在可能的情况下,一切都以面向组件的方式实现. XNA有一个(Where possible, everything has been implemented in a component oriented way. XNA has a) GameComponent
您可以从中继承的类,从而指导您进行这种工作.基本上,所有功能都可以分三个阶段或步骤来表示:(class you can inherit from, which guides you in this way of working. Basically, all the functionality can be expressed in three stages or steps:)
- 初始化:为对象的启动,内存保留(无论如何)调用一次(Initialization: Called once for the object’s boot up, mem reserving, whatever)
- 更新:每帧调用一次,以进行必要的状态更新(Update: Called once per frame, to do any update of states necessary)
- 绘制:每帧调用一次,以绘制可见对象,UI和类似内容.(Draw: Called once per frame, to paint visible objects, UI, and stuff like that.)
3D场景初步(3D scene preliminary)
任何3D应用程序都需要一些数据来构成场景,通常是:(Any 3D application needs some data to compose the scenes, typically:)
-
3D或2D几何(3D or 2D geometry)
-
贴图(Textures)
-
材质和着色器(Materials and shaders)
-
任何其他信息,例如物理特性,游戏特定的属性,…(Any additional information, like physics, game-specific properties, …) 通常在Photoshop,Gimp或其他任何设计套件中创建纹理.通常在3D建模包(如3DSMax,Maya或其他)中创建材质和3D几何.此处纹理也被合并到3D场景中.对于着色器创建,有很多选项:(Textures are usually created in a design suite like Photoshop, Gimp, or any other. Materials and 3D geometry are usually created in a 3D modeling package like 3DSMax, Maya, or others. Textures are incorporated to 3D scenes here as well. For shaders creation, there are many options:)
-
使用特定的软件,例如ATI RenderMonkey或nVidia Shader FX(Use specific software, like ATI RenderMonkey or nVidia Shader FX)
-
使用3D建模包本身(Use the 3D modeling package itself)
-
“手动"编写它们(使用有史以来最强大的工具:记事本(Write them “by hand” (with the most powerful tool ever created: the Notepad, as) 这个(this) 家伙总是说-嗨,Chema!-;)(guy always says - Hi Chema!-;)) 其他对象属性可以通过建模包(带有自定义属性)包含在场景中,也可以在单独的文件中手动指定(例如,我更喜欢这种方式,例如使用XML文件).(Additional object properties can be included in the scene through the modeling package (with custom attributes), or can be specified by hand in separate files (I prefer this way, using XML files, for example).) 尚不清楚如何导出所有这些信息:能否将所有信息都包含在一个文件中?在很多情况下,没有.(What is not so clear is how to export all this bunch of information: Can it all be included in a single file? In many cases, no.)
那么,我该怎么做呢?(So, how did I do it?)
- 免费下载模型组成的3D场景(3D scenes composed by free-downloaded models)
- 在3DSMax中建模的一些基本几何图形(墙等)(Some basic geometry modeled in 3DSMax (walls, etc.))
- 用Gimp完成的基本纹理(Basic textures done with Gimp)
- 使用我们自己的软件完成的高级照明纹理(Advanced lighting textures done with our own software)
- 3DSMax中的材质(Materials composed in 3DSMax)
- 由3DSMax组成的着色器(带有DirectX着色器材料)(Shaders composed in 3DSMax (with DirectX Shader materials))
- 一切都导出到一个X文件(Everything exported to an X-File with) KW X导出插件(KW X Export plug-in) 对于Max9(for Max9)
使用代码(Using the code)
您将在Zip文件中找到的项目是一个可以正常运行的XNA Windows Game项目.(The project you will find in the Zip file is a full working XNA Windows Game project.) 我会讲一点有关它包括的类的信息.出于篇幅原因,我不能过多地扩展此部分,但随时询问您可能需要的其他信息:(I’ll talk a little bit about the classes it includes. For space reasons, I cannot extend this section too much, but feel free to ask for any additional information you may need:)
PositionNormalTexture
:这是XNA内容处理器.它用于将用于3D几何图形的X文件转换为(: this is a XNA Content Processor. It is used to transform the X Files used for 3D geometry into the)**.xnb(.xnb)**格式.它基本上以特定格式写入所有几何图形,因此每个模型都以相同的方式表示.(format. It basically writes all the geometry in a specific format, so every model is expressed in the same way.)TargetedCamera
:准备用作轨道定位相机的相机类.您只需要告诉它一个初始目标和视点,然后调用Orbit方法即可.(: a camera class ready to work as an orbit-targeted camera. You just have to tell it an initial target and eyepoint, and call the Orbit methods.)CMouseInput
:包括所有鼠标读取功能,并公开了两个有用的事件:(: Includes all the mouse reading functionality, and exposes two useful events:)ButtonChanged
和(and)OnMouseMove
.两者的工作方式都与Windows Forms事件非常相似.(. Both work in a very similar way to Windows Forms events.)Model3D
:设计为(: Designed as a)GameComponent
封装了3D模型.此类已准备就绪,可以读取从3DS Max导出的X文件.它支持默认材料(通过(that encapsulates a 3D model. This class is ready to read X Files exported from 3DS Max. It supports default materials (treated through the)BasicEffect
着色器(XNA中的默认着色器)和DirectX着色器材质(在3DSMax中设计的着色器).(shader, a default in XNA), and DirectX Shader Materials (shaders designed in 3DSMax).)Scene
:此类仅是场景管理的示例.在这种情况下,房间和台球桌是不同的型号.他们俩在一起(: This class is just an example of scene management. In this case, the room and the pool table are different models. They are both together in the)Scene
类,因为它们是场景的一部分.例如,如果您想将此示例扩展到完整的台球游戏,那么该类将是放置一组球的理想场所.(class, as they are part of the scene. If you’d want to extend this example to a full Billiards game, this class would be the perfect place to put a collection of balls, for example.)Settings
:我将此类包含在项目中,以说明如何处理应用程序设置以使其与Windows项目和XBox项目兼容,因为此类问题的常规.NET类((: I included this class in the project to explain how application settings can be handled to be compatible both with Windows projects and XBox projects, because the normal .NET classes for such issues ()System.Configuration
)在精简框架(因此为360)上不可用.() are not available on the Compact Framework (and therefore 360).)MySprite
:此类是绘制Sprite的助手,在一个类中包含一个(: This class is a helper for drawing Sprites, holding in a single class a)SpriteBatch
,Texture2D,用于渲染精灵的位置以及其他功能,例如精灵的色调颜色.请注意,使用(, a Texture2D, a position to render the sprite in, and other features, like a tint color for the sprite. Please note that using)MySprite
渲染大量精灵的效率不高,因为需要一个(to render lots of sprites is not efficient, as it will require one)SpriteBatch
对于每个精灵.您应该使用一个(for each sprite. You should use a single)SpriteBatch
而不是渲染大量精灵,但是对于本示例来说,这就足够了(而且非常简单).它用于在光标位置渲染"狙击"精灵.(to render lots of sprites instead, but for this example, this suffices (and is really simple). It is used to render a “sniper” sprite at cursor position.)MySpriteButton
:继承自(: Inherits from)MySprite
但增加了类似按钮的功能.它包括一个(but adds a button-like functionality. It includes a)Click
事件,非常类似于Windows窗体中的事件,鼠标悬停效果会在鼠标悬停在其上方时更改精灵色的颜色.它用于UI按钮,允许用户为相机选择不同的目标.(event, very much like the one in Windows Forms, and a mouse hover effect changing the sprite tint color when the mouse is over it. It’s used for the UI buttons that allow the user to select different targets for the camera.)BilliardsGame
:这是XNA游戏本身.它继承(: This is the XNA game itself. It inherits)Microsoft.Xna.Framework.Game
并基本上持有并使用所有其他:(and basically holds and uses all the others:)GraphicsDevice
,(,)ContentManager
,(, the)Settings
类的实例,(class' instance,)CMouseInput
实例(instance,)Scene
实例(instance,)TargetedCamera
实例(instance,)MySprites
和(and)MySpritesButtons
对于用户界面,…(for the User Interface, …) 希望你觉得它有用(Hope you find it useful)
兴趣点(Points of interest)
请务必看一看(Make sure to take a look at the) XNA主页(XNA Home Page) 有关框架,教程,入门工具包的更新,请始终查看(for updates of the framework, tutorials, starter kits, and always look at the) 论坛(forums) 当您有问题时.也许有人已经拥有它,而其他人已经找到了解决方案.(when you have a problem. Maybe someone already had it and someone else already found a solution.) 那里有很多好人! ;)(There are a lot of good people there! ;))
历史(History)
- 初始版本:2007年11月19日.(Initial version: November 19, 2007.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# C#2.0 WinXP Windows .NET XBox .NET2.0 Visual-Studio VS2005 Dev 新闻 翻译