GLSL II.

Slides:



Advertisements
Similar presentations
Informationsteknologi Wednesday, December 12, 2007Computer Graphics - Class 171 Today’s class OpenGL Shading Language.
Advertisements

MAT 594CM S10Fundamentals of Spatial ComputingAngus Forbes Week 5 : GLSL Shaders Topics: Shader syntax, passing textures into shaders, per-pixel lighting,
Programming with OpenGL Part 1: Background
GLSL II Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New.
GLSL I May 28, 2007 (Adapted from Ed Angel’s lecture slides)
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
TA: Zhicheng Yan, Sushma S Kini, Mary Pietrowicz
Mohan Sridharan Based on slides created by Edward Angel GLSL I 1 CS4395: Computer Graphics.
An Introduction to the OpenGL Shading Language Benj Lipchak Rob Simpson Bill Licea-Kane.
A Non-Photorealistic Fragment Shader in OpenGL 2.0 Bert Freudenberg Institut für Simulation und Graphik University of Magdeburg, Germany.
CS 4363/6353 TEXTURE MAPPING PART II. WHAT WE KNOW We can open image files for reading We can load them into texture buffers We can link that texture.
CS 480/680 Computer Graphics Shader Applications Dr. Frederick C Harris, Jr. Fall 2011.
Lecture by: Martin Deschamps CSE 4431
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
OpenGL Shader Language Vertex and Fragment Shading Programs.
Wilf Comp Shaders. Wilf Comp 4002 Shaders Shaders are programs that you can download onto the card Can be used.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Shader Applications Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics.
Shaders in OpenGL Marshall Hahn. Introduction to Shaders in OpenGL In this talk, the basics of OpenGL Shading Language will be covered. This includes.
CS418 Computer Graphics John C. Hart
Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E.
OpenGL Shading Language (GLSL)
CSE 381 – Advanced Game Programming GLSL. Rendering Revisited.
OpenGL Shading Language (GLSL)
1 Graphics CSCI 343, Fall 2015 Lecture 21 Lighting and Shading III.
GLSL II Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New.
Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Ed Angel Professor Emeritus of Computer Science.
Programmable Pipelines. 2 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Black Box View Geometry Processor Frame Buffer Fragment Processor.
CS 480/680 Computer Graphics Programming with Open GL Part 5: Putting it all together Dr. Frederick C Harris, Jr. Fall 2011.
GLSL I.
Programming with OpenGL Part 5: More GLSL Isaac Gang University of Mary Hardin-Baylor E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley.
OpenGL Shading Language
3D Objects in WebGL Loading 3D model files. Announcement Midterm exam on 12/2 –No Internet connection. –Example code and exam questions will be on USB.
OpenGL Shading Language (GLSL)
Programming with OpenGL Part 3: Shaders Ed Angel Professor of Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive.
OpenGl Shaders Lighthouse3d.com.
GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global.
Advanced Texture Mapping Bump Mapping & Environment Mapping (Reflection)
GLSL II Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New.
MP3.. Start at the very beginning. Almost. Either start from nothing yourself, or use the empty template for this MP. Run through the provided files are.
Environment Maps Ed Angel Professor Emeritus of Computer Science
Shaders, part 2 alexandri zavodny.
Introduction to Computer Graphics with WebGL
Shader.
CSE Real Time Rendering
GLSL II Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New.
Tips for Environment Mapping
Texture Mapping Part II
Objectives Simple Shaders Programming shaders with GLSL
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Introduction to Computer Graphics with WebGL
Programming with OpenGL
Introduction to Shaders
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
ICG 2018 Fall Homework1 Guidance
Programming with OpenGL Part 3: Shaders
Hw03 : shader.
Programming with OpenGL Part 5: More GLSL
Advanced Texture Mapping
Programming with OpenGL Part 5: More GLSL
CS 480/680 Computer Graphics GLSL Overview.
CS 480/680 Part 1: Model Loading.
Computer Graphics Shading in OpenGL
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science
Programmable Pipelines
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

GLSL II

Objectives Coupling GLSL to Applications Example applications GLSL 2

Linking Shaders to OpenGL OpenGL Extensions ARB_shader_objects ARB_vertex_shader ARB_fragment_shader OpenGL 2.0 Almost identical to using extensions Avoids extension suffixes on function names GLSL 2

Program Object Container for shaders GLuint myProgObj; Can contain multiple shaders Other GLSL functions GLuint myProgObj; myProgObj = glCreateProgram(); /* define shader objects here */ glUseProgram(myProgObj); glLinkProgram(myProgObj); GLSL 2

Reading a Shader Shader added to program object and compiled Usual method of passing shader null-terminated string using function glShaderSource If shader in file can write reader to convert file to string GLSL 2

Shader Reader #include <stdio.h> char* readShaderSource(const char* 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 */ GLSL 2

Shader Reader (cont) buf = (char*) malloc(statBuf.st_size + 1 * sizeof(char)); fread(buf, 1, size, fp); buf[size] = '\0'; /null termination */ fclose(fp); return buf; } GLSL 2

