Cube::Cube(const Vec3f &pos, const Vec3f &scale, const Mat4f &rot, const std::string &name, std::shared_ptr<Bsdf> bsdf) : Primitive(name), _rot(rot), _invRot(rot.transpose()), _pos(pos), _scale(scale*0.5f), _bsdf(std::move(bsdf)) { _transform = Mat4f::translate(_pos)*rot*Mat4f::scale(Vec3f(scale)); }
void Camera::applyViewingTransform() { if( mDirtyTransform ) calculateViewingTransformParameters(); ModelerDrawState *mds = ModelerDrawState::Instance(); if(mds->m_rayFile) { fprintf( mds->m_rayFile, "camera {\n\tposition = (%f, %f, %f);\n\tlook_at = (%f, %f, %f);\n\taspectratio = 1\n\tfov = 30; }\n\n", mPosition[0], mPosition[1], mPosition[2], mLookAt[0], mLookAt[1], mLookAt[2]); } // Place the camera at mPosition, aim the camera at // mLookAt, and twist the camera such that mUpVector is up /*gluLookAt( mPosition[0], mPosition[1], mPosition[2], mLookAt[0], mLookAt[1], mLookAt[2], mUpVector[0], mUpVector[1], mUpVector[2]);*/ // You Will Have to implement this (gluLookAt() ) yourself! // what fun that will be! Vec3f n = mPosition - mLookAt; Vec3f v = mUpVector - ((mUpVector * n) / (n * n)) * n; Vec3f u = v ^ n; u.normalize(); v.normalize(); n.normalize(); Mat4f mat; mat[0][0] = u[0]; mat[0][1] = v[0]; mat[0][2] = n[0]; mat[1][0] = u[1]; mat[1][1] = v[1]; mat[1][2] = n[1]; mat[2][0] = u[2]; mat[2][1] = v[2]; mat[2][2] = n[2]; mat = mat.transpose(); mat = mat * mat.createTranslation(-mPosition[0], -mPosition[1], -mPosition[2]); // Transpose the final matrix so that n is in column-major order to match OpenGL. mat = mat.transpose(); glMultMatrixf(mat.n); }
void TestApp::test_matrix_mat4() { Console::write_line(" Class: Mat4"); Console::write_line(" Function: inverse()"); { Mat4f test_src = Mat4f::rotate((Angle(30, angle_degrees)), 1.0, 0.0, 0.0, true); Mat4f test_inv; Mat4f test_dest; Mat4f test_ident = Mat4f::identity(); test_dest = test_src; test_dest.inverse(); test_dest = test_dest * test_src; if (test_ident != test_dest) fail(); } static int test_a_values[] = {3, 1, 2, 4, 5 ,6, 4, 2, 1, 4, 6, 7, 6, 3, 7, 2}; static int test_b_values[] = {4, 7, 2, 5, 3, 5, 2, 9, 3, 3, 6, 9, 2, 4, 6, 2}; Mat4i test_a(test_a_values); Mat4i test_b(test_b_values); Mat4f test_c(test_a); Mat4f test_c_scaled(test_c); { float x = 2.0f; float y = 3.0f; float z = 4.0f; test_c_scaled[0 + 4 * 0] *= x; test_c_scaled[0 + 4 * 1] *= y; test_c_scaled[0 + 4 * 2] *= z; test_c_scaled[1 + 4 * 0] *= x; test_c_scaled[1 + 4 * 1] *= y; test_c_scaled[1 + 4 * 2] *= z; test_c_scaled[2 + 4 * 0] *= x; test_c_scaled[2 + 4 * 1] *= y; test_c_scaled[2 + 4 * 2] *= z; test_c_scaled[3 + 4 * 0] *= x; test_c_scaled[3 + 4 * 1] *= y; test_c_scaled[3 + 4 * 2] *= z; } Console::write_line(" Function: add() and operator"); { int answer_values[] = {7, 8, 4, 9, 8, 11, 6, 11, 4, 7, 12, 16, 8, 7, 13, 4}; Mat4i answer(answer_values); Mat4i result = test_a + test_b; if (result != answer) fail(); result = Mat4i::add(test_a, test_b); if (result != answer) fail(); } Console::write_line(" Function: subtract() and operator"); { int answer_values[] = {-1, -6, 0, -1, 2, 1, 2, -7, -2, 1, 0, -2, 4, -1, 1, 0}; Mat4i answer(answer_values); Mat4i result = test_a - test_b; if (result != answer) fail(); result = Mat4i::subtract(test_a, test_b); if (result != answer) fail(); } Console::write_line(" Function: translate()"); { int answer_values[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 2, 3, 4, 1}; Mat4i answer(answer_values); Mat4i result = Mat4i::translate(2, 3, 4); if (result != answer) fail(); } Console::write_line(" Function: translate_self() (int)"); { Mat4i answer(test_a); Mat4i result = test_a; result = result * Mat4i::translate(2, 3, 4); Mat4i result2 = test_a; result2.translate_self(2,3,4); if (result != result2) fail(); } Console::write_line(" Function: translate_self() (float)"); { Mat4f answer(test_a); Mat4f result(test_a); result = result * Mat4f::translate(2, 3, 4); Mat4f result2(test_a); result2.translate_self(2, 3, 4); if (!result.is_equal(result2, 0.00001f)) fail(); } Console::write_line(" Function: scale_self()"); { Mat4i answer(test_a); Mat4i result = test_a; result = result * Mat4i::scale(2, 3, 4); Mat4i result2 = test_a; result2.scale_self(2,3,4); if (result != result2) fail(); Mat4f test = test_c; test.scale_self(2.0f, 3.0f, 4.0f); if (!test.is_equal(test_c_scaled, 0.00001f)) fail(); } Console::write_line(" Function: rotate (using euler angles)"); { Mat4f mv = Mat4f::identity(); mv = mv * Mat4f::rotate(Angle(30.0f, angle_degrees), 0.0f, 0.0f, 1.0f, false); mv = mv * Mat4f::rotate(Angle(10.0f, angle_degrees), 1.0f, 0.0f, 0.0f, false); mv = mv * Mat4f::rotate(Angle(20.0f, angle_degrees), 0.0f, 1.0f, 0.0f, false); Mat4f test_matrix; test_matrix = Mat4f::rotate(Angle(10.0f, angle_degrees), Angle(20.0f, angle_degrees), Angle(30.0f, angle_degrees), order_YXZ); if (!test_matrix.is_equal(mv, 0.00001f)) fail(); } Console::write_line(" Function: rotate (using euler angles) and get_euler"); { test_rotate_and_get_euler(order_XYZ); test_rotate_and_get_euler(order_XZY); test_rotate_and_get_euler(order_YZX); test_rotate_and_get_euler(order_YXZ); test_rotate_and_get_euler(order_ZXY); test_rotate_and_get_euler(order_ZYX); } Console::write_line(" Function: transpose() (float)"); { Mat4f original(test_a); Mat4f transposed_matrix; transposed_matrix[0] = original[0]; transposed_matrix[1] = original[4]; transposed_matrix[2] = original[8]; transposed_matrix[3] = original[12]; transposed_matrix[4] = original[1]; transposed_matrix[5] = original[5]; transposed_matrix[6] = original[9]; transposed_matrix[7] = original[13]; transposed_matrix[8] = original[2]; transposed_matrix[9] = original[6]; transposed_matrix[10] = original[10]; transposed_matrix[11] = original[14]; transposed_matrix[12] = original[3]; transposed_matrix[13] = original[7]; transposed_matrix[14] = original[11]; transposed_matrix[15] = original[15]; Mat4f test = original; test.transpose(); if (!test.is_equal(transposed_matrix, 0.00001f)) fail(); } }