Shaders in OpenGL Marshall Hahn. Introduction to Shaders in OpenGL In this talk, the basics of OpenGL Shading Language will be covered. This includes.

Slides:



Advertisements
Similar presentations
EECS 700: Computer Modeling, Simulation, and Visualization Dr. Shontz Chapter 2: Shader Fundamentals (continued)
Advertisements

GLSL Basics Discussion Lecture for CS 418 Spring 2015 TA: Zhicheng Yan, Sushma S Kini, Mary Pietrowicz.
Informationsteknologi Wednesday, December 12, 2007Computer Graphics - Class 171 Today’s class OpenGL Shading Language.
MAT 594CM S10Fundamentals of Spatial ComputingAngus Forbes Week 4 : GLSL Shaders Topics: Shader programs, vertex & fragment shaders, passing data into.
MAT 594CM S10Fundamentals of Spatial ComputingAngus Forbes Week 5 : GLSL Shaders Topics: Shader syntax, passing textures into shaders, per-pixel lighting,
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.
Mohan Sridharan Based on slides created by Edward Angel GLSL I 1 CS4395: Computer Graphics.
GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.
REAL-TIME VOLUME GRAPHICS Christof Rezk Salama Computer Graphics and Multimedia Group, University of Siegen, Germany Eurographics 2006 Real-Time Volume.
COMP 261 Lecture 14 3D Graphics 2 of 2. Doing better than 3x3? Translation, scaling, rotation are different. Awkward to combine them. Use homogeneous.
WebGL: Hands On Zhenyao Mo Software Engineer, Google, Inc. Chrome GPU Team.
GPU Programming Robert Hero Quick Overview (The Old Way) Graphics cards process Triangles Graphics cards process Triangles Quads.
Programmable Pipelines. Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
An Introduction to the OpenGL Shading Language Benj Lipchak Rob Simpson Bill Licea-Kane.
Computing & Information Sciences Kansas State University Lecture 13 of 42CIS 636/736: (Introduction to) Computer Graphics Lecture 13 of 42 Wednesday, 20.
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum.
Programmable Pipelines. 2 Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
Real-Time High Quality Rendering CSE 291 [Winter 2015], Lecture 4 Brief Intro to Programmable Shaders
Lecture by: Martin Deschamps CSE 4431
OpenGL Shading Language Jian Huang CS594, Spring 2005.
GPU Shading and Rendering: OpenGL Shading Language Marc Olano UMBC.
GRAPHICS PIPELINE & SHADERS SET09115 Intro to Graphics Programming.
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.
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)
GLSL II.
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.
Programmable Pipelines. 2 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Black Box View Geometry Processor Frame Buffer Fragment Processor.
The OpenGL API Patrick Cozzi University of Pennsylvania CIS Fall 2012.
 Learn some important functions and process in OpenGL ES  Draw some triangles on the screen  Do some transformation on each triangle in each frame.
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
OpenGL Shading Language
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.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016.
GLSL Basics Slides by : Wei-Wen Feng.
COMP3421 The programmable pipeline and Shaders. TODO.
Shaders, part 2 alexandri zavodny.
Ying Zhu Georgia State University
Shader.
Carl Johan Gribel, PhD student
Introduction to Computer Graphics with WebGL
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
Day 05 Shader Basics.
Chapter VI OpenGL ES and Shader
Introduction to Computer Graphics with WebGL
Introduction to Shaders
Programming with OpenGL Part 3: Shaders
Mickaël Sereno Shaders Mickaël Sereno 25/04/2019 Mickaël Sereno -
Advanced Texture Mapping
CIS 441/541: Introduction to Computer Graphics Lecture 15: shaders
CS 480/680 Computer Graphics GLSL Overview.
Language Definitions Chap. 3 of Orange Book.
CS 480/680 Part 1: Model Loading.
Computer Graphics Shading in OpenGL
Introduction to Computer Graphics with WebGL
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

Shaders in OpenGL Marshall Hahn

Introduction to Shaders in OpenGL In this talk, the basics of OpenGL Shading Language will be covered. This includes topics such as: vertex shaders, fragment shaders, and how they communicate useful built-in functions and datatypes an example shader that implements bump mapping how to compile, initialize and activate a shader

OpenGL 1.5

OpenGL 2.0

OpenGL Shading Language (GLSL) A C-based language, has similar syntax and flow control a little more restrictive in some areas  Functions: return by value, pass by value Has special built-in variables for input\output. Their names always begin with gl_ Has built-in datatypes for matrices and vectors Useful built-in functions: dot, cos, sin, mix, …

OpenGL Shading Language (GLSL): Swizzling The normal structure-member selector (.) is also used to SWIZZLE components of a vector: select or rearrange components by listing their names after the swizzle operator (.)SWIZZLE vec4 v4; v4.rgba; // is a vec4 and the same as just using v4, v4.rgb; // is a vec3, v4.b; // is a float, v4.xy; // is a vec2, v4.xgba; // is illegal - the component names do not come from // the same set. The component names can be out of order to rearrange the components, or they can be replicated to duplicate the components:

