Shaders In Processing
uniform mat4 transform; attribute vec4 vertex; attribute vec4 color; varying vec4 vertColor; void main(){ gl_Position = transform * vertex; vertColor = color; }
varying vec4 vertColor; void main(){ gl_FragColor = vertColor; }
Traslation
Vectorially we get:
[xy][dxdy][x′y′]
P′=P+T
Scaling
Vectorially we get:
[xy][sx00sy][x′y′]
P′=S∗P
Rotation
Vectorially we get:
[xy][cosβ−sinβsinβcosβ][x′y′]
P′=R∗P
Rotation
From the triangulation:
x=rcosα
y=rsinα
x′=rcos(α+β)=rcosαcosβ−rsinαsinβ
y′=rsin(α+β)=rcosαsinβ−rsinαcosβ
x′=xcosβ−ysinβ
y′=xsinβ+ycosβ
Rotation
We have
x′=x+h∗y,y′=y,where h is the sheared parameter, to obtain:
Dx=[1h01]Dy=[10h1]P′=DxDy∗PT=[dxdy]
S=[sx00sy]
R=[cosβ−sinβsinβcosβ]
Dx=[1h01]Dy=[10h1]
Trasformations Summary
Traslation
P′=P+T
Scaling
P′=S∗P
Rotation
P′=R∗P
Sheared
P′=DxDy∗P
1. Linear
F(a+b)=F(a)+F(b)F(λa)=λF(a)→F(0)=0 , Thus is a nonlinear transformation
Note: Matrix multiplication is always linear
2. Related
Linear + Traslation →p′=M∗p+b
3. Invertibles
Lets take a look at M−1, the inverse matrix of the general transformation matrix M
P′=M∗P, M−1P′=M−1MPHomogeneous coordinates
(x,y,w)2D Space → Homogeneous Space
(x,y)→(x,y,1)
(xw,yw)→(x,y,w)
3D Space → Homogeneous Space
(x,y,z)→(x,y,z,1)
(xw,yw,zw)→(x,y,z,w)
T=[10dx01dy001]
S=[sx000sy0001]
R=[cosβ−sinβ0sinβcosβ0001]
Dx=[1h0010001]Dy=[100h10001]
P′=[x′y′1]P=[xy1]
Traslation
P′=T(dx,dy)∗P
Scaling
P′=T(sx,sy)∗P
Rotation
P′=R(β)∗P
Sheared
P′=DxDy∗P
Observation about the inverse matrix of the transformation matrix
Lets take a look at M−1, the inverse matrix of the general transformation matrix M
P′=M∗P, M−1P′=M−1MPM−1P′=I P=P
T=[10dx01dy001]T−1=[10−dx01−dy001]
S=[sx000sy0001]S−1=[1sx0001sy0001]
P′=[x′y′1]P=[xy1]
Traslation
P′=T(dx,dy)∗P
Scaling
P′=T(sx,sy)∗P
Observation about the inverse matrix of the transformation matrix
Rotation
P′=R(β)∗P R=[cosβ−sinβ0sinβcosβ0001] R=[cos(−β)−sin(−β)0sin(−β)cos(−β)0001]=[cosβsinβ0−sinβcosβ0001]Successive transformations example
Some special compositions
Rotation from pivot point
Some special compositions
Rotation from pivot point
Some special compositions
Rotation from pivot point
[10xr01yr001] ∙ [cosβ−sinβ0sinβcosβ0001] ∙ [10−xr01−yr001] =[cosβ−sinβxr(1−cosβ)+yrsinβsinβcosβyr(1−cosβ)−xrsinβ001]Some special compositions
Scaling from static point
Some special compositions
Scaling from static point
Some special compositions
Scaling from static point
[10xf01yf001] ∙ [sx000sy0001] ∙ [10−xf01−yf001] =[sx0xf(1−sx)0syyf(1−sy)001]Some special compositions
General directions to scaling
1. The object is rotated to angle β, so the directions s1 y s2 match with the axes.
2. Scale the object
3. The object is rotated on the opposite way.
Some special compositions
General directions to scaling
[s1cos2β+s2sin2β(s2−s1)cosβsinβ0(s2−s1)cosβsinβs1sin2β+s2cos2β0001]Specification
Vectorially we get:
[xyz] [dxdydz] [x′y′z′]
P′= P + T
In homogeneous coordinates we get:
[xyz1] [100dx010dy001dz0001] [x′y′z′1]
P′= T∗ P
Specification
Vectorially we get:
[xyz] [sx000sy000sz] [x′y′z′]
P′= S∗ P
In homogeneous coordinates we get:
[xyz1] [sx0000sy0000sz00001] [x′y′z′1]
P′= S∗ P
Parameters used in rotations
Rotation respect z-axis
Notice z=z′, we get:
x′=xcosβ−ysinβ
y′=xsinβ+ycosβ
z′=z
In homogeneous coordinates we get:
[xyz1] [cosβ−sinβ00sinβcosβ0000100001] [x′y′z′1]
P′=R∗P
Rotation respect x-axis
Doing y→z→x→y
In homogeneous coordinates we get:
[xyz1] [10000cosβ−sinβ00sinβcosβ00001] [x′y′z′1]
P′=R∗P
Rotation respect y-axis
Doing y→z→x→y
In homogeneous coordinates we get:
[xyz1] [cosβ0senβ00100−sinβ0cosβ00001] [x′y′z′1]
P′=R∗P
General Rotations
1st step. Rotation parameter specifications
V=P2−P1
u=V|V|=(a,b,c)
a=(x2−x1)|V|
b=(y2−y1)|V|
a=(z2−z1)|V|
2nd step. Translation from P1 to origin
T=[100−x1010−y1001−z10001]
3rd step. Turn of P2 in z-axis
3rd step
Rx(α)=[10000cd−bd00bdcd00001]We get:
u= (a,b,c). From u projection in y-z plane:
u′= (0,b,c). α is the angle between u′ y uz→
cosα=(u′∙uz)(|u′||uz|)=cd
whered=√b2+c2=|u′|,
Finally from cross product:
1.u′xuz=ux|u′||uz|sinα (Idempotent form) y
2.u′xuz=uxb (cartesian from)
Thus:
sinα=bd
3rd step
Ry(λ)=[d0−a00100a0d00001]We get:
u= (a,b,c). From u projection in y-z plane:
u′= (0,b,c).
From u rotation respect x-axis we get:
u″= (a,0,d )with d=√b2+c2 (u′ magnitude)
λ is the angle between u″ y uz→
cosλ=(u″∙uz)(|u″||uz|)=d,since|u″|=|uz|=1
Finally from cross product:
1.u″xuz=uy|u″||uz|sinλ (Idempotent form) y
2.u″xuz=uy(−a) (cartesian from)
Thus: sinλ=−a
Rz(β)=[cosβ−sinβ00sinβcosβ0000100001]Rx(α)−1∙Ry(λ)−1T−1
Another way to calculate the composite transformation matrix:
Ry(λ)∙Rx(α)
Another way to calculate the composite transformation matrix:
Ry(λ)∙Rx(α)
V=P2−P1
u=V|V|=(a,b,c)
a=(x2−x1)|V|
b=(y2−y1)|V|
c=(z2−z1)|V|
Another way to calculate the composite transformation matrix:
Ry(λ)∙Rx(α)
u=V|V|=(a,b,c)
u′z=u(z′-axis aligns with z)
u′x=any unit vector(in the orthogonal plane to u′z)
u′y=u x u′x
u′x=(u′x1,u′x2,u′x3)
u′y=(u′y1,u′y2,u′y3)
u′x=(u′z1,u′z2,u′z3)
Rotations and Quaternions - Review
Concept: q=s+ia+jb+kc
The coefficients a,b and c, in the imaginary terms i,j and k, are real.
S parameter is a real number known as scalar part.
i2=j2=k2=−1 ij=−ji=k(Definition)
jk=−kj=i ki=−ik=j(Derived)
Sum (q1,q2): q1,q2=(s1+s2)+i(a1+a2)+j(b1+b2)+k(c1+c2)
Scalar Multiplication (d): dq=(ds)+i(da)+j(db)+k(dc)
Rotations and Quaternions - Review
Vectorial Notation: q=(s,v) where v=(a,b,c)
Product(q1,q2): q1q2=(s1s2−v1∙v2,s1v2+s2v1+v1xv2)
|q|2=s2+v∙v
q−1=(1|q|2)(s,−v) note:(s,−v) is called the"conjugate"of q
thus: qq−1=q−1q=(1,0)
The hypersphere of rotations(2D case)
Rotation angle : β(->latitude α)
Observations
1. North and south poles -> identity rotation. No axis is defined
2. Each rotation can be represented as a rotation about some axis, or, equivalently, as a negative rotation about an axis pointing in the opposite direction: antipodal point.
3. The set of rotations is not closed under composition.
The hypersphere of rotations(3D case)
Rotation angle : β(->latitude α)
Observations
1. Hypersphere in four dimensional space.
2. The "latitude" on the hypersphere will be half of the corresponding angle of rotation.
3. The set of rotations is closed under composition.
4. A general quaternion represents a point in 4D, but constraining it to have unit magnitude yields a three dimensional space equivalent to the surface of a hypersphere.
Parameterizing the space of rotations(2D case)
Problem
1. Latitude and longitude are degenerated.
2. It can be shown that no two-parameter coordinate system can avoid such degeneracy.
Idea
Parameterize the space with three Cartesian coordinates (here w,x,y)
North pole -> (w,x,y)=(1,0,0) South pole -> (w,x,y)=(−1,0,0) equator -> w=0,x2+y2=1
Points on the sphere satisfy the constraint: w2+x2+y2=1.
A point (w,x,y) on the sphere represents a rotation
Horizontal axis directed by the vector (x,y,0).
* Angle β=2∗cos−1w
1.w=cosα,sinceα=β2→w=cos(β/2)
2. Since w2=cos2(β/2)→(x2+y2=sin2(β/2))
Parameterizing the space of rotations(3D case)
Problem
1. Latitude and longitude are degenerated.
2. It can be shown that no two-parameter coordinate system can avoid such degeneracy.
Idea
Parameterize the space with four Cartesian coordinates (here w,x,y,z)
Points on the hypersphere satisfy the constraint:w2+x2+y2+z2=1
A point (w,x,y,z) on the sphere represents a rotation
\ast Axis directed by the vector (x,y,z).
\ast Angle β=2∗cos−1w
1.w=cosα,sinceα=β2→w=cos(β/2)
2. Since w2=cos2(β/2)→(x2+y2+z2=sin2(β/2))
Rotations and Quaternions
u is a unit vector
β is the rotation angle
For the rotation around an axis across the origin
1.Defined the unit quaternion:
q=(s,v) where s=cos(β/2)
v=usin(β/2)
The position of any point P(to turn through this quaternion), it can be represented as:P=(0,p)
With p=(x,y,z) (coordinates), we get:P=(0,p)
2.P′=qPq−1, the rotation point is defined.
With q−1=(s,−v) this is the inverse of q
Proof and Properties
Let ube a unit vector (rotation axis) and q=(s, uv=cos(β/2)+ usin(β/2).The goal is to show that:
(1)P′=qPq−1→ yields the vector P′ upon rotation of the original vector P by an angleβ around the axis u.
Proof: expand (1) to reduce it to rodrigues rotation formula. See next slide.
Important properties
1. Quaternion multiplication is composition of rotations, for if p and q are quaternions representing rotations, then rotation (conjugation) by pq is: pqP(pq)−1 =pqPq−1p−1 = p(qPq−1)p−1 which is the same as rotating (conjugating) by q and then by p.
2. The quaternion inverse of a rotation is the opposite rotation, since q−1(qPq−1)q=P
3.qn is a rotation by n times the angle around the same axis as q.
4. This (3) can be extended to arbitrary real n, allowing for smooth interpolation between spatial orientations.
Rodrigues rotation formula
The following formula rotates v around the unit vectorz by an angle β:
vrot=vcosβ+(zxv)sin\beta+z(z\bulletv)(1-cos\beta)
Observation
Let \phi be the angle between z and v.
z\bullet v =|z||v|cos\phi(1),
|zx v |=|z||v|sin\phi(2)
Since|z|=1 \rightarrow
a)|y|=|v|sin\phi(from(2))
b)z\bullet v =|(z\bulletv)z|=|v| cos\phi
Conclusion: |x|=|y|
x_{rot}=xcos\beta +ysin\beta
x_{rot}=(v-(z\bulletv)z) cos\beta +(zxv) sin\beta
v_{rot}=xrot+(z\bulletv)z
Rotations and Quaternions
Consider the traslation:
R(\beta)=T^{-1}\bullet M_R(\beta) \bulletT
We got before:
u is a unit vector
\beta is the rotation angle
q =(s,v) ,with s= cos(\beta/2)
v=usin(\beta/2)
q^{-1}=(s,-v)\space \space p= (x,y,z)
P'=qPq^{-1}=(0,p')
p'= s^2p+v(p\bulletv) + 2s(vxp)+vx(vxp)
Taking v= (a,b,c)we get:
M_R(\beta)=\begin{bmatrix} 1-2b^2-2c^2 & 2ab-2sc & 2ac+2sb & 0 \cr 2ab+2sc & 1-2a^2-2c^2 & 2bc-2sa & 0 \cr 2ac-2sb & 2bc+2sa & 1-sa^2-2b^2 & 0 \cr 0 & 0 & 0 & 1 \end{bmatrix}
R(\beta)=T^{-1}\bullet R_x(\alpha)^{-1} \bullet R_y(\lambda)^{-1} \bullet R_z(\beta) \bullet R_y (\lambda) \bullet R_x(\alpha) \bulletT
Three-dimensional/ respect to z-axis
D_z=\begin{bmatrix} 1 & 0 & a & 0 \cr 0 & 1 & b & 0 \cr 0 & 0 & 1 & 0 \cr 0 & 0 & 0 & 1 \end{bmatrix} \space \space \spaceThus:
x'= x +a\ast z
y'= y +b\ast z
z'= z
World and local coordinates systems
Transformations between coordinates systems
Note:Transformations between systems may involve till three consecutive rotations
Transformations between coordinates systems/ Hierarchy(Graph)
Transformations between coordinates systems/ Orthogonal Matrix method
Coordinate transformation involve 1 traslation and 3 rotations: \space R \bullet T
Alternative way to calculate the composed rotation matrix: R
u_x'=(u_x'1,u_x'2,u_x'3)
u_y'=(u_y'1,u_y'2,u_y'3)
u_x'=(u_z'1,u_z'2,u_z'3)
OpenGL Example
R_1T_1:S_1\rightarrowS_wtranslate and rotateS_w\rightarrowS_1
R_2T_2:S_2\rightarrowS_1translate and rotateS_1\rightarrowS_2
R_3T_3:S_3\rightarrowS_1translate and rotateS_3\rightarrowS_1
R_4T_4:S_4\rightarrowS_2translate and rotateS_2\rightarrowS_4
R_cT_c:S_c\rightarrowS_wtranslate and rotateS_w\rightarrowS_c
OpenGL Example
OpenGL matrix stack (model-view)
pushMatrix: multiply current matrix and adds to the head
popMatrix: remove head