OpenGL Shader Language Vertex and Fragment Shading Programs.

Slides:



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

 The success of GL lead to OpenGL (1992), a platform-independent API that was  Easy to use  Close enough to the hardware to get excellent performance.
As well as colors, normals, and other vertex data PUSHIN’ GEO TO THE GPU JEFF CHASTINE 1.
Informationsteknologi Wednesday, December 12, 2007Computer Graphics - Class 171 Today’s class OpenGL Shading Language.
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.
Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides Shaders Feb 18, 2011 Creative Commons Attribution 3.0 (Except copyrighted.
Open Graphics Library (OpenGL)
Michael Robertson Yuta Takayama. WebGL is OpenGL on a web browser. OpenGL is a low-level 3D graphics API Basically, WebGL is a 3D graphics API that generates.
A Really (too) Short Introduction to OpenGL Peter Rautek.
Geometric Objects and Transformations. Coordinate systems rial.html.
CS 418: Interactive Computer Graphics Introduction to WebGL: HelloTriangle.html Eric Shaffer.
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum.
WebGL: in-browser 3D graphics Nick Whitelegg Maritme and Technology Faculty Southampton Solent University.
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.
1 Graphics CSCI 343, Fall 2015 Lecture 4 More on WebGL.
Computer Graphics The Rendering Pipeline - Review CO2409 Computer Graphics Week 15.
GRAPHICS PIPELINE & SHADERS SET09115 Intro to Graphics Programming.
OPENGL INTRODUCTION 林宏祥 2014/10/06. 此投影片已被稍微簡化過,跟今日課堂上的內容不太一樣。 如果想要看更詳細說明的人,請參考每頁投影片下面的『備忘稿』
1 3D API OPENGL ES v1.0 Owned by Silicon Graphics (SGL) Control was then transferred to Khronos Group Introduction.
Wilf Comp Shaders. Wilf Comp 4002 Shaders Shaders are programs that you can download onto the card Can be used.
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
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-ES 3.0 And Beyond Boston Photo credit :Johnson Cameraface OpenGL Basics.
OpenGL Shading Language (GLSL)
Vertex Buffer Objects and Shader Attributes. For Further Reading Angel 7 th Ed: –Most parts of Chapter 2. Beginning WebGL: –Chapter 1: vertex Buffer Objects,
Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9.
CS 480/680 Computer Graphics Programming with Open GL Part 5: Putting it all together Dr. Frederick C Harris, Jr. Fall 2011.
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.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
 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.
CS 4363/6353 SHADERS/GLSL. ANOTHER LOOK AT THE PIPELINE Vertex Processing Clipping/Assembly Rasterization Fragment Processing.
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.
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.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016.
Pushin’ Geo to the GPU As well as colors, normals, and other vertex data Made by Jeff Chastine, Modified by Chao Mei Jeff Chastine.
Shader.
Real-Time Rendering Buffers in OpenGL 3.3
Texture Mapping Part II
Objectives Simple Shaders Programming shaders with GLSL
OpenGL Texture Mapping
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
HW for Computer Graphics
OpenGL Texture Mapping
Programming with OpenGL Part 3: Shaders
Programming with OpenGL Part 5: More GLSL
Mickaël Sereno Shaders Mickaël Sereno 25/04/2019 Mickaël Sereno -
Programming with OpenGL Part 5: More GLSL
CS 480/680 Computer Graphics GLSL Overview.
Language Definitions Chap. 3 of Orange Book.
CS 480/680 Part 1: Model Loading.
CS 480/680 (Fall 2018) Part 2: Texture Loading
Computer Graphics Shading in OpenGL
Mickaël Sereno Graphics Memory Mickaël Sereno 11/07/2019 Mickaël Sereno -
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science
Introduction to Computer Graphics with WebGL
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

OpenGL Shader Language Vertex and Fragment Shading Programs

Shader Pipeline Vertex Shader Program Fragment Shader Program Geometry Shader Program Vertex shader takes in attributes (like normals), performs calculations, and sends the colour out to the fragment shader. Fragment shader takes data in from the vertex shader and calculates pixel (fragment) colour. Geometry Shaders are covered in chapter 11 of the OpenGL Super Bible.

Data types Primitives: bool, int, uint, float Vectors: vec2, vec3, vec4(floats) ivec2, ivec3, ivec4(integers) uvec2.... bvec2... (uints and bools) Matrices: *floating point only mat2 = mat2x2, mat3 = mat3x3, mat4=mat4x4 mat2x3, mat2x4, mat3x2, mat3x4, mat4x2, mat4x3

Vector Operations Initializing: vec4 vPos; vec4 vPos = vec4(0,0,0,1.0f); Addition: vPos = vOldPos + vOffset;(also +=) Scaled: vPos *= float; Reassigning:xyzw, rgba, stpq vPos.x = 1.0f; vPos.xy = vec2(0,1.0f); vPos.xyz = vNewPos.xyz; (use any three any time but don't mix: ~.st = ~.xt)

Matrix Operations Matrices are arrays of column vectors. Initializing: mat2 m = mat2(1.0f,0.0f,0.0f,1.0f); mat4x4 mvpMatrix = mat4(1.0f); mvpMatrix[3] = vec4(0,0,0,1.0f); Multiplication: vOutPos = mvpMatrix * vVertex; Extraction: vec4 vTrans = mvpMatrix[3].xyz;

Storage Qualifiers 1 For vertex and fragment program globals. only local variable constsame as c++ invariable passed from previous stage outpass to the next stage or assign a return value in a function inoutread/write for local function params uniformclient code value that doesn't change centroiduses centroid interpolation on ins/outs (see chapter 9 on multi sampling)

Storage Qualifiers 2 By default parameters are smoothly interpolated between stages in a perspective correct manner. nopersepective outdisable perspective inter. flat outdisable all interpolation smooth outspecify smooth inter. in a perspective correct manner (the default)

A Vertex Shader #version 330//shader language version in vec4 vVertex;//input vertex position data in vec4 vColour;//input vertex colour data out vec4 vOutColour;//output colour to frag. void main (void) { vOutColour = vColour; gl_Position = vVertex; //needed by geometry } (page 236 from the Super Bible)

A Fragment Shader #version 330 out vec4 vFragColour; //output colour in vec4 vOutColour;//in from vertex stage void main(void){ vFragColour = vOutColour; } (page 236 from the Super Bible)

Using the Shader Check gltLoadShaderPairWithAttributes(....) function in ~~~~.cpp for a look at how this works. Note that this calls gltLoadShaderFile(.....) in the file ~~~~~.cpp

Creating a Shader (GLTools) A custom shader can be created with the following command: Glint myShader = gltLoadShaderPairWithAttributes(“shader.vp”, “shader.fp”,2,GLT_ATTRIBUTE_VERTEX, “vVertex”,GLT_ATTRIBUTE_NORMAL,”vNor mal”) 2 = # of input attributes to vertex shader vVertex = changing vertex position to v Shader cNormal = changing vertex normal to v Shader

Creating a Shader (Stand Alone) First open the file and extract all information to a GLchar array. GLchar* vertShader = (GLchar*) fileText; GLuint vertShaderNum; glShaderSource(shaderNum, 1, vertShader, Null); The variable 'vertShaderNum' is what we will use to imply the vertex shader for remaining calls to openGL.

Compile, Combine, Delete To compile the shaders: glCompileShader(vertShaderNum); To create the main shader program: GLint shaderProg = glCreateProgram(); glAttachShader(shaderProg,vertShaderNum); glAttachShader(shaderProg,fragShaderNum); glLinkProgram(shaderProg); To delete the shaders and shader program: glDeleteShader(GLint shaderNumber); glDeleteProgram(shaderProg);

Input Attribute Locations You will need to get the attribute locations of the 'in' variables for the vertex shader. glBindAttribLocation(shaderProg, index, “vVertex”); shaderProg = our compiled shader program GLint index = index that “vVertex” has in the shader's variable list “vVertex” = an input vector for the shader program specifying the vertex positions

Vertex Buffer Objects Vertex Array Objects VBOs can be used by the OpenGL shader to make efficient decisions on how to use your vertex data on the graphics card. VBOs can store data such as vertex positions, colors, normals, etc. VAOs can be used to store multiple VBOs. GLuint vao[1], vbo; glGenVertexArrays(1, &vao); glBindVertexArrays(vao[0]); glGenBuffers(1, &vbo) glBindBuffer(GL_ARRAY_BUFFER, vbo);

Initializing the VBO Allocate space: glBufferData(GL_ARRAY_BUFFER, sizeof(positions)+sizeof(colors), NULL, GL_STATIC_DRAW ) where positions and colors are float arrays, we need their size to determine how big the buffer is. NULL can be a pointer to the data but here we will show how to store the info by hand page 692 of Super Bible for an in depth discussion of the other two parameters

Loading data into the VBO Loading data is done easily with the command: glBufferSubData(type, startPos,sizeofDat,data); where type = GL_ARRAY_BUFFER for now startPos = 0, sizeof(verts), sizeof(verts) + sizeof(colors)... the index to start data at data = GLfloat* / GLfloat[] Do this for all vertex data then then for another buffer load faces the same way except: type = GL_ELEMENT_ARRAY

Mapping VBO vertex data to the Shader First enable the input attribute that we want to map with the index obtained in slide 14: glEnableVertexAttribArray(index); Now we will provide the data to map: glVertexAttribPoint(index, size, type, stride, pointer) size: the size of a single component (1,2,3,4) type: kind of datastride: offset between data pointer: the startPos as in slide 17 for data in the currently bound GL_ARRAY_BUFFER

Uniforms You may want some data to be the same across all vertices in the shader such as the projection and model view matrices. First get the location during setupRC(): GLint MVPlocation = glGetUniformLocation(shader, “mvpMatrix”); Then change it in renderScene() right before drawing and after glUseProgram(shader): glUniformMatrix4fv(MVPlocation,1,GL_FALSE, transformPipeline.getModelViewPro....) 1 = number of elements to modify, GL_FALSE = transpose mat status

Error Checking Sometimes errors may arise during compilation or shader use. glGetShaderiv(program, symbol, &outValue); where program = individual or complete shader program symbol = GL_COMPILE_STATUS, GL_LINK_STATUS, etc. outValue = a boolean passed by reference you can also check for any errors that have occured with: cout << glGetError() << endl;