/** * LookTowards * * Note 1: Function will exit if front_vec is (close to) parallel to the Y-axis; supply your own up_vec if this is the case. * Note 2: Not fully tested, use at own risk... * * Example: * Make camera look at 'my_node'. Note that world space positions are used. * camera->lookAt( my_node->getWorldPosition() - camera->getWorldPosition() ); */ void Transformable::lookTowards( vec3f front_vec, vec3f up_vec ) { vec3f right; vec3f up; vec3f prev_up; front_vec.normalize(); up_vec.normalize(); if (abs(up_vec.dot(front_vec)) > 0.99999f) { return; } if (parent && parent->is_transformable) { mat3f mat; R = ((Transformable *) parent)->getWorldMatrix().rotationMatrix(); R.inv(); prev_up = up_vec; right = front_vec.cross(prev_up); up = right.cross(front_vec); right.normalize(); up.normalize(); mat.setCol(0, right); mat.setCol(1, up); mat.setCol(2, -front_vec); R = R * mat; } else { prev_up = up_vec; right = front_vec.cross(prev_up); up = right.cross(front_vec); right.normalize(); up.normalize(); R.setCol(0, right); R.setCol(1, up); R.setCol(2, -front_vec); } }
void DepthBuffer::setCamera(const mat44f& viewProjection,float znear,float zfar,float fov,vec3f cameraPos,vec3f cameraDir,vec3f cameraUp){ viewProjection_ = viewProjection; znear_ = znear; zfar_ = zfar; fov_ = fov; float aspect = float(size_.x)/float(size_.y); cameraPos_ = cameraPos; cameraDir_ = cameraDir; vec3f cameraRight = cameraDir.cross(cameraUp).normalize(); cameraUp = cameraRight.cross(cameraDir); // auto upLength = tanf(fov/2.0f)*znear; auto rightLength = upLength * (aspect); cameraRight = cameraRight*rightLength; cameraUp = cameraUp*upLength; cameraRight_ = vec4f(cameraRight.x,cameraRight.y,cameraRight.z,1); cameraUp_ = vec4f(cameraUp.x,cameraUp.y,cameraUp.z,1); aabbFrontFaceMask = calculateBoxVisibleFaceMask(cameraDir); }