• No results found

CSE 5542 - Real Time Rendering Week 4

N/A
N/A
Protected

Academic year: 2021

Share "CSE 5542 - Real Time Rendering Week 4"

Copied!
89
0
0

Loading.... (view fulltext now)

Full text

(1)CSE 5542 - Real Time Rendering. Week 4.

(2) Slides(Mostly) Courtesy –  E. Angel and D. Shreiner.

(3) Recap from Recent Past.

(4) The Sierpinski Gasket.

(5) Sierpinski Vertex Shader. …. // Load shaders and use the resulting shader program. GLuint program = InitShader( "vshader21.glsl", "fshader21.glsl" );. glUseProgram( program );. ... attribute vec4 vPosition;. void. main(). {. gl_Position = vPosition;. }.

(6) Sierpinski Fragment Shader. …. // Load shaders and use the resulting shader program. GLuint program = InitShader( "vshader21.glsl", "fshader21.glsl" );. glUseProgram( program );. ... void. main(). {. gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );. }.

(7) Fragment vs Vertex Shader . per vertex lighting. 7. per fragment lighting.

(8) OpenGL and GLSL. • Shader based OpenGL is based less on a state machine model than a data flow model. • Most state variables, attributes and related pre 3.1 OpenGL functions have been deprecated. • Action happens in shaders. • Job is application is to get data to GPU. 8.

(9) GLSL. • C-like with . – Matrix and vector types (2, 3, 4 dimensional). – Overloaded operators. – C++ like constructors. • Similar to Nvidia’s Cg and Microsoft HLSL. • Code sent to shaders as source code. 9.

(10) Still Maximal Portability . • Display device independent . • Window system independent . • Operating system independent .

(11) A Few More Things.

(12) Hardware Rendering Pipeline . host. interface. vertex. processing. triangle. setup. pixel. processing . memory. interface.

(13) OpenGL Primitives GL_POINTS. GL_LINES. GL_LINE_STRIP. GL_LINE_LOOP. GL_TRIANGLES. GL_TRIANGLE_STRIP. 13. GL_TRIANGLE_FAN.

(14) Triangles • Triangles must be. – Simple: edges cannot cross. – Convex: All points on line segment between two points in a polygon are also in the polygon. – Flat: all vertices are in the same plane. • User must create triangles (triangulation). • OpenGL contains a tessellator. nonsimple polygon. 14. nonconvex polygon.

(15) Space ?. point2 vertices[3] = {point2(0.0, 0.0),. point2( 0.0, 1.0), point2(1.0, 1.0)};.

(16) Transform Spaces. Object Space. Screen Space.

(17) Coordinate Systems. • The units in points can be object, world, model or problem coordinates. • Viewing specifications are also in object coordinates. • Same for lights. • Eventually pixels will be produced in window coordinates . 17.

(18) Default Camera. • Camera at origin in object  space pointing in -z direction. • Default viewing volume. - box centered at  origin with sides of . length 2. 18.

(19) Orthographic Viewing Points projected forward along z axis onto plane z=0. z=0. z=0. 19.

(20) Viewports. • Use partial window for image: glViewport(x,y,w,h). • w, h – pixel coordinates. • x,y – lower corner. 20.

(21) Writing Shaders. 21.

(22) Simple Vertex Shader. in vec4 vPosition;. void main(void). {. gl_Position = vPosition;. }. 22.

(23) Execution Model. Vertex data. Shader Program. Vertex. Shader. Application. Program. glDrawArrays. 23. GPU. Primitive. Assembly. Vertex.

(24) Simple Fragment Program void main(void) { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); }. 24.

(25) Execution Model. Application. Shader Program. Fragment. Shader. Rasterizer. Fragment. 25. Frame Buffer. Fragment. Color.

(26) Data Types. • C types: int, float, bool. • Vectors: . – float vec2, vec3, 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). 26.

(27) Pointers. • There are no pointers in GLSL. • C structs which can be copied back from functions. • Matrices and vectors can be passed to and fro GLSL functions, e.g. mat3 func(mat3 a). 27.

