• 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!
91
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

attribute vec4 vPosition;

void main() {

gl_Position = vPosition;

}

// Load shaders and use the resulting shader program

GLuint program = InitShader( "vshader21.glsl",

"fshader21.glsl" );

glUseProgram( program );

..

(6)

Sierpinski Fragment Shader

void main() {

gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );

}

// Load shaders and use the resulting shader program

GLuint program = InitShader( "vshader21.glsl",

"fshader21.glsl" );

glUseProgram( program );

..

(7)

7

Fragment vs Vertex Shader

per vertex lighting 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

(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_TRIANGLE_STRIP

GL_TRIANGLE_STRIP GL_TRIANGLE_FANGL_TRIANGLE_FAN GL_POINTS

GL_POINTS

GL_LINES GL_LINES

GL_LINE_LOOP GL_LINE_LOOP GL_LINE_STRIP

GL_LINE_STRIP

GL_TRIANGLES GL_TRIANGLES

(14)

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 polygonnonconvex 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

(18)

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

(19)

Orthographic Viewing

z=0

z=0

Points projected forward along z axis onto

plane z=0

(20)

20

Viewports

• Use partial window for image:

glViewport(x,y,w,h)

• w, h – pixel coordinates

• x,y – lower corner

(21)

Writing Shaders

(22)

22

Simple Vertex Shader

in vec4 vPosition;

void main(void) {

gl_Position = vPosition;

}

(23)

Execution Model

Vertex Shader

GPU

Primitive Assembly Application

Program

glDrawArrays Vertex Vertex data

Shader Program

(24)

24

Simple Fragment Program

void main(void) {

gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);

}

(25)

Execution Model

Fragment Shader Application

Frame Buffer Rasterizer

Fragment Fragment Color

Shader Program

(26)

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)

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

(28)

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);

(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;

}

(30)

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;

