GLSL Applications: 2 of 2 Assignment 3 University of ...cis565/Lectures2011S/Lecture6.pdf · CIS...

14
1 GLSL Applications: 2 of 2 Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2011 Administrivia Assignment 2 due today 11:59pm on Blackboard Assignment 3 handed out today Due Wednesday, 02/09 at 11:59pm Agenda Finish last Monday’s slides VBO Layout OpenGL Multithreading Today’s slides OpenGL Textures and Multitexturing OpenGL Framebuffers and Deferred Shading Ambient Occlusion Textures unsigned char *pixels = // ... GLuint id; glGenTextures(1, &id); glBindTexture(GL_TEXTURE_2D, id); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, pixels); // ... glDeleteTextures(1, &id);

Transcript of GLSL Applications: 2 of 2 Assignment 3 University of ...cis565/Lectures2011S/Lecture6.pdf · CIS...

Page 1: GLSL Applications: 2 of 2 Assignment 3 University of ...cis565/Lectures2011S/Lecture6.pdf · CIS 565 - Spring 2011 Administrivia Assignment 2 due today 11:59pm on Blackboard Assignment

1

GLSL Applications:

2 of 2

Patrick Cozzi

University of Pennsylvania

CIS 565 - Spring 2011

Administrivia

� Assignment 2 due today

�11:59pm on Blackboard

� Assignment 3

�handed out today

�Due Wednesday, 02/09 at 11:59pm

Agenda

� Finish last Monday’s slides

�VBO Layout

�OpenGL Multithreading

� Today’s slides

�OpenGL Textures and Multitexturing

�OpenGL Framebuffers and Deferred Shading

�Ambient Occlusion

Textures

unsigned char *pixels = // ...

GLuint id;

glGenTextures(1, &id);

glBindTexture(GL_TEXTURE_2D, id);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA,

GL_UNSIGNED_BYTE, pixels);

// ...

glDeleteTextures(1, &id);

Page 2: GLSL Applications: 2 of 2 Assignment 3 University of ...cis565/Lectures2011S/Lecture6.pdf · CIS 565 - Spring 2011 Administrivia Assignment 2 due today 11:59pm on Blackboard Assignment

2

Textures

unsigned char *pixels = // ...

GLuint id;

glGenTextures(1, &id);

glBindTexture(GL_TEXTURE_2D, id);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA,

GL_UNSIGNED_BYTE, pixels);

// ...

glDeleteTextures(1, &id);

Pixels for an image insystem memory.

Textures

unsigned char *pixels = // ...

GLuint id;

glGenTextures(1, &id);

glBindTexture(GL_TEXTURE_2D, id);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA,

GL_UNSIGNED_BYTE, pixels);

// ...

glDeleteTextures(1, &id);

Standard business.

Textures

unsigned char *pixels = // ...

GLuint id;

glGenTextures(1, &id);

glBindTexture(GL_TEXTURE_2D, id);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA,

GL_UNSIGNED_BYTE, pixels);

// ...

glDeleteTextures(1, &id);

I hate global state. You should too.

What is the alternative design?

Textures

unsigned char *pixels = // ...

GLuint id;

glGenTextures(1, &id);

glBindTexture(GL_TEXTURE_2D, id);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA,

GL_UNSIGNED_BYTE, pixels);

// ...

glDeleteTextures(1, &id);

Sampler state. More info to follow.

Page 3: GLSL Applications: 2 of 2 Assignment 3 University of ...cis565/Lectures2011S/Lecture6.pdf · CIS 565 - Spring 2011 Administrivia Assignment 2 due today 11:59pm on Blackboard Assignment

3

Textures

unsigned char *pixels = // ...

GLuint id;

glGenTextures(1, &id);

glBindTexture(GL_TEXTURE_2D, id);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA,

GL_UNSIGNED_BYTE, pixels);

// ...

glDeleteTextures(1, &id); Transfer from system memory to

driver-controlled (likely, GPU) memory. Does it need to block?

Textures

