OpenGL Shading Language (GLSL)

Slides:



Advertisements
Similar presentations
GLSL Basics Discussion Lecture for CS 418 Spring 2015 TA: Zhicheng Yan, Sushma S Kini, Mary Pietrowicz.
Advertisements

Understanding the graphics pipeline Lecture 2 Original Slides by: Suresh Venkatasubramanian Updates by Joseph Kider.
TEXTURE MAPPING JEFF CHASTINE 1. TEXTURE MAPPING Applying an image (or a texture ) to geometry 2D images (rectangular) 3D images (volumetric – such as.
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.
Status – Week 277 Victor Moya.
Mohan Sridharan Based on slides created by Edward Angel GLSL I 1 CS4395: Computer Graphics.
GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.
GPU Programming Yanci Zhang Game Programming Practice.
REAL-TIME VOLUME GRAPHICS Christof Rezk Salama Computer Graphics and Multimedia Group, University of Siegen, Germany Eurographics 2006 Real-Time Volume.
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.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)
Programmable Pipelines. 2 Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
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.
Real-Time High Quality Rendering CSE 291 [Winter 2015], Lecture 4 Brief Intro to Programmable Shaders
Lecture by: Martin Deschamps CSE 4431
Shader Basics Chap. 2 of Orange Book. 2 Contents Why write shaders OpenGL programmable processors Language overview System overview.
OpenGL Shading Language Jian Huang CS594, Spring 2005.
GRAPHICS PIPELINE & SHADERS SET09115 Intro to Graphics Programming.
Programmable Pipelines Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University.
OpenGL Shader Language Vertex and Fragment Shading Programs.
CH8 Frame Buffer Object 1. Introduction Name OpenGL :Frame Buffer Object DirectX:Render Target Usage Render to Texture 2.
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.
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.
GLSL OpenGL Programming and Reference Guides, other sources
OpenGL Shading Language (GLSL)
CSE 381 – Advanced Game Programming GLSL. Rendering Revisited.
OpenGL Shading Language (GLSL)
Geometry Shader (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.
1 Graphics CSCI 343, Fall 2015 Lecture 25 Texture Mapping.
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.
Details of Texture Mapping Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, December 1, 2003.
OpenGL Shading Language
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.
GLSL Review Monday, Nov OpenGL pipeline Command Stream Vertex Processing Geometry processing Rasterization Fragment processing Fragment Ops/Blending.
GLSL Basics Slides by : Wei-Wen Feng.
COMP3421 The programmable pipeline and Shaders. TODO.
Shaders, part 2 alexandri zavodny.
Shader.
Draw a Simple Object.
CSE 381 – Advanced Game Programming GLSL Syntax
Chapter 6 GPU, Shaders, and Shading Languages
OpenGL Texture Mapping
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 Shaders
Shader Basics Chap. 2 of Orange Book.
OpenGL Texture Mapping
Programming with OpenGL Part 3: Shaders
Mickaël Sereno Shaders Mickaël Sereno 25/04/2019 Mickaël Sereno -
CS 480/680 Computer Graphics GLSL Overview.
CS 480/680 Part 1: Model Loading.
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

OpenGL Shading Language (GLSL)

OpenGL Fixed Pipeline Vertex pipeline Transformation & Lighting Viewport culling & clipping Primitive assembly Rasterizer setup Pixel pipeline Texture blending Per Fragment operations Buffer operations Framebuffer

Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation Per-vertex lighting Transformation & Lighting Primitive assembly Viewport culling & clipping Vertex processor Rasterizer setup

Geometry Shader Add/remove primitives Add/remove vertices Edit vertex position Supported by OpenGL Extension (Glew 1.4+) or DX10 Stream output Vertex processor Primitive assembly Viewport culling & clipping Geometry processor Rasterizer setup

Fragment (pixel) Shader Operations on interpolated values Texture access Texture application Fog Color sum Rasterizer setup Texture blending Per Fragment operations Buffer operations Fragment processor

Programmable Pipeline Vertex pipeline Viewport culling & clipping Vertex Shader Geometry Shader Rasterizer setup Pixel pipeline Pixel Shader Per Fragment operations Buffer operations Framebuffer

Qualifiers Used to management the input and output of shaders. attribute Communicate frequently changing variables from the application to a vertex shader. uniform Communicate infrequently changing variables from the application to any shader. varying Communicate interpolated variables from a vertex shader to a fragment shader

Qualifiers in Pipeline (x,y,z) (x’,y’,z’) Vertex Shader Fragment Shader attribute rasterizer Buffer Op… varying varying uniform

Qualifiers in Pipeline (x,y,z) Vertex Shader Geometry Shader Fragment Shader attribute rasterizer varying in varying out uniform

Vertex Shader Vertex processer Build-in Uniform: gl_ModelviewMatrix Build-in Attributes: gl_Color gl_Normal gl_Vertex gl_MultiTexCoord0 gl_MultiTexCoord1 … User-defined Attributes: Tangent Bitangent … Build-in Uniform: gl_ModelviewMatrix gl_ProjectionMatrix gl_LightSource[0~n] gl_FrontMaterial … User-defined Uniform: Texture EyePosition Time ScaleValue … Vertex processer Build-in Varying: gl_FrontColor gl_TexCoord[0~n] … Special Output: gl_Position gl_PointSize … User-defined Varying: normal tangent …

Fragment Shader Fragment processer Build-in Varying: gl_TexCoord[0~n] gl_FragCoord gl_FrontColor … User-defined Varying: normal tangent … Build-in Uniform: gl_ModelviewMatrix gl_ProjectionMatrix gl_LightSource[0~n] gl_FrontMaterial … User-defined Uniform: Texture EyePosition Time ScaleValue … Fragment processer Special Output: gl_FragColor / gl_FragData[] gl_FragDepth

Geometry Shader Geometry processor Coord. gl_Position gl_TexCoord[]; Vertex Color gl_FrontColorIn[gl_VerticesIn]; gl_BackColorIn[gl_VerticesIn]; gl_FrontSecondaryColorIn[gl_VerticesIn]; gl_BackSecondaryColorIn[gl_VerticesIn]; gl_FogFragCoordIn[gl_VerticesIn]; Vertex Coord. gl_TexCoordIn[gl_VerticesIn][]; gl_PositionIn[gl_VerticesIn]; Resterization Info. gl_PointSizeIn[gl_VerticesIn]; gl_ClipVertexIn[gl_VerticesIn]; Geometry processor Number of Vertices gl_VerticesIn Color gl_FrontColor; gl_BackColor; gl_FrontSecondaryColor; gl_BackSecondaryColor; gl_FogFragCoord; Coord. gl_Position gl_TexCoord[];

GLSL Language Definition Data Type Description int Integer float Floating-point bool Boolean (true or false). vec2 Vector with two floats. vec3 Vector with three floats. vec4 Vector with four floats. mat2 2x2 floating-point matrix. mat3 3x3 floating-point matrix. mat4 4x4 floating-point matrix.

Vector Vector is like a class You can use following to access Example: .r .g .b .a .x .y .z .w .s .t .p .q Example: vec4 color; color.rgb = vec3(1.0 , 1.0 , 0.0 ); color.a = 0.5 or color = vec4(1.0 , 1.0 , 0.0 , 0.5); or color.xy = vec2(1.0 , 1.0); color.zw =vec2(0.0 , 0.5);

Texture Sampler sampler*DShadow sampler{1,2,3}D sampler{1,2}DShadow samplerCube Texture unit to access the content of texture. sampler*DShadow The depth texture for shadow map. The cube map.

Addition Information Array Static cast by function Similar to C. No union, enum, class Static cast by function float() int()

Phong Shading Use varying variable to save the vertex normal or other information. Compute the Phong lighting in pixel shader with the information.

Vertex Shader Code Example varying vec3 normal, lightDir, eyeDir; void main() { normal = gl_NormalMatrix * gl_Normal; vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex); lightDir = vec3(gl_LightSource[0].position.xyz - vVertex); eyeDir = -vVertex; gl_Position = ftransform(); //gl_Position = gl_ProjectionMatrix*gl_ModelViewMatrix*gl_Vertex; }

Fragment Shader Code Example varying vec3 normal, lightDir, eyeDir; void main (void) { vec4 final_color = (gl_FrontLightModelProduct.sceneColor * gl_FrontMaterial.ambient) + (gl_LightSource[0].ambient * gl_FrontMaterial.ambient); vec3 N = normalize(normal); vec3 L = normalize(lightDir); float lambertTerm = dot(N,L); if(lambertTerm > 0.0){ final_color += gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse * lambertTerm; vec3 E = normalize(eyeDir); vec3 R = reflect(-L, N); float specular = pow( max(dot(R, E), 0.0), gl_FrontMaterial.shininess ); final_color += gl_LightSource[0].specular * gl_FrontMaterial.specular * specular; } gl_FragColor = final_color;

OpenGL Gouraud Shading Result OpenGL Gouraud Shading GLSL Phong Shading

Geometry Shader It can change the primitive. Application Add/remove primitives Add/remove vertices Edit vertex position Application

Geometry Shader Example Code void main(void) { vec2 lineDir = gl_PositionIn[1].xy - gl_PositionIn[0].xy; vec2 normal = vec2(-lineDir.y, lineDir.x); //CCW 90 degree vec4 v[4]; v[0] = gl_PositionIn[1]; v[1] = gl_PositionIn[1]; v[2] = gl_PositionIn[0]; v[3] = gl_PositionIn[0]; v[0].xy -= normal*0.125; v[1].xy += normal*0.125; v[2].xy += normal*0.125; v[3].xy -= normal*0.125;

gl_Position = v[0]; EmitVertex(); gl_Position = v[1]; gl_Position = v[2]; gl_Position = v[3]; EndPrimitive(); // GL_LINE_STRIP }

Original input primitive Result Original input primitive Output primitive

New Input Primitives GL_LINES_ADJACENCY GL_LINE_STRIP_ADJACENCY GL_TRIANGLES_ADJACENCY GL_TRIANGLE_STRIP_ADJECENCY

Applications

Applications

Use GLSL in OpenGL glew.h glew32.lib glew32.dll You need those head and library files glew.h glew32.lib glew32.dll

Use the Shader Code in C/C++ Initialize the shader. Use the shader you made. Draw what you want.

Shader Initialization Vertex Shader glCreateShade Vertex Shader Code glShaderSource glCreateProgram glCompileShader glAttachShader Fragment Shader glAttachShader glCreateShade glLinkProgram Fragment Shader Code glShaderSource glUseProgram glCompileShader

Part of Example Code (C++) int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(100,100); glutInitWindowSize(320,320); glutCreateWindow("GPU"); . glewInit(); setShaders(); glutMainLoop(); return 0; }

void setShaders() { //a few strings // will hold onto the file read in void setShaders() { //a few strings // will hold onto the file read in! char *vs = NULL, *fs = NULL, *gs = NULL; //First, create our shaders v = glCreateShader(GL_VERTEX_SHADER); f = glCreateShader(GL_FRAGMENT_SHADER); g = glCreateShader(GL_GEOMETRY_SHADER_EXT); //Read in the programs vs = textFileRead("../GeometryShader/ShaderCode/shader.vert"); fs = textFileRead("../GeometryShader/ShaderCode/shader.frag"); gs = textFileRead("../GeometryShader/ShaderCode/shader.geom");

//Setup a few constant pointers for below const char //Setup a few constant pointers for below const char * ff = fs; const char * vv = vs; const char * gg = gs; glShaderSource(v, 1, &vv, NULL); glShaderSource(f, 1, &ff, NULL); glShaderSource(g, 1, &gg, NULL); free(vs);free(fs);free(gs); glCompileShader(v); glCompileShader(f); glCompileShader(g); p = glCreateProgram();

glAttachShader(p,f); glAttachShader(p,v); glAttachShader(p,g); glProgramParameteri(p,GL_GEOMETRY_INPUT_TYPE,GL_LINES); glProgramParameteri(p,GL_GEOMETRY_OUTPUT_TYPE,GL_LINE_STRIP); int temp; glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES,&temp); glProgramParameteri(p,GL_GEOMETRY_VERTICES_OUT,temp); glLinkProgram(p); glUseProgram(p); }

Multi-texture Pixel Color Op 0 Op 1 Op 2 Op n final color glActiveTexture( GL_TEXTURE0 ); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, …); Op 1 glActiveTexture( GL_TEXTURE1 ); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, …); Op 2 glActiveTexture( GL_TEXTURE2 ); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, …); Op n final color glActiveTexture( GL_TEXTUREn ); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, …);

