SLIDE 1 More OpenGL More OpenGL
Gustav Taxén, Ph. D.
Associate Professor CSC / KTH
gustavt@csc.kth.se
SLIDE 2 Fixed Fixed function function pipeline pipeline
Application
CPU Per-vertex
Primitive assembly Rasteriza- tion Per-fragment
Framebuffer Graphics hardware
SLIDE 3 Application
CPU Programmable vertex units Primitive assembly Rasteriza- tion Programmable fragment units Framebuffer Graphics hardware (GPU) Render target
Programmable Programmable pipeline pipeline
SLIDE 4
Vertex unit
Vertex shader
SLIDE 5 N2 L
float3 main(float3 N : NORMAL, uniform float3 L) { float d = dot(N, L); return float3(d, d, d); }
N1 L
SLIDE 6
Pixel unit
Pixel shader
SLIDE 7 float3 main(float2 uv : TEXCOORD0, sampler2D normalMap, uniform float3 L) { float3 N = tex2D(normalMap, uv); float d = dot(N, L); return float3(d, d, d); } http://nehe.gamedev.net
SLIDE 8
Stream Stream processors processors
Stream processors Data in Raster ops
SLIDE 9
VU PU VU VU VU PU PU PU PU PU PU PU 4 Vertex Units + 8 Pixel Units
SLIDE 10
VU PU VU VU VU PU PU PU PU PU PU PU Vertex-intensive application
SLIDE 11
VU PU VU VU VU PU PU PU PU PU PU PU Pixel-intensive application
SLIDE 12
12 stream processors VU VU PU PU PU PU PU PU PU PU PU PU
SLIDE 13
- Limited instruction set
- Optimized for 4D float
vectors
- Inefficient branching
- Small, fast RAM
- Small, fast cache
- Excellent for SIMD ops
Stream processor Data Stream processor ...
SLIDE 14
SLIDE 15 Geometry specification Geometry specification
glBegin(GL_QUADS); glVertex3f(...); ... glEnd();
6 sides x 4 corners per side: 24 vertices / 24 function calls
SLIDE 16 Vertex arrays Vertex arrays
1 2 3 5 4 6 7 x y z
0 1 2 3 4 5 ...
Vertex Array index
1 2 3 5 4 6 7
SLIDE 17 Indexed primitives Indexed primitives
1 2 3 5 4 6 7
0 1 2 3 4 5 ...
Vertex Array index
1 2 3 5 4 6 7 1 2 3 5 4
0 1 2 3 4 5...
Quad Array index
7 4 3 0
8 vertices, 2 function calls!
SLIDE 18 Interleaved vertex arrays Interleaved vertex arrays
1 2 3
0 1 2 3 4 5 ...
Vertex Array index
x y z nx ny nz stride = 6 floats
SLIDE 19 Reflection mapping Reflection mapping
(Rx Ry Rz )
SLIDE 21 Gene Miller, Michael Shou m.fl. 1982-1983
SLIDE 22 Interface, Lance Williams, 1985
SLIDE 23 Terminator 2, 1991
SLIDE 24
Sphere mapping Sphere mapping
bildplan
SLIDE 25 Sphere mapping Sphere mapping
2
1 2 2 2
1 2 2 1 , 2 1
z y x y x
R R R m m R t m R s
Reflection vector R = (Rx Ry Rz ) to texture coordinates (s, t):
SLIDE 26 Sphere mapping Sphere mapping -
issues
Resolution varies substantially with the reflection direction
Texture interpolation for “backwards” directions.
SLIDE 27
Cube mapping Cube mapping
SLIDE 28
SLIDE 29
Buffer tests in OpenGL Buffer tests in OpenGL
Frame buffer Depth test Stencil test Alpha test
SLIDE 30 Alpha test Alpha test
RGBA = (1, 1, 1, 0.7) RGBA = (1, 1, 1, 0.2) glAlphaFunc(GL_GREATER,0.5); glEnable(GL_ALPHA_TEST);
SLIDE 31 Stencil test Stencil test
Stencil buffer Fragment Reference value
SLIDE 32 Stencil test Stencil test
– configures the test
– specifies how the reference value should be updated
SLIDE 33
glStencilFunc(GL_EQUAL, 1, ~0);
Stencil test returns TRUE if the reference value equals 1 ~0 is a bit mask and specifies which bits in the reference value that should be compared (~0 = 0xFFFF, all bits)
Stencil test Stencil test
SLIDE 34 Using stencils for reflections Using stencils for reflections
Use stencil buffer to cut this part
SLIDE 35
Alpha blending Alpha blending
Rout = Rin SR + Rframebuffer DR
where (SR,DR), (SG,DG), (SB,DB) are blending factors
Gout = Gin SG + Gframebuffer DG Bout = Bin SB + Bframebuffer DB
SLIDE 36 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Rout = Rin Ain + Rframebuffer (1 – Ain )
Step 1: Draw yellow triangle Step 2: glEnable(GL_BLEND); Draw blue triangle with A < 1 SR DR
SLIDE 37
Multitexturing Multitexturing
SLIDE 38 Multitexturing Multitexturing
RGBA från lighting (or glColor) RGBA from texture 0 Texture Unit 0 RGBA from texture 1 Texture Unit 1 ...
SLIDE 39
#include <glew.h> glActiveTextureARB(GL_TEXTURE0_ARB); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, ...); glActiveTextureARB(GL_TEXTURE1_ARB); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, ...);
SLIDE 40 glBegin(GL_TRIANGLES); glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0); glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.1, 0.2); glVertex3f(0.0, 0.0, 0.0); glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 0.0); glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.6, 0.2); glVertex3f(1.0, 0.0, 0.0); glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.5, 1.0); glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.3, 0.5); glVertex3f(0.5, 1.0, 0.0); glEnd();
SLIDE 41 Parsing images Parsing images
- No image parser in OpenGL!
- If you’re on Linux / Mac OS X, use libjpeg
source at http://www.ijg.org/
- If you’re on Windows, use
http://corona.sourceforge.net/