大三05计算机图形学ii_第1页
大三05计算机图形学ii_第2页
大三05计算机图形学ii_第3页
大三05计算机图形学ii_第4页
大三05计算机图形学ii_第5页
已阅读5页,还剩25页未读 继续免费阅读

下载本文档

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

文档简介

1、GLSL II原著Ed AngelProfessor of Computer Science, Electrical and Computer Engineering, and Media ArtsUniversity of New Mexico编辑 武汉大学计算机学院图形学课程组2Angel: Interactive Computer Graphics 5E Addison-Wesley 2009ObjectivesCoupling GLSL to ApplicationsExample applications3Angel: Interactive Computer Graphics 5E

2、 Addison-Wesley 2009Linking Shaders to OpenGLOpenGL ExtensionsARB_shader_objectsARB_vertex_shaderARB_fragment_shaderOpenGL 2.0Almost identical to using extensionsAvoids extension suffixes on function names4Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Program ObjectContainer for shaders

3、 Can contain multiple shadersOther GLSL functionsGLuint myProgObj;myProgObj = glCreateProgram(); /* define shader objects here */glUseProgram(myProgObj);glLinkProgram(myProgObj);5Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Reading a ShaderShader are added to the program object and com

4、piledUsual method of passing a shader is as a null-terminated string using the function glShaderSourceIf the shader is in a file, we can write a reader to convert the file to a string6Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Shader Reader#include char* readShaderSource(const char*

5、shaderFile) FILE* fp = fopen(shaderFile, r); char* buf; long size; if(fp = NULL) return(NULL); fseek(fp, 0L, SEEK_END); /* end of file */ size = ftell(fp); fseek(fp, )L, SEEK_SET); /* start of file */Shader Reader (cont) 7Angel: Interactive Computer Graphics 5E Addison-Wesley 2009 buf = (char*) mall

