more opengl more opengl
play

More OpenGL More OpenGL Gustav Taxn, Ph. D. Associate Professor - PowerPoint PPT Presentation

More OpenGL More OpenGL Gustav Taxn, 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


  1. More OpenGL More OpenGL Gustav Taxén, Ph. D. Associate Professor CSC / KTH gustavt@csc.kth.se

  2. Fixed function function pipeline pipeline Fixed CPU Application Graphics hardware Per-vertex Primitive Rasteriza- operations assembly tion Per-fragment Framebuffer operations

  3. Programmable pipeline pipeline Programmable Application CPU Graphics hardware (GPU) Programmable Primitive Rasteriza- vertex units assembly tion Framebuffer Programmable fragment units Render target

  4. Vertex shader Vertex unit

  5. N 1 N 2 L L float3 main(float3 N : NORMAL, uniform float3 L) { float d = dot(N, L); return float3(d, d, d); }

  6. Pixel shader Pixel unit

  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

  8. Stream processors processors Stream Data in Stream processors Raster ops

  9. 4 Vertex Units + 8 Pixel Units VU VU VU VU PU PU PU PU PU PU PU PU

  10. Vertex-intensive application VU VU VU VU PU PU PU PU PU PU PU PU

  11. Pixel-intensive application VU VU VU VU PU PU PU PU PU PU PU PU

  12. 12 stream processors PU PU PU PU PU PU VU PU PU VU PU PU

  13. • 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 ...

  14. Geometry specification Geometry specification glBegin(GL_QUADS); glVertex3f(...); ... glEnd(); 6 sides x 4 corners per side: 24 vertices / 24 function calls

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

  16. 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!

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

  18. Reflection mapping Reflection mapping ( R x R y R z )

  19. Jim Blinn, 1976

  20. Gene Miller, Michael Shou m.fl. 1982-1983

  21. Interface , Lance Williams, 1985

  22. Terminator 2 , 1991

  23. Sphere mapping Sphere mapping bildplan

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

  25. Sphere mapping - - issues issues Sphere mapping Resolution varies substantially with the reflection direction Texture interpolation for “backwards” directions.

  26. Cube mapping Cube mapping

  27. Buffer tests in OpenGL Buffer tests in OpenGL Frame buffer Stencil test Alpha test Depth test

  28. 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)

  29. Stencil test Stencil test Reference value Stencil buffer Fragment

  30. Stencil test Stencil test – configures the test glStencilFunc() • – specifies how the glStencilOp() • reference value should be updated

  31. 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)

  32. Using stencils for reflections Using stencils for reflections Use stencil buffer to cut this part

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

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

  35. Multitexturing Multitexturing

  36. Multitexturing Multitexturing RGBA från lighting (or glColor ) Texture Unit 0 RGBA from texture 0 Texture Unit 1 RGBA from texture 1 ...

  37. #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, ...);

  38. 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();

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