void object::applyTrans(float **result, float **vertex, long vertCnt, bool doProj, float **proj) { long i; float **matrixA; float *tempA; if (doProj) { matrixA = matrixAlloc(); tempA = new float[4]; matrixMult(matrixA, mTrans, proj); for (i = 0; i < vertCnt; i++) { vectorMatrixMultEx(tempA, vertex[i], matrixA); result[i][0] = tempA[0] / tempA[3]; result[i][1] = tempA[1] / tempA[3]; result[i][2] = tempA[2] / tempA[3]; } matrixFree(matrixA); delete []tempA; } else for (i = 0; i < vertCnt; i++) vectorMatrixMult(result[i], vertex[i], mTrans); }
//Accepts a vertex V, a number THETA, and a flag determining which axis to rotate around. Returns the coordinates of v rotated THETA degrees about the N-axis, where N=whichever of x/y/z is 1 GLdouble* rotateVertex(Vertex v, GLdouble THETA, GLdouble x, GLdouble y, GLdouble z) { THETA*=RADIANS; //printf("ROTATING: %g, %g, %g\n", x, y, z); GLdouble* ret = {0}; if (x==1 || x==-1) //rotate THETA degrees via X rotation matrix { GLdouble rotateX[4][4] = { {1.0, 0.0, 0.0, 0.0}, {0.0, cos(x*THETA), (-1*sin(x*THETA)), 0.0}, {0.0, sin(x*THETA), cos(x*THETA), 0.0}, {0.0, 0.0, 0.0, 1.0}, }; ret = vectorMatrixMult(v, rotateX); } else if (y==1 || y==-1) //rotate THETA degrees via Y rotation matrix { GLdouble rotateY[4][4] = { {cos(y*THETA), 0.0, sin(y*THETA), 0.0}, {0.0, 1.0, 0.0, 0.0}, {(-1*sin(y*THETA)), 0.0, cos(y*THETA), 0.0}, {0.0, 0.0, 0.0, 1.0}, }; ret = vectorMatrixMult(v, rotateY); } else if (z==1 || z==-1) //rotate THETA degrees via Z rotation matrix { GLdouble rotateZ[4][4] = { {cos(z*THETA), (-1*sin(z*THETA)), 0.0, 0.0}, {sin(z*THETA), cos(z*THETA), 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}, {0.0, 0.0, 0.0, 1.0}, }; ret = vectorMatrixMult(v, rotateZ); } else { ret = vectorMatrixMult(v, identity4d); } return ret; }
//scales the passed in vertex V by Sx/Sy/Sz GLdouble* scaleVertex(Vertex v, GLdouble Sx, GLdouble Sy, GLdouble Sz) { GLdouble* ret; GLdouble S[4][4] = { {Sx, 0.0, 0.0, 0.0}, {0.0, Sy, 0.0, 0.0}, {0.0, 0.0, Sz, 0.0}, {0.0, 0.0, 0.0, 1.0}, }; ret = vectorMatrixMult(v, S); return ret; }
//translates some vertex V by amount provided by x/y/z GLdouble* translateVertex(Vertex v, GLdouble x, GLdouble y, GLdouble z) { GLdouble translate[4][4] = { {1.0, 0.0, 0.0, x}, {0.0, 1.0, 0.0, y}, {0.0, 0.0, 1.0, z}, {0.0, 0.0, 0.0, dw}, }; GLdouble* ret = vectorMatrixMult(v, translate); return ret; }
//Rotates the given vertex V about the specified local axis... GLdouble* rotateVertexLocalized(Vertex v, GLdouble THETA, GLdouble x, GLdouble y, GLdouble z) { THETA*=RADIANS; GLdouble* ret; GLdouble M[4][4] = { {(pow(x, 2)+(cos(THETA)*(1-pow(x, 2)))), (x*(y*(1-cos(THETA)))-(z*sin(THETA))), (z*(x*(1-cos(THETA)))+(y*sin(THETA))), 0.0}, {(x*(y*(1-cos(THETA)))+(z*sin(THETA))), (pow(y, 2)+(cos(THETA)*(1-pow(y, 2)))), (y*(z*(1-cos(THETA)))-(x*sin(THETA))), 0.0}, {(z*(x*(1-cos(THETA)))-(y * sin(THETA))), (y*(z*(1-cos(THETA)))+(x*sin(THETA))), (pow(z, 2)+(cos(THETA)*(1-pow(z, 2)))), 0.0}, {0.0, 0.0, 0.0, 1.0}, }; ret = vectorMatrixMult(v, M); return ret; }
float *vectorFromAngles(float *angles) { float *result; float **tempA; float **tempB; float **applyTrans; tempA = matrixRotation('x', angles[1]); tempB = matrixRotation('y', angles[2]); applyTrans = matrixIdent(4); applyTrans = matrixUpdate(applyTrans, matrixMult(applyTrans, tempA, 4), 4); applyTrans = matrixUpdate(applyTrans, matrixMult(applyTrans, tempB, 4), 4); result = vectorNull(4); result[2] = 1; result[3] = 1; result = vectorUpdate(result, vectorMatrixMult(result, applyTrans, 4)); matrixFree(tempA, 4); matrixFree(tempB, 4); matrixFree(applyTrans, 4); return result; }