TextDisplay::TextDisplay() { text = new osgText::Text(); textGeode = new osg::Geode(); projectionMatrix = new osg::Projection; int x, y, width, height; ((osgViewer::GraphicsWindow* )getViewer()->getCamera()->getGraphicsContext())->getWindowRectangle(x, y, width, height); projectionMatrix->setMatrix(osg::Matrix::ortho2D(x, windowWidth, y, windowHeight)); viewMatrix = new osg::MatrixTransform; viewMatrix->setMatrix(osg::Matrix::identity()); viewMatrix->setReferenceFrame(osg::Transform::ABSOLUTE_RF); //root->addChild(projectionMatrix); addToSceneGraph(projectionMatrix); projectionMatrix->addChild(viewMatrix); viewMatrix->addChild(textGeode); textGeode->addDrawable(text); osg::StateSet* stateSet = new osg::StateSet(); textGeode->setStateSet(stateSet); stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF); stateSet->setRenderBinDetails(11, "RenderBin"); text->setAxisAlignment(osgText::Text::SCREEN); text->setPosition(osg::Vec3(0, height - 25, 0)); text->setCharacterSize(25); text->setFont("fonts/arial.ttf"); textGeode->setUpdateCallback(new TextGeodeCallback(this)); setDefaultText(); }
TemporaryText::TemporaryText(std::string text, osg::Vec3 position, float duration) { _transformNode = new osg::PositionAttitudeTransform(); _transformNode->setPosition(position); _timeRemaining = duration; _text = new osgText::Text(); _text->setText(text); _textGeode = new osg::Geode(); _transformNode->addChild(_textGeode); addToSceneGraph(_transformNode); _textGeode->addDrawable(_text); osg::StateSet* stateSet = new osg::StateSet(); _textGeode->setStateSet(stateSet); stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF); stateSet->setRenderBinDetails(11, "RenderBin"); _text->setAxisAlignment(osgText::Text::SCREEN); _text->setCharacterSize(.25); _text->setFont("fonts/arial.ttf"); _textGeode->setUpdateCallback(new OwnerUpdateCallback<TemporaryText>(this)); tempTexts.push_back(this); }
TiledMap::TiledMap(std::string mapFilename, Level* level) { _level = level; mapData = new Tmx::Map(); mapData->ParseFile(mapFilename); int sizeX = mapData->GetWidth(); int sizeY = mapData->GetHeight(); tileWidth = mapData->GetTileWidth(); tileHeight = mapData->GetTileHeight(); int numLayers = mapData->GetNumLayers(); int numObjectGroups = mapData->GetNumObjectGroups(); std::string imageFilename = mapData->GetTileset(0)->GetImage()->GetSource(); osg::Image* image = osgDB::readImageFile(imageFilename); if (!image) logError("Could not load tileset image."); geode = new osg::Geode(); // Set up the StateSet for this map. state = new osg::StateSet; osg::Texture2D* texture = new osg::Texture2D; texture->setImage(image); texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST); texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST); state->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON); // Apply the texture state->setRenderBinDetails(1, "transparent"); // Use a different rendering bin that gets rendered after the default one state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); // Tell OpenGL that this is a transparent bin, and thus everything should be rendered back-to-front for(int i = 0; i < numLayers; ++i) for(int y = 0; y < sizeY; ++y) for(int x = 0; x < sizeX; ++x) { int gid = mapData->GetLayer(i)->GetTileId(x, y); if(gid > 0) // Don't create a tile for blank ones (gid 0). geode->addDrawable( createTile(osg::Vec3(x, -y - 1, i), 1.0f, 1.0f, gid)); } for(int i = 0; i < numObjectGroups; ++i) { if(mapData->GetObjectGroup(i)->GetName() == "Collision") loadCollisionLayer(mapData->GetObjectGroup(i)); else if(mapData->GetObjectGroup(i)->GetName() == "Entities") loadEntityLayer(mapData->GetObjectGroup(i)); else { std::ostringstream stream; stream << "Unable to determine function of object layer \"" << mapData->GetObjectGroup(i)->GetName() << "\"."; logWarning(stream.str()); } } transformNode = new osg::PositionAttitudeTransform(); transformNode->addChild(geode); addToSceneGraph(transformNode); }
void BasePlayerImplementation::setCameraMode( unsigned int mode ) { switch ( mode ) { case Spheric: { _p_camera->setCameraOffsetPosition( _playerAttributes._camPosOffsetSpheric ); osg::Quat rot; rot.makeRotate( osg::DegreesToRadians( _playerAttributes._camRotOffsetSpheric.x() ), osg::Vec3f( 0.0f, 1.0f, 0.0f ), // roll osg::DegreesToRadians( _playerAttributes._camRotOffsetSpheric.y() ), osg::Vec3f( 1.0f, 0.0f, 0.0f ), // pitch osg::DegreesToRadians( _playerAttributes._camRotOffsetSpheric.z() ), osg::Vec3f( 0.0f, 0.0f, 1.0f ) // yaw ); _p_camera->setCameraOffsetRotation( rot ); // re-add to scenegraph ( actually we remove it first just to be on safe side ) removeFromSceneGraph(); addToSceneGraph(); } break; case Ego: { _p_camera->setCameraOffsetPosition( _playerAttributes._camPosOffsetEgo ); osg::Quat rot; rot.makeRotate( osg::DegreesToRadians( _playerAttributes._camRotOffsetEgo.x() ), osg::Vec3f( 0.0f, 1.0f, 0.0f ), // roll osg::DegreesToRadians( _playerAttributes._camRotOffsetEgo.y() ), osg::Vec3f( 1.0f, 0.0f, 0.0f ), // pitch osg::DegreesToRadians( _playerAttributes._camRotOffsetEgo.z() ), osg::Vec3f( 0.0f, 0.0f, 1.0f ) // yaw ); _p_camera->setCameraOffsetRotation( rot ); removeFromSceneGraph(); } break; default: assert( NULL && "requesting for an invalid camera mode" ); } _cameraMode = static_cast< CameraMode >( mode ); }
void EnMesh::enable( bool en ) { if ( _enable == en ) return; if ( _mesh.valid() ) { if ( en ) { addToSceneGraph(); } else { removeFromSceneGraph(); } _enable = en; } else { _enable = false; } }
void EnMesh::handleNotification( const yaf3d::EntityNotification& notification ) { // handle notifications, add and remove the transformation node to / from scenegraph on menu entring / leaving switch( notification.getId() ) { case YAF3D_NOTIFY_MENU_ENTER: { if ( _enable ) { if ( _usedInMenu ) { addToSceneGraph(); } else { removeFromSceneGraph(); } } } break; case YAF3D_NOTIFY_MENU_LEAVE: { if ( _enable ) { if ( _usedInMenu ) { removeFromSceneGraph(); } else { addToSceneGraph(); } } } break; //! Note: by default the level manager re-adds persistent entity transformation nodes to its entity group while unloading a level. // thus we have to remove shadow nodes from that entity group on unloading a level; addToSceneGraph() does this job. case YAF3D_NOTIFY_ENTITY_TRANSNODE_CHANGED: { if ( _usedInMenu ) { // this method really removes the node from all its parents yaf3d::EntityManager::get()->removeFromScene( this ); // now add it to scene graph again addToSceneGraph(); } } break; case YAF3D_NOTIFY_ENTITY_ATTRIBUTE_CHANGED: { // re-setup mesh if ( _mesh.valid() ) removeFromTransformationNode( _mesh.get() ); _mesh = setupMesh(); if ( _mesh.valid() && _enable ) addToTransformationNode( _mesh.get() ); // if the attribute changes then we enable rendering regardless of current state // this method really removes the node from all its parents yaf3d::EntityManager::get()->removeFromScene( this ); // now add it to scene graph again addToSceneGraph(); } break; case YAF3D_NOTIFY_UNLOAD_LEVEL: if ( !_usedInMenu ) removeFromSceneGraph(); break; // if used in menu then this entity is persisten, so we have to trigger its deletion on shutdown case YAF3D_NOTIFY_SHUTDOWN: { removeFromTransformationNode( _mesh.get() ); if ( _usedInMenu ) yaf3d::EntityManager::get()->deleteEntity( this ); } break; default: ; } }
void PlayerImplStandalone::handleNotification( const yaf3d::EntityNotification& notification ) { // handle some notifications switch( notification.getId() ) { case YAF3D_NOTIFY_MENU_ENTER: getChatManager()->show( false ); if ( _p_inputHandler ) _p_inputHandler->setMenuEnabled( true ); // reset player's movement and sound if ( _p_playerPhysics ) _p_playerPhysics->stopMovement(); if ( _p_playerAnimation ) _p_playerAnimation->animIdle(); if ( _p_playerSound ) _p_playerSound->stopPlayingAll(); // very important: diable the camera when we enter menu! if ( _p_camera ) _p_camera->setEnable( false ); // players are all rendered in menu, regardless of their camera mode if ( _cameraMode == Ego ) addToSceneGraph(); break; case YAF3D_NOTIFY_MENU_LEAVE: { getChatManager()->show( true ); if ( _p_inputHandler ) { _p_inputHandler->setMenuEnabled( false ); // refresh our configuration settings getConfiguration(); } // very important: enable the camera when we leave menu! if ( _p_camera ) _p_camera->setEnable( true ); // if we are in ego mode then disable player avatar rendering if ( _cameraMode == Ego ) removeFromSceneGraph(); } break; case YAF3D_NOTIFY_SHUTDOWN: case YAF3D_NOTIFY_UNLOAD_LEVEL: { // destroy the chat manager destroyChatManager(); } break; default: ; } }