Opengl shader language vertex and fragment shading programs
1 / 20

OpenGL Shader Language Vertex and Fragment Shading Programs - PowerPoint PPT Presentation

  • Uploaded on

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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' OpenGL Shader Language Vertex and Fragment Shading Programs' - jackson-lambert

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Opengl shader language vertex and fragment shading programs
OpenGL Shader LanguageVertex andFragment Shading Programs

Shader pipeline
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.










Data types
Data types


bool, int, uint, float


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,


Vector operations
Vector Operations


vec4 vPos; vec4 vPos = vec4(0,0,0,1.0f);


vPos = vOldPos + vOffset; (also +=)


vPos *= float;

Reassigning: xyzw, rgba, stpq

vPos.x = 1.0f; vPos.xy = vec2(0,1.0f); =;

(use any three any time but don't mix: = ~.xt)

Matrix operations
Matrix Operations

Matrices are arrays of column vectors.


mat2 m = mat2(1.0f,0.0f,0.0f,1.0f);

mat4x4 mvpMatrix = mat4(1.0f);

mvpMatrix[3] = vec4(0,0,0,1.0f);


vOutPos = mvpMatrix * vVertex;


vec4 vTrans = mvpMatrix[3].xyz;

Storage qualifiers 1
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)

Storage qualifiers 2
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)

A vertex shader
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
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
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
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

Creating a shader stand alone
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
Compile, Combine, Delete

To compile the shaders:


To create the main shader program:

GLint shaderProg = glCreateProgram();




To delete the shaders and shader program:

glDeleteShader(GLint shaderNumber);


Input attribute locations
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
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);


glGenBuffers(1, &vbo)

glBindBuffer(GL_ARRAY_BUFFER, vbo);

Initializing the 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 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:


Mapping vbo vertex data to the shader
Mapping VBO vertex data to the Shader

First enable the input attribute that we want to map with the index obtained in slide 14:


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


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
Error Checking

Sometimes errors may arise during compilation or shader use.

glGetShaderiv(program, symbol, &outValue);

where program = individual or complete shader program


outValue = a boolean passed by reference

you can also check for any errors that have occured with:

cout << glGetError() << endl;