实现逐顶点的ambient,diffuse,andspecularshadingphong光照模型_第1页
实现逐顶点的ambient,diffuse,andspecularshadingphong光照模型_第2页
实现逐顶点的ambient,diffuse,andspecularshadingphong光照模型_第3页
实现逐顶点的ambient,diffuse,andspecularshadingphong光照模型_第4页
实现逐顶点的ambient,diffuse,andspecularshadingphong光照模型_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实现逐顶点的ambient, diffuse, and specular (ADS) shadingphong 光照模型本文参照了OpenGL 4.0 Shading Language CookbookOpenGL 4.0 渲染管线如图: 1. shader 数据的输入可以是从OpenGL main application中输入或者是来自上个渲染管线,例如一个fragment shader 的输入可能来自vertexshader 的 output 变量也可能来自程序的uniform变量。2. 每个顶点会执行一次 vertex shader ( 通常是并行进行) 。3. 顶点位置在 vertex

2、 shader 中必须被转换到裁剪坐标系(clip coordinates)并赋值给gl_position变量。4.在vertexshader和fragmentshader 之间的渲染管线中,顶点被装配成片元(primiteves)、然后裁切(clipping)、应用viewporttransformation、然后进行光栅化和填充多边形 (如果需要的话)。每个多边形的片元 fragment或 (像素 pixel) 会执行一次fragment shader(通常是并行)- ( The fragmentshader is executed once for each fragment (pixe

3、l) of the polygonbeing rendered (typically in parallel).5. 来自vertex shader的数据默认情况下是 通过插值以透视的方式提供给fragment shader 然后 fragment shader 确定 最终像素的颜色 并发送给 frame buffer 。6. 深度值信息被自动处理 Implementing per-vertex ambient,diffuse, and specular (ADS)shadingOpenGL 固定的渲染管线实现了一个默认的光照系统(和本文的Ads 模型类似)它综合了环境光Ambient、漫反射

4、 Diffuse 、镜面光 Specular。The ambient component is intended to model light that has been reflected so many times that it appears to be emanating uniformly (均匀地 散发) fromall directionsThe diffuse component wasomnidirectional(全方位的) reflectionrepresents 见用单光源实现逐顶点漫反射光照模型The specular component models the sh

5、ininess of the surface and represents reflection around a preferred directionCombining these three components together can model a nice (but limited) variety of surface types. This shading model is also sometimes called thePhong reflection model (or Phong shading model), after Bui Tuong Phong 如下图的 A

6、DS 光照模型 ADS 光照模型的总强度是 Ambient 、Diffuse 、和 Specular 的光照强度总和。 The ambient component represents light that illuminates all surfaces equally and reflects equally in all directions 。 It is often used to help brighten some of the darker areas within a scene。由于环境光 (Ambient )不受入射光和出射光的影响,因此可以简单的用光源光照密度(La)

7、和物体便面的反射系数(Ka)Diffuse 光照模型镜面反射模型:计算反射光的公式:要对镜面反射(Specular)建模,需要这几个向量(都是单位化的),从物体表面到光源的方向向量( S),理想反射光方向向量( r) ,朝向 viewer 的方向向量( V ),和物体表面的法线向量 (n),如下图显示 We would like the reflection to be maximal when the viewer is aligned with the vector r, and to fall off quickly as the viewer moves furtheraway fro

8、m alignment with r. This can be modeled using the cosine of the angle between v and r raised to some power(f).The specular component creates specular highlights (bright spots) that are typical of glossy surfaces. The larger the power of f in the equation, the smaller the specular highlight and the s

9、hinier the surface appears. The value for f is typically chosen to be somewhere between 1 and 200.最后,最终的 ADS 光照模型公式如下顶点 shadercpp view plain copy print?#version430layout (location = 0) in vec3 VertexPosition;layout(location = 1) in vec3 VertexNormal;out vec3LightIntensity;struct LightInfo vec4 Posit

10、ion; / Lightposition in eye coords.vec3 La;/ Ambient lightintensityvec3 Ld;/ Diffuse light intensityvec3Ls;/ Specular light intensity; uniform LightInfoLight;struct MaterialInfo vec3 Ka;/Ambient reflectivityvec3 Kd;/ Diffusereflectivityvec3 Ks;/ Specular reflectivityfloat Shininess;/ Specular shinin

11、ess factor ; uniformMaterialInfo Material;uniform mat4 ModelViewMatrix;uniform mat3 NormalMatrix;uniform mat4 ProjectionMatrix;uniform mat4 MVP;void main()vec3 tnorm =normalize( NormalMatrix * VertexNormal);vec4eyeCoords = ModelViewMatrix * vec4(VertexPosition,1.0);vec3 s = normalize(vec3(Light.Posi

12、tion - eyeCoords);vec3 v = normalize(-eyeCoords.xyz);vec3 r = reflect( -s,tnorm );float sDotN = max( dot(s,tnorm), 0.0 );vec3 ambient = Light.La * Material.Ka;vec3 diffuse =Light.Ld * Material.Kd * sDotN;vec3 spec = vec3(0.0);if( sDotN > 0.0 )spec = Light.Ls * Material.Ks *pow( max( dot(r,v), 0.0 ), Material.Shininess );LightIntensity = ambient + diffuse + spec;gl_Position

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论