about 图形学中的纹理贴图,结合上课课件和博文

属于图形学系列文章

背景

Per-vertex color is insufficient for realistic appearance

Naïve solution: using very fine meshes expensive!

Texture Mapping: Wrap (map) a image onto a surface (like gift wrapping),Fragment colors are given by the image contents

纹理管线

第一步。通过将投影方程(projector function)运用于空间中的点 ,从而得到一组称为参数空间值(parameter-space values)的关于纹理的数值。

第二步。在使用这些新值访问纹理之前,可以使用一个或者多个映射函数(corresponder function)将参数空间值(parameter-space values )转换到纹理空间。

第三步。使用这些纹理空间值(texture-space locations)从纹理中获取相应的颜色值(obtain value)。

第四步。再使用值变换函数(value transform function)对检索结果进行值变换,最后使用得到的新值来改变表面属性,如材质或者着色法线等等。

具体的例子:

找到物体空间中的位置(x,y,z),比如点(-2.3,7.1,88.2),然后对该位置运用投影函数。这个投影函数通常将向量(x,y,z)转换为一个二元向量(u,v)。在此示例中使用的投影函数是一个正交投影,类似一个投影仪,将具有光泽的砖墙图像投影到多边形表面上。再考虑砖墙这边,其实这个投影过程就是将砖墙平面上的点变换为值域为0到1之间的一对(u,v)值,如图,(0.32,0.29)就是这个我们通过投影函数得到的uv值。而我们图像的分辨率是256 x 256,所以,将256分别乘以(0.32,0.29),去掉小数点,得到纹理坐标(81, 74)。通过这个纹理坐标,可以在纹理贴图上查找到坐标对应的颜色值,所以,我们接着找到砖块图像上像素位置为(81,74)处的点,得到颜色(0.9,0.8,0.7)。而由于原始砖墙的颜色太暗,因此可以使用一个值变换函数,给每个向量乘以1.1,就可以得到我们纹理管线过程的结果——颜色值(0.99,0.88,0.77)。

坐标系变换

Texture Space to Object Space

The texture images will be stretched differently at different places on the surface (based on the curvature)

Two methods: direct & indirect

Direct Method

参数表示: For each vertex of a tessellated object at P = f(u, v), assign (s, t), a normalized version of (u, v), as the texture coordinate of P 这个也叫 投影函数 (将空间中的三维点转化为纹理坐标)

例子:

Open Cylinder

Sphere

Indirect Method

Define intermediate simpler surface (e.g., plane, sphere, or cylinder) and parameterize it. Enclose arbitrary geometry(对象) within the intermediate one 选择的简单表面越像,效果越好

Object Space to Screen Space

这个时候给定物体的坐标,我们已经能知道它对应的纹理坐标了。那我们该怎么映射到 screen space 呢?

这部分没时间去弄懂了,希望考试不要踩到

Sampling the Texture

当一个比较小的贴图,贴到一个比较大的3D模型上,就会存在一个问题,3D模型上的点,不会正好对应贴图上的像素,3D模型上的点可能就会落在贴图上像素点与像素点之间。

Nearest-Neighbor Sampling

如果选择Point模式,就会选择贴图对应3D模型上的点,距离最近的一个贴图像素点,来插值采样,纹理像素看起来就会一块块的,形似马赛克。

Let I_1 be a 1D texture with n pixels

To evaluate I_1(u) for some 0 < u < 1:

  • Let i = floor(n*u), which is an integer
  • Return I_1[i]
  • Returns the color of the pixel that is closest to u

OpenGL -> GL_NEAREST 模式

Linear Sampling

如果选择Bilinear模式,就会选择贴图对应3D模型上的点,周围的四个贴图像素点,来插值采样,纹理像素看起来就会过渡的更加自然。

条件同上一个:

  • Let i = floor(n*u), which is an integer
  • Let w = fract(n*u) = n*u – i, which is a float between 0 and 1
  • Return (1 - w)*I_1[i] + w*I_1[i + 1]
  • Linearly interpolates the values of two neighboring pixels

但是还是会产生走样(Aliasing)的问题问题 :像素采样不足

那我们该如何 Anti-Aliasing (AA) 呢?

  1. more samples -> slow
  2. Prefiltering

Idea: subsampling the textures to reduce its frequency content

At render time, one queries color values from the subsampled image to reduce aliasing

The lower frequency content will make the sampling adequate

Mipmaping

Progressively subsample the original image into a number of levels

Up one level, half the resolution

如果选择Trilinear模式,还会在 Mip map(多级渐远纹理)之间进行混合,如果纹理没有采用Mip map技术,那么Trilinear得到结果跟Bilinear是一样的。

