「同次座標系で計算」の編集履歴(バックアップ)一覧はこちら
「同次座標系で計算」(2014/04/17 (木) 00:47:51) の最新版変更点
追加された行は緑色になります。
削除された行は赤色になります。
<p><strong>それでは、実際に計算してみます。<br />
正しく処理されているのがわかると思います。</strong></p>
<p><img alt="" src="http://www21.atwiki.jp/opengl?cmd=upload&act=open&pageid=181&file=geo.png" /></p>
<table width="600" border="1" cellspacing="1" cellpadding="1"><tbody><tr><td>
<p><span style="font-size:small;">//#pragma comment(linker,
"/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")<br />
#include <GL/freeglut/freeglut.h><br />
#include <stdio.h></span></p>
<p><span style="font-size:small;">#define WIDTH 640<br />
#define HEIGHT 480</span></p>
<p><span style="font-size:small;">//マトリクス構造体<br />
struct MATRIX {<br />
union {<br />
struct {<br />
float _11, _12, _13, _14;<br />
float _21, _22, _23, _24;<br />
float _31, _32, _33, _34;<br />
float _41, _42, _43, _44;<br />
};<br />
float mat_4x4[4][4];<br />
float mat_16[16];<br />
};<br />
MATRIX(){//単位行列に初期化<br />
for(int i=0;i<16;i++){<br />
this->mat_16[i]=0;<br />
}<br />
this->_11=this->_22=this->_33=this->_44=1;<br />
}<br />
void PRINT(char* text){<br />
printf("%s\n%f,%f,%f,%f\n%f,%f,%f,%f\n%f,%f,%f,%f\n%f,%f,%f,%f\n\n",<br />
text,<br />
this->_11,this->_21,this->_31,this->_41,<br />
this->_12,this->_22,this->_32,this->_42,<br />
this->_13,this->_23,this->_33,this->_43,<br />
this->_14,this->_24,this->_34,this->_44);<br />
}<br />
};</span></p>
<p><span style="font-size:small;">//4つのベクトル<br />
struct Vector4f{<br />
union {<br />
struct {<br />
float x;<br />
float y;<br />
float z;<br />
float w;<br />
};<br />
float Index[4];<br />
};<br />
Vector4f(){};<br />
Vector4f(float _x,float _y,float _z,float _w){<br />
x=_x;y=_y;z=_z;w=_w;<br />
};<br />
Vector4f GeometricTransform(MATRIX& mat);//幾何変換<br />
void PRINT(char* text){<br />
printf("%s\n%f,%f,%f,%f\n\n",text,this->x,this->y,this->z,this->w);<br />
}<br />
}vec4d;<br />
Vector4f Vector4f::GeometricTransform(MATRIX &mat){<br />
Vector4f ret;<br />
for(int y=0;y<4;y++){<br />
ret.Index[y]=mat.mat_16[y]*this->x+mat.mat_16[y+4]*this->y+mat.mat_16[y+8]*this->z+mat.mat_16[y+12]*this->w;<br />
}<br />
return ret;<br />
}</span></p>
<p><span style="font-size:small;">void display(void)<br />
{</span></p>
<p><span style="font-size:small;">}</span></p>
<p><span style="font-size:small;">void Init(){</span></p>
<p><span style="font-size:small;"> Vector4f test;<br />
test.x=3.0f;<br />
test.y=1.0f;<br />
test.z=5.0f;<br />
test.w=1.0f;<br />
test.PRINT("同次座標を設定");</span></p>
<p><span style="font-size:small;"> MATRIX mat;<br />
mat._41=4.0f;<br />
mat._42=8.0f;<br />
mat._43=2.0f;<br />
mat.PRINT("適当に作成した平行移動行列");</span></p>
<p><span style="font-size:small;"> Vector4f
ret=test.GeometricTransform(mat);<br />
ret.PRINT("積を行った結果");</span></p>
<p><span style="font-size:small;">}<br />
int main(int argc, char *argv[])<br />
{<br />
glutInitWindowPosition(100, 100);<br />
glutInitWindowSize(WIDTH, HEIGHT);<br />
glutInit(&argc, argv);<br />
glutCreateWindow("同次座標で計算");<br />
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);<br />
glutDisplayFunc(display);<br />
Init();<br />
glutMainLoop();<br />
return 0;<br />
}</span></p>
</td>
</tr></tbody></table>
<p><strong>それでは、実際に計算してみます。<br />
正しく処理されているのがわかると思います。</strong></p>
<p><img alt="" src="http://cdn21.atwikiimg.com/opengl?cmd=upload&act=open&pageid=181&file=geo.png" /></p>
<table border="1" cellpadding="1" cellspacing="1" width="600"><tbody><tr><td>
<p>//#pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")<br />
#include <GL/freeglut/freeglut.h><br />
#include <stdio.h></p>
<p>#define WIDTH 100<br />
#define HEIGHT 50</p>
<p>//マトリクス構造体<br />
struct MATRIX {<br />
union {<br />
struct {<br />
float _11, _12, _13, _14;<br />
float _21, _22, _23, _24;<br />
float _31, _32, _33, _34;<br />
float _41, _42, _43, _44;<br />
};<br />
float mat_4x4[4][4];<br />
float mat_16[16];<br />
};<br />
MATRIX(){//単位行列に初期化<br />
for(int i=0;i<16;i++){<br />
this->mat_16[i]=0;<br />
}<br />
this->_11=this->_22=this->_33=this->_44=1;<br />
}<br />
void PRINT(char* text){<br />
printf("%s\n%f,%f,%f,%f\n%f,%f,%f,%f\n%f,%f,%f,%f\n%f,%f,%f,%f\n\n",<br />
text,<br />
this->_11,this->_21,this->_31,this->_41,<br />
this->_12,this->_22,this->_32,this->_42,<br />
this->_13,this->_23,this->_33,this->_43,<br />
this->_14,this->_24,this->_34,this->_44);<br />
}<br />
};</p>
<p>//4つのベクトル<br />
struct Vector4f{<br />
union {<br />
struct {<br />
float x;<br />
float y;<br />
float z;<br />
float w;<br />
};<br />
float Index[4];<br />
};<br />
Vector4f(){};<br />
Vector4f(float _x,float _y,float _z,float _w){<br />
x=_x;y=_y;z=_z;w=_w;<br />
};<br />
Vector4f GeometricTransform(MATRIX& mat);//幾何変換<br />
void PRINT(char* text){<br />
printf("%s\n%f,%f,%f,%f\n\n",text,this->x,this->y,this->z,this->w);<br />
}<br />
}vec4d;<br />
Vector4f Vector4f::GeometricTransform(MATRIX &mat){<br />
Vector4f ret;<br />
for(int y=0;y<4;y++){<br />
ret.Index[y]=mat.mat_16[y]*this->x+mat.mat_16[y+4]*this->y+mat.mat_16[y+8]*this->z+mat.mat_16[y+12]*this->w;<br />
}<br />
return ret;<br />
}</p>
<p>void display(void)<br />
{</p>
<p>}</p>
<p>void Init(){</p>
<p> Vector4f test;<br />
test.x=3.0f;<br />
test.y=1.0f;<br />
test.z=5.0f;<br />
test.w=1.0f;<br />
test.PRINT("同次座標を設定");</p>
<p> MATRIX mat;<br />
mat._41=4.0f;<br />
mat._42=8.0f;<br />
mat._43=2.0f;<br />
mat.PRINT("適当に作成した平行移動行列");</p>
<p> Vector4f ret=test.GeometricTransform(mat);<br />
ret.PRINT("積を行った結果");</p>
<p>}<br />
int main(int argc, char *argv[])<br />
{<br />
glutInitWindowPosition(100, 100);<br />
glutInitWindowSize(WIDTH, HEIGHT);<br />
glutInit(&argc, argv);<br />
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);<br />
glutCreateWindow("同次座標で計算");<br />
glutDisplayFunc(display);<br />
Init();<br />
glutMainLoop();<br />
return 0;<br />
}</p>
</td>
</tr></tbody></table>