inf3320 computer graphics and discrete geometry

50
INF3320 Computer Graphics and Discrete Geometry The programmable pipeline Christopher Dyken, Martin Reimers and Johan Seland 19.11.2011 Page 1

Upload: others

Post on 11-Feb-2022

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: INF3320 Computer Graphics and Discrete Geometry

INF3320

Computer Graphics and Discrete Geometry

The programmable pipeline

Christopher Dyken, Martin Reimers and Johan Seland

19.11.2011

Page 1

Page 2: INF3320 Computer Graphics and Discrete Geometry

The programmable pipeline

Real Time Rendering:

I The Graphics Processing Unit (GPU) (Chapter 3)

The Red Book:

I The OpenGL Shading Language (Chapter 15)

Other sources:

I GLSL 1.3 spec: http://www.opengl.org/documentation/specs/

I Geometry shader: http://www.opengl.org/registry/specs/ARB/geometry_shader4.txt

I Frame buffer objects:http://www.opengl.org/registry/specs/ARB/framebuffer_object.txt

Page 2

Page 3: INF3320 Computer Graphics and Discrete Geometry

Pipeline operations

Page 3

Page 4: INF3320 Computer Graphics and Discrete Geometry

OpenGL 2.1 provides a programmable pipeline:

Program objects replace part of the pipeline ops with shaders,executed on the GPU:

I vertex shaders can replace vertex transform

I geometry shaders can replace some of primitive setup

I fragment shader can replace some of texturing and fragment operations

Green = programmable, yellow = configurable, red = fixed

VertexShader

GeometryShader

ClippingScreen

Mapping

PrimitiveSetupand

Traversal

FragmentShader

Merger

Page 4

Page 5: INF3320 Computer Graphics and Discrete Geometry

Example shader setup

main.cpp

GLuint v , f , p ;v o i d s e t S h a d e r s ( ) {

v = g l C r e a t e S h a d e r (GL VERTEX SHADER ) ;f = g l C r e a t e S h a d e r (GL FRAGMENT SHADER ) ;

c h a r ∗vs = t e x t F i l e R e a d ( ”vs−code . g l s l ”) ;c h a r ∗ f s = t e x t F i l e R e a d ( ”f s−code . g l s l ”) ;g l S h a d e r S o u r c e ( v , 1 , &vs , NULL ) ;g l S h a d e r S o u r c e ( f , 1 , &f s , NULL ) ;

g l C o m p i l e S h a d e r ( v ) ;g l C o m p i l e S h a d e r ( f ) ;

p = g l C r e a t e P r o g r a m ( ) ;g l A t t a c h S h a d e r ( p , v ) ;g l A t t a c h S h a d e r ( p , f ) ;

g l L i n k Pr o g r a m ( p ) ;g lUseProgram ( p ) ;

}

vs-code.glsl

v o i d main ( ) {g l P o s i t i o n = g l P r o j e c t i o n M a t r i x ∗ g l M o d e l V i e w M a t r i x ∗ g l V e r t e x ;

}

fs-code.glsl

v o i d main ( ) {g l F r a g C o l o r = vec4 ( 0 . 4 , 0 . 4 , 0 . 8 , 1 . 0 ) ;

}Page 5

Page 6: INF3320 Computer Graphics and Discrete Geometry

Why a programmable pipeline?

The fixed function pipeline is, well, fixed - limited applications.Programmability yields a whole new world of possibilities:

ExamplesI Other lighting models

I Realistic materials (metals, stone, wood)

I Procedural textures

I Image Processing

I Non-photorealistic rendering

I General purpose programmming (GPGPU)

I High performance computing

The possibilities are endless!

Page 6

Page 7: INF3320 Computer Graphics and Discrete Geometry

GLSL - OpenGL Shading Language

Page 7

Page 8: INF3320 Computer Graphics and Discrete Geometry

Shading Languages

Can implement shaders using shading languages

I API for shader programming

I Shading languages started with Pixar Renderman, for makinghigh quality movies etc.

I Nowadays heavily used for real time rendering

Shading languages for consumer hardware:

I Nvidia Cg (OpenGL/DirectX)

