Presentation is loading. Please wait.

Presentation is loading. Please wait.

Clever Uses of OpenGL Kurt Akeley CS248 Lecture 16 15 November 2007

Similar presentations


Presentation on theme: "Clever Uses of OpenGL Kurt Akeley CS248 Lecture 16 15 November 2007"— Presentation transcript:

1 Clever Uses of OpenGL Kurt Akeley CS248 Lecture November 2007

2 CS248 Lecture 16Kurt Akeley, Fall 2007 Emphasis Is on OpenGL mechanisms and their application n OpenGL is a power tool n It can be applied in clever and non-obvious ways Is not full coverage of useful graphics algorithms n Many will not be covered n But what we do cover will be useful

3 CS248 Lecture 16Kurt Akeley, Fall 2007 Reference Advanced Graphics Programming Using OpenGL n Tom McReynolds (NVIDIA) n David Blythe (Microsoft, Direct3D 10 architect)

4 CS248 Lecture 16Kurt Akeley, Fall 2007 Informal taxonomy of clever uses Accumulation n Z-buffer n Transparent surfaces n Multisample antialiased surfaces with pre-filtered lines n Image composition Texture n Contour mapping n Image warping n Billboards n Implementing pre-filter antialiasing with texture lookup n Volume rendering Polygon offset n Coplanar primitives n Hidden-line rendering Stencil n Capping n Shadow volumes GPGPU

5 CS248 Lecture 16Kurt Akeley, Fall 2007 Invariance On a single machine n Appendix A n Invariant enable/disable n Consistent input sequence E.g., use glFrontFace to reverse facing direction, rather than reordering the vertexes or reflecting by scaling Cross-platform n Be careful! n OpenGL’s design emphasized cross-platform compatibility n But there are still many differences between platforms n Endian issues and support

6 CS248 Lecture 16Kurt Akeley, Fall 2007 Accumulation

7 CS248 Lecture 16Kurt Akeley, Fall 2007 Accumulation Basic idea: n Build up a final image in the framebuffer by depth buffering and/or blending multiple images Examples n Z-buffer n Transparent surfaces n Multisample solids with pre-filtered antialiased lines n Image composition

8 CS248 Lecture 16Kurt Akeley, Fall 2007 Z-buffer glEnable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST); glDepthFunc(GL_NEVER | GL_LESS | GL_EQUAL | GL_LEQUAL | GL_GREATER | GL_NOTEQUAL | GL+GEQUAL | GL_ALWAYS); glDepthFunc(GL_ALWAYS); // invariant disable glDepthMask(GL_TRUE); // enable writing glDepthMask(GL_FALSE); // disable writing if (Zfrag depthfunc Zpixel) { if ( Rcolormask ) Rpixel  Rfrag; if ( Gcolormask ) Gpixel  Gfrag; if ( Bcolormask ) Bpixel  Bfrag; if ( Acolormask ) Apixel  Afrag; if ( depthmask ) Zpixel  Zfrag; }

9 CS248 Lecture 16Kurt Akeley, Fall 2007 Transparent surfaces glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); draw opaque objects glDepthMask(GL_FALSE); // key OpenGL mode glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_CULL_FACE); // optional glCullFace(GL_BACK); draw transparent surfaces in any order glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glDepthMask(GL_TRUE); glDisable(GL_BLEND); glDisable(GL_CULL_FACE);

10 CS248 Lecture 16Kurt Akeley, Fall 2007 Multisample and pre-filter antialiasing glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_MULTISAMPLE); draw solid objects (triangles) glDepthMask(GL_FALSE); glDisable(GL_MULTISAMPLE); glEnable(GL_LINE_SMOOTH); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE); glDisable(GL_LIGHTING); // optional draw pre-filter antialiased lines in any order glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glDepthMask(GL_TRUE); glDisable(GL_LINE_SMOOTH); glDisable(GL_BLEND);

11 CS248 Lecture 16Kurt Akeley, Fall 2007 Image composition (fade) glEnable(GL_BLEND); glBlendFunc(GL_CONSTANT_ALPHA, GL_ONE); glBlendColor(0, 0, 0, first weight ); glDrawPixels( first image ); glBlendColor(0, 0, 0, second weight ); glDrawPixels( second image ); glDisable(GL_BLEND);

12 CS248 Lecture 16Kurt Akeley, Fall 2007 Image composition (over) glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ZERO); glDrawPixels( first image ); gllendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDrawPixels( second image ); glDisable(GL_BLEND);

13 CS248 Lecture 16Kurt Akeley, Fall 2007 Texture

14 CS248 Lecture 16Kurt Akeley, Fall 2007 Texture Basic idea: n Use texture mapping mechanisms for creative purposes Examples n Contour mapping n Image warping n Billboards n Implementing pre-filter antialiasing texture lookup n Volume rendering