unsigned char *pixels = // ...

GLuint id;

glGenTextures(1, &id);

glBindTexture(GL_TEXTURE_2D, id);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA,

GL_UNSIGNED_BYTE, pixels);

// ...

glDeleteTextures(1, &id);

Pixel data format and datatype

Textures

unsigned char *pixels = // ...

GLuint id;

glGenTextures(1, &id);

glBindTexture(GL_TEXTURE_2D, id);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA,

GL_UNSIGNED_BYTE, pixels);

// ...

glDeleteTextures(1, &id);

Internal (GPU) texture format

Texture Wrap Parameters

Images from: http://http.download.nvidia.com/developer/NVTextureSuite/Atlas_Tools/Texture_Atlas_Whitepaper.pdf

GL_MIRRORED_REPEAT

GL_REPEAT

GL_CLAMP

� Set with:

� glTexParameteri()

Page 4: GLSL Applications: 2 of 2 Assignment 3 University of ...cis565/Lectures2011S/Lecture6.pdf · CIS 565 - Spring 2011 Administrivia Assignment 2 due today 11:59pm on Blackboard Assignment

4

Multitexturing

� Using multiple textures in the same rendering pass

� Each is bound to a different texture unitand accessed with a different sampler

uniform in GLSL

Multitexturing: Light Map

� Recall our Light Map example:

x =

Precomputed light Surface color

“lit” surface

Multitexturing: Light Map

uniform sampler2D lightMap;

uniform sampler2D surfaceMap;

in vec2 fs_TxCoord;

in vec3 out_Color;

void main(void)

{

float intensity = texture(lightMap, fs_TxCoord).r;

vec3 color = texture(surfaceMap, fs_TxCoord).rgb;

out_Color = intensity * color;

}

Each texture is accessed

with a different sampler

Multitexturing: Light Map

uniform sampler2D lightMap;

uniform sampler2D surfaceMap;

in vec2 fs_TxCoord;

in vec3 out_Color;

void main(void)

{

float intensity = texture(lightMap, fs_TxCoord).r;

vec3 color = texture(surfaceMap, fs_TxCoord).rgb;

out_Color = intensity * color;

}

Pass the sampler to texture()

to read from a particular texture

Page 5: GLSL Applications: 2 of 2 Assignment 3 University of ...cis565/Lectures2011S/Lecture6.pdf · CIS 565 - Spring 2011 Administrivia Assignment 2 due today 11:59pm on Blackboard Assignment

5

Multitexturing: Terrain

� How was this rendered?

Image courtesy of A K Peters, Ltd. www.virtualglobebook.com

Multitexturing: Terrain

� First hint: two textures

Images courtesy of A K Peters, Ltd. www.virtualglobebook.com

Grass Stone

Multitexturing: Terrain

� Second hint: terrain slope

Image courtesy of A K Peters, Ltd. www.virtualglobebook.com

Multitexturing: Terrain

� Second hint: terrain slope

Image courtesy of A K Peters, Ltd. www.virtualglobebook.com

•0 is flat

•1 is steep

Page 6: GLSL Applications: 2 of 2 Assignment 3 University of ...cis565/Lectures2011S/Lecture6.pdf · CIS 565 - Spring 2011 Administrivia Assignment 2 due today 11:59pm on Blackboard Assignment

6

Multitexturing: Terrain

� Third and final hint: a blend ramp

Image courtesy of A K Peters, Ltd. www.virtualglobebook.com

Multitexturing: Terrain

uniform sampler2D grass;

uniform sampler2D stone;

uniform sampler2D blendRamp;

in vec3 out_Color;

void main(void)

{

// ...

out_Color = intensity * mix(

texture(grass, repeatTextureCoordinate).rgb,

texture(stone, repeatTextureCoordinate).rgb,

texture(u_blendRamp, vec2(0.5, slope)).r);

}

Multitexturing: Terrain

uniform sampler2D grass;

uniform sampler2D stone;

uniform sampler2D blendRamp;

