Top Banner
CS361 Week 3 - Friday
44

CS361

Feb 23, 2016

Download

Documents

Avi

Week 3 - Friday. CS361. Last time. What did we talk about last time? Vertex shaders Geometry shaders Pixel shaders. Questions?. Project 1. Using BasicEffect in XNA. Student Lecture: Vectors. Yeah… just what do you know about vectors?. Vectors. Euclidean space. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS361

CS361Week 3 - Friday

Page 2: CS361

Last time

What did we talk about last time? Vertex shaders Geometry shaders Pixel shaders

Page 3: CS361

Questions?

Page 4: CS361

Project 1

Page 5: CS361

Effects

Page 6: CS361

All this programming gets complicated… So, people in the industry have tried to

collect useful programs for rendering things

A collection of shaders to achieve a particular rendering effect can be stored in an effect file (commonly with extension .fx)

The syntax of the effects language allows your application to set specific arguments

Page 7: CS361

Tools to generate effects You can download

existing .fx files or write your own

There are also tools like NVIDIA's FX Composer 2.5 that allow you to create effects with a GUI

Now, let's examine the book's example effect file for Gooch shading

Page 8: CS361

Standard variables Camera parameters are supplied automatically Syntax is type id : semantic

type is a system defined type or a user defined struct id is whatever identifier the user wants semantic is a system defined use

float4x4 WorldXf : World;float4x4 WorldITXf : WorldInverseTranspose;float4x4 WvpXf : WorldViewProjection;

Page 9: CS361

User variables Default values are given for these variables The annotations given inside angle brackets

allow the outside program to set themfloat3 Lamp0Ps : Position <

string Object = "PointLight0";string UIName = "Lamp 0 Position";string Space = "World"; > = {-0.5f, 2.0f, 1.25f};

float3 WarmColor < string UIName = "Gooch Warm Tone";string UIWidget = "Color"; > = {1.0f, 0.9f, 0.15f};

float3 CoolColor < string UIName = "Gooch Cool Tone";string UIWidget = "Color"; > = {0.05f, 0.05f, 0.6f};

Page 10: CS361

User defined structs Input and output types are usually defined by the user The TEXCOORD1 and TEXCOORD2 semantics are used for

historical reasons

struct appdata {float3 Position : POSITION;float3 Normal : NORMAL;

}

struct vertexOutput {float4 HPosition : POSITION;float3 LightVec : TEXCOORD1;float3 WorldNormal : TEXCOORD2;

};

Page 11: CS361

Vertex shader

vertexOutput std_VS(appdata IN){

vertexOutput OUT;float4 No = float4(IN.Normal,0);OUT.WorldNormal = mul(No,WorldITXf).xyz;float4 Po = float4(IN.Position,1);float4 Pw = mul(Po,WorldXf);OUT.LightVec = (Lamp0Pos – Pw.xyz);OUT.HPosition = mul(Po,WvpXf);return OUT;

}

Page 12: CS361

Pixel shader

We linearly interpolate between cool and warm colors based on the dot productfloat4 gooch_PS(vertexOutput IN) : COLOR

{float3 Ln = normalize(IN.LightVec);float3 Nn = normalize(IN.WorldNormal);float ldn = dot(Ln,Nn);float mixer = 0.5 * (ldn + 1.0);float4 result = lerp(CoolColor,

WarmColor, mixer);return result;

}

Page 13: CS361

Putting it all together

Z-buffer configuration is done here

technique Gooch < string Script = "Pass=p0;"; >{

pass p0 < string Script = "Draw=geometry;"; > {

VertexShader = compile vs_2_0 std_VS();PixelShader = compile ps_2_a gooch_PS();ZEnable = true;ZWriteEnable = true;ZFunc = LessEqual;AlphaBlendEnable = false;

}}

Page 14: CS361

Shader output

The result of the shader given before applied to a teapot:

Page 15: CS361

All kinds of different effects

Page 16: CS361

Why a teapot?

The Utah teapot was modeled in 1975 by graphics pioneer Martin Newell at the University of Utah

It's actually taller than it looks They distorted the model so that it

would look right on their non-square pixel displays

Page 17: CS361

Original vs. modern

Original Modern

Page 18: CS361

There's a Stanford Bunny too…

Page 19: CS361

Student Lecture: VectorsYeah… just what do you know about vectors?

Page 20: CS361

Vectors

