//----------------------------------------------- // updateCamera : // Update the camera (position, target, roll, fov) //----------------------------------------------- void CSceneParser::updateCamera(double timeInSec) { // If there is a play list for the camera. if(_PlayList) { // Get the Id of the animation in the slot 0. uint idAnim = _PlayList->getAnimation(0); if(idAnim != UPlayList::empty) { UAnimation *animation = _AnimationSet->getAnimation(idAnim); if(animation) { // Get Camera informations from the animation (Pos, Target, Roll). UTrack* trackRollCam = animation->getTrackByName("Camera.roll"); UTrack* trackFovCam = animation->getTrackByName("Camera.fov"); UTrack* trackPosCam = animation->getTrackByName("Camera.PathPos"); UTrack* trackPosTarget = animation->getTrackByName("Camera.Target.PathPos"); if(trackPosCam && trackPosTarget) { float rollCam = 0.f; CVector posCam; CVector posTarget; float difTime = (float)(timeInSec-_TimeStart); if(trackRollCam) trackRollCam->interpolate(difTime, rollCam); trackPosCam->interpolate(difTime, posCam); trackPosTarget->interpolate(difTime, posTarget); // Update camera transformations. UCamera cam = Scene->getCam(); if(cam) { cam->setTransformMode(UTransformable::RotQuat); cam->lookAt(posCam, posTarget, rollCam); if(trackFovCam) { float fov; trackFovCam->interpolate(difTime, fov); CFrustum fr= cam->getFrustum(); // change only the fov cam->setPerspective(fov, fr.getAspectRatio(), fr.Near, fr.Far); } } // Update camera transformations for the Root. cam = SceneRoot->getCam(); if(cam) { cam->setTransformMode(UTransformable::RotQuat); cam->lookAt(posCam, posTarget, rollCam); if(trackFovCam) { float fov; trackFovCam->interpolate(difTime, fov); CFrustum fr= cam->getFrustum(); // change only the fov cam->setPerspective(fov, fr.getAspectRatio(), fr.Near, fr.Far); } } } } } } }// updateCamera //
void initCamera() { if (ConfigFile->getVar("HMDEnable").asBool()) { std::vector<NL3D::CStereoDeviceInfo> devices; IStereoDisplay::listDevices(devices); for (std::vector<NL3D::CStereoDeviceInfo>::iterator it(devices.begin()), end(devices.end()); it != end; ++it) { std::stringstream name; name << std::string("[") << it->Serial << "] [" << IStereoDisplay::getLibraryName(it->Library) << " - " << it->Manufacturer << " - " << it->ProductName << "]"; nlinfo("Stereo Display: %s", name.str().c_str()); } CStereoDeviceInfo *deviceInfo = NULL; std::string hmdDeviceCfg = ConfigFile->getVar("HMDDevice").asString(); if (hmdDeviceCfg == std::string("Auto") && devices.begin() != devices.end()) { for (std::vector<NL3D::CStereoDeviceInfo>::iterator it(devices.begin()), end(devices.end()); it != end; ++it) { if (it->AllowAuto) { deviceInfo = &devices[0]; } } } else { std::string hmdDeviceId = ConfigFile->getVar("HMDDeviceId").asString(); for (std::vector<NL3D::CStereoDeviceInfo>::iterator it(devices.begin()), end(devices.end()); it != end; ++it) { std::stringstream name; name << IStereoDisplay::getLibraryName(it->Library) << " - " << it->Manufacturer << " - " << it->ProductName; if (name.str() == hmdDeviceCfg) deviceInfo = &(*it); if (hmdDeviceId == it->Serial) break; } } if (deviceInfo) { nlinfo("Create VR stereo display device"); StereoDisplay = IStereoDisplay::createDevice(*deviceInfo); if (StereoDisplay) { if (deviceInfo->Class == CStereoDeviceInfo::StereoHMD) { nlinfo("Stereo display device is a HMD"); StereoHMD = static_cast<IStereoHMD *>(StereoDisplay); StereoHMD->setScale(3.0f); // snowballs is about 4 units per meter } StereoDisplay->setDriver(Driver); // move after driver creation, move stereodisplay before driver creation StereoDisplay->attachToDisplay(); } } } IStereoDisplay::releaseUnusedLibraries(); // Set up directly the camera Camera = Scene->getCam(); Camera.setTransformMode (UTransformable::DirectMatrix); Camera.setPerspective((float)Pi/2.f, ConfigFile->getVar("ScreenWidth").asFloat() / ConfigFile->getVar("ScreenHeight").asFloat(), 0.1f, 1000.f); Camera.lookAt (CVector(ConfigFile->getVar("StartPoint").asFloat(0), ConfigFile->getVar("StartPoint").asFloat(1), ConfigFile->getVar("StartPoint").asFloat(2)), CVectorD (0,0,0)); CamCollisionEntity = VisualCollisionManager->createEntity(); CamCollisionEntity->setCeilMode(true); // Create the snowing particle system Snow = Scene->createInstance("snow.ps"); // And setup it Snow.setTransformMode (UTransformable::DirectMatrix); // // Setup the sky scene // // -- -- not sure what the sky has to do with the camera SkyScene = Driver->createScene(false); SkyCamera = SkyScene->getCam (); SkyCamera.setTransformMode (UTransformable::DirectMatrix); // Set the very same frustum as the main camera SkyCamera.setFrustum (Camera.getFrustum ()); Sky = SkyScene->createInstance("sky.shape"); Sky.setTransformMode (UTransformable::DirectMatrix); Sky.setMatrix(CMatrix::Identity); }
void CObjectViewer::init(nlWindow wnd, uint16 w, uint16 h) { //H_AUTO2 nldebug("CObjectViewer::init"); // load and set remap extensions from config //Modules::config().configRemapExtensions(); // load and set search paths from config //Modules::config().configSearchPaths(); // set background color from config Modules::config().setAndCallback("BackgroundColor", CConfigCallback(this, &CObjectViewer::cfcbBackgroundColor)); // set graphics driver from config Modules::config().setAndCallback("GraphicsDriver",CConfigCallback(this,&CObjectViewer::cfcbGraphicsDriver)); // create the driver nlassert(!_Driver); _Driver = UDriver::createDriver(NULL, _Direct3D, NULL); nlassert(_Driver); // initialize the window with config file values _Driver->setDisplay(wnd, NL3D::UDriver::CMode(w, h, 32)); _Light = ULight::createLight(); // set mode of the light _Light->setMode(ULight::DirectionalLight); // set position of the light _Light->setPosition(CVector(-20.f, 30.f, 10.f)); // white light _Light->setAmbiant(CRGBA(255, 255, 255)); // set and enable the light _Driver->setLight(0, *_Light); _Driver->enableLight(0); // Create a scene _Scene = _Driver->createScene(true); _PlayListManager = _Scene->createPlayListManager(); _Scene->enableLightingSystem(true); // create the camera UCamera camera = _Scene->getCam(); camera.setTransformMode (UTransformable::DirectMatrix); setSizeViewport(w, h); // camera will look at entities updateCamera(0,0,0); NLMISC::CVector hotSpot=NLMISC::CVector(0,0,0); _MouseListener = _Driver->create3dMouseListener(); _MouseListener->setMatrix(Modules::objView().getScene()->getCam().getMatrix()); _MouseListener->setFrustrum(Modules::objView().getScene()->getCam().getFrustum()); _MouseListener->setHotSpot(hotSpot); _MouseListener->setMouseMode(U3dMouseListener::edit3d); }