I Microsoft HLSL (DirectX)

I OpenGL Shading Language - GLSL (OpenGL)

Page 8

Page 9: INF3320 Computer Graphics and Discrete Geometry

GLSL - The OpenGL shading language

I GLSL - API to OpenGL programmable pipeline

I Based on C/C++

I Shader is specified as source code

I Compile - Link - Run cycle

I Compiler is embedded in driver

I Hardware independent

I Communication with OpenGL through various mechanisms

I Version 1.3/1.4 with OpenGL 3.0/3.1

I Latest version: GLSL 4.2, aug. 2011

I Extension/deprecation mechanism

Page 9

Page 10: INF3320 Computer Graphics and Discrete Geometry

Digression: Handling extensions with Glew

Extensions-mechanism to add functionality

I One vendor: GL APPLE client storage

I More vendors: GL EXT geometry shader4

I Blessed by Arch. Review Board: GL ARB framebuffer object

GL Extension Wrangler (GLEW): Crossplatform C/C++ library forhandling extensions

Simple Glew example

#i n c l u d e <GL/ glew . h>v o i d main ( i n t argc , c h a r ∗∗a r g v ) {

g l u t I n i t (& argc , a r g v ) ;. . .g l e w I n i t ( ) ;i f ( GL ARB geometry shader4 )

p r i n t f ( ”Ready f o r geometry s h a d i n g !\ n ”) ;e l s e {

p r i n t f ( ”NOT r e a d y f o r geometry s h a d i n g \n ”) ;e x i t ( 1 ) ;

}}

Page 10

Page 11: INF3320 Computer Graphics and Discrete Geometry

GLSL at a glance

I C/C++-like syntax

I Built-in Vector (2d,3d, 4d)

I Built-in Matrix(2× 2, 3× 3, 4× 4)

I Built-in standardlibrary of functions

I Variable qualifiers

I Built in variables

ExamplesI looping for, while,

do-while

I selection if, if-else

I basic datatypes bool, int,

float, struct, arrays

I functions (call by value)

I no pointers → no bitwiseoperators

I no strings

I no type promotion float f =

0.0;

Page 11

Page 12: INF3320 Computer Graphics and Discrete Geometry

GLSL at a glance

I C/C++-like syntax

I Built-in Vector (2d,3d, 4d)

I Built-in Matrix(2× 2, 3× 3, 4× 4)

I Built-in standardlibrary of functions

I Variable qualifiers

I Built in variables

Examples

I vec4 v = vec4(1.0, 0.0,

-3.0, 2.0 );

I Swizzlingvec3 w = v.yzx;

vec3 c = v.gbr;

// (0.0, -3.0, 1.0)

I Indexingfloat f = v[2]; // -3.0

I Component-wise operationw.x = v.x + f;

I Component-wise mathopsvec4 v = v+w;

Page 12

Page 13: INF3320 Computer Graphics and Discrete Geometry

GLSL at a glance

I C/C++-like syntax

I Built-in Vector (2d,3d, 4d)

I Built-in Matrix(2× 2, 3× 3, 4× 4)

I Built-in standardlibrary of functions

I Variable qualifiers

I Built in variables

Examples

I mat2 m = mat2(1.0, 2.0,3.0, 4.0 ); //

[1.0 3.02.0 4.0

]I mat4 n = mat4(1.0);

// n = diag(1.0)

I vec2 v = m[0];

// v = (1.0, 2.0)T

I float f = m[1][0];

// f = 3

Page 13

Page 14: INF3320 Computer Graphics and Discrete Geometry

GLSL at a glance

I C/C++-like syntax

I Built-in Vector (2d,3d, 4d)

I Built-in Matrix(2× 2, 3× 3, 4× 4)

I Built-in standardlibrary of functions

I Variable qualifiers

I Built in variables

ExamplesI Trigonometric: sin, tan,

atan....

I Common: abs, sign, min,

max...

I Geometric: length, dot,

cross...

I Vector relationalI Interpolation:

I mix(x,y,a) = (1.0-a)*x + a*yI step(e,x): x <= e ? 0.0 : 1.0I smoothstep(e0,e1,x)

