Wiki内検索
最近更新したページ
2011-08-24
2010-05-18
2010-05-03
2010-02-18
2010-02-10
2010-02-09
2010-02-08
2010-02-07
2010-02-02
2009-12-24
2009-12-05
2009-07-05
2009-07-03
最新コメント
MenuBar1 by stunning seo guys
MenuBar1 by stunning seo guys
MenuBar1 by stunning seo guys
MenuBar1 by awesome things!
MenuBar1 by awesome things!
MenuBar1 by awesome things!
MenuBar1 by check it out
MenuBar1 by awesome things!
MenuBar1 by check it out
MenuBar1 by stunning seo guys
Menu
Programming Tips
タグ

Matrix3DCG



行列計算クラス



行列によるアフィン変換は、行列の掛け算が
基本にあります。横掛ける縦の順で掛けて行きます。


public class Matrix {

float a0, a1, a2, a3;
float b0, b1, b2, b3;
float c0, c1, c2, c3;
float d0, d1, d2, d3;

Matrix t = null;

Matrix()
{
loadIdentity();
}

void tmpInit()
{
if (t == null) t = new Matrix();
t.loadIdentity();
}


void loadIdentity()
{
a0 = a1 = a2 = a3 = 0;
b0 = b1 = b2 = b3 = 0;
c0 = c1 = c2 = c3 = 0;
d0 = d1 = d2 = d3 = 0;

a0 = b1 = c2 = d3 = 1;
}

void multi(Vertex v1, Vertex v2, Vertex v3)
{
multi(v1); multi(v2); multi(v3);
}

void multi(Vertex v)
{ //tmpx, tmpy, tmpz
float tmpx = v.x * a0 + v.y * b0 + v.z * c0 + d0;
float tmpy = v.x * a1 + v.y * b1 + v.z * c1 + d1;
float tmpz = v.x * a2 + v.y * b2 + v.z * c2 + d2;
v.x = tmpx; v.y = tmpy; v.z = tmpz;
}

/*
float a0, a1, a2, a3;
float b0, b1, b2, b3;
float c0, c1, c2, c3;
float d0, d1, d2, d3;

/


void multi(Matrix m)
{ //横X縦 thisMat * パラメーターMat
float ma0 = a0 * m.a0 + a1 * m.b0 + a2 * m.c0 + a3 * m.d0;
float mb0 = b0 * m.a0 + b1 * m.b0 + b2 * m.c0 + b3 * m.d0;
float mc0 = c0 * m.a0 + c1 * m.b0 + c2 * m.c0 + c3 * m.d0;
float md0 = d0 * m.a0 + d1 * m.b0 + d2 * m.c0 + d3 * m.d0;

float ma1 = a0 * m.a1 + a1 * m.b1 + a2 * m.c1 + a3 * m.d1;
float mb1 = b0 * m.a1 + b1 * m.b1 + b2 * m.c1 + b3 * m.d1;
float mc1 = c0 * m.a1 + c1 * m.b1 + c2 * m.c1 + c3 * m.d1;
float md1 = d0 * m.a1 + d1 * m.b1 + d2 * m.c1 + d3 * m.d1;

float ma2 = a0 * m.a2 + a1 * m.b2 + a2 * m.c2 + a3 * m.d2;
float mb2 = b0 * m.a2 + b1 * m.b2 + b2 * m.c2 + b3 * m.d2;
float mc2 = c0 * m.a2 + c1 * m.b2 + c2 * m.c2 + c3 * m.d2;
float md2 = d0 * m.a2 + d1 * m.b2 + d2 * m.c2 + d3 * m.d2;

float ma3 = a0 * m.a3 + a1 * m.b3 + a2 * m.c3 + a3 * m.d3;
float mb3 = b0 * m.a3 + b1 * m.b3 + b2 * m.c3 + b3 * m.d3;
float mc3 = c0 * m.a3 + c1 * m.b3 + c2 * m.c3 + c3 * m.d3;
float md3 = d0 * m.a3 + d1 * m.b3 + d2 * m.c3 + d3 * m.d3;

a0 = ma0; a1 = ma1; a2 = ma2; a3 = ma3;
b0 = mb0; b1 = mb1; b2 = mb2; b3 = mb3;
c0 = mc0; c1 = mc1; c2 = mc2; c3 = mc3;
d0 = md0; d1 = md1; d2 = md2; d3 = md3;
}

void rotationX(float r)
{
float cos = (float)Math.cos(r);
float sin = (float)Math.sin(r);
tmpInit();
t.b1 = cos; t.b2 = sin; t.c1 = -sin; t.c2 = cos;
multi(t);
}

void rotationY(float r)
{
float cos = (float)Math.cos(r);
float sin = (float)Math.sin(r);
tmpInit();
t.a0 = cos; t.a2 = -sin;

t.c0 = sin; t.c2 = cos;
multi(t);
}

void rotationZ(float r)
{
float cos = (float)Math.cos(r);
float sin = (float)Math.sin(r);
tmpInit();
t.a0 = cos; t.a1 = sin;
t.b0 = -sin; t.b1 = cos;
multi(t);
}

void translate(float x, float y, float z)
{
tmpInit();
d0 = x; d1 = y; d2 = z;
multi(t);
}

void scale(float x, float y, float z)
{
tmpInit();
a0 = x; b1 = y; c2 = z;
multi(t);
}

}
2008年01月07日(月) 19:18:05 Modified by eruvasu




スマートフォン版で見る