1 / 20

OpenGL Shader Language Vertex and Fragment Shading Programs

OpenGL Shader Language Vertex and Fragment Shading Programs. Shader Pipeline. 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.

Download Presentation

OpenGL Shader Language Vertex and Fragment Shading Programs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OpenGL Shader LanguageVertex andFragment Shading Programs

  2. Shader Pipeline 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. Vertex Shader Program Fragment Shader Program Geometry Shader Program

  3. 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

  4. 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)

  5. 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;

  6. Storage Qualifiers 1 For vertex and fragment program globals. <none> only local variable const same as c++ in variable passed from previous stage out pass to the next stage or assign a return value in a function inout read/write for local function params uniform client code value that doesn't change centroid uses centroid interpolation on ins/outs (see chapter 9 on multi sampling)

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

  8. 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)

  9. 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)

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

  11. 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,”vNormal”) 2 = # of input attributes to vertex shader vVertex = changing vertex position to v Shader cNormal = changing vertex normal to v Shader

  12. 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.

  13. 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);

  14. 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

  15. Vertex Buffer ObjectsVertex 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);

  16. 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

  17. 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

  18. 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 data stride: offset between data pointer: the startPos as in slide 17 for data in the currently bound GL_ARRAY_BUFFER

  19. 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

  20. 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;

More Related