Page 14

Page 15: INF3320 Computer Graphics and Discrete Geometry

GLSL at a glance

I C/C++-like syntax

I Built-in Vector (2d,3d, 4d)

I Built-in Matrix(2× 2, 3× 3, 4× 4)

I Built-in standardlibrary of functions

I Variable qualifiers

I Built in variables

ExamplesI const

I uniform (over primitive)

I attribute (GLSL<1.3)

I varying (GLSL<1.3)

I in (input variable)

I out (output variable)

Page 15

Page 16: INF3320 Computer Graphics and Discrete Geometry

GLSL at a glance

I C/C++-like syntax

I Built-in Vector (2d,3d, 4d)

I Built-in Matrix(2× 2, 3× 3, 4× 4)

I Built-in standardlibrary of functions

I Variable qualifiers

I Built in variables

ExamplesI Uniforms: gl ModelViewMatrix

I Attributes: gl color, gl vertex,gl normal

I Varyings: gl FrontColor,gl BackColor

I Special: gl Position,gl FragDepth, gl FragColor

Page 16

Page 17: INF3320 Computer Graphics and Discrete Geometry

More GLSL

I Preprocessor, similar to the C/C++ preprocessormacros, conditionals, pragma

I OpenGL extension handling, e.g.#extension GL_EXT_geometry_shader4 : enable

I user defined functionsI mostly like C-functionsI call by value (no references)I in/out/inout qualifiers, e.g

void getSomeValue( out int retval)

Page 17

Page 18: INF3320 Computer Graphics and Discrete Geometry

GLSL - OpenGL Communication

The host (CPU) program pass data to shaders (GPU) through

1. State variables (accessible in shaders)

2. Used defined variables (uniform, attribute, varying)

3. Textures

The GPU pass data back to the CPU through a framebuffer

Page 18

Page 19: INF3320 Computer Graphics and Discrete Geometry

Passing variables to shaders

Three types of variables

1. Uniform variablesI “Global” variables for shader program (common uniform

namespace for all shaders)I Can be updated once pr. primitive

2. Vertex attribute variablesI Allows for passing additional vertex dataI Can be updated for every vertex

3. Interpolated variablesI For communication from vertex to fragment shaderI Value is set per vertex and interpolated per fragment

I smooth - perspective correct interpolationI flat - constant over primitve (flat shading)I noperspective - linear interpolation in screen coords

Page 19

Page 20: INF3320 Computer Graphics and Discrete Geometry

Setting a uniform

1. Activate the program

2. Get the location of the uniform

3. Pass value of uniform

Passing uniforms

glUseProgram ( prog ) ;i n t l o c = g l G e t U n i f o r m L o c a t i o n ( prog , ‘ ‘ beta ’ ’ ) ;g l U n i f o r m 1 f ( l o c , 0 . 2 5 ) ;

I Can pass glUniformMatrix and other variants

I Cannot change between begin/end

Page 20

Page 21: INF3320 Computer Graphics and Discrete Geometry

Passing vertex attributes

1. Activate the program

2. Get the location of attributes

3. For every vertex, pass vertex data

Example

glUseProgram ( prog ) ;i n t l o c = g l G e t A t t r i b L o c a t i o n ( prog , ‘ ‘ tangent ’ ’ ) ;g l B e g i n ( GL TRIANGLES ) ;g l N o r m a l 3 f v ( norma l s [ 0 ] ) ;g l C o l o r 3 f v ( c o l o r s [ 0 ] ) ;g l V e r t e x A t t r i b 3 f v ( l o c , t a n g e t s [ 0 ] ) ;g l V e r t e x 3 f v ( v e r t i c e s [ 0 ] ) ;.g l E n d ( ) ;

I Many variants of glVertexAttrib*fI Can also use attribute arrays - like vertex arrays:

glEnableVertexAttribArray(GLint loc) andglVertexAttribPointer Page 21

Page 22: INF3320 Computer Graphics and Discrete Geometry

Varying (interpolated) Variables

1. Specify with same name/type in vertex and fragment shaders

2. Set value in vertex shader (attribute/out)

3. Read in fragment shader (varying/in)

