// YOUR CODE HERE // Given a vector n, form an orthogonal matrix with n as the last column, i.e., // a coordinate system aligned such that n is its local z axis. static __forceinline Mat3f formBasis( const Vec3f& n ) { Mat3f r; Vec3f q, b, t; // Get the minimum component of n int minIdx = 0; for (int i = 0; i < 3; ++i) { if ( abs(n[i]) < abs(n[minIdx]) ) minIdx = i; } // Replace the minimum component by 1.0f and take a cross-product to get a vector perpendicular to n q = n; q[minIdx] = 1.0f; t = (cross(q, n)).normalized(); // Get the last perpendicular vector by taking n x t b = (cross(n, t)).normalized(); // Construct the rotation matrix r.setCol(0, t); r.setCol(1, b); r.setCol(2, n); return r; }
void matrices2quats( int n ){ glBegin ( GL_POINTS ); for( int i=0; i<n; i++ ){ Mat3f M; M.fromRand( {randf(),randf(),randf()} ); Quat4f q; q.fromMatrix(M); glVertex3f( (float)q.x, (float)q.y, (float)q.z ); } glEnd(); }
Mat4f CameraControls::getWorldToCamera(void) const { Mat3f orient = getOrientation(); Vec3f pos = orient.transposed() * m_position; Mat4f r; r.setRow(0, Vec4f(orient.col(0), -pos.x)); r.setRow(1, Vec4f(orient.col(1), -pos.y)); r.setRow(2, Vec4f(orient.col(2), -pos.z)); return r; }
Mat4f CameraControls::getCameraToWorld(void) const { Mat3f orient = getOrientation(); Mat4f r; r.setCol(0, Vec4f(orient.col(0), 0.0f)); r.setCol(1, Vec4f(orient.col(1), 0.0f)); r.setCol(2, Vec4f(orient.col(2), 0.0f)); r.setCol(3, Vec4f(m_position, 1.0f)); return r; }
Mat3f Quatf::rotation() const{ float ww = w*w, xx = x*x, yy = y*y, zz = z*z; float wx = w*x, wy = w*y, wz = w*z; float xy = x*y, xz = x*z; float yz = y*z; Mat3f mat; mat.setRow(0, Vec3f(ww + xx - yy - zz , 2 * xy - 2 * wz , 2 * xz + 2 * wy)); mat.setRow(1, Vec3f(2 * xy + 2 * wz , ww - xx + yy - zz , 2 * yz - 2 * wx)); mat.setRow(2, Vec3f(2 * xz - 2 * wy , 2 * yz + 2 * wx , ww - xx - yy + zz)); return mat; }
void computeAffineMap( const Mat1f &scale_map, const Mat1f &depth_map, float sw, float min_px_scale, Mat3f& affine_map ) { affine_map.create( depth_map.rows, depth_map.cols ); const float nan = std::numeric_limits<float>::quiet_NaN(); for ( int y = 0; y < depth_map.rows; y++ ) { for ( int x = 0; x < depth_map.cols; ++x ) { Vec2f grad; const float sp = getScale(scale_map[y][x], sw); if ( isnan(sp) || sp < min_px_scale || !computeGradient( depth_map, x, y, sp, grad ) ) { affine_map[y][x][0] = nan; affine_map[y][x][1] = nan; affine_map[y][x][2] = nan; } else { getAffParams( sw, grad, affine_map[y][x] ); } } } }
void computeAffineMapFixed( const Mat1f &depth_map, float sp, float f, Mat3f& affine_map ) { affine_map.create( depth_map.rows, depth_map.cols ); const float sp_div_f = sp / f; for ( int y = 0; y < depth_map.rows; y++ ) { for ( int x = 0; x < depth_map.cols; ++x ) { Vec2f grad; if ( isnan(depth_map[y][x]) || !computeGradient( depth_map, x, y, sp, grad ) ) { affine_map[y][x][0] = nan; affine_map[y][x][1] = nan; affine_map[y][x][2] = nan; } else { const float sw = sp_div_f * depth_map[y][x]; getAffParams( sw, grad, affine_map[y][x] ); } } } }
Mat3f formBasis(const Vec3f& n) { // YOUR CODE HERE (R3) Mat3f R; Vec3f Q = n; float min = 99; int ind = -1; for(int i = 0; i<3; ++i) if(abs(n[i]) < min) { min = abs(n[i]); ind = i; } Q[ind] = 1; Q.normalize(); Vec3f T = cross(Q, n).normalized(); Vec3f B = cross(n, T).normalized(); R.setCol(0, T); R.setCol(1, B); R.setCol(2, n); float f = det(R); return R; }
Mat3f CameraControls::getOrientation(void) const { Mat3f r; r.col(2) = -m_forward.normalized(); r.col(0) = m_up.cross(r.col(2)).normalized(); r.col(1) = ((Vec3f)r.col(2)).cross(r.col(0)).normalized(); return r; }
bool CameraControls::handleEvent(const Window::Event& ev) { if (!m_commonControls) fail("CameraControls attached to a window without CommonControls!"); CommonControls& cc = *m_commonControls; FW_ASSERT(m_window == ev.window || ev.type == Window::EventType_AddListener); // Initialize movement. Mat3f orient = getOrientation(); Vec3f rotate = 0.0f; Vec3f move = 0.0f; // Handle events. switch (ev.type) { case Window::EventType_AddListener: FW_ASSERT(!m_window); m_window = ev.window; m_timer.unstart(); m_dragLeft = false; m_dragMiddle = false; m_dragRight = false; cc.addStateObject(this); addGUIControls(); repaint(); return false; case Window::EventType_RemoveListener: cc.removeStateObject(this); removeGUIControls(); repaint(); m_window = NULL; return false; case Window::EventType_KeyDown: if (ev.key == FW_KEY_MOUSE_LEFT) m_dragLeft = true; if (ev.key == FW_KEY_MOUSE_MIDDLE) m_dragMiddle = true; if (ev.key == FW_KEY_MOUSE_RIGHT) m_dragRight = true; if (ev.key == FW_KEY_WHEEL_UP) m_speed *= 1.2f; if (ev.key == FW_KEY_WHEEL_DOWN) m_speed /= 1.2f; break; case Window::EventType_KeyUp: if (ev.key == FW_KEY_MOUSE_LEFT) m_dragLeft = false; if (ev.key == FW_KEY_MOUSE_MIDDLE) m_dragMiddle = false; if (ev.key == FW_KEY_MOUSE_RIGHT) m_dragRight = false; break; case Window::EventType_Mouse: { Vec3f delta = Vec3f((F32)ev.mouseDelta.x, (F32)-ev.mouseDelta.y, 0.0f); if (m_dragLeft) rotate += delta * s_mouseRotateSpeed; if (m_dragMiddle) move += delta * m_speed * s_mouseStrafeSpeed; if (m_dragRight) move += Vec3f(0.0f, 0.0f, (F32)ev.mouseDelta.y) * m_speed * s_mouseStrafeSpeed; } break; case Window::EventType_Paint: { F32 timeDelta = m_timer.end(); F32 boost = cc.getKeyBoost(); Vec3f rotateTmp = 0.0f; bool alt = m_window->isKeyDown(FW_KEY_ALT); if (m_window->isKeyDown(FW_KEY_A) || (m_window->isKeyDown(FW_KEY_LEFT) && alt)) move.x -= 1.0f; if (m_window->isKeyDown(FW_KEY_D) || (m_window->isKeyDown(FW_KEY_RIGHT) && alt)) move.x += 1.0f; if (m_window->isKeyDown(FW_KEY_F) || m_window->isKeyDown(FW_KEY_PAGE_DOWN)) move.y -= 1.0f; if (m_window->isKeyDown(FW_KEY_R) || m_window->isKeyDown(FW_KEY_PAGE_UP)) move.y += 1.0f; if (m_window->isKeyDown(FW_KEY_W) || (m_window->isKeyDown(FW_KEY_UP) && alt)) move.z -= 1.0f; if (m_window->isKeyDown(FW_KEY_S) || (m_window->isKeyDown(FW_KEY_DOWN) && alt)) move.z += 1.0f; if (m_window->isKeyDown(FW_KEY_LEFT) && !alt) rotateTmp.x -= 1.0f; if (m_window->isKeyDown(FW_KEY_RIGHT) && !alt) rotateTmp.x += 1.0f; if (m_window->isKeyDown(FW_KEY_DOWN) && !alt) rotateTmp.y -= 1.0f; if (m_window->isKeyDown(FW_KEY_UP) && !alt) rotateTmp.y += 1.0f; if (m_window->isKeyDown(FW_KEY_E) || m_window->isKeyDown(FW_KEY_HOME)) rotateTmp.z -= 1.0f; if (m_window->isKeyDown(FW_KEY_Q) || m_window->isKeyDown(FW_KEY_INSERT)) rotateTmp.z += 1.0f; move *= timeDelta * m_speed * boost; rotate += rotateTmp * timeDelta * s_keyRotateSpeed * boost; } break; default: break; } // Apply movement. if (m_enableMovement) { if (!move.isZero()) m_position += orient * move; if (rotate.x != 0.0f || rotate.y != 0.0f) { Vec3f tmp = orient.col(2) * cos(rotate.x) - orient.col(0) * sin(rotate.x); m_forward = (orient.col(1) * sin(rotate.y) - tmp * cos(rotate.y)).normalized(); if (!m_keepAligned) m_up = (orient.col(1) * cos(rotate.y) + tmp * sin(rotate.y)).normalized(); else if (-m_forward.cross(m_up).dot(tmp.cross(m_up).normalized()) < s_inclinationLimit) m_forward = -tmp.normalized(); } if (rotate.z != 0.0f && !m_keepAligned) { Vec3f up = orient.transposed() * m_up; m_up = orient * Vec3f(up.x * cos(rotate.z) - sin(rotate.z), up.x * sin(rotate.z) + up.y * cos(rotate.z), up.z); } } // Apply alignment. if (m_alignY) m_up = Vec3f(0.0f, 1.0f, 0.0f); m_alignY = false; if (m_alignZ) m_up = Vec3f(0.0f, 0.0f, 1.0f); m_alignZ = false; // Update stereo mode. if (hasFeature(Feature_StereoControls)) { GLContext::Config config = m_window->getGLConfig(); config.isStereo = (m_enableStereo && GLContext::isStereoAvailable()); m_window->setGLConfig(config); } // Repaint continuously. if (ev.type == Window::EventType_Paint) repaint(); return false; }
void setUniform (int loc, const Mat3f& v) { if (loc >= 0) glUniformMatrix3fv(loc, 1, false, v.getPtr()); }