GPU编程实验三.docx

  1. 1、本文档共11页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
GPU编程实验三

GPU编程实验三实验内容Per-Vertex LightingPer-fragment lightingTexture MappingCube Mapping完成步骤Per-Vertex Lighting新建Position effect文件在Vertex Shader里加代码(关于打灯光的一系列变量)//Light source:uniform vec4 lightPos;uniform vec4 specularLight; uniform vec4 diffuseLight; uniform vec4 ambientLight; //Material reflection properties:uniform vec4 Ke;uniform vec4 Ka; uniform vec4 Kd; uniform vec4 Ks; uniform float n_specular; //Eye positionuniform vec4 eyePos;在左侧Workspace中把变量添加进来双击变量,调整变量数值。Ka, Kd, Ks在0 — 1之间;n_specular在 1 —200注意,eyePos是ViewPosition型语义参数添加varying vec4 ColorAtVertex变量到Vertex Shader和 Fragment Shader里,作为传递变量。计算法线N、光源向量L,再算出反射向量R,最后计算R在眼睛方向上的分量V。// Output transformed vertex position: gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; //or gl_Position = ftransform(); vec4Pos = gl_Vertex; // Compute the light vector in world space: vec3 L = normalize(lightPos.xyz - Pos.xyz); vec3 N = normalize(gl_Normal); // Compute light reflection vector world space: vec3 R = reflect(-L, N); // Compute view vector in world space: vec3 V = normalize(eyePos.xyz-Pos.xyz);根据基本光照模型计算光照。基本光照模型及实现代码如下://Compute ambient term: vec4 ambient = ambientLight * Ka; // Compute diffuse term: vec4vDiff = diffuseLight * Kd * max(0.0, dot(N, L)); // Compute specular term: vec4vSpec = specularLight * Ks * pow(max(0.0, dot(R, V)), n_specular); ColorAtVertex = Ke + ambient + vDiff + vSpec ;在Fragment Shader里:gl_FragColor = ColorAtVertex;调整参数数值,使显示效果更使人满意。实验效果图:(紫色为世界坐标下的效果;橙色为在视坐标下的效果)总结:世界坐标系下,光源是随视线而动的;故改变视线时,亮点都在茶壶的同一位置;视坐标系下,光源是固定的,故改变视线时,茶壶上的亮点会动;Per-fragment lighting原理步骤与上述Per-Vertex 差不多,只是把主要代码转移到Fragment Shader中实现实验效果图:Per-fragment效果较好,它能提高渲染结果的准确性。它是通过插值物体表面的顶点和发向 ?而不是每个顶点的颜色,传递到fragment shader 中进行光照模型计算。(比如,高光在一个polygon中间时,用逐点Per-Vertex显然会影响精确度)Texture Mapping在Vertex Shader中:varying vec2 Texcoord;void main(void){gl_Position = ftransform();Texcoord = gl_MultiTexCoord0.xy;}在FagmentShader中:添加变量把贴图引进来;添加变量过程不再赘述。uniform sampler2D baseMap;uniform sampler2D maskMap;void main(){vec4TexCol= texture2D( baseMap, Texco

文档评论(0)

xcs88858 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:8130065136000003

1亿VIP精品文档

相关文档