in vec3 out_Color;

void main(void)

{

// ...

out_Color = intensity * mix(

texture(grass, repeatTextureCoordinate).rgb,

texture(stone, repeatTextureCoordinate).rgb,

texture(u_blendRamp, vec2(0.5, slope)).r);

}

• Three samplers• blendRamp could be 1D; it is just 1xn

Multitexturing: Terrain

uniform sampler2D grass;

uniform sampler2D stone;

uniform sampler2D blendRamp;

in vec3 out_Color;

void main(void)

{

// ...

out_Color = intensity * mix(

texture(grass, repeatTextureCoordinate).rgb,

texture(stone, repeatTextureCoordinate).rgb,

texture(u_blendRamp, vec2(0.5, slope)).r);

}

Use terrain slope to look up a blend value in the range [0, 1]

Page 7: GLSL Applications: 2 of 2 Assignment 3 University of ...cis565/Lectures2011S/Lecture6.pdf · CIS 565 - Spring 2011 Administrivia Assignment 2 due today 11:59pm on Blackboard Assignment

7

Multitexturing: Terrain

uniform sampler2D grass;

uniform sampler2D stone;

uniform sampler2D blendRamp;

in vec3 out_Color;

void main(void)

{

// ...

out_Color = intensity * mix(

texture(grass, repeatTextureCoordinate).rgb,

texture(stone, repeatTextureCoordinate).rgb,

texture(u_blendRamp, vec2(0.5, slope)).r);

}

Linearly blend between grass and stone

Multitexturing: Globe

� How will you render this?

Imagery from http://planetpixelemporium.com/

Multitexturing: Globe

� How will you render this?

Imagery from http://planetpixelemporium.com/

Day textureNight texture

Multitexturing: Globe

Imagery from http://planetpixelemporium.com/

Day Texture Day Night

Page 8: GLSL Applications: 2 of 2 Assignment 3 University of ...cis565/Lectures2011S/Lecture6.pdf · CIS 565 - Spring 2011 Administrivia Assignment 2 due today 11:59pm on Blackboard Assignment

8

Multitexturing: Globe

� Videos

�Night and Day

�Clouds

�Specularity

Framebuffer Objects (FBOs)

� Framebuffer Objects (FBOs)

�Allow fragment shader to write to one or more

off-screen buffers

�Can then use the off-screen buffer as a

texture in a later rendering pass

�Allows render to texture

�Don’t worry about the OpenGL API; we’ve

already coded it for you

Framebuffer Objects (FBOs)

� FBOs are lightweight containers of textures

FBO

Depth Texture

Color Texture 0

Color Texture 1

Framebuffer Objects (FBOs)

� FBO use case: post processing effects

�Render scene to FBO with depth and color

attachment

�Render a viewport-aligned quad with texture

that was the color attachment and apply effect

�How would you design a post processing

framework?

Page 9: GLSL Applications: 2 of 2 Assignment 3 University of ...cis565/Lectures2011S/Lecture6.pdf · CIS 565 - Spring 2011 Administrivia Assignment 2 due today 11:59pm on Blackboard Assignment

9

Deferred Shading

� FBO use case: deferred shading

�Render scene in two passes

� 1st pass: Visibility tests

� 2nd pass: Shading

Deferred Shading

� 1st Pass: Render geometry into G-Buffers

Fragment Colors Normals

Depth Edge Weight

Images from http://http.developer.nvidia.com/GPUGems3/gpugems3_ch19.html

Deferred Shading

� 2nd pass: shading == post processing effects

� Render viewport-aligned quads that read from G-Buffers

� Objects are no longer needed

Deferred Shading

� Light accumulation result

Image from http://http.developer.nvidia.com/GPUGems3/gpugems3_ch19.html

Page 10: GLSL Applications: 2 of 2 Assignment 3 University of ...cis565/Lectures2011S/Lecture6.pdf · CIS 565 - Spring 2011 Administrivia Assignment 2 due today 11:59pm on Blackboard Assignment

