ogre 3d 1.7 beginner's guide 第三章 第一小节 翻译
3 Camera, Light, and Shadow
【 第三章 摄像机,灯光和阴影 】
We already learned how to create a complex scene, but without light and
shadow, a scene won't be complete.
【 我们已经学习过如果创建一个复杂的场景。但是如果没有灯光和阴影,一个场景是不完整的。】
In this chapter, we will learn about:
【 在这张,我们将会学习到: 】
* The types of different light sources Ogre 3D supports and how they work
* Adding shadows to a scene and the different shadow techniques available
* What a camera and viewport are and why we need to have them
【
* Ogre3D支持的不同类型的灯光资源和它们是如何使用的。
* 对一个场景添加阴影和添加可用的不同的阴影技术。
* 什么是摄像机和视口和我们为什么需要使用它们。
】
Creating a plane
【 创建一个平面】
Before we can add lights to our scene, we first need to add a plane, onto which shadows and
light are projected, and therefore visible to us. A normal application wouldn't need a plane
because there would be a terrain or a floor to project light onto. Light calculation would work
without the plane, but we wouldn't be able to see the effect of the light.
【 在我们添加灯光到我们的场景之前,我们首先需要添加一个可以投射阴影和灯光的平面,这样我们就可以看到阴影了。通常一个应用程序不需要一个平面,因为项目的本身有可以打上灯光的地形和地板。灯光计算可以在一个没有平面的程序中,但是那样我们就看不到灯光的效果了。 】
Time for action – creating a plane
【 实践时刻——创建一个平面】
Until now, we have always loaded a 3D model from a file. Now we will create one directly:
【 目前为止,我们总是从一个文件中加载3D模型。现在我们就直接创建一个平面:】
1. Delete all the code inside the createScene() function.
【 1.删除createScene() 函数中的所有代码:】
2. Add the following line to define a plane in the createScene() function:
【 2. 在createScene() 函数中添加下面一行代码来定义一个平面。】
Ogre::Plane plane(Vector3::UNIT_Y, -10);
- Now create the plane into your memory:
【 现在创建一个平面写入到你的内存中。】
Ogre::MeshManager::getSingleton().createPlane("plane",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane,
1500,1500,20,20,true,1,5,5,Vector3::UNIT_Z);
- Create an instance of the plane:
【 创建一个平面的实例。】
Ogre::Entity* ent = mSceneMgr->createEntity("LightPlaneEntity", "plane");
- Attach the plane to the scene:
【 关联平面到场景。】
mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent);
6. To get anything other than a white plane, set the material of the plane to an
existing material:
【 为了得到一个不同于白色的平面,设置平面的纹理为一个已经存在的材质。】
ent->setMaterialName("Examples/BeachStones");
- Compile the application and run it. You should see some dark stones.
【 编译程序并运行,你将会看到一些暗石头。】
We have inverted the colors for ease of reading!
【 我们转变了文字的颜色以便于阅读!】(译者注:估计是指的书的文字颜色。)
What just happened?
【 刚刚发生了什么?】
We just created a plane and added it to the scene. Step 2 created an instance of
Ogre::Plane. This class describes a plane using the normal vector of the plane and
an offset from the null point using the normal vector.
A normal vector (or in short, just normal) is an often-used construct in 3D graphics.
The normal of a surface is a vector that stands perpendicular on this surface. The
length of the normal is often 1 and is used extensively in computer graphics for light
and occlusion calculation.
【
我们刚刚创建了一个平面并且把它添加到了场景中。在第二部中我们创建了一个Ogre::Plane的实例。这个类描述了一个使用法向量和原点偏移量的平面。
一个法向量(或平面法向量)是在3D图形学中一个常用的概念。一个平面法向量指的是一个垂直于平面的向量。法向量的长度通常是 1 并且它被广泛的应用的计算机图形学的灯光计算和遮挡计算。
】
In Step 3, we used the plane definition to create a mesh out of it. To do this, we used the Ogre MeshManager. This manager manages meshes, which shouldn't be a surprise. Besides managing meshes that we loaded from a file, it can also create planes from our plane definition, as well as a lot of other things.
【 在第三步中,我们使用了一个定义一个外面有网格的平面。为了实现这个,我们使用了Ogre MeshManager(Ogre 网格管理器)。这个管理器管理着场景中的网格。除了管理从文件加载的网格,这个管理器也创建一个由我们自己定义的平面,当然也创建别的一些东西。】
Ogre::MeshManager::getSingleton().createPlane("plane",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane,
1500,1500,20,20,true,1,5,5,Vector3::UNIT_Z);
Besides the plane definition, we need to give the plane a name. When loading meshes from the disk, the file's name is used as the resource name, resource name. It also needs an resource group it belongs to, resource groups are like namespaces in C++. The third parameter is the plane definition and the fourth and fifth parameters are the size of the plane. The sixth and seventh parameters are used to say how many segments the plane should have. To understand what a segment is, we will take a small detour on how 3D models are represented in 3D space.
【 除了定义平面,我们需要给定义的平面一个名称。当从磁盘加载网格的时候,该文件的名称作为该资源的名称。它也需要一个属于它的资源组,这个资源组就好像C++的命名空间一样。第三个参数是定义的平面然后第四个参数和第五个参数是定义平面的面积大小。第六个和第七个参数是用于描述平面被切片的程度。为理解到底什么是切片,我们将会绕一个小弯,先给大家讲述一下在3D空间中3D 模型是如何表示的。】
Representing models in 3D
【 在3D空间中表示模型 】
To render a 3D model, it needs to be described in a way a computer can understand and render
it most effectively. The most common form to represent 3D models in real-time application is
triangles. Our plane can be represented using two triangles to form a quad. With the segment
option for the x-and the y-axis, we can control how many triangles are generated for the
plane. In the following image, we see the triangles that make up the plane with one, two, or
three segments for each axis. To see this effect, we start the application and then press the R
key. This will switch the rendering mode first to wireframe mode, where we see the triangles.
Another key press will change the mode to point mode, where we see only the points of the
triangles. Another press will set the render mode to normal.
【 渲染一个3D的模型需要以某种计算机可以理解并且可以渲染的更有效率的描述方式。在实时程序中描述3D模型的最常见形式就是三角形。我们的平面可以用两个三角形可以组成一个四边形的方式来表示。因为切片的有X和Y轴的大小的平面参数,我们可以控制用多少三角形来生成一个平面。在下面的图片中,我们将会看到用每个轴一个,二个或者三个三角形切片组成平面。为看到这种效果,我们运行程序然后按下R键。这样就可以从第一渲染模式变为线框模式,这样我们就可以看到三角形了。再按一下R键将会改变现有模式为点模式,我们将会三角形的顶点了。再按一下R键将会改变为正常的渲染模式。 】
After we have defined how many segments we want, we pass a Boolean parameter
which tells Ogre 3D that we want the normal of the plane to be calculated. As said before,
a normal is a vector which stands vertically on the surface of the plane. The next three
parameters are used for texturing. To texture something all points need texture coordinates.
Texture coordinates tell the render engine how to map the texture onto the triangle.
Because a picture is a 2D surface, the texture coordinates also consist of two values,
namely, x and y. They are presented as a tuple (x,y). The value range of texture coordinates
is normalized from zero to one. (0,0) means the upper-left corner of the texture and (1,1)
the bottom-right corner. Sometimes the values can be greater than 1. This means that the
texture could be repeated depending on the set mode. This topic will be explained in a later
chapter extensively. (2,2) could repeat the texture twice across both axis. The tenth and
eleventh parameters tell Ogre 3D how often we want the texture to be tiled across the plane.
The ninth parameter defines how many textures' coordinates we want. This can be useful
when working with more than one texture for one surface. The last parameter defines the
"up" direction for our textures. This also affects how the texture coordinates are generated.
We simply say that the z-axis should be "up" for our plane.
【 在定义完我们想要的切片的程度,我们传递一个布尔的参数来告诉Ogre 3D 平面的法向量是否被计算。正如之前所述,法向量是垂直于平面的一个向量。最后的那三个参数是作用用于纹理。渲染的纹理时,所有的点都需要纹理坐标。纹理坐标告诉渲染引擎如何映射材质到三角形。因为一张图片是一个2D的表面,纹理的坐标包含两个值即—— x 和 y 。它们被表示为一个数组(x,y)。纹理坐标值正常初始化的范围为从0到1。(0,0)表示纹理的左上角,(1,1)表示为右下角。有时候它们的值会大于1,这表示纹理可以根据设置模式来进行重复。这个话题我们将在接下来的章节展开来谈。(2,2)可能表示纹理横跨两轴重复两次。第十和第十一个参数告诉Ogre 3D我们想要纹理平铺平面的频率。第九个参数定义了我们需要多少个纹理坐标系。当我们使用超过1个的表面纹理的时候,这个参数就变的很有用了。最后一个参数定于了纹理”up”的方向。这也会影响到纹理坐标的生成。我们简单的说Z轴应”up”我们的平面。】
In step 4, we created an instance of the plane that we just created with the MeshManager.
To do this, we need to use the name we gave the plane during creation. Step 5 attached the
entity to the scene.
【 在第四步,我们创建了一个刚经过MeshManage(网格管理器)创建的平面的实例。要做到这一点,我们需要使用在创建过程中我们给予平面的名称。在第五步中我们关联实体到场景。】
In step 6, we set a new material to the instance of the entity. Each entity has a material
assigned to it. The material describes which texture to use, how the lighting interacts with
the material, and much more. We will learn about all of this in the chapter on materials.
The plane we created doesn't have a material yet and therefore would be rendered white.
Because we want to see the effect of lights we create later, white isn't the best color to use.
We used a material that is already defined in the media folder. This material simply adds a
stone texture to our plane.
【 在第六步中,我们设置了实体实例的一个新材质。每个实体会有一个材质分配给它。这个材质描述了我们使用的纹理所具有的灯光与材料的相互作用。在我们设置这个创建好的平面纹理之前,它将会被渲染为白色。因为我们想要看到创建的灯光的效果,但是白色不是可使用的最佳颜色。我们使用了一个已经在media文件夹下定义的材质。这个材质简单对我们的平面的添加了石头纹理。 】