00001 /* 00002 * cameraMgr.cpp 00003 * 00004 * Created on: Feb 19, 2012 00005 * Author: sushil 00006 */ 00007 00008 #include <OgreRoot.h> 00009 00010 #include <cameraMgr.h> 00011 #include <GraphicsInteractionManager.h> 00012 #include <iostream> 00013 #include "DEBUG.h" 00014 00015 00016 //#include <gfxMgr.h> 00017 00018 OgreGFX::CameraMgr::CameraMgr(GraphicsInteractionManager *gim): GFXMgr(gim) { //Engine *eng, Ogre::SceneManager *sm, Ogre::Camera* cam) : Mgr(eng) { 00019 // gfx = gim; 00020 camera = gim->mCamera; 00021 orbiting = false; 00022 zooming = false; 00023 topSpeed = 250; 00024 topRotation = 0.001; 00025 forward = false; 00026 back = false; 00027 left = false; 00028 right = false; 00029 up = false; 00030 down = false; 00031 yawLeft = false; 00032 yawRight = false; 00033 pitchUp = false; 00034 pitchDown = false; 00035 00036 fastMove = false; 00037 //camera->setPosition(0,0,0); 00038 //camera->setOrientation(Ogre::Quaternion::ZERO); 00039 sceneManager = gim->mSceneMgr; 00040 cameraNode = sceneManager->getRootSceneNode()->createChildSceneNode("cameraNode", Ogre::Vector3(0, 1000, 500)); 00041 pitchNode = cameraNode->createChildSceneNode("pitchNode"); 00042 //yawNode = yawNode->createChildSceneNode("pitchNode"); 00043 pitchNode->attachObject(camera); 00044 pitchNode->pitch(Ogre::Radian(-1.3)); 00045 //pitchNode->lookAt(Ogre::Vector3(0,0,0), Ogre::Node::TS_WORLD, Ogre::Vector3(0,0,0)); 00046 00047 mouseScrollTranslation = Ogre::Vector3::ZERO; 00048 mouseScrollTranslationSrc = Ogre::Vector3::ZERO; 00049 zeroTranslation = Ogre::Vector3::ZERO; 00050 lerpTime = 0.0f; 00051 timeToStop = 1.0f; 00052 mouseScrollSpeed = 10; 00053 maxHeight = 6000.0; 00054 minHeight = 1000.0; 00055 00056 } 00057 00058 //void OgreGFX::CameraMgr::init(){ 00059 // 00060 //} 00061 00062 00063 Ogre::Vector3 mylerp(Ogre::Vector3 &src, Ogre::Vector3 &dest, float fraction) { 00064 00065 return src + (dest - src)*fraction; 00066 00067 } 00068 00069 void OgreGFX::CameraMgr::injectKeyDown(const OIS::KeyEvent& evt) 00070 { 00071 00072 if (evt.key == OIS::KC_W ) forward = true; 00073 else if (evt.key == OIS::KC_S) back = true; 00074 else if (evt.key == OIS::KC_A) left = true; 00075 else if (evt.key == OIS::KC_D) right = true; 00076 else if (evt.key == OIS::KC_PGUP) up = true; 00077 else if (evt.key == OIS::KC_PGDOWN) down = true; 00078 else if (evt.key == OIS::KC_LSHIFT) fastMove = true; 00079 00080 else if (evt.key == OIS::KC_Q) yawLeft = true; 00081 else if (evt.key == OIS::KC_E) yawRight = true; 00082 else if (evt.key == OIS::KC_Z) pitchUp = true; 00083 else if (evt.key == OIS::KC_X) pitchDown = true; 00084 00085 } 00086 void OgreGFX::CameraMgr::injectKeyUp(const OIS::KeyEvent& evt){ 00087 00088 if (evt.key == OIS::KC_W ) forward = false; 00089 else if (evt.key == OIS::KC_S) back = false; 00090 else if (evt.key == OIS::KC_A) left = false; 00091 else if (evt.key == OIS::KC_D) right = false; 00092 else if (evt.key == OIS::KC_PGUP) up = false; 00093 else if (evt.key == OIS::KC_PGDOWN) down = false; 00094 else if (evt.key == OIS::KC_LSHIFT) fastMove = false; 00095 00096 else if (evt.key == OIS::KC_Q) yawLeft = false; 00097 else if (evt.key == OIS::KC_E) yawRight = false; 00098 else if (evt.key == OIS::KC_Z) pitchUp = false; 00099 else if (evt.key == OIS::KC_X) pitchDown = false; 00100 00101 00102 } 00103 00104 void OgreGFX::CameraMgr::handleMouseScrollCameraZoom(const OIS::MouseEvent &e) 00105 { 00106 float curPos = cameraNode->getPosition().y; 00107 float translation = -mouseScrollSpeed * e.state.Z.rel; 00108 float nextPos = curPos + translation; 00109 00110 DEBUG(std::cout << "Mouse height: " << curPos << "|" << translation << std::endl;) 00111 00112 if (nextPos < minHeight || nextPos > maxHeight) 00113 { 00114 if (translation < 0) translation = minHeight - curPos; 00115 else translation = maxHeight - curPos; 00116 } 00117 00118 mouseScrollTranslationSrc.y = translation; 00119 lerpTime = 0.0f; 00120 } 00121 00122 00123 bool OgreGFX::CameraMgr::frameRenderingQueued(const Ogre::FrameEvent& evt){ 00124 float speed = 0.0f; 00125 float yaw = 0.0f; 00126 float pitch = 0.0f; 00127 float rot; 00128 Ogre::Vector3 translation = Ogre::Vector3::ZERO; 00129 00130 speed = (fastMove ? topSpeed * 20.0f: topSpeed); 00131 00132 if (forward) translation.z = -speed; 00133 if (back) translation.z = speed; 00134 if (right) translation.x = speed; 00135 if (left) translation.x = -speed; 00136 if (up) translation.y = speed; 00137 if (down) translation.y = -speed; 00138 00139 cameraNode->translate(cameraNode->getOrientation() * translation * evt.timeSinceLastFrame); 00140 Ogre::Vector3 camp = cameraNode->getPosition(); 00141 if (camp.y < 5){ 00142 camp.y = 5; 00143 cameraNode->setPosition(camp); 00144 } 00145 00146 00147 if(lerpTime < timeToStop) { 00148 lerpTime += evt.timeSinceLastFrame; 00149 mouseScrollTranslation = mylerp(mouseScrollTranslationSrc, zeroTranslation, lerpTime/timeToStop); 00150 cameraNode->translate(cameraNode->getOrientation() * mouseScrollTranslation * evt.timeSinceLastFrame); 00151 } 00152 00153 rot = (fastMove ? topRotation * 10: topRotation); 00154 00155 if (yawLeft) yaw = rot; 00156 if (yawRight) yaw = -rot; 00157 if (pitchUp) pitch = rot; 00158 if (pitchDown)pitch = -rot; 00159 00160 cameraNode->yaw(Ogre::Radian(yaw)); 00161 pitchNode->pitch(Ogre::Radian(pitch)); 00162 00163 00164 00165 return true; 00166 00167 00168 00169 }