OpenGL 行列演算目次はじめに
OpenGL の行列演算についてのメモを記載しています。 tips
行列初期化void InitMatrix(float &m) { for(int i=0; i<16; i++) { m[i] = 0.0f; } m[0+4*0] = 1.0f; m[1+4*1] = 1.0f; m[2+4*2] = 1.0f; m[3+4*3] = 1.0f; return; } 行列コピーvoid CopyMatrix(float &dst, const float &src) { for(int i=0; i<16; i++) { dst[i] = src[i]; } return; } 行列乗算void MultMatrix(float &dst, const float &src1, const float &src2) { for(int i=0; i<16; i++) { double sum = 0.0f; for(int j=0; j<4; j++) { sum += src1[i%4*4 + j] * src2[j*4 + i>>2]; } dst[i] = sum; } return; } アフィン変換
アフィン変換。 void glMatrixMode(GLenum mode);を使用し、マトリクスモード(OpenGL が持っている行列のスタック)を指定する。
現在の行列を調べるには GLfloat model_view[4][4];現在の行列を、任意の行列に置き換えるには void glLoadMatrixf(&model_view[0][0]);double 精度なら void glLoadMatrixd(&model_view_d[0][0]);注) 以下は右から行列をかけているのでOpenGL ではなくDirect X OpenGL は左から。 拡大縮小 [ Sx 0 0 0 ] [X' Y' Z' W'] = [X Y Z 1] [ 0 Sy 0 0 ] [ 0 0 Sz 0 ] [ 0 0 0 1 ] X' = X * Sx; Y' = Y * Sy; Z' = Z * Sz;X 回転 [ 1 0 0 0 ] [X' Y' Z' W'] = [X Y Z 1] [ 0 cosθx sinθx 0 ] [ 0 -sinθx cosθx 0 ] [ 0 0 0 1 ] X' = X; Y' = Y * cosθx - Z * sinθx; Z' = Y * sinθx + Z * cosθx;Y 回転 [ cosθy 0 -sinθy 0 ] [X' Y' Z' W'] = [X Y Z 1] [ 0 1 0 0 ] [ sinθy 0 cosθy 0 ] [ 0 0 0 1 ] X' = X * cosθy + Z * sinθy; Y' = Y; Z' = -X * sinθy + Z * cosθy;Z 回転 [ cosθz sinθz 0 0 ] [X' Y' Z' W'] = [X Y Z 1] [ -sinθz cosθz 0 0 ] [ 0 0 1 0 ] [ 0 0 0 1 ] X' = X * cosθz - Y * sinθz; Y' = X * sinθz + Y * cosθz; Z' = Z;注) 上記の回転マトリクスは通常ひとつずつは呼ばれない。 XYZ 軸それぞれのマトリクスをあらかじめ乗算したマトリクスを 使用する。 インプリ例は glRotatef() など。 以下のマトリクスはすべてfloat [16] の大きさで演算する。 平行移動void Translate(float &m, const float x_trans, const float y_trans, const float z_trans) { float m1[16]; float m2[16]; m1[0*4 + 0] = 1.0f; m1[0*4 + 1] = 0.0f; m1[0*4 + 2] = 0.0f; m1[0*4 + 3] = 0.0f; m1[1*4 + 0] = 0.0f; m1[1*4 + 1] = 1.0f; m1[1*4 + 2] = 0.0f; m1[1*4 + 3] = 0.0f; m1[2*4 + 0] = 0.0f; m1[2*4 + 1] = 0.0f; m1[2*4 + 2] = 1.0f; m1[2*4 + 3] = 0.0f; m1[3*4 + 0] = x_trans; m1[3*4 + 1] = y_trans; m1[3*4 + 2] = z_trans; m1[3*4 + 3] = 1.0f; MultMatrix(m2, m1, m); CopyMatrix(m, m2); return; } スケーリングvoid Scale(float &m, const float x_scale, const float y_scale, const float z_scale) { float m1[16]; float m2[16]; m1[0*4 + 0] = x_scale; m1[0*4 + 1] = 0.0f; m1[0*4 + 2] = 0.0f; m1[0*4 + 3] = 0.0f; m1[1*4 + 0] = 0.0f; m1[1*4 + 1] = y_scale; m1[1*4 + 2] = 0.0f; m1[1*4 + 3] = 0.0f; m1[2*4 + 0] = 0.0f; m1[2*4 + 1] = 0.0f; m1[2*4 + 2] = z_scale; m1[2*4 + 3] = 0.0f; m1[3*4 + 0] = 0.0f; m1[3*4 + 1] = 0.0f; m1[3*4 + 2] = 0.0f; m1[3*4 + 3] = 1.0f; MultMatrix(m2, m1, m); CopyMatrix(m, m2); return; } z 回転void RotateZ(float &m, const float z_angle) { float m1[16]; float m2[16]; double radians = 6.283185308f / (360.0f / z_angle); double s = sin(radians); double c = cos(radians); m1[0*4 + 0] = c; m1[0*4 + 1] = s; m1[0*4 + 2] = 0.0f; m1[0*4 + 3] = 0.0f; m1[1*4 + 0] = -s; m1[1*4 + 1] = c; m1[1*4 + 2] = 0.0f; m1[1*4 + 3] = 0.0f; m1[2*4 + 0] = 0.0f; m1[2*4 + 1] = 0.0f; m1[2*4 + 2] = 1.0f; m1[2*4 + 3] = 0.0f; m1[3*4 + 0] = 0.0f; m1[3*4 + 1] = 0.0f; m1[3*4 + 2] = 0.0f; m1[3*4 + 3] = 1.0f; MultMatrix(m2, m1, m); CopyMatrix(m, m2); return; } x 回転void RotateX(float &m, const float x_angle) { float m1[16]; float m2[16]; double radians = 6.283185308f / (360.0f / z_angle); double s = sin(radians); double c = cos(radians); m1[0*4 + 0] = 1.0f; m1[0*4 + 1] = 0.0f; m1[0*4 + 2] = 0.0f; m1[0*4 + 3] = 0.0f; m1[1*4 + 0] = 0.0f; m1[1*4 + 1] = c; m1[1*4 + 2] = s; m1[1*4 + 3] = 0.0f; m1[2*4 + 0] = 0.0f; m1[2*4 + 1] = -s; m1[2*4 + 2] = c; m1[2*4 + 3] = 0.0f; m1[3*4 + 0] = 0.0f; m1[3*4 + 1] = 0.0f; m1[3*4 + 2] = 0.0f; m1[3*4 + 3] = 1.0f; MultMatrix(m2, m1, m); CopyMatrix(m, m2); return; } y 回転void RotateY(float &m, const float y_angle) { float m1[16]; float m2[16]; double radians = 6.283185308f / (360.0f / y_angle); double s = sin(radians); double c = cos(radians); m1[0*4 + 0] = c; m1[0*4 + 1] = 0.0f; m1[0*4 + 2] = -s; m1[0*4 + 3] = 0.0f; m1[1*4 + 0] = 0.0f; m1[1*4 + 1] = 1.0f; m1[1*4 + 2] = 0.0f; m1[1*4 + 3] = 0.0f; m1[2*4 + 0] = s; m1[2*4 + 1] = 0.0f; m1[2*4 + 2] = c; m1[2*4 + 3] = 0.0f; m1[3*4 + 0] = 0.0f; m1[3*4 + 1] = 0.0f; m1[3*4 + 2] = 0.0f; m1[3*4 + 3] = 1.0f; MultMatrix(m2, m1, m); CopyMatrix(m, m2); return; } 統合された回転T.B.Dvoid Rotate(float &m, const float x_angle, const float y_angle, const float z_angle) { float m1[16]; float m2[16]; double radians = 6.283185308f / (360.0f / y_angle); double s = sin(radians); double c = cos(radians); m1[0*4 + 0] = c; m1[0*4 + 1] = 0.0f; m1[0*4 + 2] = -s; m1[0*4 + 3] = 0.0f; m1[1*4 + 0] = 0.0f; m1[1*4 + 1] = 1.0f; m1[1*4 + 2] = 0.0f; m1[1*4 + 3] = 0.0f; m1[2*4 + 0] = s; m1[2*4 + 1] = 0.0f; m1[2*4 + 2] = c; m1[2*4 + 3] = 0.0f; m1[3*4 + 0] = 0.0f; m1[3*4 + 1] = 0.0f; m1[3*4 + 2] = 0.0f; m1[3*4 + 3] = 1.0f; MultMatrix(m2, m1, m); CopyMatrix(m, m2); return; }
|
||||||||||
|
||||||||||
ご意見、ご感想、誤字、間違い等、お気づきの点がありましたら KGussan@Gmail.com まで連絡ください。
このウェブページの注意・免責事項
Copyright (C) 2004-2012 KGussan.
[トップページ]
[PR:元国税局勤務の起業家向け、遺産相続の相談なら植村洋税理士事務所:一時間無料税理士相談。お気軽に。] |