我在两个对象描述的游戏引擎中有我的对象: 位置向量:(x,y,z);和 旋转四元数:(w,a,b,c)
我现在正在寻找构建模型视图矩阵所需的数学,我有数学而不是编程背景,所以教程我找到了参考函数,我宁愿更好地理解原始计算。
它是用Java构建的,以opengl作为渲染引擎
顺便说一下,有没有人知道java中用于设置模型矩阵的方法?即:
Float [3] [3] matrix = new matrix; ModelMatrix.set(矩阵)
答案 0 :(得分:2)
以下是来自jMonkeyEngine's Quaternion class的方法 这是一个4x4矩阵结构的浮点数[16]。
public float[] toRotationMatrix(float[] result) {
float norm = norm();
// we explicitly test norm against one here, saving a division
// at the cost of a test and branch. Is it worth it?
float s = (norm == 1f) ? 2f : (norm > 0f) ? 2f / norm : 0;
// compute xs/ys/zs first to save 6 multiplications, since xs/ys/zs
// will be used 2-4 times each.
float xs = x * s;
float ys = y * s;
float zs = z * s;
float xx = x * xs;
float xy = x * ys;
float xz = x * zs;
float xw = w * xs;
float yy = y * ys;
float yz = y * zs;
float yw = w * ys;
float zz = z * zs;
float zw = w * zs;
// using s=2/norm (instead of 1/norm) saves 9 multiplications by 2 here
result[0] = 1 - (yy + zz);
result[1] = (xy - zw);
result[2] = (xz + yw);
result[4] = (xy + zw);
result[5] = 1 - (xx + zz);
result[6] = (yz - xw);
result[8] = (xz - yw);
result[9] = (yz + xw);
result[10] = 1 - (xx + yy);
return result;
}
因为你要求整个模型矩阵: 翻译在同一矩阵中占据位置3,7,11。 编辑:查询显示我的规模是错误的。
答案 1 :(得分:1)
这就是我使用的,q
是四元数:
matrix = new float[3][3];
float x2 = q.x*q.x, y2 = q.y*q.y, z2 = q.z*q.z;
matrix[0][0] = (1-2*z2-2*y2);
matrix[0][1] = (2*q.x*q.y+2*q.w*q.z);
matrix[0][2] = (2*q.x*q.z-2*q.w*q.y);
matrix[1][0] = (2*q.x*q.y-2*q.w*q.z);
matrix[1][1] = (1-2*z2-2*x2);
matrix[1][2] = (2*q.y*q.z+2*q.w*q.x);
matrix[2][0] = (2*q.x*q.z+2*q.w*q.y);
matrix[2][1] = (2*q.y*q.z-2*q.w*q.x);
matrix[2][2] = (1-2*y2-2*x2);