6、oc(statBuf.st_size + 1 * sizeof(char); fread(buf, 1, size, fp); bufsize = 0; /null termination */ fclose(fp); return buf;8Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Adding a Vertex ShaderGLint vShader;GLunit myVertexObj;GLchar vShaderfile = “my_vertex_shader”;GLchar* vSource = readSh

7、aderSource(vShaderFile);glShaderSource(myVertexObj, 1, &vertexShaderFile, NULL);myVertexObj = glCreateShader(GL_VERTEX_SHADER); pileShader(myVertexObj);glAttachObject(myProgObj, myVertexObj);9Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Vertex AttributesVertex attributes are named in t

8、he shadersLinker forms a table Application can get index from table and tie it to an application variableSimilar process for uniform variables10Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Vertex Attribute ExampleGLint colorAttr;colorAttr = glGetAttribLocation(myProgObj, myColor);/* my

9、Color is name in shader */GLfloat color4;glVertexAttrib4fv(colorAttrib, color);/* color is variable in application */11Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Uniform Variable ExampleGLint angleParam;angleParam = glGetUniformLocation(myProgObj, angle);/* angle defined in shader */

10、* my_angle set in application */GLfloat my_angle;my_angle = 5.0 /* or some other value */glUniform1f(angleParam, my_angle);12Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Vertex Shader ApplicationsMoving verticesMorphing Wave motionFractalsLightingMore realistic modelsCartoon shaders13A

11、ngel: Interactive Computer Graphics 5E Addison-Wesley 2009Wave Motion Vertex Shader uniform float time;uniform float xs, zs, / frequencies uniform float h; / height scalevoid main() vec4 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_M

12、odelViewProjectionMatrix*t;14Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Particle Systemuniform vec3 init_vel;uniform float g, m, t;void main()vec3 object_pos;object_pos.x = gl_Vertex.x + vel.x*t;object_pos.y = gl_Vertex.y + vel.y*t + g/(2.0*m)*t*t;object_pos.z = gl_Vertex.z + vel.z*t

13、;gl_Position = gl_ModelViewProjectionMatrix* vec4(object_pos,1);15Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Modified Phong Vertex Shader Ivoid main(void)/* modified Phong vertex shader (without distance term) */ gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; vec4 ambient, d

14、iffuse, specular; vec4 eyePosition = gl_ModelViewMatrix * gl_Vertex; vec4 eyeLightPos = gl_LightSource0.position; vec3 N = normalize(gl_NormalMatrix * gl_Normal); vec3 L = normalize(eyeLightPos.xyz - eyePosition.xyz); vec3 E = -normalize(eyePosition.xyz); vec3 H = normalize(L + E);16Angel: Interacti

15、ve Computer Graphics 5E Addison-Wesley 2009Modified Phong Vertex Shader II /* compute diffuse, ambient, and specular contributions */ float Kd = max(dot(L, N), 0.0); float Ks = pow(max(dot(N, H), 0.0), gl_FrontMaterial.shininess); float Ka = 0.0; ambient = Ka*gl_FrontLightProduct0.ambient; diffuse =

16、 Kd*gl_FrontLightProduct0.diffuse; specular = Ks*gl_FrontLightProduct0.specular; gl_FrontColor = ambient+diffuse+specular;17Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Pass Through Fragment Shader/* pass-through fragment shader */void main(void) gl_FragColor = gl_Color;18Angel: Intera

17、ctive Computer Graphics 5E Addison-Wesley 2009Vertex Shader for per Fragment Lightingvarying vec3 N, L, E, H;void main() gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; vec4 eyePosition = gl_ModelViewMatrix * gl_Vertex; vec4 eyeLightPos = gl_LightSource0.position; N = normalize(gl_NormalMatr

18、ix * gl_Normal); L = normalize(eyeLightPos.xyz - eyePosition.xyz); E = -normalize(eyePosition.xyz); H = normalize(L + E);19Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Fragment Shader for Modified Phong Lighting I varying vec3 N; varying vec3 L; varying vec3 E; varying vec3 H;void main

19、() vec3 Normal = normalize(N); vec3 Light = normalize(L); vec3 Eye = normalize(E); vec3 Half = normalize(H);20Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Fragment Shader for Modified Phong Lighting II float Kd = max(dot(Normal, Light), 0.0); float Ks = pow(max(dot(Half, Normal), 0.0),

20、 gl_FrontMaterial.shininess); float Ka = 0.0; vec4 diffuse = Kd * gl_FrontLightProduct0.diffuse; vec4 specular = Ks * gl_FrontLightProduct0.specular; vec4 ambient = Ka * gl_FrontLightProduct0.ambient; gl_FragColor = ambient + diffuse + specular;21Angel: Interactive Computer Graphics 5E Addison-Wesle

21、y 2009Vertex vs Fragment Lightingper vertex lightingper fragment lighting22Angel: Interactive Computer Graphics 5E Addison-Wesley 2009SamplersProvides access to a texture objectDefined for 1, 2, and 3 dimensional textures and for cube mapsIn shader: uniform sampler2D myTexture;Vec2 texcoord;Vec4 tex

22、color = texture2D(mytexture, texcoord);In application: texMapLocation = glGetUniformLocation(myProg,“myTexture”);glUniform1i(texMapLocation, 0);/* assigns to texture unit 0 */23Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Fragment Shader ApplicationsTexture mappingsmooth shadingenviron

23、ment mappingbump mapping24Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Cube MapsWe can form a cube map texture by defining six 2D texture maps that correspond to the sides of a boxSupported by OpenGLAlso supported in GLSL through cubemap samplervec4 texColor = textureCube(mycube, texco

24、ord);Texture coordinates must be 3D25Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Environment MapUse reflection vector to locate texture in cube map26Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Environment Maps with ShadersEnvironment map usually computed in world coordi

25、nates which can differ from object coordinates because of modeling matrixMay have to keep track of modeling matrix and pass it shader as a uniform variableCan also use reflection map or refraction map (for example to simulate water)27Angel: Interactive Computer Graphics 5E Addison-Wesley 2009Reflection Map Vertex Shadervarying vec3 R;void main(void) gl_Position = gl_M

温馨提示

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

评论

0/150

提交评论