Week 3 - Friday. What did we talk about last time? Vertex shaders Geometry shaders Pixel...

44
CS361 Week 3 - Friday

Transcript of Week 3 - Friday. What did we talk about last time? Vertex shaders Geometry shaders Pixel...

CS361Week 3 - Friday

Last time

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

Questions?

Project 1

Effects

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

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

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;

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

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;

};

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;

}

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;

}

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;

}}

Shader output

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

All kinds of different effects

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

Original vs. modern

Original Modern

There's a Stanford Bunny too…

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

Vectors

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

Operations

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

Addition

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

n

nnnn vu

vu

uv

v

v

v

u

u

u

Rvu

11

11

00

1

1

0

1

1

0

Associativity and commutativity

Vector addition is associative:

Vector addition is commutative:

)()( wvuwvu

uvvu

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

Scalar multiplication

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

n

nau

au

au

a Ru

1

1

0

Scalar rules

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

uu

vuvu

uuu

uu

1

)(

)(

)()(

aaa

baba

baab

Dot product

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

Rvu

1

0

n

iiivu

Dot product rules

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

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

Rules for the norm

Geometric Interpretation

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

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

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

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)

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

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

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 vv

vvvu

vv

vuw t

2

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

vuvu

vuvu

vuvu

w

w

w

vuw

Cross product rules

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

Upcoming

Next time…

More linear algebra Matrices Homogeneous notation Geometric techniques

Reminders

Keep reading Appendix AAssignment 1 due tonight by

midnight! Keep working on Project 1, due next

Friday, February 6 by 11:59