9 6 GLSL sampler sampler1 2 3D 1






![9. 6 GLSL语言 数据类型-采样器(sampler) Ø 纹理的不透明句柄 sampler[1, 2, 3]D 访问 1 D 、 2 9. 6 GLSL语言 数据类型-采样器(sampler) Ø 纹理的不透明句柄 sampler[1, 2, 3]D 访问 1 D 、 2](https://slidetodoc.com/presentation_image_h2/429b6ee715a6d5bfedcc8b44b80ce0f7/image-7.jpg)
















![9. 6 GLSL语言 纹理坐标变换 Ø Open. GL的每一个纹理单元都定义了一个纹理矩 阵, 可用内置的uniform变量 gl_Texture. Matrix访问 gl_Tex. Coord[0] = 9. 6 GLSL语言 纹理坐标变换 Ø Open. GL的每一个纹理单元都定义了一个纹理矩 阵, 可用内置的uniform变量 gl_Texture. Matrix访问 gl_Tex. Coord[0] =](https://slidetodoc.com/presentation_image_h2/429b6ee715a6d5bfedcc8b44b80ce0f7/image-24.jpg)




































- Slides: 60
9. 6 GLSL语言 数据类型-采样器(sampler) Ø 纹理的不透明句柄 sampler[1, 2, 3]D 访问 1 D 、 2 D 、 3 D纹理 sampler. Cube 访问立方图纹理 Ø 例子: uniform sampler 2 D Grass; vec 4 color = texture 2 D(Grass, coord); 7
9. 6 GLSL语言 内置顶点属性(attribute) 应用程序提供顶点信息:位置、法向、颜色、纹理 坐标gl. Vertex, gl. Normal, gl. Color, gl. Tex. Coord, gl. Draw. Arrays 内置顶点属性 attribute vec 4 gl_Vertex; // gl. Vertex attribute vec 4 gl_Color; // gl. Color attribute vec 4 gl_Secondary. Color; // gl. Secondary. Color attribute vec 3 gl_Normal; // gl. Normal attribute vec 4 gl_Multi. Tex. Coordn; //gl. Multi. Tex. Coord(n, …), n=0, … 7 attribute float gl_Fog. Coord; // gl. Fog. Coord 16
9. 6 GLSL语言 特殊输出变量 gl_Position:顶点着色器必须写入的齐次裁剪坐标,将用于 图元装配、裁剪、剔除 gl_Point. Size:将被光栅化的点大小 gl_Clip. Vertex:相对于用户裁剪平面的顶点位置 vec 4 gl_Position; // must be written to float gl_Point. Size; // may be written to vec 4 gl_Clip. Vertex; // may be written to 17
9. 6 GLSL语言 内置常量 const int gl_Max. Lights = 8; const int gl_Max. Clip. Planes = 6; const int gl_Max. Texture. Units = 2; const int gl_Max. Texture. Coords = 2; const int gl_Max. Vertex. Attribs = 16; const int gl_Max. Vertex. Uniform. Components = 512; const int gl_Max. Varying. Floats = 32; const int gl_Max. Vertex. Texture. Image. Units = 0; const int gl_Max. Texture. Image. Units = 2; const int gl_Max. Fragment. Uniform. Components = 64; const int gl_Max. Combined. Texture. Image. Units = 2; const int gl_Max. Draw. Buffers = 1; 19
9. 6 GLSL语言 内置uniform变量 uniform mat 4 gl_Model. View. Matrix; uniform mat 4 gl_Projection. Matrix; uniform mat 4 gl_Model. View. Projection. Matrix; uniform mat 4 gl_Texture. Matrix[gl_Max. Texture. Coords]; uniform mat 3 gl_Normal. Matrix; uniform vec 4 gl_Clip. Plane[gl_Max. Clip. Planes]; uniform gl_Material. Parameters gl_Front. Material; uniform gl_Material. Parameters gl_Back. Material; uniform gl_Light. Source. Parameters gl_Light. Source[gl_Max. Lights]; uniform gl_Light. Model. Parameters gl_Light. Model; uniform gl_Fog. Parameters gl_Fog; 20
9. 6 GLSL语言 固定功能着色器变换 // Transform vertex to clip space gl_Position = gl_Model. View. Projection. Matrix * gl_Vertex; //or this: gl_Position = ftransform(); 21
9. 6 GLSL语言 视点坐标位置计算 Ø 下述代码把顶点位置变换为视点坐标 vec 4 ec. Position; vec 3 ec. Position 3; // in 3 D space // Transform vertex to eye coordinates if (Need. Eye. Position) {ec. Position = gl_Model. View. Matrix * gl_Vertex; ec. Position 3 = (vec 3(ec. Position)) / ec. Position. w; } 22
9. 6 GLSL语言 法向变换 Ø 在视点空间计算光照时,法向也必须变换到视 点 空间。 Ø 法向规范化:gl. Enable(GL_NORMALIZE Ø 法向缩放:缩放比例因子为内置uniform变量 gl_Normal. Scale normal = gl_Normal. Matrix * gl_Normal; normal = normalize(normal); normal = normal * gl_Normal. Scale; 23
9. 6 GLSL语言 纹理坐标变换 Ø Open. GL的每一个纹理单元都定义了一个纹理矩 阵, 可用内置的uniform变量 gl_Texture. Matrix访问 gl_Tex. Coord[0] = gl_Texture. Matrix[0] * gl_Multi. Tex. Coord 0; 24
9. 7 GLSL程序使用 内置顶点属性gl_Color /gl_Normal /gl_Vertex等gl. Begin/gl. End间用gl. Color /gl. Normal /gl. Vertex等函数指定 顶点数组方式 28
9. 8 顶点波动 波动顶点着色器 uniform float time; uniform float xs, zs, // frequencies uniform float h; // height scale void main() { vec 4 t = gl_Vertex; t. y = gl_Vertex. y + h*sin(time + xs*gl_Vertex. x) + h*sin(time + zs*gl_Vertex. z); gl_Position = gl_Model. View. Projection. Matrix*t; } attribute变量和uniform变量在顶点着色器内只读 30
9. 8 顶点波动 Glint time. Param; time. Param = gl. Get. Uniform. Location(program, "time"); void idle() { gl. Uniform 1 f(time. Param, (GLfloat) glut. Get(GLUT_ELAPSED_TIME)); glut. Post. Redisplay(); } 31
9. 9 二维图形渐变 一个顶点通过gl_Vertex传入,对应顶点用顶点属 性变量传入。 attribute vec 4 vertices 2; uniform float time; void main() {float s = 0. 5*(1. 0+sin(0. 001*time)); vec 4 t = mix(gl_Vertex, vertices 2, s); gl_Position = gl_Model. View. Projection. Matrix*t; gl_Front. Color = gl_Color; } mix(x, y, a) 返回x * (1. 0 -a) + y * a 34
9. 9 二维图形渐变 GLint vertices 2 Param; vertices 2 Param=gl. Get. Attrib. Location(program, "vertices 2"); #define N 50 GLfloat vertices_one[N][3], vertices_two[N][3]; gl. Begin(GL_TRIANGLES); for (int i = 0; i < N; i++) { gl. Vertex. Attrib 3 fv(vertices 2 Param, vertices_two[i]); gl. Vertex 3 fv(vertices_one[i]); } gl. End(); 35
9. 10 非真实感着色 Ø 根据光线和法向的夹角给对象赋两种颜色 Ø 根据视线和方向的夹角把对象轮廓赋为黑色 const vec 4 yellow = vec 4(1. 0, 0. 0, 1. 0); const vec 4 red = vec 4(1. 0, 0. 0, 1. 0); const vec 4 black = vec 4(0. 0, 1. 0); if(dot(L, N) > 0. 5) gl_Front. Color = yellow; else gl_Front. Color = red; if(abs(dot(E, N)) < 0. 1) gl. Front. Color = black; 37
9. 11 基于可编程着色器的光照计 算 内置uniform变量 struct gl_Light. Source. Parameters { vec 4 ambient; // Acli vec 4 diffuse; // Dcli vec 4 specular; // Scli vec 4 position; // Ppli vec 4 half. Vector; // Derived: Hi vec 3 spot. Direction; // Sdli float spot. Exponent; // Srli float spot. Cutoff; // Crli // (range: [0. 0, 90. 0], 180. 0) float spot. Cos. Cutoff; // Derived: cos(Crli) // (range: [1. 0, 0. 0], -1. 0) float constant. Attenuation; // K 0 float linear. Attenuation; // K 1 float quadratic. Attenuation; // K 2}; uniform gl_Light. Source. Parameters gl_Light. Source[gl_Max. Lights]; 40
9. 11 基于可编程着色器的光照计 算 struct gl_Light. Model. Parameters { vec 4 ambient; // Acs }; uniform gl_Light. Model. Parameters gl_Light. Model; struct gl_Material. Parameters { vec 4 emission; // Ecm vec 4 ambient; // Acm vec 4 diffuse; // Dcm vec 4 specular; // Scm float shininess; // Srm }; uniform gl_Material. Parameters gl_Front. Material; uniform gl_Material. Parameters gl_Back. Material; 41
9. 11 基于可编程着色器的光照计 算 void main(void) /* modified Phong vertex shader (without distance term) */ { gl_Position = gl_Model. View. Projection. Matrix * gl_Vertex; vec 4 ambient, diffuse, specular; vec 4 eye. Position = gl_Model. View. Matrix * gl_Vertex; vec 4 eye. Light. Pos = gl_Light. Source[0]. position; vec 3 N = normalize(gl_Normal. Matrix * gl_Normal); vec 3 L = normalize(eye. Light. Pos. xyz -eye. Position. xyz); vec 3 E = -normalize(eye. Position. xyz); vec 3 H = normalize(L + E); 43
9. 11 基于可编程着色器的光照计 算 改进的Phong顶点着色器II /* compute diffuse, ambient, and specular contributions */ float f = 1. 0; float Kd = max(dot(L, N), 0. 0); float Ks = pow(max(dot(N, H), 0. 0), gl_Front. Material. shininess); if (dot(L, N) < 0. 0) f = 0. 0; ambient = gl_Front. Light. Product[0]. ambient; diffuse = Kd*gl_Front. Light. Product[0]. diffuse; specular = f*Ks*gl_Front. Light. Product[0]. specular; gl_Front. Color = ambient+diffuse+specular; } 44
9. 11 基于可编程着色器的光照计 算 基于片元光照的顶点着色器 varying vec 3 N, L, E; void main() { gl_Position = gl_Model. View. Projection. Matrix * gl_Vertex; vec 4 eye. Position = gl_Model. View. Matrix * gl_Vertex; vec 4 eye. Light. Pos = gl_Light. Source[0]. position; N = normalize(gl_Normal. Matrix * gl_Normal); L = normalize(eye. Light. Pos. xyz -eye. Position. xyz); E = -normalize(eye. Position. xyz); } 46
9. 11 基于可编程着色器的光照计 算 改进Phong光照片元着色器I varying vec 3 N; varying vec 3 L; varying vec 3 E; void main() { vec 3 Normal = normalize(N); vec 3 Light = normalize(L); vec 3 Eye = normalize(E); vec 3 Half = normalize(Eye + Light); } 47
9. 11 基于可编程着色器的光照计 算 改进Phong光照片段着色器II float f = 1. 0; float Kd = max(dot(Normal, Light), 0. 0); float Ks = pow(max(dot(Half, Normal), 0. 0), gl_Front. Material. shininess); vec 4 diffuse = Kd * gl_Front. Light. Product[0]. diffuse; if (dot(Normal, Light) < 0. 0) f = 0. 0; vec 4 specular = f * Ks * gl_Front. Light. Product[0]. specular; vec 4 ambient = gl_Front. Light. Product[0]. ambient; gl_Frag. Color = ambient + diffuse + specular; } 48
9. 12 采样器(Samplers) 提供对纹理对象的访问 定义了1, 2, 和3维纹理以及立方体贴图的采样器 在着色器中: uniform sampler 2 D my. Texture; vec 2 texcoord; vec 4 texcolor = texture 2 D(mytexture, texcoord); 在应用程序中: tex. Map. Location = gl. Get. Uniform. Location(my. Prog, “my. Texture”); gl. Uniform 1 i(tex. Map. Location, 0); /* assigns to texture unit 0 */ 51
9. 13 立方体贴图 反射贴图顶点着色器 varying vec 3 R; void main(void) { gl_Position = gl_Model. View. Projection. Matrix* gl_Vertex; vec 3 N = normalize(gl_Normal. Matrix*gl_Normal); vec 4 eye. Pos = gl_Model. View. Matrix*gl_Vertex; R = reflect(-eye. Pos. xyz, N); } 58
9. 13 立方体贴图 反射贴图片段着色器 varying vec 3 R; uniform sampler. Cube tex. Map; void main(void) { gl_Frag. Color=texture. Cube(tex. Map, R); } 59






















