c# - 3D Graphics - Matrix math is not working -
i'm new 3d graphics, expect have done fundamentally wrong. have built 4 matrixes handle objects in 3d space, scaling matrix, translation matrix, perspective matrix , rotation matrix.
the rotation , scaling matrix work fine, perspective , translation matrixes not behave expected, understand translation , perspective matrixes like:
translation perspective [1, 0, 0, x] [1, 0, 0 , 0] [0, 1, 0, y] [0, 1, 0 , 1] [0, 0, 1, z] [0, 0, 1 , 0] [0, 0, 0, 1] [0, 0, 1/d, 1]
to understand how works behind scenes writing own math lib - create matrixes defined above have following methods:
public static asmatrix4 createperspectivematrix(double focallength) { // create matrix identity matrix var m = new asmatrix4(); // set perspective matrix m.matrix[3].points[2] = 1 / focallength; m.matrix[1].points[3] = 1; return m; } public static asmatrix4 createtranslationmatrix(double x, double y, double z) { // reset identity matrix var m = new asmatrix4(); // set translation matrix m.matrix[0].points[3] = x; m.matrix[1].points[3] = y; m.matrix[2].points[3] = z; return m; }
nothing complicated, when multiply matrixes together, nothing happens - can rotate axis, cannot translate. note above new asmatrix4()
initialises new 4x4 matrix identity matrix.
to multiply matrixes following:
m_scaling = asmatrix4.createscalingmatrix(s, s, s); m_translation = asmatrix4.createtranslationmatrix(tx, ty, tz); m_perspective = asmatrix4.createperspectivematrix(f); m_rotation = asmatrix4.rotatebydegrees(rx, ry, rz); var transformationmatrix = m_scaling*m_translation*m_perspective*m_rotation;
i wrote operator overload handle multiplying matrixes together:
public static asmatrix4 operator *(asmatrix4 ma, asmatrix4 mb) { // creates new identity matrix var m = new asmatrix4(); // multiply 2 matrixes , return output matrix (var = 0; < 4; i++) { (var j = 0; j < 4; j++) { m.matrix[i].points[j] = (mb.matrix[i].points[0]*ma.matrix[0].points[j]) + (mb.matrix[i].points[1]*ma.matrix[1].points[j]) + (mb.matrix[i].points[2]*ma.matrix[2].points[j]) + (mb.matrix[i].points[3]*ma.matrix[3].points[j]) } } return m; }
finally transform mesh, have array of faces, draw 2d rendering context, method below transforms each face transformation matrix defined above.
private void transformmesh() { var transformationmatrix = m_scaling*m_translation*m_perspective*m_rotation; (var = 0; < m_numfaces; i++) { m_meshdata[i] = m_meshdata[i].transformface(transformationmatrix); } }
the transformface
method looks described below:
public asface transformface(asmatrix4 matrix) { var transformedface = new asface(); (var = 0; < 3; i++) { transformedface.m_points[i] = m_points[i].transformvector(matrix); transformedface.m_points[i] = transformedface.m_points[i].rescale(); } return transformedface; }
this first method in loop transforms homogenous point input matrix, , rescale method called normalise vector.
i'm confused have done wrong? thinking order of operations on multiplying matrixes wrong not sure, suggestions appreciated.
i see 2 problems in posted code. first, createperspectivematrix method not set (1, 3) element 1; either method or initial definition of perspective matrix wrong. (or both!) second, in multiplication code seem multiplying sub-matrix of first 3 rows , columns. since these sub-matrices unit matrices, it's not surprising nothing happens.
Comments
Post a Comment