15 CS248 Lecture 16Kurt Akeley, Fall 2007 Contour mapping glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_TEXTURE_1D); glEnable(GL_TEXTURE_GEN_S); glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); glTexGenfv(GL_S, GL_EYE_PLANE, vec4f(f, 0, 10, 0, 0)); draw objects without specifying texture coordinates glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glDisable(GL_TEXTURE_1D); glDisable(GL_TEXTURE_GEN_S); Today a vertex shader is a more general TexGen mechanism. But the notion of generated texture coordinates remains important.

16 CS248 Lecture 16Kurt Akeley, Fall 2007 Image warping glEnable(GL_TEXTURE_2D); for (y=0; y<(height-1); ++y) { glBegin(GL_QUAD_STRIP); for (x=0; x

17 CS248 Lecture 16Kurt Akeley, Fall 2007 Billboards Poster-child application of geometry shaders! Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations Framebuffer Advanced Graphics Programming Using OpenGL Figure 13.4

18 CS248 Lecture 16Kurt Akeley, Fall 2007 The magic of machine shops Sewing machines make clothes But machine tools make machine tools n And computers are this century’s machine tools Hagley Machine Shop, Wilmington, DE Photo by Incaz, Flickr

19 CS248 Lecture 16Kurt Akeley, Fall 2007 Pre-filter antialiasing via texture lookup Another ideal geometry-shader application // draw pre-filtered point at (x,y) const float h = 1.5; // 3x3 filter glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2f(x-h, y-h); glTexCoord2f(0, 1); glVertex2f(x-h, y+h); glTexCoord2f(1, 1); glVertex2f(x+h, y+h); glTexCoord2f(1, 0); glVertex2f(x+h, y-h); glEnd(); Vertex assembly Primitive assembly Rasterization Fragment operations Display Vertex operations Application Primitive operations Framebuffer (0 0)(1 0) (1 1)(0 1) (x-h y-h)(x+h y-h) (x+h y+h)(x-h y+h)

20 CS248 Lecture 16Kurt Akeley, Fall 2007 Volume rendering Advanced Graphics Programming Using OpenGL Figure Advanced Graphics Programming Using OpenGL Figure 20.13

21 CS248 Lecture 16Kurt Akeley, Fall 2007 Polygon Offset

22 CS248 Lecture 16Kurt Akeley, Fall 2007 Polygon offset Basic idea: n Avoid depth fighting by biasing Z values Examples n Coplanar primitives n Hidden lines n Silhouette edges

23 CS248 Lecture 16Kurt Akeley, Fall 2007 Polygon mode glPolygonMode(GLenum face, GLenum mode); GL_FILL, GL_LINE, GL_POINT GL_FRONT, GL_BACK, GL_FRONT_AND_BACK GL_FILLGL_LINEGL_POINT Face culling happens before conversion to lines or points!

24 CS248 Lecture 16Kurt Akeley, Fall 2007 Polygon offset glEnable/glDisable(GL_POLYGON_OFFSET_FILL | GL_POLYGON_OFFSET_LINE | GL_POLYGON_OFFSET_POINT); glPolygonOffset(GLfloat factor, GLfloat units ); Triangle (on edge) Line (on edge) View position -z Correspond to polygon modes Minimum resolvable z- buffer difference

25 CS248 Lecture 16Kurt Akeley, Fall 2007 Coplanar primitives glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset( maxwidth /2, 1); draw planar surface glDepthMask(GL_FALSE); glDisable(GL_POLYGON_OFFSET_FILL); draw points, lines, and polygons on the planar surface glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glDepthMask(GL_TRUE);

26 CS248 Lecture 16Kurt Akeley, Fall 2007 Hidden lines glEnable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glColorMask( false, false, false, false ); glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset( maxwidth /2, 1); draw solid objects glDepthMask(GL_FALSE); glColorMask( true, true, true, true ); glColor3f( linecolor ); glDisable(GL_POLYGON_OFFSET_FILL); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); draw solid objects again glDisable(GL_DEPTH_TEST); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glDepthMask(GL_TRUE);

27 CS248 Lecture 16Kurt Akeley, Fall 2007 Silhouette lines (true hidden-line drawing) glEnable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glColorMask( false, false, false, false ); glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset( maxwidth /2, 1); draw solid objects glDepthMask(GL_FALSE); glColorMask( true, true, true, true ); glColor3f(1, 1, 1); glDisable(GL_POLYGON_OFFSET_FILL); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); draw solid objects again draw true edges // for a complete hidden-line drawing glDisable(GL_DEPTH_TEST); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glDepthMask(GL_TRUE); glDisable(GL_CULL_FACE); Additions to the hidden- line algorithm (previous slide) highlighted in red

28 CS248 Lecture 16Kurt Akeley, Fall 2007 Stencil

29 CS248 Lecture 16Kurt Akeley, Fall 2007 Stencil Basic idea: n Implement a simple state machine in every pixel Examples n Capping n Shadow volumes

