Brick Shader Overview
砖块Shader概要
One approach to writing shaders is to come up with a description of the effect that you're trying to achieve and then decide which parts of the shader need to be implemented in the vertex shader, which need to be implemented in the fragment shader, and how the application will tie everything together.
一种实现shader的方法是,你先提出一个要实现效果的描述,然后再决定这个shader的哪些部分由vertex shader来实现,哪些部分应该在fragment shader中实现,并且最桥头应用程序该如何将这些联结起来。
In this example, we develop a shader that applies a computed brick pattern to all objects that are drawn. We don't attempt the most realistic looking brick shader, but rather a fairly simple one that illustrates many of the concepts we introduced in the previous chapters. We don't use textures for this brick pattern; the pattern itself is generated algorithmically. We can build a lot of flexibility into this shader by parameterizing the different aspects of our brick algorithm.
在这个例子中,我们开发一个能实现绘制砖块花纹(到所有被绘制物体的)shader,我们不试图实现最真实的砖块效果shader,而是一个相当简单的示例,例子中有许多我们在之前章节介绍过的概念。我们并不使用纹理来绘制砖块纹理,花纹由算法自动生成。通过使用不同的砖块算法,我们可以建立一个非常灵活的shader。
Let's first come up with a description of the overall effect we're after. We want
A single light source
Diffuse and specular reflection characteristics (反射特征)
A brick pattern based on the position in modeling coordinates of the object being renderedwhere the x coordinate is related to the brick horizontal position and the y coordinate is related to the brick vertical position
Alternate rows of bricks offset by one-half the width of a single brick
Easy-to-modify colors and ratios: brick color, mortar color, brick-to-brick horizontal distance, brick-to-brick vertical distance, brick width fraction (ratio of the width of a brick to the overall horizontal distance between two adjacent bricks), and brick height fraction (ratio of the height of a brick to the overall vertical distance between two adjacent bricks)
The brick geometry parameters that we use to control geometry and color are illustrated in Figure 6.1. Brick size and brick percentage parameters are both stored in user-defined uniform variables of type vec2. The horizontal distance between two bricks, including the width of the mortar, is provided by BrickSize.x. The vertical distance between two rows of bricks, including the height of the mortar, is provided by BrickSize.y. These two values are given in units of modeling coordinates. The fraction of BrickSize.x represented by the brick only is provided by BrickPct.x. The fraction of BrickSize.y represented by the brick only is provided by BrickPct.y. These two values are in the range [0,1]. Finally, the brick color and the mortar color are represented by the variables BrickColor and MortarColor.
砖块尺寸和砖块比例参数都被存储于用户定义的vec2类型uniform变量,BrickSize.x代表:水平方向上的两个砖块的距离(包含水泥浆的宽度);BrickSize.y代表:垂直方向上的两砖块的距离(包含水泥浆的高度)。这两个值是以模型坐标单位给出的。
Now that we're armed with a firm grasp of our desired outcome, we'll design our vertex shader, then our fragment shader, and then the application code that will tie it all together.
既然我们已经牢固地理解了我们想要的结果,我们将设计我们的vertex shader,然后设计fragment shader,最后应该程序代码将会把它们联系起来。
注意的词组:
1、come up with (针对XX问题)提出(注意,想法); come up with a description
2、approach to 接近,通往...的方法;
3、overall 全面的。
4、armed with 用…… 做准备;备有