10

Deferred Shading

� What are the benefits:

�Shading and depth complexity?

�Memory requirements?

�Memory bandwidth?

�Material and light decoupling?

Ambient Occlusion

� Ambient Occlusion (AO)

� "shadowing of ambient light“

� "darkening of the ambient shading

contribution“

Image from Bavoil and Sainz. http://developer.download.nvidia.com/SDK/10.5/direct3d/Source/ScreenSpaceAO/doc/ScreenSpaceAO.pdf

Ambient Occlusion

� Ambient Occlusion

� "the crevices of the model are realistically

darkened, and the exposed parts of the model

realistically receive more light and are thus

brighter“

� "the soft shadow generated by a sphere light

of uniform intensity surrounding the scene"

Ambient Occlusion

Image from Iñigo Quílez. http://iquilezles.org/www/articles/ssao/ssao.htm

Page 11: GLSL Applications: 2 of 2 Assignment 3 University of ...cis565/Lectures2011S/Lecture6.pdf · CIS 565 - Spring 2011 Administrivia Assignment 2 due today 11:59pm on Blackboard Assignment

11

Ambient Occlusion

Images courtesy of A K Peters, Ltd. http://www.realtimerendering.com/

Evenly lit from all directions Ambient Occlusion Global Illumination

Ambient Occlusion

Image from Bavoil and Sainz. http://developer.download.nvidia.com/SDK/10.5/direct3d/Source/ScreenSpaceAO/doc/ScreenSpaceAO.pdf

� "the integral of the occlusion contributed from inside a

hemisphere of a given radius R, centered at the current surface point P and oriented towards the normal n at P"

Object Space Ambient Occlusion

� AO does not depend on light direction

� Precompute AO for static objects using ray

casting

�How many rays?

�How far do they go?

�Local objects? Or all objects?

Object Space Ambient Occlusion

Image courtesy of A K Peters, Ltd. http://www.realtimerendering.com/

• Cosine weight rays• or use importance sampling: cosine distribute number of rays

Page 12: GLSL Applications: 2 of 2 Assignment 3 University of ...cis565/Lectures2011S/Lecture6.pdf · CIS 565 - Spring 2011 Administrivia Assignment 2 due today 11:59pm on Blackboard Assignment

12

Object Space Ambient Occlusion

� Depends on scene complexity

� Stored in textures or vertices

� How can we

�Support dynamic scenes

�Be independent of scene complexity

Screen Space Ambient Occlusion

� Apply AO as a post processing effect using a combination of depth, normal, and

position buffers

� Not physically correct but plausible

� Visual quality depends on

�Screen resolution

�Number of buffers

�Number of samples

Depth Buffer Normal Buffer

Page 13: GLSL Applications: 2 of 2 Assignment 3 University of ...cis565/Lectures2011S/Lecture6.pdf · CIS 565 - Spring 2011 Administrivia Assignment 2 due today 11:59pm on Blackboard Assignment

13

View Space Eye Position Buffer Screen Space Ambient Occlusion

Images courtesy of A K Peters, Ltd. http://www.realtimerendering.com/

Screen Space Ambient Occlusion

Image from Martin Mittring. http://developer.amd.com/documentation/presentations/legacy/Chapter8-Mittring-Finding_NextGen_CryEngine2.pdf

Screen Space Ambient Occlusion

Image from Martin Mittring. http://developer.amd.com/documentation/presentations/legacy/Chapter8-Mittring-Finding_NextGen_CryEngine2.pdf

Page 14: GLSL Applications: 2 of 2 Assignment 3 University of ...cis565/Lectures2011S/Lecture6.pdf · CIS 565 - Spring 2011 Administrivia Assignment 2 due today 11:59pm on Blackboard Assignment

14

Screen Space Ambient Occlusion

Image from Mike Pan. http://mikepan.com

• Blur depth buffer• Subtract it from original depth buffer

• Scale and clamp image, then subtract from original

• Superficially resembles AO but fast