30 CS248 Lecture 16Kurt Akeley, Fall 2007 Stencil glEnable(GL_STENCIL_TEST); glDisable(GL_STENCIL_TEST); glStencilFunc(GLenum func, GLint ref, GLuint mask ); glStencilOp(GLenum fail, GLenum zfail, GLenum zpass ); glStencilMask(GLuint mask ); GL_NEVER, GL_LESS, GL_LEQUAL, GL_GREATER, GL_GEQUAL, GL_EQUAL, GL_NOTEQUAL, GL_ALWAYS GL_KEEP, GL_ZERO, GL_REPLACE (with ref ), GL_INCR, GL_DECR, GL_INVERT Bitmask, not Boolean flag

31 CS248 Lecture 16Kurt Akeley, Fall 2007 Z-buffer operation (again) if (Zfrag depthfunc Zpixel) { if ( Rcolormask ) Rpixel  Rfrag; if ( Gcolormask ) Gpixel  Gfrag; if ( Bcolormask ) Bpixel  Bfrag; if ( Acolormask ) Apixel  Afrag; if ( depthmask ) Zpixel  Zfrag; }

32 CS248 Lecture 16Kurt Akeley, Fall 2007 Stencil operation if (( ref & mask ) stencilfunc (Spixel & mask )) { if (Zfrag depthfunc Zpixel) { if ( Rcolormask ) Rpixel  Rfrag; if ( Gcolormask ) Gpixel  Gfrag; if ( Bcolormask ) Bpixel  Bfrag; if ( Acolormask ) Apixel  Afrag; if ( depthmask ) Zpixel  Zfrag; StencilOp( zpass ); } else { StencilOp( zfail ); } } else { StencilOp( fail ); } Z-buffer operation Stencil implements a state machine in each pixel. (A programmable action occurs in every cases)

33 CS248 Lecture 16Kurt Akeley, Fall 2007 Capping glEnable(GL_DEPTH_TEST); // remains enabled glEnable(GL_LIGHTING); for (int i=0; i

34 CS248 Lecture 16Kurt Akeley, Fall 2007 Shadow volumes Similar to capping: n Render the scene n Render shadow volumes n Don’t change color or depth n Use stencil to determine in/out n Reduce intensities of pixels in shadow Common game technique n E.g., Quake, Doom Simple frustum culling fails! n Must keep light sources and occluders that cast shadows on geometry within the frustum

35 CS248 Lecture 16Kurt Akeley, Fall 2007 GPGPU

36 CS248 Lecture 16Kurt Akeley, Fall 2007 GPGPU Basic idea: n General-purposes computing on GPUs n Take advantage of the huge compute power of modern GPUs

37 CS248 Lecture 16Kurt Akeley, Fall 2007 Multi-pass vector processing (2000) #include “marble.h” surface marble() { varying color a; uniform string fx; uniform float x; x = ½; fx = “noisebw.tx”; FB = texture(tx, scale(x,x,x)); repeat(3) { x = x * 0.5; FB *= 0.5; FB += texture(tx, scale(x,x,x)); } FB = lookup(FB,tab); a = FB; FB = diffuse; FB *= a; FB += environment(“env”); } Treat OpenGL as a very long instruction word Compute vector style n Apply inst. to all pixels Build up final image in many passes Peercy, Olano, Airey, and Ungar, Interactive Multi-Pass Programmable Shading, SIGGRAPH 2000 (Figure adapted from the SIGGRAPH paper)

38 CS248 Lecture 16Kurt Akeley, Fall 2007 GPGPU Still operates on images n Conceptually 2-D arrays of data elements Deemphasizes VLIW thinking n Most pipeline stages are not used n What is used: n Rasterization (to generate and schedule data elements) n Fragment operations (specifically the programmable shader) n Texture lookup and filter (gather, not a stream processor) n Fragment/framebuffer operations (usually limited to write) Emphasizes data-parallel programmability Clever solutions have been developed for n Scatter n Reduction n Sorting …

39 CS248 Lecture 16Kurt Akeley, Fall 2007 Modern GPGPU Graphics APIs (OpenGL, Direct 3D) being replaced: n CUDA (NVIDIA) n CTM (AMD) Great results being achieved: n Technical: 10x performance improvement in some cases n Business: multi-billion dollars anticipated soon Coming soon: n IEEE double precision arithmetic n Greater exposure of hardware details (AMD) n Intel Larrabee n …

40 CS248 Lecture 16Kurt Akeley, Fall 2007 Summary Powerful OpenGL mechanisms (some introduced by IRIS/OpenGL): n 8-way comparison and masks (depth, stencil, alpha, …) n Texture features: n 3-D n TexGen and Texture coordinate matrix n Homogeneous coordinates n Application to all primitives (not just triangles) n glPolygonOffset n Stencil (state machine in a pixel) Shaders have devalued some of these (e.g., TexGen) but most remain valuable It’s fun and productive to devise clever uses of OpenGL

41 CS248 Lecture 16Kurt Akeley, Fall 2007 Assignments No class next week Next lecture: Color theory (Tuesday 27 November) Reading assignment: FvD 13.2 through 13.6

42 CS248 Lecture 16Kurt Akeley, Fall 2007 End


Download ppt "Clever Uses of OpenGL Kurt Akeley CS248 Lecture 16 15 November 2007"

Similar presentations


Ads by Google