Example

/∗ VERTEX SHADER ∗/#v e r s i o n 130smooth out vec3 Normal ;v o i d main ( ) {

Normal = g l N o r m a l M a t r i x∗g l N o r m a l ;. . .

}

/∗ FRAGMENT SHADER ∗/#v e r s i o n 130smooth i n vec3 Normal ;v o i d main ( ) {

vec3 d i f f u s e = dot ( Normal , l i g h t V e c ) ;. . .

}

Page 22

Page 23: INF3320 Computer Graphics and Discrete Geometry

Specifying textures

1. Activate texture as regular OpenGL

2. Specify texture as uniform (glGetLocation etc.)

3. Read and use in either vertex or fragment shader

Shader texture example

uni form sampler2D t e x t u r e ;v o i d main ( ) {

vec4 t e x = t e x t u r e 2 D ( t e x t u r e , g l TexCoord [ 0 ] . s t ) ;.

}

I Other sampler types, 1-3D, Cube, Shadow1D/2D

Dependent texture reads

uni form sampler1D c o o r d s ;uni form sampler3D volume ;v o i d main ( ) {

vec3 t e x C o o r d s = t e x t u r e 1 D ( coords , g l TexCoord [ 0 ] . s ) ;vec3 vo lumeCo lo r = t e x t u r e 3 D ( volume , t e x C o o r d s ) ;.

}Page 23

Page 24: INF3320 Computer Graphics and Discrete Geometry

Shaders

Page 24

Page 25: INF3320 Computer Graphics and Discrete Geometry

Vertex Shader

I Fully programmable stage, replace fixed stage

I Execute same shader for all vertices

I Has no knowledge of neighboring vertices

I Has no knowledge of primitive

I Can alter the (x,y,z,w) coordinate of a vertex

I 1 vertex in, 1 vertex out

I Read only attributes/out, uniforms

I Read texture (not required in spec)

I Write to varyings/out

Page 25

Page 26: INF3320 Computer Graphics and Discrete Geometry

Variables in Vertex shader

Inputs (Read Only)

a t t r i b u t e vec4 g l V e r t e x ;a t t r i b u t e vec3 g l N o r m a l ;a t t r i b u t e vec4 g l C o l o r ;a t t r i b u t e vec4 g l S e c o n d a r y C o l o r ;a t t r i b u t e vec4 g l M u l t i T e x C o o r d 0 ;. . .a t t r i b u t e vec4 g l M u l t i T e x C o o r d 7 ;a t t r i b u t e f l o a t g l FogCoord ;

Vertex shaders must output gl_Position

Varying Outputs (Read/Write)

v a r y i n g vec4 g l F r o n t C o l o r ;v a r y i n g vec4 g l B a c k C o l o r ; e n a b l e GL VERTEX PROGRAM TWO SIDEv a r y i n g vec4 g l F r o n t S e c o n d a r y C o l o r ;v a r y i n g vec4 g l B a c k S e c o n d a r y C o l o r ;v a r y i n g vec4 g l TexCoord [ ] ; MAX=gl MaxTextureCoordsv a r y i n g f l o a t g l F o g F ra g C o o r d ;

Page 26

Page 27: INF3320 Computer Graphics and Discrete Geometry

Vertex shaders

Pass-through vertex shader

#v e r s i o n 130v o i d main ( ) {

// t he f o l l o w i n g t h r e e l i n e s p r o v i d e th e same r e s u l t// g l P o s i t i o n = g l P r o j e c t i o n M a t r i x ∗ g l M o d e l V i e w M a t r i x ∗ g l V e r t e x ;// g l P o s i t i o n = g l M o d e l V i e w P r o j e c t i o n M a t r i x ∗ g l V e r t e x ;g l P o s i t i o n = f t r a n s f o r m ( ) ;

}

Page 27

Page 28: INF3320 Computer Graphics and Discrete Geometry

Fragment (Pixel) Shader

I Fully programmable stage, replace fixed stage

I Execute same shader for all fragments

I Has no knowledge of neighboring fragments

I 1 fragment in, 1 or 0 fragments out (discard)

