Your SlideShare is downloading. ×
  • Like
ゲームグラフィックス特論 第4回
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

ゲームグラフィックス特論 第4回

  • 1,080 views
Published

四元数,線形補間,曲線補間

四元数,線形補間,曲線補間

Published in Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,080
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
82
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. 4 (2)
  • 2. 2
  • 3. •  •  ˆq = qx,qy,qz,qw( ), qv = qx,qy,qz( )⇒ ˆq = qv,qw( ) ˆq = qv,qw( )= iqx + jqy + kqz + qw = qv + qw qv = iqx + jqy + kqz( )= qx,qy,qz( ) i2 = j2 = k2 = ijk = −1 jk = −kj = i, ki = −ik = j, ij = − ji = k qw: qv: i, j, k: 3
  • 4. i j k ij 4
  • 5. •  ˆqˆr = iqx + jqy + kqz + qw( ) irx + jry + krz +rw( ) = i qyrz − qzry +rwqx + qwrx( ) + j qzrx − qxrz +rwqy + qwry( ) +k qxry − qyrx +rwqz + qwrz( ) +qwrw − qxrx − qyry − qzrz = qv ×rv +rwqv + qwrv,qwrw − qv ⋅rv( ) 5
  • 6. /*! ** p <- q * r! */! void qmul(float *p, const float *q, const float *r)! {! p[0] = q[1]*r[2] - q[2]*r[1] + r[3]*q[0] + q[3]*r[0];! p[1] = q[2]*r[0] - q[0]*r[2] + r[3]*q[1] + q[3]*r[1];! p[2] = q[0]*r[1] - q[1]*r[0] + r[3]*q[2] + q[3]*r[2];! p[3] = q[3]*r[3] - q[0]*r[0] - r[1]*q[1] - q[2]*r[2];! }! 6
  • 7. •  •  •  •  ˆq+ ˆr = qv,qw( )+ rv,rw( )= qv + rv,qw +rw( ) ( ) ( )wvwv qq ,,ˆ ** qqq −== ( ) 2222** ˆˆˆˆˆ wzyx qqqqn +++=== qqqqq ( )1,ˆ 0i = 7
  • 8. •  •  ( )2 * 1 ˆ ˆ ˆ q q q n =− ( ) ( ) 1 ˆ ˆˆ ˆˆˆ * * =⇔= q qq qqq n n 8
  • 9. •  •  •  •  •  ( ) ( ) ( ) *** *** ** ˆˆˆˆ ˆˆˆˆ ˆˆ qrrq rqrq qq = +=+ = ( ) ( ) ( ) ( ) ( )rqrq qq ˆˆˆˆ ˆˆ* nnn nn = = ( ) ( ) rqrprqp rpqprqp ˆˆˆˆˆˆˆ ˆˆˆˆˆˆˆ tsts tsts +=+ +=+ ( ) ( )rqprqp ˆˆˆˆˆˆ = 9
  • 10. •  •  •  ( ) ( ) φφφφ cossincos,sinˆ1ˆ +==⇒= qqn uuqq ( )wv q,ˆ qq = n ˆq( )= n sinφ uq,cosφ( ) = sin2 φ uq ⋅uq( )+cos2 φ = sin2 φ +cos2 φ =1 qu 1=qu 10 cos + i sin = ei ˆq = sin uq + cos = e uq
  • 11. •  •  ( ) ( ) q q e uq u φ φ == logˆlog ( ) tte q tt q t q φφφφ φ cossincossinˆ +==+= uuq u 11
  • 12. •  •  •  •  •  q-1 = q* p = px, py, pz, pw( ) ( ) ( )zyxv wv ppp p ,, ,ˆ = = p pp ( )φφ cos,sinˆ quq = : p uq 2φ *1 ˆˆ qq =− qˆ qˆ−qˆ 12 uq φ" φ" ˆp ˆp´ ˆp0 = ˆqˆpˆq 1 ˆp0 = ˆqˆpˆq⇤
  • 13. 13 ˆqx(✓) = ✓ sin ✓ 2 , 0, 0, cos ✓ 2 ◆ = i sin ✓ 2 + cos ✓ 2 ˆqy(✓) = ✓ 0, sin ✓ 2 , 0, cos ✓ 2 ◆ = j sin ✓ 2 + cos ✓ 2 ˆqz(✓) = ✓ 0, 0, sin ✓ 2 , cos ✓ 2 ◆ = k sin ✓ 2 + cos ✓ 2
  • 14. /*! ** q <- (x, y, z) (a)! */! void qmake(float *q, float x, float y, float z, float a)! {! float l = x * x + y * y + z * z;! ! if (l != 0.0f) {! float s = sin(a *= 0.5f) / sqrt(l);! ! q[0] = x * s;! q[1] = y * s;! q[2] = z * s;! q[3] = cos(a);! }! }! 14
  • 15. •  (ux, uy, uz) = (0, 1, 0) π / 2 qˆ •  pˆ = (1, 0, 0, 0,) qˆ rˆ = qˆpˆqˆ* 15 ˆq = iqx + jqy + kqz + qw = sin ✓ (iux + iuy + iuz) + cos ✓ cos ⇡ 4 = sin ⇡ 4 = 1p 2
  • 16. •  q r •  ( ) ( ) ( ) ( ) ( )*** ** ˆˆˆˆˆˆˆˆˆˆ ˆˆˆˆˆ qrpqrrqpqr rqpqr == ˆrˆq → 16
  • 17. 17 Mq = 0 B B @ 1 s(q2 y + q2 z) s(qxqy qwqz) s(qzqx + qwqy) 0 s(qxqy + qwqz) 1 s(q2 z + q2 x) s(qyqz qwqx) 0 s(qzqx qwqy) s(qyqz + qwqx) 1 s(q2 x + q2 y) 0 0 0 0 1 1 C C A Mq = 0 B B @ 1 2(q2 y + q2 z) 2(qxqy qwqz) 2(qzqx + qwqy) 0 2(qxqy + qwqz) 1 2(q2 z + q2 x) 2(qyqz qwqx) 0 2(qzqx qwqy) 2(qyqz + qwqx) 1 2(q2 x + q2 y) 0 0 0 0 1 1 C C A ˆq = (qx, qy, qz, qw) s = 2/n(ˆq)
  • 18. /*! ** m <- q! */! void qrot(! float *m, const float *q)! {! float xx = q[0] * q[0] * 2.0f;! float yy = q[1] * q[1] * 2.0f;! float zz = q[2] * q[2] * 2.0f;! float xy = q[0] * q[1] * 2.0f;! float yz = q[1] * q[2] * 2.0f;! float zx = q[2] * q[0] * 2.0f;! float xw = q[0] * q[3] * 2.0f;! float yw = q[1] * q[3] * 2.0f;! float zw = q[2] * q[3] * 2.0f;! ! m[ 0] = 1.0f - yy - zz;! m[ 1] = xy + zw;! m[ 2] = zx - yw;! m[ 4] = xy - zw;! m[ 5] = 1.0f - zz - xx;! m[ 6] = yz + xw;! m[ 8] = zx + yw;! m[ 9] = yz - xw;! m[10] = 1.0f - xx - yy;! m[ 3] = m[ 7] = m[11] =! m[12] = m[13] = m[14] = 0.0f;! m[15] = 1.0f;! }! 18
  • 19. ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ! ! ! ! ! " # $ $ $ $ $ % & +−+− −+−+ +−+− = ! ! ! ! ! " # $ $ $ $ $ % & = 1000 02122 02212 02221 22 22 22 33323130 23222120 13121110 03020100 yxxwzyywxz xwzyxzzwyx ywxzzwyxzy qqqq qqqq qqqq qqqq q qqqqqqqqqq qqqqqqqqqq qqqqqqqqqq mmmm mmmm mmmm mmmm M tr Mq ( )= mii q i=0 3 ∑ = m00 q + m11 q + m22 q + m33 q = 4− 4 qx 2 + qy 2 + qz 2 ( )= 4qw 2 19
  • 20. qw = 1 2 tr Mq ( ) 4qx 2 = +m00 q − m11 q − m22 q + m33 q 4qy 2 = −m00 q + m11 q − m22 q + m33 q 4qz 2 = −m00 q − m11 q + m22 q + m33 q 20 qx = m21 q − m12 q 4qw qy = m02 q − m20 q 4qw qz = m10 q − m01 q 4qw
  • 21. (2) t s u p s×t = sin2φ = 2sinφ cosφ2φ" ( )φφ cos,sinˆ uq = ˆq = sinφ sin2φ s×t,cosφ " # $ % & ' = 1 2cosφ s×t,cosφ " # $ % & ' = 1 2 1+e( ) s×t, 2 1+e( ) 2 " # $ $ % & ' ' u = s×t s×t s⋅t = cos2φ = e cosφ = 1+e 2 21
  • 22. R s,t( )= e+ hux 2 huxuy −uz huzux +uy 0 huxuy +uz e+ huy 2 huyuz −ux 0 huzux −uy huyuz +ux e+ huz 2 0 0 0 0 1 " # $ $ $ $ $$ % & ' ' ' ' '' u = ux uy uz( )= s×t s×t e = cos2φ = s⋅t h = 1−cos2φ sin2 2φ = 1−e u⋅u ˆq = qv,qw( )= 1 2 1+e( ) s×t, 2 1+e( ) 2 " # $ $ % & ' ' 22
  • 23. 23
  • 24. •  •  •  t t( )= P t( ) ( ) E h, p,r( )= Ry h( )Rx p( )Rz r( ) ⇓ R t( )= Ry h t( )( )Rx p t( )( )Rz r t( )( ) M = R t 0T 1 ! " # $ % & = I t 0T 1 ! " # $ % & R 0 0T 1 ! " # $ % & t: R: I: 24
  • 25. •  •  glfwSetTime(), glfwGetTime() const double cycle(5.0);! …! // ! glfwSetTime(0.0);! …! // while (window.shouldClose() == GL_FALSE)! {! …! // ! float t((float)(fmod(glfwGetTime(), cycle) / cycle));! …! 25
  • 26. P t( )= P0 1−t( )+ P1t P0 P1 P(t) t 26
  • 27. /*! ** ! */! void translate(float *m, float x, float y, float z)! {! m[ 3] = x; m[ 7] = y; m[11] = z;! m[ 0] = m[ 5] = m[10] = m[15] = 1.0f;! m[ 1] = m[ 2] = m[ 4] =! m[ 6] = m[ 8] = m[ 9] =! m[12] = m[13] = m[14] = 0.0f;! }! 27
  • 28. 2 /*! ** ! */! void linearMotion(! float *m, const float *p0, const float *p1, float t)! {! float x = (p1[0] - p0[0]) * t + p0[0];! float y = (p1[1] - p0[1]) * t + p0[1];! float z = (p1[2] - p0[2]) * t + p0[2];! translate(m, x, y, z);! }! 28
  • 29. Cubic Hermite Spline P t( )= P0 2t3 −3t2 +1( )+ m0 t3 − 2t2 +t( ) +P1 −2t3 +3t2 ( )+ m1 t3 −t2 ( ) P0 P1P(t) t m0 m1 29
  • 30. Catmull-Rom Spline • Cubic Hermite Spline m0 = P1 − P−1 2 m1 = P2 − P0 2 30 P0 P1 P(t) t m0 m1 P-1 P2
  • 31. Catmull-Rom Spline /*! ** Catmull-Rom Spline! */ static float catmull_rom(! float x0, float x1, float x2, float x3, float t)! {! float m0 = (x2 - x0) * 0.5f, m1 = (x3 - x1) * 0.5f;! ! return (((2.0f * x1 - 2.0f * x2 + m0 + m1) * t! - 3.0f * x1 + 3.0f * x2 - 2.0f * m0 - m1) * t! + m0) * t + x1;! }! 31
  • 32. Catmull-Rom Spline /*! ** Catmull-Rom Spline ! */ void interpolate(! float *p,! const float *p0, const float *p1,! const float *p2, const float *p3,! float t)! {! p[0] = catmull_rom(p0[0], p1[0], p2[0], p3[0], t);! p[1] = catmull_rom(p0[1], p1[1], p2[1], p3[1], t);! p[2] = catmull_rom(p0[2], p1[2], p2[2], p3[2], t);! }! 32
  • 33. Kochanek-Bartels Spline •  Cubic Hermite Spline si = 1−t( ) 1+ b( ) 1−c( ) 2 Pi − Pi−1( )+ 1−t( ) 1− b( ) 1+c( ) 2 Pi+1 − Pi( ) di = 1−t( ) 1+ b( ) 1+c( ) 2 Pi − Pi−1( )+ 1−t( ) 1− b( ) 1−c( ) 2 Pi+1 − Pi( ) Pi si di+1Pi+1 Pi-1 m0 m1 si+1di Pi+2 t: Tension b: Bias c: Continuity 33
  • 34. ˆqt = 1−t( ) ˆq0 +tˆqn, t ∈ 0,1[ ] ˆqt=0 = ˆq0 ˆqt+Δt = 1− t + Δt( )( ) ˆq0 + t + Δt( ) ˆqn = 1−t( ) ˆq0 +tˆqn + Δt ˆqn − ˆq0( ) = ˆqt + Δˆq qn q0qtqt+ t Δˆq = Δt ˆqn − ˆq0( ) 34
  • 35. (Slerp) ( ) ( ) [ ]1,0,ˆˆˆ,ˆ,ˆˆ 1 ∈= − tt t qqrrqs ( ) ( ) rrqs qrqs ˆ,ˆ,ˆˆ1 ˆ,ˆ,ˆˆ0 →⇒→ →⇒→ tt tt ( ) ( ) ( )( ) ( )rqrqrqs ˆ sin sin ˆ sin 1sin ,ˆ,ˆ,ˆ,ˆˆ φ φ φ φ tt tt + − ==slerp ( ) ( )2 1 ˆˆ1sin ˆˆcos ˆˆcos rq rq rq ⋅−= ⋅= +++=⋅= − φ φ φ wwzzyyxx rqrqrqrq qˆ rˆ ( )t,ˆ,ˆˆ rqs 35
  • 36. #include <cmath>! ! /*! ** p ← q r t */! void slerp(float *p,! const float *q,! const float *r,! const float t)! {! float qr #= q[0] * r[0]! #+ q[1] * r[1]! #+ q[2] * r[2]! #+ q[3] * r[3];! float ss #= 1.0f - qr * qr;! ! if (ss == 0.0) {! p[0] = q[0];! p[1] = q[1];! p[2] = q[2];! p[3] = q[3];! }! else {! float sp = sqrt(ss);! float ph = acos(qr);! float pt = ph * t;! float t1 = sin(pt) / sp;! float t0 = sin(ph - pt) / sp;! ! p[0] = q[0] * t0 + r[0] * t1;! p[1] = q[1] * t0 + r[1] * t1;! p[2] = q[2] * t0 + r[2] * t1;! p[3] = q[3] * t0 + r[3] * t1;! }! }! ! 36
  • 37. 2 ( )itii −= + ,ˆ,ˆˆ 1qqq Slerp[ ]1 ˆ,ˆ +ii qq 37
  • 38. ( ) ( ) ( ) ( ) ( ) ( )( )tttt t iiii iiii iiii ii − = " # $ % & ' + −= ++ ++ + − − − 12,,ˆ,ˆ,,ˆ,ˆ ,ˆ,ˆ,ˆ,ˆ 4 ˆˆlogˆˆlog expˆˆ 11 11 1 1 1 1 aaqq aaqq qqqq qa slerpslerpslerp squad iqˆ 1 ˆ −iq 1 ˆ +iq 38
  • 39. •  •  https://github.com/tokoik/ggsample04 •  (1, 0, 0) 1 (0, 0, 1) 2 •  •  •  •  main.cpp •  tokoi@sys.wakayama-u.ac.jp 39
  • 40. 40