(28) Selection and Swizzling. • Access 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 to manipulate components. vec4 a;. a.yz = vec2(1.0, 2.0);. 28.

(29) Example: Vertex Shader. const vec4 red = vec4(1.0, 0.0, 0.0, 1.0);. out vec3 color_out;. void main(void). {. gl_Position = vPosition;. color_out = red;. }. 29.

(30) Fragment Shader. in vec3 color_out;. void main(void). {. gl_FragColor = color_out;. }. // in latest version use form. // out vec4 fragcolor;. // fragcolor = color_out;. 30.

(31) Qualifiers. • GLSL has many qualifiers like const as C/C++. • 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. 31.

(32) Passing values. • • • • . Call by value-return. Variables are copied in. Returned values are copied back. Two possibilities. – in. – out. 32.

(33) Attribute Qualifier. • Attribute-qualified variables can change at most once per vertex. • User defined (in application program) . – Use in qualifier to get to shader. – in float temperature. – in vec3 velocity. 33.

(34) Uniform Qualified. • Variables that are constant for an entire primitive. • Can be changed in application and sent to shaders. • Cannot be changed in shader. • Used to pass information to shader such as the bounding box of a primitive. 34.

(35) Example. GLint aParam;. aParam = glGetUniformLocation(myProgObj, . "angle");. /* angle defined in shader */. /* my_angle set in application */. GLfloat my_angle;. my_angle = 5.0 /* or some other value */. glUniform1f(aParam, my_angle);. 35.

(36) Varying Qualified. • Variables passed from vertex to fragment shader. • Automatically interpolated by the rasterizer. • Old style - varying vec4 color. • Use out in vertex shader and in in fragment shader out vec4 color;. 36.

(37) Wave Motion Vertex Shader. in vec4 vPosition;. uniform float xs, zs, // frequencies . uniform float h; // height scale. void main(). {. vec4 t = vPosition;. t.y = vPosition.y . + h*sin(time + xs*vPosition.x). + h*sin(time + zs*vPosition.z);. gl_Position = t;. }. 37.

(38) Particle System. in vec3 vPosition;. uniform mat4 ModelViewProjectionMatrix;. uniform vec3 init_vel;. uniform float g, m, t;. void main(). { vec3 object_pos;. object_pos.x = vPosition.x + vel.x*t;. object_pos.y = vPosition.y + vel.y*t . + g/(2.0*m)*t*t;. object_pos.z = vPosition.z + vel.z*t;. gl_Position = . ModelViewProjectionMatrix*vec4(object_pos,1);. }. 38.

(39) Fragment Shader. /* pass-through fragment shader */. in vec4 color;. void main(void). {. gl_FragColor = color;. }. 39.

(40) Vertex Shader Applications. • Moving vertices. – Morphing . – Wave motion. – Fractals. • Lighting. – More realistic models. – Cartoon shaders. 40.

(41) 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. 41.

(42) Adding Color. • Send color to the shaders as a vertex attribute or as a uniform variable . • Choice depends on frequency of change. • Associate a color with each vertex. • Set up an array of same size as positions. • Send to GPU as a vertex buffer object. 42.

