// Moves void Camera::move(const bool spherical, const float * const moves, const float * const angles, const float radius) { float cameraNewPos[3]; for (unsigned int iCoord=0 ; iCoord<3 ; ++iCoord) { cameraNewPos[iCoord]=this->c[iCoord] +this->x[iCoord]*moves[0] +this->y[iCoord]*moves[1] +this->z[iCoord]*moves[2]; } float xAxis[]= {1.0, 0.0, 0.0}; float yAxis[]= {0.0, 1.0, 0.0}; float rotateAroundX[16]; setToRotate(rotateAroundX, -angles[1], xAxis); float rotateAroundY[16]; setToRotate(rotateAroundY, angles[0], yAxis); float t[]= {-cameraNewPos[0], -cameraNewPos[1], -cameraNewPos[2]}; float translate[16]; setToTranslate(translate, t); setToIdentity(this->view); if (spherical) { float tRadius[]= {0.0, 0.0, -radius}; float translateRadius[16]; setToTranslate(translateRadius, tRadius); multMatrixBtoMatrixA(this->view, translateRadius); } multMatrixBtoMatrixA(this->view, rotateAroundX); multMatrixBtoMatrixA(this->view, rotateAroundY); multMatrixBtoMatrixA(this->view, translate); for (unsigned int iCoord=0 ; iCoord<3 ; ++iCoord) { // Updates the axis with values in view this->x[iCoord]=this->view[iCoord*4+0]; this->y[iCoord]=this->view[iCoord*4+1]; this->z[iCoord]=this->view[iCoord*4+2]; // Updates the position of the camera c this->c[iCoord]=cameraNewPos[iCoord]; } // Updates in shaders this->shaders.setView(this->view, this->c); }
/* * Create rotation matrix with rotation center (translate + rotates + inverse translate) */ Matrix4 constructRotationMatrix(const Ofx3DPointD& rotationCenter, const double& angleX, const double& angleY, const double& angleZ) { // define axes Axis axisX = {{1, 0, 0}}; // define axe X Axis axisY = {{0, 1, 0}}; // define axe Y Axis axisZ = {{0, 0, 1}}; // define axe Z // Define and create rotation + translation matrix Matrix4 rotationTranslationMatrix; // initialize setToIdentity(rotationTranslationMatrix); // set matrix center to identity // Define and construct rotation matrices Matrix4 rotationMatrixX; // initialize rotation matrix X setToRotate(rotationMatrixX, angleX / 10.0, axisX); // construct rotation matrix X Matrix4 rotationMatrixY; // initialize rotation matrix Y setToRotate(rotationMatrixY, angleY / 10.0, axisY); // construct rotation matrix Y Matrix4 rotationMatrixZ; // initialize rotation matrix Z setToRotate(rotationMatrixZ, angleZ / 10.0, axisZ); // construct rotation matrix Z // Define and construct translation matrix Matrix4 translationMatrix; // initialize translation matrix // Construct translation vector Vect4 translationVector = {{rotationCenter.x, rotationCenter.y, rotationCenter.z, 1.0}}; setToTranslate(translationMatrix, translationVector); // construct translation matrix // Define and construct inverse translation matrix Matrix4 translationInverseMatrix; // initialize inverse translation matrix // Construct inverse translation vector Vect4 inverseTranslationVector = {{-rotationCenter.x, -rotationCenter.y, -rotationCenter.z, 1.0}}; setToTranslate(translationInverseMatrix, inverseTranslationVector); // construct inverse translation matrix // Construct final reference center matrix (inverse order of application) multMatrixBtoMatrixA(rotationTranslationMatrix, translationMatrix); // translation matrix multMatrixBtoMatrixA(rotationTranslationMatrix, rotationMatrixZ); // rotation Z axis multMatrixBtoMatrixA(rotationTranslationMatrix, rotationMatrixY); // rotation Y axis multMatrixBtoMatrixA(rotationTranslationMatrix, rotationMatrixX); // rotation X axis multMatrixBtoMatrixA(rotationTranslationMatrix, translationInverseMatrix); // inverse translation matrix // return result return rotationTranslationMatrix; }