void Camera::moveTarget(float dx, float dy, float dz) { // Move the target position mTarget.x += dx; mTarget.y += dy; mTarget.z += dz; buildViewMatrix(); }
void Camera::move(float dx, float dy, float dz) { // Move the camera position mPos.x += dx; mPos.y += dy; mPos.z += dz; buildViewMatrix(); }
void GCamera::Update() { XMVECTOR m_moveCommand= GMathFV(XMFLOAT3(0,0,0)); XMVECTOR forward= GMathFV(mLook); XMVECTOR right= GMathFV(mRight); if (m_forward) m_moveCommand += forward; if (m_back) m_moveCommand -= forward; if (m_left) m_moveCommand -= right ; if (m_right) m_moveCommand += right ; XMMATRIX R = XMMatrixRotationAxis(GMathFV(mRight), m_pitch); XMVECTOR vecLookW = XMVector3TransformCoord(GMathFV(mLook), R); XMVECTOR vecUpW = XMVector3TransformCoord(GMathFV(mUp), R); R = XMMatrixRotationY(m_yaw); XMVECTOR vecRightW = XMVector3TransformCoord(GMathFV(mRight), R); vecUpW = XMVector3TransformCoord(vecUpW, R); vecLookW = XMVector3TransformCoord(vecLookW, R); mRight = GMathVF(vecRightW); mUp = GMathVF(vecUpW); mLook = GMathVF(vecLookW); // make sure that 45 degree cases are not faster buildViewMatrix(); mPosition = GMathVF(m_moveCommand*mSpeed + GMathFV(mPosition)); m_pitch = 0; m_yaw = 0; m_forward = false; m_back = false; m_right = false; m_left = false; }
GCamera::GCamera(void) { mPosition = XMFLOAT3(0.0f, 0.0f, -1.0f); mTarget = XMFLOAT3(0.0f, 0.0f, 0.0f); mUp = GMathVF(GMathFV(mPosition) + GMathFV(XMFLOAT3(0, 1, 0))); //this->lookatViewMatrix(); mAngle = 0.5f; mClientWidth = 0.0f; mClientHeight = 0.0f; mNearest = 0.0f; mFarthest = 1000.0f; mSpeed = 0.5f; mRight = XMFLOAT3(1, 0, 0); mUp = XMFLOAT3(0, 1, 0); mLook = XMFLOAT3(0, 0, 1); XMStoreFloat4x4(&mView, XMMatrixIdentity()); XMStoreFloat4x4(&mProj, XMMatrixIdentity()); XMStoreFloat4x4(&mOrtho, XMMatrixIdentity()); buildViewMatrix(); }
//----------------------------------------------------------------------- void LiSPSMShadowCameraSetup::getShadowCamera (const SceneManager *sm, const Camera *cam, const Viewport *vp, const Light *light, Camera *texCam) const { // check availability - viewport not needed OgreAssert(sm != NULL, "SceneManager is NULL"); OgreAssert(cam != NULL, "Camera (viewer) is NULL"); OgreAssert(light != NULL, "Light is NULL"); OgreAssert(texCam != NULL, "Camera (texture) is NULL"); mLightFrustumCameraCalculated = false; // calculate standard shadow mapping matrix Matrix4 LView, LProj; calculateShadowMappingMatrix(*sm, *cam, *light, &LView, &LProj, NULL); // build scene bounding box const VisibleObjectsBoundsInfo& visInfo = sm->getShadowCasterBoundsInfo(light); AxisAlignedBox sceneBB = visInfo.aabb; sceneBB.merge(sm->getVisibleObjectsBoundsInfo(cam).aabb); sceneBB.merge(cam->getDerivedPosition()); // in case the sceneBB is empty (e.g. nothing visible to the cam) simply // return the standard shadow mapping matrix if (sceneBB.isNull()) { texCam->setCustomViewMatrix(true, LView); texCam->setCustomProjectionMatrix(true, LProj); return; } // calculate the intersection body B mPointListBodyB.reset(); calculateB(*sm, *cam, *light, sceneBB, &mPointListBodyB); // in case the bodyB is empty (e.g. nothing visible to the light or the cam) // simply return the standard shadow mapping matrix if (mPointListBodyB.getPointCount() == 0) { texCam->setCustomViewMatrix(true, LView); texCam->setCustomProjectionMatrix(true, LProj); return; } // transform to light space: y -> -z, z -> y LProj = msNormalToLightSpace * LProj; // calculate LVS so it does not need to be calculated twice // calculate the body L \cap V \cap S to make sure all returned points are in // front of the camera calculateLVS(*sm, *cam, *light, sceneBB, &mPointListBodyLVS); // fetch the viewing direction const Vector3 viewDir = getLSProjViewDir(LProj * LView, *cam, mPointListBodyLVS); // The light space will be rotated in such a way, that the projected light view // always points upwards, so the up-vector is the y-axis (we already prepared the // light space for this usage).The transformation matrix is set up with the // following parameters: // - position is the origin // - the view direction is the calculated viewDir // - the up vector is the y-axis LProj = buildViewMatrix(Vector3::ZERO, viewDir, Vector3::UNIT_Y) * LProj; // calculate LiSPSM projection LProj = calculateLiSPSM(LProj * LView, mPointListBodyB, mPointListBodyLVS, *sm, *cam, *light) * LProj; // map bodyB to unit cube LProj = transformToUnitCube(LProj * LView, mPointListBodyB) * LProj; // transform from light space to normal space: y -> z, z -> -y LProj = msLightSpaceToNormal * LProj; // LView = Lv^-1 // LProj = Switch_{-ls} * FocusBody * P * L_r * Switch_{ls} * L_p texCam->setCustomViewMatrix(true, LView); texCam->setCustomProjectionMatrix(true, LProj); }
void DiFocusedShadowPolicy::calculateShadowMappingMatrix(const DiSceneManager& sm, const DiCamera& cam, const DiLight& light, DiMat4 *out_view, DiMat4 *out_proj, DiCamera *out_cam) const { DiVec3 lightPos = light.GetDerivedPosition(); DiVec3 lightDir = light.GetDerivedDirection(); // get the shadow frustum's far distance float shadowDist = sm.GetShadowFarDistance(); if (!shadowDist) { // need a shadow distance, make one up shadowDist = cam.GetNearClipDistance() * 3000; } float shadowOffset = shadowDist * sm.GetShadowDirLightTextureOffset(); if (light.GetType() == LIGHT_DIRECTIONAL) { // generate view matrix if requested if (out_view != NULL) { DiVec3 pos; #if 0 if (sm.getCameraRelativeRendering()) { pos = DiVec3::ZERO; } else #endif { pos = cam.GetDerivedPosition(); } *out_view = buildViewMatrix(pos, lightDir, cam.GetDerivedUp()); } // generate projection matrix if requested if (out_proj != NULL) { *out_proj = DiMat4::getScale(1, 1, -1); //*out_proj = DiMat4::IDENTITY; } // set up camera if requested if (out_cam != NULL) { out_cam->SetProjectionType(PT_ORTHOGRAPHIC); out_cam->SetDirection(lightDir); out_cam->SetPosition(cam.GetDerivedPosition()); out_cam->SetFOVy(DiDegree(90)); out_cam->SetNearClipDistance(shadowOffset); } } else if (light.GetType() == LIGHT_SPOT) { const DiSpotLight* spot = static_cast<const DiSpotLight*>(&light); // generate view matrix if requested if (out_view != NULL) { *out_view = buildViewMatrix(light.GetDerivedPosition(), light.GetDerivedDirection(), cam.GetDerivedUp()); } // generate projection matrix if requested if (out_proj != NULL) { // set FOV slightly larger than spotlight range mTempFrustum->SetFOVy(DiMath::Clamp<DiRadian>(spot->GetOuterAngle() * 1.2, DiRadian(0), DiRadian(DiMath::PI/2.0f))); mTempFrustum->SetNearClipDistance(light.DeriveShadowNearClipDistance(&cam)); mTempFrustum->SetFarClipDistance(light.DeriveShadowFarClipDistance(&cam)); *out_proj = mTempFrustum->GetProjectionMatrix(); } // set up camera if requested if (out_cam != NULL) { out_cam->SetProjectionType(PT_PERSPECTIVE); out_cam->SetDirection(light.GetDerivedDirection()); out_cam->SetPosition(light.GetDerivedPosition()); out_cam->SetFOVy(DiMath::Clamp<DiRadian>(spot->GetOuterAngle() * 1.2, DiRadian(0), DiRadian(DiMath::PI/2.0f))); out_cam->SetNearClipDistance(light.DeriveShadowNearClipDistance(&cam)); out_cam->SetFarClipDistance(light.DeriveShadowFarClipDistance(&cam)); } } #if 0 else if (light.GetType() == LIGHT_POINT) { // target analogue to the default shadow textures // Calculate look at position // We want to look at a spot shadowOffset away from near plane // 0.5 is a little too close for angles DiVec3 target = cam.GetDerivedPosition() + (cam.GetDerivedDirection() * shadowOffset); lightDir = target - lightPos; lightDir.normalise(); // generate view matrix if requested if (out_view != NULL) { *out_view = buildViewMatrix(lightPos, lightDir, cam.GetDerivedUp()); } // generate projection matrix if requested if (out_proj) { // set FOV to 120 degrees mTempFrustum->SetFOVy(DiDegree(120)); mTempFrustum->SetNearClipDistance(light._deriveShadowNearClipDistance(&cam)); mTempFrustum->SetFarClipDistance(light._deriveShadowFarClipDistance(&cam)); *out_proj = mTempFrustum->GetProjectionMatrix(); } // set up camera if requested if (out_cam) { out_cam->SetProjectionType(PT_PERSPECTIVE); out_cam->SetDirection(lightDir); out_cam->SetPosition(lightPos); out_cam->SetFOVy(DiDegree(120)); out_cam->SetNearClipDistance(light._deriveShadowNearClipDistance(&cam)); out_cam->SetFarClipDistance(light._deriveShadowFarClipDistance(&cam)); } } #endif }
void DiFocusedShadowPolicy::getShadowCamera (const DiSceneManager *sm, const DiCamera *cam, const DiViewport *vp, const DiLight *light, DiCamera *texCam, size_t iteration) const { // check availability - viewport not needed DI_ASSERT_MESSAGE(sm != NULL, "SceneManager is NULL"); DI_ASSERT_MESSAGE(cam != NULL, "Camera (viewer) is NULL"); DI_ASSERT_MESSAGE(light != NULL, "Light is NULL"); DI_ASSERT_MESSAGE(texCam != NULL, "Camera (texture) is NULL"); mLightFrustumCameraCalculated = false; texCam->SetNearClipDistance(light->DeriveShadowNearClipDistance(cam)); texCam->SetFarClipDistance(light->DeriveShadowFarClipDistance(cam)); // calculate standard shadow mapping matrix DiMat4 LView, LProj; calculateShadowMappingMatrix(*sm, *cam, *light, &LView, &LProj, NULL); // build scene bounding box auto& visInfo = texCam->GetVisBoundsInfo(); DiAABB sceneBB = visInfo.aabb; DiAABB receiverAABB = cam->GetVisBoundsInfo().receiverAabb; sceneBB.Merge(receiverAABB); sceneBB.Merge(cam->GetDerivedPosition()); // in case the sceneBB is empty (e.g. nothing visible to the cam) simply // return the standard shadow mapping matrix if (sceneBB.IsNull()) { texCam->SetCustomViewMatrix(true, LView); texCam->SetCustomProjectionMatrix(true, LProj); return; } // calculate the intersection body B mPointListBodyB.reset(); calculateB(*sm, *cam, *light, sceneBB, receiverAABB, &mPointListBodyB); // in case the bodyB is empty (e.g. nothing visible to the light or the cam) // simply return the standard shadow mapping matrix if (mPointListBodyB.getPointCount() == 0) { texCam->SetCustomViewMatrix(true, LView); texCam->SetCustomProjectionMatrix(true, LProj); return; } // transform to light space: y -> -z, z -> y LProj = msNormalToLightSpace * LProj; // calculate LVS so it does not need to be calculated twice // calculate the body L \cap V \cap S to make sure all returned points are in // front of the camera mPointListBodyLVS.reset(); calculateLVS(*sm, *cam, *light, sceneBB, &mPointListBodyLVS); // fetch the viewing direction const DiVec3 viewDir = getLSProjViewDir(LProj * LView, *cam, mPointListBodyLVS); // The light space will be rotated in such a way, that the projected light view // always points upwards, so the up-vector is the y-axis (we already prepared the // light space for this usage).The transformation matrix is set up with the // following parameters: // - position is the origin // - the view direction is the calculated viewDir // - the up vector is the y-axis LProj = buildViewMatrix(DiVec3::ZERO, viewDir, DiVec3::UNIT_Y) * LProj; // map bodyB to unit cube LProj = transformToUnitCube(LProj * LView, mPointListBodyB) * LProj; // transform from light space to normal space: y -> z, z -> -y LProj = msLightSpaceToNormal * LProj; // set the two custom matrices texCam->SetCustomViewMatrix(true, LView); texCam->SetCustomProjectionMatrix(true, LProj); }
//----------------------------------------------------------------------- void FocusedShadowCameraSetup::calculateShadowMappingMatrix(const SceneManager& sm, const Camera& cam, const Light& light, Matrix4 *out_view, Matrix4 *out_proj, Camera *out_cam) const { // get the shadow frustum's far distance Real shadowDist = light.getShadowFarDistance(); if (!shadowDist) { // need a shadow distance, make one up shadowDist = cam.getNearClipDistance() * 3000; } Real shadowOffset = shadowDist * sm.getShadowDirLightTextureOffset(); if (light.getType() == Light::LT_DIRECTIONAL) { // generate view matrix if requested if (out_view != NULL) { Vector3 pos; if (sm.getCameraRelativeRendering()) { pos = Vector3::ZERO; } else { pos = cam.getDerivedPosition(); } *out_view = buildViewMatrix(pos, light.getDerivedDirection(), cam.getDerivedUp()); } // generate projection matrix if requested if (out_proj != NULL) { *out_proj = Matrix4::getScale(1, 1, -1); //*out_proj = Matrix4::IDENTITY; } // set up camera if requested if (out_cam != NULL) { out_cam->setProjectionType(PT_ORTHOGRAPHIC); out_cam->setDirection(light.getDerivedDirection()); out_cam->setPosition(cam.getDerivedPosition()); out_cam->setFOVy(Degree(90)); out_cam->setNearClipDistance(shadowOffset); } } else if (light.getType() == Light::LT_POINT) { // target analogue to the default shadow textures // Calculate look at position // We want to look at a spot shadowOffset away from near plane // 0.5 is a little too close for angles Vector3 target = cam.getDerivedPosition() + (cam.getDerivedDirection() * shadowOffset); Vector3 lightDir = target - light.getDerivedPosition(); lightDir.normalise(); // generate view matrix if requested if (out_view != NULL) { *out_view = buildViewMatrix(light.getDerivedPosition(), lightDir, cam.getDerivedUp()); } // generate projection matrix if requested if (out_proj != NULL) { // set FOV to 120 degrees mTempFrustum->setFOVy(Degree(120)); mTempFrustum->setNearClipDistance(light._deriveShadowNearClipDistance(&cam)); mTempFrustum->setFarClipDistance(light._deriveShadowFarClipDistance(&cam)); *out_proj = mTempFrustum->getProjectionMatrix(); } // set up camera if requested if (out_cam != NULL) { out_cam->setProjectionType(PT_PERSPECTIVE); out_cam->setDirection(lightDir); out_cam->setPosition(light.getDerivedPosition()); out_cam->setFOVy(Degree(120)); out_cam->setNearClipDistance(light._deriveShadowNearClipDistance(&cam)); out_cam->setFarClipDistance(light._deriveShadowFarClipDistance(&cam)); } } else if (light.getType() == Light::LT_SPOTLIGHT) { // generate view matrix if requested if (out_view != NULL) { *out_view = buildViewMatrix(light.getDerivedPosition(), light.getDerivedDirection(), cam.getDerivedUp()); } // generate projection matrix if requested if (out_proj != NULL) { // set FOV slightly larger than spotlight range mTempFrustum->setFOVy(Ogre::Math::Clamp<Radian>(light.getSpotlightOuterAngle() * 1.2, Radian(0), Radian(Math::PI/2.0f))); mTempFrustum->setNearClipDistance(light._deriveShadowNearClipDistance(&cam)); mTempFrustum->setFarClipDistance(light._deriveShadowFarClipDistance(&cam)); *out_proj = mTempFrustum->getProjectionMatrix(); } // set up camera if requested if (out_cam != NULL) { out_cam->setProjectionType(PT_PERSPECTIVE); out_cam->setDirection(light.getDerivedDirection()); out_cam->setPosition(light.getDerivedPosition()); out_cam->setFOVy(Ogre::Math::Clamp<Radian>(light.getSpotlightOuterAngle() * 1.2, Radian(0), Radian(Math::PI/2.0f))); out_cam->setNearClipDistance(light._deriveShadowNearClipDistance(&cam)); out_cam->setFarClipDistance(light._deriveShadowFarClipDistance(&cam)); } } }
Camera::Camera(const Vector3 &position, const Vector3 &rotation) { this->position = position; this->rotation = rotation; buildViewMatrix(); }
void Camera::update() { buildViewMatrix(); buildProjectionMatrix(); }