【2010.11.19】
球と直方体の自在に描画しよう!
パソコンの中に思い通りの描画するためには、単にプログラミングの能力があるだけでは足りません。 球や直方体などのオブジェクトを配置する際には特に、3次元空間での認識能力が必要になります。 次の図は、空間認識能力を鍛えるための課題です。
C++言語のサンプル
直方体の描画関数の引数
drowCuboid( x方向の幅, y方向の幅, z方向の幅, x座標, y座標, z座標, 色の指定)
直方体の描画関数の定義
void drowCuboid(double a, double b, double c, double x, double y, double z, MaterialStruct color){
GLdouble vertex[][3] = {
{ -a/2.0, -b/2.0, -c/2.0 },
{ a/2.0, -b/2.0, -c/2.0 },
{ a/2.0, b/2.0, -c/2.0 },
{ -a/2.0, b/2.0, -c/2.0 },
{ -a/2.0, -b/2.0, c/2.0 },
{ a/2.0, -b/2.0, c/2.0 },
{ a/2.0, b/2.0, c/2.0 },
{ -a/2.0, b/2.0, c/2.0 }
};
int face[][4] = {//面の定義
{ 3, 2, 1, 0 },
{ 1, 2, 6, 5 },
{ 4, 5, 6, 7 },
{ 0, 4, 7, 3 },
{ 0, 1, 5, 4 },
{ 2, 3, 7, 6 }
};
GLdouble normal[][3] = {//面の法線ベクトル
{ 0.0, 0.0, -1.0 },
{ 1.0, 0.0, 0.0 },
{ 0.0, 0.0, 1.0 },
{-1.0, 0.0, 0.0 },
{ 0.0,-1.0, 0.0 },
{ 0.0, 1.0, 0.0 }
};
glPushMatrix();
glMaterialfv(GL_FRONT, GL_AMBIENT, color.ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, color.diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, color.specular);
glMaterialfv(GL_FRONT, GL_SHININESS, &color.shininess);
glTranslated( x, y, z);//平行移動値の設定
glBegin(GL_QUADS);
for (int j = 0; j < 6; ++j) {
glNormal3dv(normal[j]); //法線ベクトルの指定
for (int i = 0; i < 4; ++i) {
glVertex3dv(vertex[face[j][i]]);
}
}
glEnd();
glPopMatrix();
}
色の定義
struct MaterialStruct {
GLfloat ambient[4];
GLfloat diffuse[4];
GLfloat specular[4];
GLfloat shininess;
};
//////////////////////////////////////////
//ruby(ルビー)
MaterialStruct ms_ruby = {
{0.1745, 0.01175, 0.01175, 1.0},
{0.61424, 0.04136, 0.04136, 1.0},
{0.727811, 0.626959, 0.626959, 1.0},
76.8};




