回転には,X軸を中心に回転,Y軸を中心に回転,Z軸を中心に回転,などがあり,それぞれ次の変換行列で表わされます。
X軸を中心に回転
Y軸を中心に回転
Z軸を中心に回転
Nのような文字を,左下奥を原点に合わせて描き,右上前の点をPとします。この点Pの座標(1.0, 2.0, 2.0)は文字Nの大きさになっています。そして,X軸,Y軸,Z軸は,それぞれ,赤,緑,青で表現されています。
| characterN pointP xyzAxes compoundObject | characterN := JunOpenGL3dObject characterN. characterN paint: ColorValue magenta. characterN := characterN translatedBy: characterN boundingBox corner. pointP := JunOpenGL3dObject cube. pointP := pointP scaledBy: 0.1. pointP := pointP translatedBy: characterN boundingBox corner. xyzAxes := JunOpenGL3dObject axes. xyzAxes := xyzAxes scaledBy: 3.5. compoundObject := JunOpenGL3dCompoundObject new. compoundObject add: characterN. compoundObject add: xyzAxes. compoundObject add: pointP. compoundObject show. Transcript cr; show: pointP boundingBox center printString
この文字NをX軸を中心に30度回転してみましょう。点Pの座標は(1.0, 0.732, 2.732)になります。rotateX:のメッセージを使って,Jun3dTransformationのインスタンスを作成し,文字Nに作用させます。すなわち,文字Nにtransform:のメッセージを送ります。同様に点Pも回転しておきます。
| characterN pointP aTransformation xyzAxes compoundObject | characterN := JunOpenGL3dObject characterN. characterN paint: ColorValue magenta. characterN := characterN translatedBy: characterN boundingBox corner. pointP := JunOpenGL3dObject cube. pointP := pointP scaledBy: 0.1. pointP := pointP translatedBy: characterN boundingBox corner. aTransformation := Jun3dTransformation rotateX: (JunAngle fromDeg: 30). characterN := characterN transform: aTransformation. pointP := pointP transform: aTransformation. xyzAxes := JunOpenGL3dObject axes. xyzAxes := xyzAxes scaledBy: 3.5. compoundObject := JunOpenGL3dCompoundObject new. compoundObject add: characterN. compoundObject add: xyzAxes. compoundObject add: pointP. compoundObject show. Transcript cr; show: pointP boundingBox center printString
元の文字NをY軸を中心に30度回転してみましょう。rotateY:のメッセージを使います。点Pの座標は(1.866, 2.0, 1.232)になります。
| characterN pointP aTransformation xyzAxes compoundObject | characterN := JunOpenGL3dObject characterN. characterN paint: ColorValue magenta. characterN := characterN translatedBy: characterN boundingBox corner. pointP := JunOpenGL3dObject cube. pointP := pointP scaledBy: 0.1. pointP := pointP translatedBy: characterN boundingBox corner. aTransformation := Jun3dTransformation rotateY: (JunAngle fromDeg: 30). characterN := characterN transform: aTransformation. pointP := pointP transform: aTransformation. xyzAxes := JunOpenGL3dObject axes. xyzAxes := xyzAxes scaledBy: 3.5. compoundObject := JunOpenGL3dCompoundObject new. compoundObject add: characterN. compoundObject add: xyzAxes. compoundObject add: pointP. compoundObject show. Transcript cr; show: pointP boundingBox center printString
元の文字NをZ軸を中心に30度回転してみましょう。rotateZ:のメッセージを使います。点Pの座標は(-0.134, 2.232, 2.0)になります。
| characterN pointP aTransformation xyzAxes compoundObject | characterN := JunOpenGL3dObject characterN. characterN paint: ColorValue magenta. characterN := characterN translatedBy: characterN boundingBox corner. pointP := JunOpenGL3dObject cube. pointP := pointP scaledBy: 0.1. pointP := pointP translatedBy: characterN boundingBox corner. aTransformation := Jun3dTransformation rotateZ: (JunAngle fromDeg: 30). characterN := characterN transform: aTransformation. pointP := pointP transform: aTransformation. xyzAxes := JunOpenGL3dObject axes. xyzAxes := xyzAxes scaledBy: 3.5. compoundObject := JunOpenGL3dCompoundObject new. compoundObject add: characterN. compoundObject add: xyzAxes. compoundObject add: pointP. compoundObject show. Transcript cr; show: pointP boundingBox center printString
複数軸を中心に回転させる場合には,座標変換の順序すなわち変換行列の結合順序が重要になります。X軸,Y軸,Z軸と回転させるのと,Z軸,Y軸,X軸と回転させるのでは,結果が丸っ切り異なります。
| characterN pointP aTransformation xyzAxes compoundObject | characterN := JunOpenGL3dObject characterN. characterN paint: ColorValue magenta. characterN := characterN translatedBy: characterN boundingBox corner. pointP := JunOpenGL3dObject cube. pointP := pointP scaledBy: 0.1. pointP := pointP translatedBy: characterN boundingBox corner. aTransformation := ((Jun3dTransformation rotateX: (JunAngle fromDeg: 30)) product: (Jun3dTransformation rotateY: (JunAngle fromDeg: 30))) product: (Jun3dTransformation rotateZ: (JunAngle fromDeg: 30)). characterN := characterN transform: aTransformation. pointP := pointP transform: aTransformation. xyzAxes := JunOpenGL3dObject axes. xyzAxes := xyzAxes scaledBy: 3.5. compoundObject := JunOpenGL3dCompoundObject new. compoundObject add: characterN. compoundObject add: xyzAxes. compoundObject add: pointP. compoundObject show. Transcript cr; show: pointP boundingBox center printString
| characterN pointP aTransformation xyzAxes compoundObject | characterN := JunOpenGL3dObject characterN. characterN paint: ColorValue magenta. characterN := characterN translatedBy: characterN boundingBox corner. pointP := JunOpenGL3dObject cube. pointP := pointP scaledBy: 0.1. pointP := pointP translatedBy: characterN boundingBox corner. aTransformation := ((Jun3dTransformation rotateZ: (JunAngle fromDeg: 30)) product: (Jun3dTransformation rotateY: (JunAngle fromDeg: 30))) product: (Jun3dTransformation rotateX: (JunAngle fromDeg: 30)). characterN := characterN transform: aTransformation. pointP := pointP transform: aTransformation. xyzAxes := JunOpenGL3dObject axes. xyzAxes := xyzAxes scaledBy: 3.5. compoundObject := JunOpenGL3dCompoundObject new. compoundObject add: characterN. compoundObject add: xyzAxes. compoundObject add: pointP. compoundObject show. Transcript cr; show: pointP boundingBox center printString
ある直線すなわち任意の軸に回転するには,rotate:around:のメッセージを使うと便利です。文字Nを直線((0,0,0),(1,2,2))を中心にして30度回転させてみましょう。文字Nを対角線を中心に回転させていることになりますから,点Pの座標(1.0, 2.0, 2.0)に変化はありません。
| characterN pointP aLine aTransformation xyzAxes aPolyline compoundObject | characterN := JunOpenGL3dObject characterN. characterN paint: ColorValue magenta. characterN := characterN translatedBy: characterN boundingBox corner. pointP := JunOpenGL3dObject cube. pointP := pointP scaledBy: 0.1. pointP := pointP translatedBy: characterN boundingBox corner. aLine := Jun3dLine from: 0 , 0 , 0 to: characterN boundingBox corner. aTransformation := Jun3dTransformation rotate: (JunAngle fromDeg: 30) around: aLine. characterN := characterN transform: aTransformation. pointP := pointP transform: aTransformation. xyzAxes := JunOpenGL3dObject axes. xyzAxes := xyzAxes scaledBy: 3.5. aPolyline := JunOpenGL3dPolyline vertexes: (Array with: (aLine atT: -2) with: (aLine atT: 2)). aPolyline paint: ColorValue cyan. compoundObject := JunOpenGL3dCompoundObject new. compoundObject add: characterN. compoundObject add: xyzAxes. compoundObject add: pointP. compoundObject add: aPolyline. compoundObject show. Transcript cr; show: pointP boundingBox center printString