void myCamera::LookLeftRight(float deg) { double theta = PI*double(deg)/double(180); float ux = 0; float uy = 1; float uz = 0; float ux_2 = pow(ux, 2); float uy_2 = pow(uy, 2); float uz_2 = pow(uz, 2); // rodriguez's matrix GLfloat R[4][4] = {{ux_2 + cos(theta)*(1 - ux_2), (ux*uy*(1-cos(theta))) - uz*sin(theta), (uz*ux*(1-cos(theta))) + uy*sin(theta), 0}, {(ux*uy*(1-cos(theta))) + uz*sin(theta), uy_2 + cos(theta)*(1-uy_2), (uy*uz*(1-cos(theta))) - ux*sin(theta), 0}, {(uz*ux*(1-cos(theta))) - uy*sin(theta), (uy*uz*(1-cos(theta))) + ux*sin(theta), uz_2 + cos(theta)*(1-uz_2), 0}, {0, 0, 0, 1}}; GLfloat Rrot[4][4] = {{u[0], u[1], u[2], 0}, {v[0], v[1], v[2], 0}, {w[0], w[1], w[2], 0}, {0, 0, 0, 1}}; GLfloat** temp = matrixMult4d(Rrot, R); // update axes, look, and up vectors u[0] = temp[0][0]; u[1] = temp[0][1]; u[2] = temp[0][2]; v[0] = temp[1][0]; v[1] = temp[1][1]; v[2] = temp[1][2]; up[0] = v[0]; up[1] = v[1]; up[2] = v[2]; w[0] = temp[2][0]; w[1] = temp[2][1]; w[2] = temp[2][2]; look[0] = -w[0]; look[1] = -w[1]; look[2] = -w[2]; for (int i = 0; i < 4; i++) { delete [] temp[i]; } delete [] temp; generateR(); }
void myCamera::printInfo() { double modelViewMatrix[16]; double projMatrix[16]; GLfloat v[4][4]; GLfloat p[4][4]; // calculating the world-to-film matrix glGetDoublev(GL_MODELVIEW_MATRIX, modelViewMatrix); glGetDoublev(GL_PROJECTION_MATRIX, projMatrix); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { v[j][i] = modelViewMatrix[i*4+j]; p[j][i] = projMatrix[i*4+j]; } } GLfloat** w_to_f_matrix = matrixMult4d(v, p); printf("Camera info:\n"); printf("\teye point: %f, %f, %f\n", pos[0], pos[1], pos[2]); printf("\tlook: %f, %f, %f\n", look[0], look[1], look[2]); printf("\tup: %f, %f, %f\n", up[0], up[1], up[2]); printf("\theight angle: %f\n", fovy); printf("\taspect ratio: %f\n", aspect); printf("\tnear: %f\n", zNear); printf("\tfar: %f\n", zFar); // printing out world to film matrix in row-major format printf("\tworld-to-film matrix (row-major):\n"); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { printf("\t\t[%d][%d] = %f\n", i, j, w_to_f_matrix[i][j]); } } printf("\n"); for (int i = 0; i < 4; i++) { delete [] w_to_f_matrix[i]; } delete [] w_to_f_matrix; }
void myCamera::RotateW(float deg) { // rotate around local Z float theta = deg/180*PI; GLfloat Rrot[4][4] = {{u[0], u[1], u[2], 0}, {v[0], v[1], v[2], 0}, {w[0], w[1], w[2], 0}, {0, 0, 0, 1}}; GLfloat R[4][4] = {{cos(theta), -sin(theta), 0, 0}, {sin(theta), cos(theta), 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}; GLfloat** temp = matrixMult4d(R, Rrot); // update axes, look, and up vectors u[0] = temp[0][0]; u[1] = temp[0][1]; u[2] = temp[0][2]; v[0] = temp[1][0]; v[1] = temp[1][1]; v[2] = temp[1][2]; up[0] = v[0]; up[1] = v[1]; up[2] = v[2]; w[0] = temp[2][0]; w[1] = temp[2][1]; w[2] = temp[2][2]; look[0] = -w[0]; look[1] = -w[1]; look[2] = -w[2]; for (int i = 0; i < 4; i++) { delete [] temp[i]; } delete [] temp; generateR(); }
void real_rotation(Shape *s, GLfloat deg, GLfloat x, GLfloat y, GLfloat z) { deg = deg * (M_PI / 180); GLfloat rotateMatrixX[4][4] = { {1, 0, 0, 0}, {0, cos(deg), -sin(deg), 0}, {0, sin(deg), cos(deg), 0}, {0, 0, 0, 1} }; GLfloat rotateMatrixY[4][4] = { {cos(deg), 0, sin(deg), 0}, {0, 1, 0, 0}, {-sin(deg), 0, cos(deg), 0}, {0, 0, 0, 1} }; GLfloat rotateMatrixZ[4][4] = { {cos(deg), -sin(deg),0, 0}, {sin(deg), cos(deg), 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1} }; if (x == 1) { //CTM GLfloat **newCtm = matrixMult4d(rotateMatrixX, s->ctm); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { s->ctm[i][j] = newCtm[i][j]; } } //Inverse CTM GLfloat rotateMatrixXInv[4][4] = { {1, 0, 0, 0}, {0, cos(-deg), -sin(-deg), 0}, {0, sin(-deg), cos(-deg), 0}, {0, 0, 0, 1} }; GLfloat **newInvCtm = matrixMult4d(s->invCtm, rotateMatrixXInv); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { s->invCtm[i][j] = newInvCtm[i][j]; } } //Inv dir for moving ray GLfloat **newInvDir = matrixMult4d(s->dirInv, rotateMatrixXInv); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { s->dirInv[i][j] = newInvDir[i][j]; } } for (int i = 0; i < 4; i++) { delete[] newInvDir[i]; } delete[] newInvDir; for (int i = 0; i < 4; i++) { delete[] newCtm[i]; } delete[] newCtm; for (int i = 0; i < 4; i++) { delete[] newInvCtm[i]; } delete[] newInvCtm; } else if (y == 1) { GLfloat **newCtm = matrixMult4d(rotateMatrixY, s->ctm); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { s->ctm[i][j] = newCtm[i][j]; } } GLfloat rotateMatrixYInv[4][4] = { {cos(-deg), 0, sin(-deg), 0}, {0, 1, 0, 0}, {-sin(-deg), 0, cos(-deg), 0}, {0, 0, 0, 1} }; GLfloat **newInvCtm = matrixMult4d(s->invCtm, rotateMatrixYInv); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { s->invCtm[i][j] = newInvCtm[i][j]; } } GLfloat **newInvDir = matrixMult4d(s->dirInv, rotateMatrixYInv); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { s->dirInv[i][j] = newInvDir[i][j]; } } for (int i = 0; i < 4; i++) { delete[] newInvDir[i]; } delete[] newInvDir; for (int i = 0; i < 4; i++) { delete[] newCtm[i]; } delete[] newCtm; for (int i = 0; i < 4; i++) { delete[] newInvCtm[i]; } delete[] newInvCtm; } else if (z == 1) { GLfloat **newCtm = matrixMult4d(rotateMatrixZ, s->ctm); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { s->ctm[i][j] = newCtm[i][j]; } } GLfloat rotateMatrixZInv[4][4] = { {cos(-deg), -sin(-deg), 0, 0}, {sin(-deg), cos(-deg), 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1} }; GLfloat **newInvCtm = matrixMult4d(s->invCtm, rotateMatrixZInv); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { s->invCtm[i][j] = newInvCtm[i][j]; } } GLfloat **newInvDir = matrixMult4d(s->dirInv, rotateMatrixZInv); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { s->dirInv[i][j] = newInvDir[i][j]; } } for (int i = 0; i < 4; i++) { delete[] newInvDir[i]; } delete[] newInvDir; for (int i = 0; i < 4; i++) { delete[] newCtm[i]; } delete[] newCtm; for (int i = 0; i < 4; i++) { delete[] newInvCtm[i]; } delete[] newInvCtm; } for (int r = 0; r < s->vertices.size(); r++) { for (int c = 0; c < s->vertices[0].size(); c++) { //Current vertex GLfloat vertex[4] = {s->vertices[r][c].x, s->vertices[r][c].y, s->vertices[r][c].z, s->vertices[r][c].w}; GLfloat *tNew; //New vertex position if (x == 1) { tNew = multiply(rotateMatrixX, vertex); //Get tNew } else if (y == 1) { tNew = multiply(rotateMatrixY, vertex); } else if (z == 1) { tNew = multiply(rotateMatrixZ, vertex); } //Update vertex with tNew after choosing which to rotate it by s->vertices[r][c].x = tNew[0]; s->vertices[r][c].y = tNew[1]; s->vertices[r][c].z = tNew[2]; delete[] tNew; //delete[] newAxis; } } }
void real_scaling(Shape *s, GLfloat sx, GLfloat sy, GLfloat sz) { GLfloat scalingMatrix[4][4] = { {sx, 0, 0, 0}, {0, sy, 0, 0}, {0, 0, sz, 0}, {0, 0, 0, 1} }; //Chang CTM GLfloat **newCtm = matrixMult4d(scalingMatrix, s->ctm); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { s->ctm[i][j] = newCtm[i][j]; } } //Change inverseCTM GLfloat scalingMatrixInv[4][4] = { {1/sx, 0, 0, 0}, {0, 1/sy, 0, 0}, {0, 0, 1/sz, 0}, {0, 0, 0, 1} }; GLfloat **newInvCtm = matrixMult4d(s->invCtm, scalingMatrixInv); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { s->invCtm[i][j] = newInvCtm[i][j]; } } //Keep track of direction inv'CTM' for moving ray GLfloat **newInvDir = matrixMult4d(s->dirInv, scalingMatrixInv); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { s->dirInv[i][j] = newInvDir[i][j]; } } for (int i = 0; i < 4; i++) { delete[] newInvDir[i]; } delete[] newInvDir; for (int i = 0; i < 4; i++) { delete[] newCtm[i]; } delete[] newCtm; for (int i = 0; i < 4; i++) { delete[] newInvCtm[i]; } delete[] newInvCtm; for (int r = 0; r < s->vertices.size(); r++) { for (int c = 0; c < s->vertices[0].size(); c++) { GLfloat vertex[4] = {s->vertices[r][c].x, s->vertices[r][c].y, s->vertices[r][c].z, s->vertices[r][c].w}; GLfloat *tNew = multiply(scalingMatrix, vertex); s->vertices[r][c].x = tNew[0]; s->vertices[r][c].y = tNew[1]; s->vertices[r][c].z = tNew[2]; delete[] tNew; } } }
void real_translation(Shape *s, GLfloat x, GLfloat y, GLfloat z) { GLfloat translateMatrix[4][4] = { {1, 0, 0, x}, {0, 1, 0, y}, {0, 0, 1, z}, {0, 0, 0, 1} }; //Multiply the translate matrix by the objects current CTM GLfloat **newCtm = matrixMult4d(translateMatrix, s->ctm); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { s->ctm[i][j] = newCtm[i][j]; } } //Multiply the translate matrix by the objects current inverse CTM GLfloat translateMatrixInv[4][4] = { {1, 0, 0, -x}, {0, 1, 0, -y}, {0, 0, 1, -z}, {0, 0, 0, 1} }; GLfloat **newInvCtm = matrixMult4d(s->invCtm, translateMatrixInv); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { s->invCtm[i][j] = newInvCtm[i][j]; } } for (int i = 0; i < 4; i++) { delete[] newCtm[i]; } delete[] newCtm; for (int i = 0; i < 4; i++) { delete[] newInvCtm[i]; } delete[] newInvCtm; //Change offset of shape for translating back to origin s->pos[0] += x; s->pos[1] += y; s->pos[2] += z; for (int r = 0; r < s->vertices.size(); r++) { for (int c = 0; c < s->vertices[0].size(); c++) { GLfloat vertex[4] = {s->vertices[r][c].x, s->vertices[r][c].y, s->vertices[r][c].z, s->vertices[r][c].w}; GLfloat *tNew = multiply(translateMatrix, vertex); s->vertices[r][c].x = tNew[0]; s->vertices[r][c].y = tNew[1]; s->vertices[r][c].z = tNew[2]; delete[] tNew; } } }