Mip maps是一个逐渐缩小的图像列表,用于优化实时3D引擎的性能。远离相机的物体使用较小的Texture版本。使用Mip maps需要使用33%以上的内存,但不使用它会导致巨大的性能损失。

缺点:运行时占用更多内存,且增加包的容量。因为Mip maps会根据摄像机远近不同而生成对应的八个贴图,运行会加载到内存中。

优点:优化显存带宽,用来减少渲染。因为可以根据距离摄像机远近,选择适合的贴图来渲染。

一般不需要距离摄像机靠近清晰,否则模糊的这种效果,取消勾选Generate Mip Maps选项。

勾选Mip maps,对处理锯齿和闪烁的很有用。

Bump Mapping 凹凸映射

Goal: Simulate detailed geometry without adding actual geometry (i.e., vertices, faces)

凹凸贴图是指计算机图形学中在三维环境中通过纹理方法来产生表面凹凸不平的视觉效果。它主要的原理是通过改变表面光照方程的法线,而不是表面的几何法线,或对每个待渲染的像素在计算照明之前都要加上一个从高度图中找到的扰动,来模拟凹凸不平的视觉特征,如褶皱、波浪等等。

Blinn于1978年提出了凹凸贴图方法。使用凹凸贴图,是为了给光滑的平面,在不增加顶点的情况下,增加一些凹凸的变化。他的原理是通过法向量的变化,来产生光影的变化,从而产生凹凸感。实际上并没有顶点(即Geometry)的变化。

表示凹凸效果的另一种方法是使用高度图来修改表面法线的方向。每个单色纹理值代表一个高度,所以在纹理中,白色表示高高度区域,黑色是低高度的区域(反之亦然)。

Normal Perturbation Theory

Implementation

操作步骤

  1. B(u, v) 初始化为 a gray scale texture
  2. Find the image Bu(u, v) by subtracting every pixel from its right neighbor(一般认为u是水平的)
  3. Find the image Bv(u, v) by subtracting every pixel from its bottom neighbor

最后:

实现步骤

  1. Store bump/normal maps as 2D textures
  2. At render time, query these textures per point (i.e., fragment) instead of per vertex
  3. Compute shading using perturbed normals

Normal Mapping 法线贴图

法线贴图(Normal mapping)是凸凹贴图(Bump mapping)技术的一种应用,法线贴图有时也称为“Dot3(仿立体)凸凹纹理贴图”。凸凹与纹理贴图通常是在现有的模型法线添加扰动不同,法线贴图要完全更新法线。与凸凹贴图类似的是,它也是用来在不增加多边形的情况下在浓淡效果中添加细节。但是凸凹贴图通常根据一个单独的灰度图像通道进行计算,而法线贴图的数据源图像通常是从更加细致版本的物体得到的多通道图像,即红、绿、蓝通道都是作为一个单独的颜色对待。

简单来说,Normal Map直接将正确的Normal值保存到一张纹理中去,那么在使用的时候直接从贴图中取即可。

Since the blue channel is always 1, it is bluish in appearance

Displacement Mapping 移位贴图

直接作用于 vertex,根据 displacement map 中对应的 vertex 的像素值,使 vertex 沿法向移动,产生真正的凹凸表面

more expensive!

Environment Mapping 环境映射

问题:Simple light sources (e.g., point, directional) create unnaturally harsh illuminations. In reality, light usually come from all directions

Environment lighting is capable of producing much more realistic results

Environment Map:

  • A special type of texture used to specify environment lighting
  • Describe incident light from all directions
  • Generate the map of the environment (On a sphere, cube or paraboloid)
  • Use a view-dependent mapping on the geometry

Generating Environment Maps:

  • Using physical measurements
    • Light probe: taking pictures of a small spherical mirror (e.g., a stainless steel ball)
  • Using computer simulation
    • Ray tracing

Cube Mapping(天空盒)

Cubemap是六个方形纹理表示对环境的反射集合。六个方形形成围绕物体的假想立方体的面。通常用于捕捉物体的反射和周围环境。例如,天空盒(Skybox)和环境反射(Reflection Probe),可用于模拟不同光滑度平面的反射。

Projective Textures

Project a texture on 3D object:

Designing Theatres:

Projective textures 一般用来实现一些复杂的视觉效果,可能需要多次渲染

Shadows: points visible to the eye but invisible to the light are in shadow

Shadow Mapping

Rendering Pass 1

Rendering Pass 2

Creates “hard shadows”

Fast but not very accurate: shadows can look pixelated when the shadow map has insufficient resolution

Prefiltering the shadow map helps reducing such artifacts