Adding a Vertex Shader GLint vShader; GLunit myVertexObj; GLchar vShaderfile[] = “my_vertex_shader”; GLchar* vSource = readShaderSource(vShaderFile); glShaderSource(myVertexObj, 1, &vertexShaderFile, NULL); myVertexObj = glCreateShader(GL_VERTEX_SHADER); glCompileShader(myVertexObj); glAttachObject(myProgObj, myVertexObj); GLSL 2

Vertex Attributes Vertex attributes named in shaders Linker forms table Application can get index from table and tie it to application variable Similar process for uniform variables GLSL 2

Vertex Attribute Example GLint colorAttr; colorAttr = glGetAttribLocation(myProgObj, "myColor"); /* myColor is name in shader */ GLfloat color[4]; glVertexAttrib4fv(colorAttrib, color); /* color is variable in application */ GLSL 2

Uniform Variable Example GLint angleParam; angleParam = glGetUniformLocation(myProgObj, "angle"); /* angle defined in shader */ /* my_angle set in application */ GLfloat my_angle; my_angle = 5.0 /* or some other value */ glUniform1f(angleParam, my_angle); GLSL 2

Vertex Shader Applications Moving vertices Morphing Wave motion Fractals Lighting More realistic models Cartoon shaders GLSL 2

Wave Motion Vertex Shader uniform float time; uniform float xs, zs, // frequencies uniform float h; // height scale void 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_ModelViewProjectionMatrix*t; } GLSL 2

Particle System uniform 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; gl_Position = gl_ModelViewProjectionMatrix* vec4(object_pos,1); } GLSL 2

Modified Phong Vertex Shader I void main(void) /* modified Phong vertex shader (without distance term) */ { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; vec4 ambient, diffuse, specular; vec4 eyePosition = gl_ModelViewMatrix * gl_Vertex; vec4 eyeLightPos = gl_LightSource[0].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); GLSL 2

Modified 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_FrontLightProduct[0].ambient; diffuse = Kd*gl_FrontLightProduct[0].diffuse; specular = Ks*gl_FrontLightProduct[0].specular; gl_FrontColor = ambient+diffuse+specular; } GLSL 2

Pass Through Fragment Shader void main(void) { gl_FragColor = gl_Color; } GLSL 2

Vertex Shader for per Fragment Lighting varying vec3 N, L, E, H; void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; vec4 eyePosition = gl_ModelViewMatrix * gl_Vertex; vec4 eyeLightPos = gl_LightSource[0].position; N = normalize(gl_NormalMatrix * gl_Normal); L = normalize(eyeLightPos.xyz - eyePosition.xyz); E = -normalize(eyePosition.xyz); H = normalize(L + E); } GLSL 2

Fragment Shader for Modified Phong Lighting I varying vec3 N; varying vec3 L; varying vec3 E; varying vec3 H; void main() { vec3 Normal = normalize(N); vec3 Light = normalize(L); vec3 Eye = normalize(E); vec3 Half = normalize(H); GLSL 2

Fragment Shader for Modified Phong Lighting II float Kd = max(dot(Normal, Light), 0.0); float Ks = pow(max(dot(Half, Normal), 0.0), gl_FrontMaterial.shininess); float Ka = 0.0; vec4 diffuse = Kd * gl_FrontLightProduct[0].diffuse; vec4 specular = Ks * gl_FrontLightProduct[0].specular; vec4 ambient = Ka * gl_FrontLightProduct[0].ambient; gl_FragColor = ambient + diffuse + specular; } GLSL 2

Vertex vs Fragment Lighting per vertex lighting per fragment lighting GLSL 2

Samplers Provides access to texture object Defined for 1, 2, and 3 dimensional textures and for cube maps In shader: uniform sampler2D myTexture; Vec2 texcoord; Vec4 texcolor = texture2D(mytexture, texcoord); In application: texMapLocation = glGetUniformLocation(myProg,“myTexture”); glUniform1i(texMapLocation, 0); /* assigns to texture unit 0 */ GLSL 2

Fragment Shader Applications Texture mapping smooth shading environment mapping bump mapping GLSL 2

Cube Maps Can form cube map texture by defining six 2D texture maps correspond to sides of box Supported by OpenGL Also supported in GLSL through cubemap sampler vec4 texColor = textureCube(mycube, texcoord); Texture coordinates must be 3D GLSL 2

Environment Map Use reflection vector to locate texture in cube map GLSL 2

Environment Maps with Shaders Environment map usually computed in world coordinates can differ from object coordinates because of modeling matrix May have to keep track of modeling matrix and pass it shader as uniform variable Can also use reflection map or refraction map (for example to simulate water) GLSL 2

Reflection Map Vertex Shader varying vec3 R; void main(void) { gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex; vec3 N = normalize(gl_NormalMatrix*gl_Normal); vec4 eyePos = gl_ModelViewMatrix*gl_Vertex; R = reflect(-eyePos.xyz, N); } GLSL 2

Refelction Map Fragment Shader varying vec3 R; uniform samplerCube texMap; void main(void) { gl_FragColor = textureCube(texMap, R); } GLSL 2

Bump Mapping Perturb normal for each fragment Store perturbation as textures GLSL 2

Normalization Maps Cube maps can be viewed as lookup tables 1-4 dimensional variables Vector from origin is pointer into table Example: store normalized value of vector in map Same for all points on that vector Use “normalization map” instead of normalization function Lookup replaces sqrt, mults and adds GLSL 2