I Can not change the (x , y) coordinate of a fragment

I Read only varyings/in, uniforms, attribute, texture

I Can change a fragments RGBA value and depth

I Fragment can output to multiple buffers, using drawBuffers

and gl_FragData[]

Page 28

Page 29: INF3320 Computer Graphics and Discrete Geometry

Variables in Fragment Shader

Varying Inputs (Read only)

i n vec4 g l C o l o r ;i n vec4 g l S e c o n d a r y C o l o r ;i n vec4 g l TexCoord [ ] ; // MAX=gl MaxTextureCoordsi n f l o a t g l F o g F r a g C o o r d ;

Special inputs (Read only)

vec4 g l F r a g C o o r d ; // p i x e l c o o r d i n a t e sboo l g l F r o n t F a c i n g ;

Special Output Variables

vec4 g l F r a g C o l o r ;vec4 g l F r a g D a t a [ ] ; // MAX= gl MaxDrawBuf fe r sf l o a t g l F r a g D e p t h ; // DEFAULT=g l F r a g C o o r d . z

Page 29

Page 30: INF3320 Computer Graphics and Discrete Geometry

Fragment shader example: per pixel shading

smooth i n vec3 Normal ;

v o i d main ( ) {

vec3 d i fMat = g l F r o n t M a t e r i a l . d i f f u s e . xyz ;vec3 specMat = g l F r o n t M a t e r i a l . s p e c u l a r . xyz ;

vec3 l i g h t V e c = n o r m a l i z e ( g l L i g h t S o u r c e [ 0 ] . p o s i t i o n . xyz−g l F r a g C o o r d . xyz ) ;vec3 normalVec = n o r m a l i z e ( Normal ) ;vec3 eyeVec = vec3 ( 0 . 0 , 0 . 0 , 1 . 0 ) ;vec3 h a l f V e c = n o r m a l i z e ( l i g h t V e c + eyeVec ) ;

// c a l c u l a t e d i f f u s e componentvec3 d i f f u s e = max ( dot ( normalVec , l i g h t V e c ) , 0 . 0 ) ∗ d i fMat ;

// c a l c u l a t e s p e c u l a r componentvec3 s p e c u l a r = pow ( max ( dot ( normalVec , h a l f V e c ) , 0 . 0 ) , 3 2 . 0 ) ∗ specMat ;

// combine d i f f u s e and s p e c u l a r c o n t r i b u t i o n s and output f ragment c o l o rg l F r a g C o l o r . rgb = d i f f u s e + s p e c u l a r ;

}

Page 30

Page 31: INF3320 Computer Graphics and Discrete Geometry

Normal mapping - Fetch normal vector from texture

Object space normal mapping

uni form sampler2D n o r m a l t e x ;v o i d main ( ) {

// decompress normal (map comps from [ 0 , 1 ] to [ −1 ,1])vec3 n = 2∗ t e x t u r e 2 D ( n o r m a l t e x , g l TexCoord [ 0 ] . xy )−1;n = g l N o r m a l M a t r i x∗n ; // t r a n s f o r m normalvec3 l i g h t V e c = g l L i g h t S o u r c e [ 0 ] . p o s i t i o n ;vec3 eyeVec = vec3 ( 0 . 0 , 0 . 0 , 1 . 0 ) ;vec3 h a l f v e c = n o r m a l i z e ( l i g h t V e c + eyeVec ) ;g l F r a g C o l o r = max ( dot ( l i g h t V e c , n ) , 0 . 0 )∗ g l C o l o r

+ pow ( max ( dot ( h a l f V e c , n ) , 0 . 0 ) , 4 0 )∗ vec4 ( 1 . 0 ) ;}

Excellent results, but difficult to animate (must update texture)!Page 31

Page 32: INF3320 Computer Graphics and Discrete Geometry

Geometry Shader

Only on DX10 HW (SM 4) - not mentioned in Red BookI Extension: GL_ARB_geometry_shader4

I Programmable additional stage, after vertex transformI Per primitive processing, i.e. point, line, triangleI 1 primitive in, n primitives out (limited amount, fixed type)I Read uniform, vertex attributes, textureI Write varyings, vertex attributesI Stream out: can write generated data to a bufferI Pipeline may terminate here