Shader Get location by name Assign layer number final color glActiveTexture( GL_TEXTURE0 ); glBindTexture(GL_TEXTURE_2D, …); glActiveTexture( GL_TEXTURE1 ); glBindTexture(GL_TEXTURE_2D, …); final color glActiveTexture( GL_TEXTURE2 ); glBindTexture(GL_TEXTURE_2D, …); glActiveTexture( GL_TEXTURE3 ); glBindTexture(GL_TEXTURE_2D, …);

The Setting of Texture Operative Ignored glActiveTexture / glBindTexture MAG and MIN Filter GL_NEAREST, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, … WRAP S, T GL_CLAMP, GL_REPEAT, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER Ignored glEnable(GL_TEXTURE_nD) / glDisable GL_TEXTURE_ENV_MODE GL_REPLACE, GL_MODULATE, GL_ADD Pixel Color Op 0 glActiveTexture( GL_TEXTURE0 ); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, …);

Send Texture to Shader Active texture channel Bind the texture Get location of the variable in shader Set the value

Get Location Glint glGetUniformLocation(GLhandleprogram, const GLchar *name) – Return an integer to represent the location of a specific uniform variable. – name is the name of the uniform variable in the shader. – The location of a variable is assigned in link time, so this function should be called after the link stage.

Set Value Following functions are used to assign values for uniform variables – Void glUniform{1,2,3,4}{f,i}(Glint location, TYPE v) – Void glUniform{1,2,3,4}{f,i}v(Glint location, Gluint count, TYPE v) – Void glUniformMatrix{2,3,4}fv(Glint location,GLuint count, GLboolean transpose, const GLfloat *v) location is the value obtained using glGetUniformLocation(). v is the value to be assigned. glUniform1i can be used to assign texture slot of glActiveTexture.