The Three Shader Variable Types Uniform variables: can be changed once per primitive  uniform bool bumpon; Attribute variables: can be changed anytime, they represent attributes that are associated with vertices  attribute vec3 tangentVec; Varying variables: are used communicate between vertex shader and fragment shader. They are automatically interpolated across the polygon.  varying vec3 lightVec;

Normal Maps An image where each pixel represents a normal:  R -> x, G -> y, B -> z Each normal is perturbed a small amount, so normal maps tend to be “bluish” in colour R, G, and B each range from 0.0 to 1.0 N.x = ( R – 0.5 ) * 2.0, …

Normal Mapping using Tangent Space Np is in tangent space, L and V are in WCS So we build a frame at the P using N, T (tangent) and N x T We then transform L and V into this frame

Normal Mapping using Tangent Space Suppose we have two points: S in Tangent Space and O in World Space This is the transformation we need:

Normal Mapping Vertex Shader 1.uniform bool bumpon; 2.attribute vec3 tangentVec; 3.varying vec3 lightVec; 4.varying vec3 eyeVec; 5. 6.void main (void) 7.{ 8. vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex; gl_Position = ftransform(); 11. gl_TexCoord[0] = gl_MultiTexCoord0;//texture mapping stuff vec3 orgLightVec = ( gl_LightSource[0].position.xyz - ecPosition.xyz); vec3 n = normalize(gl_NormalMatrix * gl_Normal); 16. vec3 t = normalize(gl_NormalMatrix * tangentVec); 17. vec3 b = cross(n, t); lightVec.x = dot( orgLightVec, t); 20. lightVec.y = dot( orgLightVec, b); 21. lightVec.z = dot( orgLightVec, n); vec3 position = -ecPosition.xyz; 24. eyeVec.x = dot( position, t); 25. eyeVec.y = dot( position, b); 26. eyeVec.z = dot( position, n); 27.}

Normal Mapping Fragment Shader 1.uniform bool bumpon; 2.uniform sampler2D normalMap;//must initialize with texture unit integer 3.varying vec3 lightVec; 4.varying vec3 eyeVec; 5. 6.void main (void) 7.{ 8. vec3 N = vec3(0.0, 0.0, 1.0); 9. if( bumpon ) N = normalize( ( (texture2D(normalMap, gl_TexCoord[0].xy).xyz) - 0.5) * 2.0 ); vec3 L = normalize(lightVec); 12. vec3 V = normalize(eyeVec); 13. vec3 R = reflect(L, N); float pf = pow( dot(R, V), gl_FrontMaterial.shininess ); vec4 GlobalAmbient = gl_LightModel.ambient; 18. vec4 Ambient = gl_LightSource[0].ambient * gl_FrontMaterial.ambient; 19. vec4 Diffuse = gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse * dot(N, L); 20. vec4 Specular = gl_LightSource[0].specular * gl_FrontMaterial.specular * pf; vec4 color1 = GlobalAmbient + Ambient + Diffuse; 23. vec4 color2 = Specular; gl_FragColor = clamp( color1 + color2, 0.0, 1.0 ); 26.}

GLEE(GL Easy Extension library) GLee (GL Easy Extension library) is a free cross-platform extension loading library for OpenGL It provides seamless support for OpenGL functions up to version 2.1 and over 360 extensions Microsoft’s OpenGL API only supports up to OpenGL 1.1

Compilation char* vertSrc = readShaderToString(vertShader); char* fragSrc = readShaderToString(fragShader); vs = glCreateShader(GL_VERTEX_SHADER); fs = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(vs, 1, &vertSrc, NULL); glShaderSource(fs, 1, &fragSrc, NULL); glCompileShader(vs); printOpenGLError(); // Check for OpenGL errors glGetShaderiv(vs, GL_COMPILE_STATUS, &vertCompiled); printShaderInfoLog(vs); glCompileShader(fs); printOpenGLError(); // Check for OpenGL errors glGetShaderiv(fs, GL_COMPILE_STATUS, &fragCompiled); printShaderInfoLog(fs); if (!vertCompiled || !fragCompiled) exit(-1);

Compilation // Create a program object and attach the two compiled shaders int program = glCreateProgram(); glAttachShader(program, vs); glAttachShader(program, fs); // Link the program object and print out the info log glLinkProgram(program); printOpenGLError(); // Check for OpenGL errors glGetProgramiv(program, GL_LINK_STATUS, &linked); printProgramInfoLog(); if (!linked) exit(-1)

Initialization and Activation Call glUseProgram( program ) to activate a shader.  The default functionality will be disabled  Can switch between multiple shaders while rendering a frame Initialization example: GLint loc = glGetUniformLocation(program, "bumpon"); glUniform1i( loc, GL_TRUE ); GLint loc = glGetUniformLocation(program, "normalMap"); glUniform1i( loc, 0 ); GLint loc = glGetUniformLocation(program, "tangentVec"); glVertexAttrib3f(loc, v1, v2, v3);

For more information, see the spec ngSpec.Full pdf