(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

(32)

32

Passing values

• Call by value-return

• Variables are copied in

• Returned values are copied back

• Two possibilities

– in

– out

(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

(34)

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

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

(36)

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;

(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;

}

(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;

}

(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

(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] = …….

(44)

Setting Up Buffer Object

44

//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);

(45)

Second Vertex Array

45

// 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,

(46)

Next Topic – Linear

Algebra

(47)

Vectors

• Physical definition:

– Direction – Magnitude

• Examples

– Light Direction

– View Direction

– Normal

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

49

Vectors – Linear Space

• 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 -vv v w

(50)

Vector Spaces

Vectors = n-tuples

Vector-vector addition

Scalar-vector multiplication

Vector space:

(51)

Linear Independence

Vectors are linear independent if the only set of scalars

iff

2

0

2 1

1

uu  

n

u

n

 

  

2

0

1

    

n

(52)

Vector Spaces

Dimension Dimension

• The greatest number of linearly independent vectors

Basis Basis

• n linearly independent vectors (n: dimension)

• Representation

• Unique expression in terms of the basis vectors

• Change of Basis: Matrix M

• Other basis

n n

v v

v

v

 

  

1 1

 

2 2

  

 

i

v

n

v

v

 

1

 , 

2

, , 

n n

v v

v

v   

1

 

1

  

2

  

2

      

 

 

 

 

 

 

 

n

n

2 1 2

1

M

(53)

Vectors

• These vectors are identical – Same length and magnitude

• Vectors spaces insufficient for geometry

– Need points

(54)

Points

• Location in space

• Operations allowed between points and vectors

– Point-point subtraction yields a vector

– Equivalent to point-vector addition

Q P

v    Q v

P   

P Q  Q R  P R

(55)

Affine Spaces

Frame

Frame: a Point and a Set of Vectors

Representations of the vector and point: n scalars

n n n

n

v v

v P

P

v v

v v

2 2 1

1 0

2 2 1

Vect 1

Poinor t

P

0

v v v

n

 

1

,

2

, ,

(56)

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)

(57)

Question

How Far Apart Are Two Points in Affine Spaces ?

Operation: Inner (dot) Product Inner (dot) Product

(58)

Euclidean (Metric) Spaces

– Magnitude (length) of a vector – Distance between two points

– Measure of the angle between two vectors

•cosθ = 0  orthogonal

•cosθ = 1  parallel

P Q   P Q

Q

P     

 cos v

u v

u  

v

v

v  

(59)

In Pictures

(60)

Euclidean Spaces

– Combine two vectors to form a real – α, β, γ, …: scalars, u, v, w,

…:vectors

 

0 if 0

0 0

0 v

v v

w v

w u

w v

u

u v v

u

 0

 v

Orthogonal: u

(61)

Projections

• Problem: Find shortest distance from a point to a line on a plane

• Given Two Vectors

– Divide into two parts: one parallel and one orthogonal

Projection of one vector onto another

u v

w   

v v v

u v

v v

w         

v v

v w

 

 

v v v

v w w

v w

u

 

 

(62)

Making New Vectors

(63)

Cross Product

(64)

Cross Product

(65)

Parametric Forms

(66)

66

Lines, Rays

• Consider all points of the form

– P()=P

0

+  d

– Set of all points that pass

through P

0

in the direction of

the vector d

(67)

2D Forms for lines

• Two-dimensional forms

– Explicit: y = mx +h

– Implicit: ax + by +c =0 – Parametric:

x(a) = ax

0

+ (1-a)x

1

y(a) = ay

0

+ (1-a)y

1

(68)

68

Rays, Line Segments

If a >= 0, then P(a) is the ray leaving P

0

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

(69)

Curves

(70)

70

Planes

Defined by a point and two vectors or by three points

P(a,b)=R+au+bv P(a,b)=R+a(Q-R)+b(P-Q) u

v

R

P

R

Q

(71)

Triangles

convex sum of P and Q

convex sum of S(a) and R

(72)

Barycentric

Coordinates

(73)

Barycentric Coordinates

Triangle is convex

Any point inside can be represented as an affine sum

P(





)=

A +B +C where





 = 1

>=0

(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

 

 

d c

b a

rows

columns

(78)

Definitions

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

– Transpose: interchanging the rows and columns of a matrix

•Column Matrices and Row Matrices – Column matrix (n x 1 matrix):

– Row matrix (1 x n matrix):

  a

ij

, i 1 , , n , j 1 , , m

  a

ij

A

 

ji

T

a

A

 

 

 

 

 

n i

b b b

b

2 1

b

b

T

(79)

Basic Operations

Addition, Subtraction, Multiplication

 

 

 

 

 

 

 

 

h d

g c

f b

e a

h g

f e

d c

b a

 

 

 

 

 

 

 

 

h d

g c

f b

e a

h g

f e

d c

b a

 

 

 

 

 

 

 

dh cf

dg ce

bh af

bg ae

h g

f e

d c

b a

add elements

subtract elements Multiply

each row by

each column

(80)

Matrix Operations

+ Scalar-Matrix Multiplication Scalar-Matrix Multiplication + Matrix-Matrix Addition Matrix-Matrix Addition

+ Matrix-Matrix Multiplication Matrix-Matrix Multiplication

+ A: n x l matrix, B: l x m  C:

n x m matrix

   a

ij

A

a

ij

b

ij

A B C

 

l k

kj ik ij

ij

b a c

c

1

AB

C

(81)

Matrix Operations

+ Properties of Scalar-Matrix Multiplication + Properties of Matrix-Matrix Addition

+ Commutative:

+ Associative:

+ Properties of Matrix-Matrix Multiplication

+ Identity Matrix I (Square Matrix)Identity Matrix

   

A A

A A







A B

B

A   

B C   A BC

A     

   

BA AB

C AB BC

A

     

 0 otherwise if

1

, i j

a a

ij ij

I IB B

A AI

(82)

Identity Matrix

 

 

1 0

0

0 1

0

0 0

1

I

(83)

Multiplication

• Is AB = BA? Maybe, but maybe not!

• Heads up: multiplication is NOT commutative!



 

  



 



 

...

...

...

bg ae

h g

f e d c

b

a

 

  



 



 

...

...

...

fc ea

d c

b a h g

f e

(84)

Row and Column Matrices

- Column Matrix

- p

T

: row matrix

- Concatenations Concatenations

- Associative

- By Row Matrix

 

 

z y x p

ABCp p

Ap p

 

 

 

T T

T T

T

T T T

A B

C p

p

A B

AB

 

(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

-1

B

-1

A

-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

 

 

 

d c

b A a

bc ad

A )   det(

 

 

 

a c

b d

bc A 1 ad

1

(87)

Area of Triangle – Cramer’s Rule

x1, y1

x2, y2

(88)

Use This Here

(89)

Transformations

References

Related documents

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

– Translate relative to lower arm and rotate about joint connecting to lower arm... Upper arm attached to

• 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’

 Communication bridge between CPU & GPU  Input: Commands from CPU; geometry information from memory  Output: Stream of vertices in object space with associated information -

• Most state variables, attributes and related pre 3.1 OpenGL functions have

Varying Length & Density • Vary overall length of hair by expanding shells • Vary density with noise texture as described • Both settings can be controlled per-pixel using