More OpenGL More OpenGL Gustav Taxén, Ph. D. Associate Professor CSC / KTH gustavt@csc.kth.se
Fixed function function pipeline pipeline Fixed CPU Application Graphics hardware Per-vertex Primitive Rasteriza- operations assembly tion Per-fragment Framebuffer operations
Programmable pipeline pipeline Programmable Application CPU Graphics hardware (GPU) Programmable Primitive Rasteriza- vertex units assembly tion Framebuffer Programmable fragment units Render target
Vertex shader Vertex unit
N 1 N 2 L L float3 main(float3 N : NORMAL, uniform float3 L) { float d = dot(N, L); return float3(d, d, d); }
Pixel shader Pixel unit
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
Stream processors processors Stream Data in Stream processors Raster ops
4 Vertex Units + 8 Pixel Units VU VU VU VU PU PU PU PU PU PU PU PU
Vertex-intensive application VU VU VU VU PU PU PU PU PU PU PU PU
Pixel-intensive application VU VU VU VU PU PU PU PU PU PU PU PU
12 stream processors PU PU PU PU PU PU VU PU PU VU PU PU
• Limited instruction set Data • Optimized for 4D float vectors • Inefficient branching Stream processor • Small, fast RAM • Small, fast cache Stream processor • Excellent for SIMD ops ...
Geometry specification Geometry specification glBegin(GL_QUADS); glVertex3f(...); ... glEnd(); 6 sides x 4 corners per side: 24 vertices / 24 function calls
7 6 5 4 z y 3 x Vertex arrays Vertex arrays 2 0 1 2 3 4 5 ... 1 0 Vertex Array index 2 1 0 3 5 6 4 7
Indexed primitives Indexed primitives 6 1 Vertex 0 1 2 3 4 5 6 7 0 7 Array 0 1 2 3 4 5 ... index 5 7 4 3 0 2 4 3 Array 0 1 2 3 4 5... index 0 1 2 3 4 5 Quad 8 vertices, 2 function calls!
Interleaved vertex arrays Interleaved vertex arrays Vertex 0 1 2 3 Array 0 1 2 3 4 5 ... index x y z nx ny nz = 6 floats stride
Reflection mapping Reflection mapping ( R x R y R z )
Jim Blinn, 1976
Gene Miller, Michael Shou m.fl. 1982-1983
Interface , Lance Williams, 1985
Terminator 2 , 1991
Sphere mapping Sphere mapping bildplan
Sphere mapping Sphere mapping Reflection vector R = ( R x R y R z ) to texture coordinates ( s, t ) : R 1 1 R y x , s t 2 2 m m 2 1 2 2 2 m 2 R R R 1 x y z
Sphere mapping - - issues issues Sphere mapping Resolution varies substantially with the reflection direction Texture interpolation for “backwards” directions.
Cube mapping Cube mapping
Buffer tests in OpenGL Buffer tests in OpenGL Frame buffer Stencil test Alpha test Depth test
Alpha test Alpha test glAlphaFunc(GL_GREATER,0.5); glEnable(GL_ALPHA_TEST); RGBA = (1, 1, 1, 0.7) RGBA = (1, 1, 1, 0.2)
Stencil test Stencil test Reference value Stencil buffer Fragment
Stencil test Stencil test – configures the test glStencilFunc() • – specifies how the glStencilOp() • reference value should be updated
Stencil test Stencil test glStencilFunc(GL_EQUAL, 1, ~0); Stencil test returns TRUE if the reference value equals 1 is a bit mask and specifies which ~0 bits in the reference value that should be compared ( ~0 = 0xFFFF , all bits)
Using stencils for reflections Using stencils for reflections Use stencil buffer to cut this part
Alpha blending Alpha blending R out = R in S R + R framebuffer D R G out = G in S G + G framebuffer D G B out = B in S B + B framebuffer D B where ( S R ,D R ), ( S G ,D G ), ( S B ,D B ) are blending factors
S R glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); D R R out = R in A in + R framebuffer (1 – A in ) Step 2: Step 1: glEnable(GL_BLEND); Draw yellow triangle Draw blue triangle with A < 1
Multitexturing Multitexturing
Multitexturing Multitexturing RGBA från lighting (or glColor ) Texture Unit 0 RGBA from texture 0 Texture Unit 1 RGBA from texture 1 ...
#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, ...);
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();
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/
Recommend
More recommend