const glm::mat4& CTransformer::getModelViewProjectionMatrix() const { if (m_modelViewProjection.m_matrixDirty) { m_modelViewProjection.m_matrix = getViewProjectionMatrix() * getModelMatrix(); m_modelViewProjection.m_matrixDirty = false; } return m_modelViewProjection.m_matrix; }
const Matrix& Camera::getInverseViewProjectionMatrix() const { if (_dirtyBits & CAMERA_DIRTY_INV_VIEW_PROJ) { getViewProjectionMatrix().invert(&_inverseViewProjection); _dirtyBits &= ~CAMERA_DIRTY_INV_VIEW_PROJ; } return _inverseViewProjection; }
const Frustum& Camera::getFrustum() const { if (_dirtyBits & CAMERA_DIRTY_BOUNDS) { // Update our bounding frustum from our view projection matrix. _bounds.set(getViewProjectionMatrix()); _dirtyBits &= ~CAMERA_DIRTY_BOUNDS; } return _bounds; }
const glm::mat4& Node::getViewProjectionMatrix() const { Scene* scene = getScene(); auto camera = scene ? scene->getActiveCamera() : nullptr; if( camera ) { return camera->getViewProjectionMatrix(); } else { static const glm::mat4 identity{1.0f}; return identity; } }
Vec2 Camera::projectGL(const Vec3& src) const { Vec2 screenPos; auto viewport = Director::getInstance()->getWinSize(); Vec4 clipPos; getViewProjectionMatrix().transformVector(Vec4(src.x, src.y, src.z, 1.0f), &clipPos); CCASSERT(clipPos.w != 0.0f, "clipPos.w can't be 0.0f!"); float ndcX = clipPos.x / clipPos.w; float ndcY = clipPos.y / clipPos.w; screenPos.x = (ndcX + 1.0f) * 0.5f * viewport.width; screenPos.y = (ndcY + 1.0f) * 0.5f * viewport.height; return screenPos; }
uint32_t rSimpleMesh::getMatrix( rMat4f **_mat, rObjectBase::MATRIX_TYPES _type ) { switch ( _type ) { case SCALE: *_mat = getScaleMatrix(); return 0; case ROTATION: *_mat = getRotationMatrix(); return 0; case TRANSLATION: *_mat = getTranslationMatrix(); return 0; case CAMERA_MATRIX: *_mat = getViewProjectionMatrix(); return 0; case MODEL_MATRIX: *_mat = getModelMatrix(); return 0; case VIEW_MATRIX: *_mat = getViewMatrix(); return 0; case PROJECTION_MATRIX: *_mat = getProjectionMatrix(); return 0; case MODEL_VIEW_MATRIX: *_mat = getModelViewMatrix(); return 0; case MODEL_VIEW_PROJECTION: *_mat = getModelViewProjectionMatrix(); return 0; case NORMAL_MATRIX: break; } return INDEX_OUT_OF_RANGE; }
void C3DCamera::project(const C3DViewport* viewport, Vector3* src, Vector2* dst) { assert(src); assert(dst); // Transform the point to clip-space. Vector4 clipPos; getViewProjectionMatrix().transformVector(Vector4(src->x, src->y, src->z, 1.0f), &clipPos); // Compute normalized device coordinates. assert(clipPos.w != 0.0f); float ndcX = clipPos.x / clipPos.w; float ndcY = clipPos.y / clipPos.w; // Compute screen coordinates by applying our viewport transformation. dst->x = viewport->x + (ndcX + 1.0f) * 0.5f * viewport->width; dst->y = viewport->y + (1.0f - (ndcY + 1.0f) * 0.5f) * viewport->height; }
void Camera::unproject(const Size& viewport, Vec3* src, Vec3* dst) const { assert(dst); Vec4 screen(src->x / viewport.width, ((viewport.height - src->y)) / viewport.height, src->z, 1.0f); screen.x = screen.x * 2.0f - 1.0f; screen.y = screen.y * 2.0f - 1.0f; screen.z = screen.z * 2.0f - 1.0f; getViewProjectionMatrix().getInversed().transformVector(screen, &screen); if (screen.w != 0.0f) { screen.x /= screen.w; screen.y /= screen.w; screen.z /= screen.w; } dst->set(screen.x, screen.y, screen.z); }
void Camera::unprojectGL(const Size& viewport, const Vec3* src, Vec3* dst) const { CCASSERT(src && dst, "vec3 can not be null"); Vec4 screen(src->x / viewport.width, src->y / viewport.height, src->z, 1.0f); screen.x = screen.x * 2.0f - 1.0f; screen.y = screen.y * 2.0f - 1.0f; screen.z = screen.z * 2.0f - 1.0f; getViewProjectionMatrix().getInversed().transformVector(screen, &screen); if (screen.w != 0.0f) { screen.x /= screen.w; screen.y /= screen.w; screen.z /= screen.w; } dst->set(screen.x, screen.y, screen.z); }
//---------------------------------------------------------------------- // //---------------------------------------------------------------------- void Camera::blur( double duration, lnFloat power, lnFloat scale, const LVector3& center, bool center_is_3d ) { LVector3 pos; if ( mProjection2D ) { LVector3::transformCoord( &pos, center, getProjectionMatrix() ); } else { LVector3::transformCoord( &pos, center, getViewProjectionMatrix() ); } mScreenEffect->blur( duration, power, scale, pos ); }
void Camera::project(const Viewport* viewport, const Vector3& position, float* x, float* y, float* depth) { // Determine viewport coords to use. float vpx, vpy, vpw, vph; if (viewport) { vpx = viewport->getX(); vpy = viewport->getY(); vpw = viewport->getWidth(); vph = viewport->getHeight(); } else { vpx = 0; vpy = 0; vpw = Game::getInstance()->getWidth(); vph = Game::getInstance()->getHeight(); } // Transform the point to clip-space. Vector4 clipPos; getViewProjectionMatrix().transformVector(Vector4(position.x, position.y, position.z, 1.0f), &clipPos); // Compute normalized device coordinates. float ndcX = clipPos.x / clipPos.w; float ndcY = clipPos.y / clipPos.w; // Compute screen coordinates by applying our viewport transformation. *x = vpx + (ndcX + 1.0f) * 0.5f * vpw; *y = vpy + (1.0f - (ndcY + 1.0f) * 0.5f) * vph; if (depth) { float ndcZ = clipPos.z / clipPos.w; *depth = ndcZ + 1.0f / 2.0f; } }
// Methods // ----------------------------------------------------------------------------- void //Camera::computeFrustum(double aspect) Camera::computeFrustum () { // The planes array contains six plane equations of the form (Ax+By+Cz+D=0), the first four values are (A,B,C,D) // which repeats for each of the planes. The planes are given in the following order: -x,+x,-y,+y,-z,+z. //camera_->GetFrustumPlanes(aspect, frustum_); pcl::visualization::getViewFrustum (getViewProjectionMatrix (), frustum_); // vtkSmartPointer<vtkHull> hull = vtkSmartPointer<vtkHull>::New (); // vtkSmartPointer<vtkPlanes> planes = vtkSmartPointer<vtkPlanes>::New (); // vtkSmartPointer<vtkPolyData> hullData = vtkSmartPointer<vtkPolyData>::New (); // // planes->SetFrustumPlanes (frustum_); // hull->SetPlanes (planes); // hull->GenerateHull (hullData, -200, 200, -200, 200, -200, 200); // // vtkSmartPointer<vtkPolyDataMapper> hull_mapper = static_cast<vtkPolyDataMapper*> (hull_actor_->GetMapper ()); // // hull_mapper->SetInputData(hullData); // // hull_actor_->SetMapper (hull_mapper); }
void Camera::setAdditionalProjection(const Mat4& mat) { _projection = mat * _projection; getViewProjectionMatrix(); }
glm::mat4 Node::getWorldViewProjectionMatrix() const { // Always re-calculate worldViewProjection matrix since it's extremely difficult // to track whether the camera has changed (it may frequently change every frame). return getViewProjectionMatrix() * getWorldMatrix(); }