1 / 79

3D Graphics Programming

Programmable Pipelines. 3D Graphics Programming. Objectives. Introduce programmable pipelines Vertex shaders Fragment shaders Introduce shading languages Needed to describe shaders RenderMan. Introduction. Recent major advance in real time graphics is programmable pipeline

omer
Download Presentation

3D Graphics Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programmable Pipelines 3D Graphics Programming

  2. Objectives • Introduce programmable pipelines • Vertex shaders • Fragment shaders • Introduce shading languages • Needed to describe shaders • RenderMan Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  3. Introduction • Recent major advance in real time graphics is programmable pipeline • First introduced by NVIDIA GForce 3 • Supported by high-end commodity cards • NVIDIA, ATI, 3D Labs • Software Support • Direct X 8 , 9, 10 • OpenGL Extensions • OpenGL Shading Language (GLSL) • Cg Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  4. Background • Two components • Vertex programs (shaders) • Fragment programs (shaders) • Requires detailed understanding of two seemingly contradictory apporachs • OpenGL pipeline • Real time • RenderMan ideas • offline Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  5. CPU Frame Buffer Fragment Processor Geometry Processor Rasterizer Black Box View fragments fragments vertices vertices Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  6. Geometric Calculations • Geometric data: set of vertices + type • Can come from program, evaluator, display list • type: point, line, polygon • Vertex data can be • (x,y,z,w) coordinates of a vertex (glVertex) • Normal vector • Texture Coordinates • RGBA color • Other data: color indices, edge flags • Additional user-defined data in GLSL Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  7. Per-Vertex Operations • Vertex locations are transformed by the model-view matrix into eye coordinates • Normals must be transformed with the inverse transpose of the model-view matrix so that v·n=v’ ·n’ in both spaces • Assumes there is no scaling • May have to use autonormalization • Textures coordinates are generated if autotexture enabled and the texture matrix is applied Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  8. Lighting Calculations • Consider a per-vertex basis Phong model • Phong model requires computation of r and v at every vertex I =kd Idl · n +ks Is (v · r )a + ka Ia Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  9. Calculating the Reflection Term angle of incidence = angle of reflection cos qi = cos qr or r·n = l·n r, n, and l are coplanar r = al + bn normalize 1 = r·r = n·n = l·l solving: r = 2(l · n)n-l Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  10. OpenGL Lighting • Modified Phong model • Halfway vector • Global ambient term • Specified in standard • Supported by hardware Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  11. Halfway Vector Blinn proposed replacing v·r by n·h where h = (l+v)/|l + v| (l+v)/2 is halfway between l and v If n, l, and v are coplanar: • y = f/2 Must then adjust exponent so that (n·h)e’≈ (r.v)e Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  12. Primitive Assembly • Vertices are next assembled into objects • Polygons • Line Segements • Points • Transformation by projection matrix • Clipping • Against user defined planes • View volume, x=±w, y=±w, z=±w • Polygon clipping can create new vertices • Perspective Division • Viewport mapping Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  13. Rasterization • Geometric entities are rasterized into fragments • Each fragment corresponds to a point on an integer grid: a displayed pixel • Hence each fragment is a potential pixel • Each fragment has • A color • Possibly a depth value • Texture coordinates Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  14. Fragment Operations • Texture generation • Fog • Antialiasing • Scissoring • Alpha test • Blending • Dithering • Logical Operation • Masking Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  15. Vertex Processor • Takes in vertices • Position attribute • Possibly color • OpenGL state • Produces • Position in clip coordinates • Vertex color Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  16. Fragment Processor • Takes in output of rasterizer (fragments) • Vertex values have been interpolated over primitive by rasterizer • Outputs a fragment • Color • Texture • Fragments still go through fragment tests • Hidden-surface removal • alpha Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  17. Programmable Shaders • Replace fixed function vertex and fragment processing by programmable processors called shaders • Can replace either or both • If we use a programmable shader we must do all required functions of the fixed function processor Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  18. Development • RenderMan Shading Language • Offline rendering • Hardware Shading Languages • UNC, Stanford • NVIDIA • OpenGL Vertex Program Extension • OpenGL Shading Language • Cg • OpenGL • Microsoft HLSL Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  19. Renderer Modeler RenderMan • Developed by Pixar • S. Upstill, The RenderMan Companion, Addison-Wesley, 1989. • Model interface file (RIB) Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  20. Modeling vs Rendering • Modeler outputs geometric model plus information for the renderer • Specifications of camera • Materials • Lights • May have different kinds of renderers • Ray tracer • Radiosity • How do we specify a shader? Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  21. Mesa • http://www.mesa3d.org/ • Software implement OpenGL • opengl32.dll build by mesa 7.2 • In glsl.zip\chapt15\shaders

  22. Build mesa lib using mingw • set path=C:\Dev-Cpp\bin;%PATH% • set LIBRARY_PATH=C:\Dev-Cpp\lib • set C_INCLUDE_PATH=C:\Dev-Cpp\include • set CPLUS_INCLUDE_PATH=C:\Dev-Cpp\include\c++\3.4.2;C:\Dev-Cpp\include\c++\3.4.2\backward;C:\Dev-Cpp\include\c++\mingw32;C:\Dev-Cpp\include • mingw32-make -f Makefile.mgw

  23. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico GLSL I

  24. Objectives • Shader applications • Vertex shaders • Fragment shaders • Programming shaders • Cg • GLSL Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  25. Vertex Shader Applications • Moving vertices • Morphing • Wave motion • Fractals • Lighting • More realistic models • Cartoon shaders Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  26. Fragment Shader Applications Per fragment lighting calculations per vertex lighting per fragment lighting Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  27. Fragment Shader Applications Texture mapping smooth shading environment mapping bump mapping Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  28. Writing Shaders • First programmable shaders were programmed in an assembly-like manner • OpenGL extensions added for vertex and fragment shaders • Cg (C for graphics) C-like language for programming shaders • Works with both OpenGL and DirectX • Interface to OpenGL complex • OpenGL Shading Language (GLSL) Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  29. GLSL • OpenGL Shading Language • Part of OpenGL 2.0 • High level C-like language • New data types • Matrices • Vectors • Samplers • OpenGL state available through built-in variables Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  30. Simple Vertex Shader const vec4 red = vec4(1.0, 0.0, 0.0, 1.0); void main(void) { gl_Position = gl_ProjectionMatrix *gl_ModelViewMartrix*gl_Vertex; gl_FrontColor = red; } Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  31. Execution Model Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  32. Simple Fragment Program void main(void) { gl_FragColor = gl_Color; } Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  33. Execution Model Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  34. Data Types • C types: int, float, bool • Vectors: • float vec2, vec 3, vec4 • Also int (ivec) and boolean (bvec) • Matrices: mat2, mat3, mat4 • Stored by columns • Standard referencing m[row][column] • C++ style constructors • vec3 a =vec3(1.0, 2.0, 3.0) • vec2 b = vec2(a) Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  35. Pointers • There are no pointers in GLSL • We can use C structs which can be copied back from functions • Because matrices and vectors are basic types they can be passed into and output from GLSL functions, e.g. matrix3 func(matrix3 a) Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  36. Qualifiers • GLSL has many of the same qualifiers such as const as C/C++ • Need others due to the nature of the execution model • Variables can change • Once per primitive • Once per vertex • Once per fragment • At any time in the application • Vertex attributes are interpolated by the rasterizer into fragment attributes Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  37. Attribute Qualifier • Attribute-qualified variables can change at most once per vertex • Cannot be used in fragment shaders • Built in (OpenGL state variables) • gl_Color • gl_ModelViewMatrix • User defined (in application program) • attribute float temperature • attribute vec3 velocity Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  38. Uniform Qualified • Variables that are constant for an entire primitive • Can be changed in application outside scope of glBegin and glEnd • Cannot be changed in shader • Used to pass information to shader such as the bounding box of a primitive Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  39. Varying Qualified • Variables that are passed from vertex shader to fragment shader • Automatically interpolated by the rasterizer • Built in • Vertex colors • Texture coordinates • User defined • Requires a user defined fragment shader Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  40. Example: Vertex Shader const vec4 red = vec4(1.0, 0.0, 0.0, 1.0); varying vec3 color_out; void main(void) { gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex; color_out = red; } Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  41. Required Fragment Shader varying vec3 color_out; void main(void) { gl_FragColor = color_out; } Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  42. Passing values • call by value-return • Variables are copied in • Returned values are copied back • Three possibilities • in • out • inout Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  43. Operators and Functions • Standard C functions • Trigonometric • Arithmetic • Normalize, reflect, length • Overloading of vector and matrix types mat4 a; vec4 b, c, d; c = b*a; // a column vector stored as a 1d array d = a*b; // a row vector stored as a 1d array Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  44. Swizzling and Selection • Can refer to array elements by element using [] or selection (.) operator with • x, y, z, w • r, g, b, a • s, t, p, q • a[2], a.b, a.z, a.p are the same • Swizzling operator lets us manipulate components vec4 a; a.yz = vec2(1.0, 2.0); Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

  45. Ch15 example shaders

  46. String based shader GLchar vsString[] = "void main(void)\n" "{\n" " // This is our Hello World vertex shader\n" " // notice how comments are preceded by '//'\n" "\n" " // normal MVP transform\n" " vec4 clipCoord = gl_ModelViewProjectionMatrix * gl_Vertex;\n" " gl_Position = clipCoord;\n" "\n" " // Copy the primary color\n" " gl_FrontColor = gl_Color;\n" "\n" " // Calculate NDC\n" " vec4 ndc = vec4(clipCoord.xyz, 0) / clipCoord.w;\n" "\n" " // Map from [-1,1] to [0,1] before outputting\n" " gl_FrontSecondaryColor = (ndc * 0.5) + 0.5;\n" "}\n";

  47. GLchar fsString[] = "uniform float flickerFactor;\n" "\n" "void main(void)\n" "{\n" " // Mix primary and secondary colors, 50/50\n" " vec4 temp = mix(gl_Color, vec4(vec3(gl_SecondaryColor), 1.0), 0.5);\n" "\n" " // Multiply by flicker factor\n" " gl_FragColor = temp * flickerFactor;\n" "}\n"; ___________________________________________________________________________ uniform float flickerFactor; void main(void) { // Mix primary and secondary colors, 50/50 vec4 temp = mix(gl_Color, vec4(vec3(gl_SecondaryColor), 1.0), 0.5); // Multiply by flicker factor gl_FragColor = temp * flickerFactor; }

  48. case 1: useVertexShader = !useVertexShader; if (useVertexShader) { glutChangeToMenuEntry(1, "Toggle vertex shader (currently ON)", 1); glAttachShader(progObj, vShader); } else { glutChangeToMenuEntry(1, "Toggle vertex shader (currently OFF)", 1); glDetachShader(progObj, vShader); } Relink(); break;

  49. Uniform // Find out where the flicker constant lives flickerLocation = glGetUniformLocation(progObj, "flickerFactor"); // Pick a random flicker factor flickerFactor += ((((GLfloat)rand())/((GLfloat)RAND_MAX)) - 0.5f) * 0.1f; if (flickerFactor > 1.0f) flickerFactor = 1.0f; if (flickerFactor < 0.0f) flickerFactor = 0.0f; glUniform1f(flickerLocation, flickerFactor);

  50. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico GLSL II

More Related