Page 21: CS361

Euclidean space

We refer to n-dimensional real Euclidean space as Rn

A vector v in this space is an n-tuple, an ordered list of n real numbers

To match the book (and because we are computer scientists), we'll index these from 0 to n – 1

We will generally write our vectors as column vectors rather than row vectors

Page 22: CS361

Operations

We will be interested in a number of operations on vectors, including: Addition Scalar multiplication Dot product Norm

Page 23: CS361

Addition

Addition of two vectors is just element-by-element addition

n

nnnn vu

vuuv

v

vv

u

uu

Rvu

11

11

00

1

1

0

1

1

0

Page 24: CS361

Associativity and commutativity Vector addition is associative:

Vector addition is commutative:

)()( wvuwvu

uvvu

Page 25: CS361

Identity and inverse

There is a unique vector for Rn which is 0 = (0,0,…,0) with a total of n zeroes

o is additive identity:

For vector v, there is a unique inverse –v = (-v0, -v1, … -vn-1)

-v is the additive inverse:

vv0

0vv )(

Page 26: CS361

Scalar multiplication

Multiplication by a scalar is just element-by-element multiplication of that scalar

n

nau

auau

a Ru

1

1

0

Page 27: CS361

Scalar rules

Rules for scalar multiplication can easily be inferred from the normal properties of reals under addition and multiplication:

uuvuvuuuu

uu

1)(

)()()(

aaababa

baab

Page 28: CS361

Dot product

The dot product is a form of multiplication between two vectors that produces a scalar

Rvu

1

0

n

iiivu

Page 29: CS361

Dot product rules

Dot product rules are slightly less obvious than scalar product and is only when ( (

Page 30: CS361

Norm!

A norm is a way of measuring the magnitude of a vector

We are actually only interested in the L2 norm, but there are many (infinite) norms for any given vector space

We'll denote the norm of u as ||u||

Page 31: CS361

Rules for the norm

Page 32: CS361

Geometric Interpretation

Page 33: CS361

Geometry

Mathematical rules are one thing Understanding how they are

interpreted in geometry is something else

Unfortunately, this means getting more math to link up the existing math with geometry

Page 34: CS361

Linear independence

A set of vectors u0, u1, … un-1 is linearly independent if the only scalars that satisfy the following identity are v0 = v1 = … = vn-1 = 0

In other words, you can't make any one vector out of any of the others

0uuu 11110 ... nno vvv

Page 35: CS361

Spanning and basis

A set of vectors u0, u1, … un-1 spans Rn if any vector v Rn can be written:

In addition, if v0, v1, … , vn-1 are uniquely determined for all v Rn, then u0, u1, … un-1 form a basis of Rn

1

0

n

iiivuv

Page 36: CS361

Describing geometry To properly describe Rn,

the vectors we give are actually scalar multiplied by each of the basis vectors ui

By convention, we leave off the basis vectors ui, because it would be cumbersome to show them

Also, they are often boring: (1,0,0), (0,1,0), and (0,0,1)

Page 37: CS361

Interpretations

A vector can either be a point in space or an arrow (direction and distance)

The norm of a vector is its distance from the origin (or the length of the arrow)

In R2 and R3, the dot product is:

where is the smallest angle between u and v

φcosvuvu

Page 38: CS361

Bases

A basis is orthogonal if every vector in the basis is orthogonal to every other (has dot product 0)

An orthogonal basis is orthonormal if every vector in it has length 1

The standard basis is orthonormal and made up of vectors ei which are all 0's except a 1 at location i

Page 39: CS361

Projections

We can find the orthogonal projection w of vector u onto vector v

Essentially, this means the part of u that's in v vvvv

vuvvvuw t

2

Page 40: CS361

Cross product

The cross product of two vectors finds a vector that is orthogonal to both

For 3D vectors u and v in an orthonormal basis, the cross product w is:

xyyx

zxxz

yzzy

z

y

x

vuvuvuvuvuvu

www

vuw

Page 41: CS361

Cross product rules

In addition wu and wv u, v, and w form a right-handed system

Page 42: CS361

Upcoming

Page 43: CS361

Next time…

More linear algebra Matrices Homogeneous notation Geometric techniques

Page 44: CS361

Reminders

Keep reading Appendix AAssignment 1 due tonight by

midnight! Keep working on Project 1, due next

Friday, February 6 by 11:59