(43) Setting Colors. typedef vec3 color3;. color3 base_colors[4] = {color3(1.0, 0.0. 0.0), ….. color3 colors[NumVertices];. vec3 points[NumVertices];. //in loop setting positions. colors[i] = basecolors[color_index]. position[i] = ……. . 43.

(44) Setting Up Buffer Object. //need larger buffer. glBufferData(GL_ARRAY_BUFFER, sizeof(points) + . sizeof(colors), NULL, GL_STATIC_DRAW);. //load data separately. glBufferSubData(GL_ARRAY_BUFFER, 0, . sizeof(points), points);. glBufferSubData(GL_ARRAY_BUFFER, sizeof(points), . sizeof(colors), colors);. 44.

(45) Second Vertex Array. // vPosition and vColor identifiers in vertex shader. loc = glGetAttribLocation(program, “vPosition”);. glEnableVertexAttribArray(loc);. glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, 0,. BUFFER_OFFSET(0));. loc2 = glGetAttribLocation(program, “vColor”);. glEnableVertexAttribArray(loc2);. glVertexAttribPointer(loc2, 3, GL_FLOAT, GL_FALSE, 0,. BUFFER_OFFSET(sizeofpoints));. 45.

(46) Next Topic – Linear Algebra.

(47) Vectors. • Physical definition: . – Direction. – Magnitude. • Examples . – Light Direction. – View Direction. – Normal . 47.

(48) Abstract Spaces. • Scalars. • (Linear) Vector Space. – Scalars and vectors. • Affine Space. – Scalars, vectors, and points. • Euclidean Space. – Scalars, vectors, points. – Concept of distance. • Projections.

(49) Vectors – Linear Space. 49. • Every vector. – has an inverse. – can be multiplied by a scalar. • There exists a zero vector. – Zero magnitude, undefined orientation. • The sum of any two vectors is a vector - closure. v. -v. αv. w. v. u.

(50) Vector Spaces. n Vectors = n-tuples. The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.. n Vector-vector addition. The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.. n Scalar-vector multiplication. The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.. n Vector space: . The image cannot be displayed. Your computer may not. The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again..

(51) Linear Independence. if the only set of scalars. ! Vectors!are linear independent ! α1u1 + α 2u2 + " + α nun = 0 iff. α1 = α 2 = ! = α n = 0 ! ! ! p = (x, y, z) = xi + yj + zk.

(52) Vector Spaces. • Dimension. • The greatest number of linearly independent vectors. • Basis. {β i }. • n linearly independent vectors (n: dimension). • Representation v! = β1v!1 + β 2v!2 + " + β n v!n • Unique expression in terms of the basis vectors. ⎡ β1ʹ′ ⎤ ⎡ β1 ⎤ • Change of Basis: Matrix M. ⎢ β ʹ′ ⎥ ⎢ β ⎥ 2 ⎥ 2 ⎥ ⎢ ⎢ ! ! ! = M • Other basis v1ʹ′, v2ʹ′ ,… , vnʹ′ ⎢ ! ⎥ ⎢ ! ⎥ ! ! ! ! ⎢ ⎥ ⎢ ⎥ v = β1ʹ′v1ʹ′ + β 2ʹ′v2ʹ′ + " + β nʹ′vnʹ′ ⎣ β nʹ′ ⎦ ⎣ β n ⎦.

(53) Vectors . • These vectors are identical. – Same length and magnitude. • Vectors spaces insufficient for geometry. – Need points. 53.

(54) Points. • Location in space. • Operations allowed between points and vectors. – Point-point subtraction yields a vector. – Equivalent to point-vector addition . (P − Q) + (Q − R) = (P − R). ! v = P −Q ! P = v +Q.

(55) Affine Spaces. ! ! ! Frame: a Point P0 and a Set of Vectors v1 , v2 ,…, vn. Representations of the vector and point: n scalars. Vector. Point. v = α1v1 + α 2 v2 + ! + α n vn P = P0 + β1v1 + β 2 v2 + ! + β n vn.

(56) Affine Spaces. • Point + a vector space. • Operations. – Vector-vector addition. – Scalar-vector multiplication. – Point-vector addition. – Scalar-scalar operations. • For any point define. – 1 • P = P. – 0 • P = 0 (zero vector). 56.

(57) Question. How Far Apart Are Two Points in Affine Spaces ?. Operation: Inner (dot) Product.

(58) Euclidean (Metric) Spaces. – Magnitude (length) of a vector. v = v⋅v – Distance between two points. P − Q = (P − Q )⋅ (P − Q ) – Measure of the angle between two vectors. u ⋅ v = u v cosθ • cosθ = 0 è orthogonal. • cosθ = 1 è parallel.

(59) In Pictures.

(60) Euclidean Spaces. – Combine two vectors to form a real. – α, β, γ, …: scalars, u, v, w, …:vectors. u ⋅v = v ⋅u. (αu + βv )⋅ w = αu ⋅ w + βv ⋅ w. v ⋅ v > 0 if v ≠ 0 0⋅0 = 0. Orthogonal: u ⋅ v = 0.

(61) Projections. • Problem: Find shortest distance from a point to a line on a plane. • Given Two Vectors. w = αv + u – Divide into two parts: one parallel and one orthogonal. w ⋅ v = αv ⋅ v + u ⋅ v = αv ⋅ v w⋅v ∴α = v⋅v w⋅v Projection of one. ∴ u = w − αv = w − v vector onto another. v⋅v.

(62) Making New Vectors.

(63) Cross Product.

(64) Cross Product.

(65) Parametric Forms.

(66) Lines, Rays. • Consider all points of the form. – P(α)=P0 + α d. – Set of all points that pass through P0 in the direction of the vector d. 66.

(67) 2D Forms for lines. • Two-dimensional forms. – Explicit: y = mx +h. – Implicit: ax + by +c =0. – Parametric: . x(a) = ax0 + (1-a)x1. y(a) = ay0 + (1-a)y1. 67.

(68) Rays, Line Segments. If a >= 0, then P(a) is the ray leaving P0 in the direction d. If we use two points to define v, then. P( a) = Q + a (R-Q)=Q+av. =aR + (1-a)Q. For 0<=a<=1 we get all the. points on the line segment. joining R and Q. 68.

(69) Curves.

(70) Planes. Defined by a point and two vectors or by three points. P. v. Q. R. u. P(a,b)=R+au+bv. 70. R. P(a,b)=R+a(Q-R)+b(P-Q).

(71) Triangles. convex sum of S(a) and R. convex sum of P and Q. for 0<=α,β<=1, we get all points in triangle. 71.

(72) Barycentric Coordinates.

(73) Barycentric Coordinates. Triangle is convex. Any point inside can be represented as an affine sum. P(α1, α2, α3)=α A + βB + γC. where . α + β + γ = 1. α, β, γ >=0. 73.

(74) Barycentric Coordinates. Calculating Areas ?.

(75) Matrices.

(76) Matrices. • • • • • • . Definitions. Matrix Operations. Row and Column Matrices. Rank. Change of Representation. Cross Product.

(77) What is a Matrix?. Elements, organized into rows and columns. rows. columns. ⎡a b ⎤ ⎢ c d ⎥ ⎣ ⎦.

(78) Definitions. [ ]. A = aij. n x m Array of Scalars (n Rows and m Columns). – n: row dimension of a matrix, m: column dimension. – m = n: square matrix of dimension n. – Element . {a }, i = 1,…, n,. j = 1, … , m – Transpose: interchanging the rows and columns of a matrix. AT = a ji ⎡ b1 ⎤ ⎢b ⎥ • Column Matrices and Row Matrices. b = [bi ] = ⎢ 2 ⎥ – Column matrix (n x 1 matrix):. ⎢ ! ⎥ T ⎢ ⎥ b – Row matrix (1 x n matrix):. ⎣bn ⎦ ij. [ ].

(79) Basic Operations. Addition, Subtraction, Multiplication. ⎡a b ⎤ ⎡ e ⎢ c d ⎥ + ⎢ g ⎣ ⎦ ⎣. f ⎤ ⎡ a + e b + f ⎤ = ⎢ ⎥ h ⎦ ⎣c + g d + h ⎥⎦. add elements. ⎡a b ⎤ ⎡ e ⎢ c d ⎥ − ⎢ g ⎣ ⎦ ⎣. f ⎤ ⎡ a − e b − f ⎤ = ⎢ ⎥ h ⎦ ⎣c − g d − h ⎥⎦. subtract elements. ⎡a b ⎤ ⎡ e ⎢ c d ⎥ ⎢ g ⎣ ⎦ ⎣. f ⎤ ⎡ae + bg = ⎢ ⎥ h ⎦ ⎣ce + dg. af + bh ⎤ cf + dh ⎥⎦. Multiply each row by each column.

(80) Matrix Operations. + Scalar-Matrix Multiplication. αA = [αaij ]. + Matrix-Matrix Addition. [. C = A + B = aij + bij. ]. + Matrix-Matrix Multiplication. + A: n x l matrix, B: l x m è C: n x m matrix. C = AB = [cij ] l. cij = ∑ aik bkj k =1.

(81) Matrix Operations . + Properties of Scalar-Matrix Multiplication. + Properties of Matrix-Matrix Addition. + Commutative:. + Associative:. A+B =B+A. A + (B + C) = (A + B) + C. + Properties of Matrix-Matrix Multiplication. + Identity Matrix I (Square Matrix). α (β A ) = (αβ )A αβ A = βα A. ⎧1 if i = j I = aij , aij = ⎨ ⎩0 otherwise. [ ]. A(BC ) = (AB )C AB ≠ BA AI = A IB = B.

(82) Identity Matrix. ⎡1 0 0⎤ ⎢ ⎥ I = ⎢0 1 0⎥ ⎢⎣0 0 1⎥⎦.

(83) Multiplication. • Is AB = BA? Maybe, but maybe not!. ⎡a b ⎤ ⎡ e ⎢ c d ⎥ ⎢ g ⎣ ⎦ ⎣. f ⎤ ⎡ae + bg ...⎤ = ⎢ ⎥ h ⎦ ⎣ ... ...⎥⎦. ⎡ e ⎢ g ⎣. f ⎤ ⎡a b ⎤ ⎡ea + fc ...⎤ = ⎢ ⎥ ⎢ ⎥ h ⎦ ⎣ c d ⎦ ⎣ ... ...⎥⎦. • Heads up: multiplication is NOT commutative!.

(84) Row and Column Matrices. - Column Matrix. - pT: row matrix . - Concatenations. - Associative. - By Row Matrix. ⎡ x ⎤ p = ⎢⎢ y ⎥⎥ ⎢⎣ z ⎥⎦ pʹ′ = Ap pʹ′ = ABCp. (AB )T. = BT A T. pʹ′T = pT CT BT AT.

(85) Inverse of a Matrix. - Identity matrix:  AI = A. - Some matrices have an inverse, such that: AA-1 = I. - Inversion is tricky: (ABC)-1 = C-1B-1A-1. - Derived from non-commutativity property.

(86) Determinant of a Matrix. • Used for inversion. • If det(A) = 0, then A has no inverse. • Can be found using factorials, pivots, and cofactors!. • And for Areas of Triangles. ⎡a b ⎤ A = ⎢ ⎥ c d ⎣ ⎦. det( A) = ad − bc 1 ⎡ d − b⎤ A = ad − bc ⎢⎣− c a ⎥⎦ −1.

(87) Area of Triangle – Cramer’s Rule. x1, y1. x2, y2. x3, y3.

(88) Use This Here.

(89) Transformations.

(90)

References

Related documents

This is roughly 0.2% of all 200 billion tweets released yearly (Twitter,.. Two additional variables were needed for the analysis and had to be determined for each tweet. First one

Affine Transformations • Every linear transformation is equivalent to a change in frames • Every affine transformation preserves lines • However, an affine transformation has only

– Use the matrix stack to store M and other matrices in tree traversal... Old Style

ü You need to be willing to get hands-dirty: OpenGL, WebGL, 3D Printing, GLSL, hardware... Will Follow

• OpenGL extensions added functions for vertex and fragment shaders.. • Cg C for graphics C-like language for

• Given a point light source and a polygon, the vertices of the shadow polygon are the projections of the original polygon’s vertices from a point source onto a surface...

• Given a point light source and a polygon, the vertices of the shadow polygon are the projections of the original polygon’s vertices from a point source onto a surface...

• Ideally perturb normal across surface of object and compute a new color at each interior point... Rougher Version n’