Usage:I Adjacency computationsI Kill primitive, refine primitivesI Simple tessellationI Particle effects, fur, hair, ...I ...

Page 32

Page 33: INF3320 Computer Graphics and Discrete Geometry

Geometry Shader in GLSL

I About the same output types as VS

I Inputs: arrays of length gl_VerticesIn

I EmitVertex() adds vertex gl_Position to current primitive

I EndPrimitive() ends the current primitive

I Set input type with glProgramParameteriEXT(prog,

GL_GEOMETRY_INPUT_TYPE_EXT, type)

I Set output type with glProgramParameteriEXT(prog,

GL_GEOMETRY_OUTPUT_TYPE_EXT, type)

Page 33

Page 34: INF3320 Computer Graphics and Discrete Geometry

Simple Geometry Shader

A pass-through geometry shader (GLSL 1.30)

#v e r s i o n 130#e x t e n s i o n GL ARB geometry shader4 : e n a b l ev o i d main ( ) {

f o r ( i n t i = 0 ; i < g l V e r t i c e s I n ; ++i ) {g l F r o n t C o l o r = g l F r o n t C o l o r I n [ i ] ; // copy c o l o rg l P o s i t i o n = g l P o s i t i o n I n [ i ] ; // copy p o s i t i o nEmitVertex ( ) ;

}}

Page 34

Page 35: INF3320 Computer Graphics and Discrete Geometry

Geometry Shader example

Geometry shader for drawing Bezier curve (GLSL 1.20)

#v e r s i o n 120#e x t e n s i o n GL EXT geometry shader4 : e n a b l ev o i d main ( v o i d ){

f o r ( i n t i =0; i <64; i ++) {f l o a t t = i / ( 6 4 . 0 −1 . 0 ) ;f l o a t m = 1.0− t ;vec4 b = vec4 (m∗m∗m, m∗m∗t , m∗ t∗t , t∗ t∗ t ) ;vec4 p = g l P o s i t i o n I n [ 0 ]∗ b . x

+ g l P o s i t i o n I n [ 1 ]∗ b . y+ g l P o s i t i o n I n [ 2 ]∗ b . z+ g l P o s i t i o n I n [ 3 ]∗ b . w ;

g l P o s i t i o n = p ;EmitVer tex ( ) ;

}E n d P r i m i t i v e ( ) ;

}

Page 35

Page 36: INF3320 Computer Graphics and Discrete Geometry

Some newer features of GLSL

GLSL 3.0 extensions

I Instanced drawing (replication)

I Transform feedback (record transformed data)

I 32 bit depth buffer support

GLSL > 3.0 extensions

I Deprecation mechanism: some functionality/variables nolonger available in shaders, but can useGL ARB compatibility extension

I Additional shader steps (tessellation control and evaluation)

I Atomic counters (Opengl 4.2)

I ...

Page 36

Page 37: INF3320 Computer Graphics and Discrete Geometry

Interfacing OpenGL

I GLSL is only half the story

I OpenGL has been extended with several API calls to allow forcompiling, installing and interfacing shaders

I Unfortunately, using a shader involves many steps

I There are libraries to ease the process

Page 37

Page 38: INF3320 Computer Graphics and Discrete Geometry

Create Shader Objects

1. Create OpenGL managed data structure for each shader

2. Provide strings of GLSL that provides the shaders source code

Example

GLhandle vs , f s ;v s = g l C r e a t e S h a d e r ( GL VERTEX SHADER ) ;f s = g l C r e a t e S h a d e r ( GL FRAGMENT SHADER ) ;

v s s r c = r e a d F i l e ( ’ ’ v e r t e x−s h a d e r . g l s l ’ ’ ) ;f s s r c = r e a d F i l e ( ’ ’ f ragment−s h a d e r . g l s l ’ ’ ) ;

g l S h a d e r S o u r c e ( vs , 10 , v s s r c , NULL ) ;g l S h a d e r S o u r c e ( f s , 20 , f s s r c , NULL ) ;

Page 38

