CS361
description
Transcript of CS361
![Page 1: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/1.jpg)
CS361Week 3 - Friday
![Page 2: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/2.jpg)
Last time
What did we talk about last time? Vertex shaders Geometry shaders Pixel shaders
![Page 3: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/3.jpg)
Questions?
![Page 4: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/4.jpg)
Project 1
![Page 5: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/5.jpg)
Effects
![Page 6: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/6.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/11.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/14.jpg)
Shader output
The result of the shader given before applied to a teapot:
![Page 15: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/15.jpg)
All kinds of different effects
![Page 16: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/16.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/17.jpg)
Original vs. modern
Original Modern
![Page 18: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/18.jpg)
There's a Stanford Bunny too…
![Page 19: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/19.jpg)
Student Lecture: VectorsYeah… just what do you know about vectors?
![Page 20: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/20.jpg)
Vectors
![Page 21: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/21.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/22.jpg)
Operations
We will be interested in a number of operations on vectors, including: Addition Scalar multiplication Dot product Norm
![Page 23: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/23.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/24.jpg)
Associativity and commutativity Vector addition is associative:
Vector addition is commutative:
)()( wvuwvu
uvvu
![Page 25: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/25.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/26.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/27.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/28.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/29.jpg)
Dot product rules
Dot product rules are slightly less obvious than scalar product and is only when ( (
![Page 30: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/30.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/31.jpg)
Rules for the norm
![Page 32: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/32.jpg)
Geometric Interpretation
![Page 33: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/33.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/34.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/35.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/36.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/37.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/38.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/39.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/40.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/41.jpg)
Cross product rules
In addition wu and wv u, v, and w form a right-handed system
![Page 42: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/42.jpg)
Upcoming
![Page 43: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/43.jpg)
Next time…
More linear algebra Matrices Homogeneous notation Geometric techniques
![Page 44: CS361](https://reader036.fdocuments.in/reader036/viewer/2022062501/568163be550346895dd4d994/html5/thumbnails/44.jpg)
Reminders
Keep reading Appendix AAssignment 1 due tonight by
midnight! Keep working on Project 1, due next
Friday, February 6 by 11:59