1 / 20

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

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.

## 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

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

Program

Fragment

Program

Geometry

Program

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

Initializing:

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

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)

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;

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)

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)

#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)

#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)

Check gltLoadShaderPairWithAttributes(....) function in ~~~~.cpp for a look at how this works.

Note that this calls gltLoadShaderFile(.....) in the file ~~~~~.cpp

A custom shader can be created with the following command:

2 = # of input attributes to vertex shader

vVertex = changing vertex position to v Shader

cNormal = changing vertex normal to v Shader

First open the file and extract all information to a GLchar array.

GLchar* vertShader = (GLchar*) fileText;

The variable 'vertShaderNum' is what we will use to imply the vertex shader for remaining calls to openGL.

To compile the shaders:

To create the main shader program:

GLint shaderProg = glCreateProgram();

To delete the shaders and shader program:

You will need to get the attribute locations of the 'in' variables for the vertex shader.

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

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

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

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

Sometimes errors may arise during compilation or shader use.