首页>
技术资讯>
详情

在游戏制作中如何投影一个纹理

2016-05-29 来源:CloudBest 阅读量: 393
关键词: 特效处理


  投影一个纹理图象到你自己合成的环境里在许多步骤上和投影渲染场景到显示器上的步骤是一样的。投影纹理的关键是纹理变换矩阵的内容。该变换矩阵由以下三个变换串联起来:
  
  1.视图/模型变换 -- 朝着需要投影到场景的方向。
  
  2.投影变换(透视或者正交)
  
  3.缩放和偏移(bias 是叫偏移吗?),它将近裁剪面到纹理坐标。
  
  Projecting a texture image into your synthetic environment requires many of the same steps that are used to project the rendered scene onto the display. The key to projecting a texture is the contents of the texture transform matrix. The matrix contains the concatenation of three transformations:
  
  1. A modelview transform to orient the projection in the scene.
  
  2. A projective transform (perspective or orthogonal).
  
  3. A scale and bias to map the near clipping plane to texture coordinates.
  
  纹理变换中模型/视图变换部分可以和普通的图形管道一样计算,可以使用一般的模型/视图变换工具,举个例子,你可以使用gluLookAt()来投射一个观察方向,也可以用glFrustum或者gluPerspective()来定义一个透视变换。
  
  The modelview and projection parts of the texture transform can be computed in the same way, with the same tools that are used for the modelview and projection transform. For example, you can use gluLookat() to orient the projection, and glFrustum() or gluPerspective() to define a perspective transformation.
  
  模型/视图变换的作用和它在OpenGL 观察管道里的作用是一样的,它将观察者沿-Z方向移动到原点和投影中心,在这种情况下,观察者就好比是是光源,近裁剪平面好比是需要投影的纹理图象的所处的位置,纹理图象可以被看作一个在投影的透明胶片。或者,你也可以想象一个观察者在观察位置,透过近平面上纹理去看那些需要被贴上纹理(投影纹理)的表面。
  
  The modelview transform is used in the same way as it is in the OpenGL viewing pipeline, to move the viewer to the origin and the projection centered along the negative z axis. In this case, viewer can be thought of a light source, and the near clipping plane of the projection as the location of the texture image, which can be thought of as printed on a transparent film. Alternatively, you can conceptualize a viewer at the view location, looking through the texture on the near plane, at the surfaces to be textured.
  
  投影操作把眼空间变换到规格化的设备空间,在这个空间里,x,y,z坐标的范围都是-1到1。投影纹理则可以被想象成安放在投影方向的近平面上,这个投影就是由模型/视图和投影变换矩阵定义的。
  
  The projection operation converts eye space into Normalized Device Coordinate (NDC) space. In this space, the x, y, and z coordinates range from -1 to 1. When used in the texture matrix, the coordinates are s, t, and r instead. The projected texture can be visualized as laying on the surface of the near plane of the oriented projection defined by the modelview and projection parts to the transform.
  
  变换的最后一部分是对纹理映射进行缩放和偏移,它让文理坐标的范围变成0到1,这样,整个纹理图象(或者期望的区域)能覆盖整个投影近平面。因为近平面被定义为NDC(规格化设备坐标)坐标。把NDC坐标下的近平面对应到纹理图象上需要把s和t方向的坐标都缩小1/2,然后平移1/2。(注:[-1,1] * 1/2 + 1/2 = [0,1])。纹理图象将居中而且覆盖整个近平面(我一直没有看懂Back Plane是什么)。纹理在投影图象的方向被改变时候也可以旋转。
  
  The final part of the transform scales and biases the texture map, which is defined in texture coordinates ranging from 0 to 1, so that the entire texture image (or the desired portion of the image) covers the near plane defined by the projection. Since the near plane is now defined in NDC coordinates, Mapping the NDC near plane to match the texture image would require scaling by 1/2, then biasing by 1/2, in both s and t. The texture image would be centered and cover the entire back plane. The texture could also be rotated if the orientation of the projected image needed to be changed.
  
  投影的次序和普通的图形管道是一样的。首先是Model/View变换,然后是投影变换,最后是缩放和平移近平面的位置到纹理图象上去:
  
  1. glMatrixMode(GL_TEXTURE);
  
  2. glLoadIdentity();开始。
  
  3. glTranslatef(0.5f,0.5f,0.5f);
  
  4. glScalef(0.5f,0.5f,1.0f);
  
  5. 设置投影矩阵(如:glFrustum())
  
  6. 设置视图/模型矩阵(如:gluLookAt())。
  
  The projections are ordered in the same as the graphics pipeline, the modelview transform happens first, then the projection, then the scale and bias to position the near plane onto the texture image:
  
  1. glMatrixModeGL_TEXTURE(GL_TEXTURE)
  
  2. glLoadIdentity() (start over)
  
  3. glTranslatef.5f, .5f, 0.f(.5f, .5f, 0.f)
  
  4. glScalef.5f, .5f, 1.f(.5f, .5f, 1.f) (texture covers entire NDC near plane)
  
  5. Set the perspective transform (e.g., glFrustum()).
  
  6. Set the modelview transform (e.g., gluLookAt()).
  
  那么该如何定义图元

热门推荐 查看更多