Page 39: INF3320 Computer Graphics and Discrete Geometry

Compile shaders and create program object

1. Compile each shader separately

2. Vertex and Fragment Shaders combined in a Program Object

Example

g l C o m p i l e S h a d e r ( vs ) ;g l C o m p i l e S h a d e r ( f s ) ;

u i n t prog = g l C r e a t e P r o g r a m ( ) ;g l A t t a c h S h a d e r ( prog , vs ) ;g l A t t a c h S h a d e r ( prog , f s ) ;

Page 39

Page 40: INF3320 Computer Graphics and Discrete Geometry

Link, install and use program

1. Our compiled program must be linked

2. then install the program as part of the current GL-state

3. We issue geometry the normal way, our shader is executedinstead of the fixed function pipeline

Example

g l L in kP ro g r am ( prog ) :g lUseProgram ( prog ) ;r e n d e r S o m e t h i n g ( ) ;

Page 40

Page 41: INF3320 Computer Graphics and Discrete Geometry

Creating a shader

1. glCreateShader creates a shader of vertex, fragment, or geometry type.

2. glShaderSource specifies the source code of a shader

3. glCompileShader compiles the source code of a shader to object code

4. glGetShader and glGetShaderInfoLog reports errors.

Creating a program

1. glCreateProgram creates a program object.

2. glAttachShader attaches a shader to a program object.Several shaders can be attached, but only one of each type.

3. if a geometry shader is attached, use glProgramParameteriEXT tospecify input and output primitive types and max generated vertices.

4. glLinkProgram links attached shaders to form a program.

5. glGetProgramiv and glGetProgramInfoLog reports errors.

Using a program

1. glUseProgram( program ) says that we want to use that program.

2. glUseProgram( 0 ) says that we want to use the fixed-function pipeline.Page 41

Page 42: INF3320 Computer Graphics and Discrete Geometry

Framebuffer Objects

Page 42

Page 43: INF3320 Computer Graphics and Discrete Geometry

A framebuffer is a set of logical buffers

red channel

green channel

blue channel

depth buffer

stencil buffer

fram

ebuf

fer

heig

ht

framebuffer width

color buffer

pixel

I color buffers (RGBA, front/back, left/right, typically 16–32 bits)

I depth buffer (typically 24–32 bits)

I stencil buffer (typically 0–8 bits)

I accumulation buffer (precision like color buffer or higher), . . .

A pixel is the contents of all logical buffers for a location.

GLUT/QT/GTK creates the onscreen framebuffer.

Page 43

Page 44: INF3320 Computer Graphics and Discrete Geometry

FrameBuffer Objects - FBOs - are offscreen framebuffers:

I use rendering to define textures

I ping-pong rendering

I multi-pass rendering with textures as in-between storage

I full-screen effects like toning, motion blur, HDR. . .

I GPGPU, etc. . .

I FBO = Render targets in DirectX

FBO1red channel

green channel

blue channel

depth buffer

stencil buffer

fram

ebuf

fer

heig

ht

framebuffer width

color buffer

pixel

FBO2red channel

green channel

blue channel

depth buffer

stencil buffer

fram

ebuf

fer

heig

ht

framebuffer width

color buffer

pixel

Page 44

Page 45: INF3320 Computer Graphics and Discrete Geometry

An extension (GL_EXT_framebuffer_object) in OpenGL 3.0

Create and render to different framebuffers using the same context.

A FBO is a set of logical buffer attachment points

Can attach render buffers and textures to the attachment points

I create using glGenFramebuffers

I bind to using glBindFramebuffer

I attach render buffer using glFramebufferRenderbuffer

I attach texture using glFramebufferTexture2D

I check if OK using glCheckFramebufferStatus

Page 45

Page 46: INF3320 Computer Graphics and Discrete Geometry

Example: framebuffer object

Create and use a framebuffer object

GLuint fb o ;g l G e n F r a m e b u f f e r s ( 1 , &f bo ) ;g l B i n d F r a m e b u f f e r (GL FRAMEBUFFER , fb o ) ;