Texture Mapping C++ Code glUseProgramObject(MyShader); glActiveTexture( GL_TEXTURE0 ); glBindTexture(GL_TEXTURE_2D, texObject[0]); GLint location = glGetUniformLocation(MyShader, "colorTexture"); if(location == -1) printf("Cant find texture name: colorTexture\n"); else glUniform1i(location, 0); int i,j; for (i=0;i < object->fTotal;i++){ glBegin(GL_POLYGON); for (j=0;j<3;j++){ glMultiTexCoord2fv(GL_TEXTURE0, object->tList[object->faceList[i][j].t].ptr); glNormal3fv(object->nList[object->faceList[i][j].n].ptr); glVertex3fv(object->vList[object->faceList[i][j].v].ptr); } glEnd(); glutSwapBuffers(); glutPostRedisplay();

Texture Mapping Vertex Shader Code void main() { gl_TexCoord[0].xy = gl_MultiTexCoord0.xy; gl_Position = ftransform(); }

Texture Mapping Pixel Shader Code uniform sampler2D colorTexture; void main (void) { gl_FragColor = texture2D(colorTexture,gl_TexCoord[0].xy).rgba; }

gl_TexCoord[0] Vertex Shader Fragment Shader (colorTexture) gl_MultiTexCoord0 rasterizer glGetUniformLocation glActiveTexture glBindTexture(..) GL_TEXTURE0 location glUniform1i(location, 0); id GL_TEXTURE1 Texture Content … GL_TEXTUREn

Result 2

Send Attribute Data You can use glMultiTexCoord instead. It is similar to uniform data. Get location glGetAttribLocation(program id, variable name) Set Values Per Vertex Set by glVertexAttrib{234}{fd}{v} Call glVertex{34}{fd}{v} You can use glMultiTexCoord instead.