// Set up a r e n d e r−b u f f e r ( depth )GLuint d e p t h b u f f e r ;g l G e n R e n d e r b u f f e r s ( 1 , &d e p t h b u f f e r ) ;g l B i n d R e n d e r b u f f e r (GL RENDERBUFFER , d e p t h b u f f e r ) ;g l R e n d e r b u f f e r S t o r a g e (GL RENDERBUFFER , GL DEPTH COMPONENT, width , h e i g h t ) ;g l F r a m e b u f f e r R e n d e r b u f f e r (GL FRAMEBUFFER , GL DEPTH ATTACHMENT,

GL RENDERBUFFER , d e p t h b u f f e r ) ;

// Renderg l P u s h A t t r i b (GL VIEWPORT BIT ) ;g l V i e w p o r t ( 0 , 0 , width , h e i g h t ) ;r e n d e r S o m e t h i n g ( ) ; // Output goes to th e b u f f e r s a t t a c h e d to f bo

// R e s t o r e and unbindg l P o p A t t r i b ( ) ;g l B i n d F r a m e b u f f e r (GL FRAMEBUFFER , 0 ) ;

Page 46

Page 47: INF3320 Computer Graphics and Discrete Geometry

Render buffers are off-screen render targetsI create using glGenRenderbuffers

I bind to using glBindRenderbuffer

I specify size and storage using glRenderbufferStorage

I attach to an FBO using glFramebufferRenderbuffer

Creating and attaching a render buffercreate a 256 × 256 depth buffer

glGenRenderbuffers( 1, &depth );

glBindRenderbuffer( GL_RENDERBUFFER, depth );

glRenderbufferStorage( GL_RENDERBUFFER,

GL_DEPTH_COMPONENT24,

256, 256 );

attach the render buffer to a FBOglBindFramebuffer( GL_FRAMEBUFFER, fbo ):

glFramebufferRenderbuffer( GL_FRAMEBUFFER,

GL_DEPTH_ATTACHMENT,

GL_RENDERBUFFER, depth);

Page 47

Page 48: INF3320 Computer Graphics and Discrete Geometry

Render to texturesI render directly to an existing texture

I attach to an FBO using glFramebufferTexture2D

Render to a texture (color buffer)Initialize texture target

glGenTextures(1, &img);

glBindTexture(GL_TEXTURE_2D, img);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

Attach textureglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, img, 0);

Render textureglBindFramebuffer(GL_FRAMEBUFFER, fbo);

glPushAttrib(GL_VIEWPORT_BIT);

glViewport(0,0,width, height);

renderSomething(); // output to texture render-buffer img

glPopAttrib();

glBindFramebuffer(GL_FRAMEBUFFER, 0);

Use texture in next rendering stepglGenTextures(1, &img);

...

Can also generate mipmaps, e.g. with glGenerateMipmapEXT

Page 48

Page 49: INF3320 Computer Graphics and Discrete Geometry

Multiple render targets in a FBO

I Each texture attached withglFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENTi,GL_TEXTURE_2D,img,0);

I Check how many with glGetIntergeri(GL_MAX_COLOR_ATTACHMENTS, &maxbuffers);

I Use glDrawBuffers to tell which to render to

I each target rendered differently with shader program

Render to multiple buffersInitialize render buffers

GLenum buffers[] = {GL_COLOR_ATTACHMENT0,GL_COLOR_ATTACHMENT1;}glDrawBuffers(2, buffers);

Shader code#version 130

void main() {

gl_FragData[0] = vec4(0.0, 1.0, 0.0);

gl_FragData[1] = vec4(0.0, 0.0, 1.0);

}

Page 49

Page 50: INF3320 Computer Graphics and Discrete Geometry

The last slide

Some litteratureI “The Orange Book”, OpenGL Shading Language, 3rd edition

I GLSL info: http://www.opengl.org/documentation/glsl/

I GLSL quick-ref:http://www.opengl.org/sdk/libs/OpenSceneGraph/glsl_quickref.pdf

I GLSL tutorial:http://www.lighthouse3d.com/opengl/glsl/

I FBO spec.:http://www.opengl.org/registry/specs/EXT/framebuffer_object.txt

Next time: Polygonal meshes

Page 50