//----------------------------------------------------------------------------------------- bool CBillboardSetEditor::load(bool async) { if(mLoaded->get()) return true; if(CNodeEditor::load()) { mBillboardSetHandle = mOgitorsRoot->GetSceneManager()->createBillboardSet(mName->get()); mHandle->attachObject(mBillboardSetHandle); mBillboardSetHandle->setBillboardType((Ogre::BillboardType)mBillboardType->get()); mBillboardSetHandle->setSortingEnabled(mSorting->get()); mBillboardSetHandle->setBillboardOrigin((Ogre::BillboardOrigin)mOrigin->get()); mBillboardSetHandle->setBillboardRotationType((Ogre::BillboardRotationType)mRotation->get()); mBillboardSetHandle->setDefaultWidth(mDefaultWidth->get()); mBillboardSetHandle->setDefaultHeight(mDefaultHeight->get()); mBillboardSetHandle->setVisibilityFlags(1 << mLayer->get()); if(mMaterial->get() != "") mBillboardSetHandle->setMaterialName(mMaterial->get()); mBillboardSetHandle->setPointRenderingEnabled(mPointRendering->get()); mBillboardSetHandle->setRenderingDistance(mRenderDistance->get()); Ogre::Vector2 v2val; Ogre::Vector3 v3val; Ogre::Vector4 v4val; Ogre::ColourValue cval; Ogre::Real rval; int ival; Ogre::String propname; int count = mBillboardCount->get(); for(int ix = 0;ix < count;ix++) { propname = "billboard" + Ogre::StringConverter::toString(ix); Ogre::Billboard *pBillboard = mBillboardSetHandle->createBillboard(Ogre::Vector3::ZERO); mProperties.getValue(propname + "::position", v3val); pBillboard->setPosition(v3val); mProperties.getValue(propname + "::colour", cval); pBillboard->setColour(cval); mProperties.getValue(propname + "::dimensions", v2val); pBillboard->setDimensions(v2val.x, v2val.y); mProperties.getValue(propname + "::rotation", rval); pBillboard->setRotation(Ogre::Degree(rval)); mProperties.getValue(propname + "::texcoordindex", ival); pBillboard->setTexcoordIndex(ival); mProperties.getValue(propname + "::texrect", v4val); pBillboard->setTexcoordRect(v4val.x, v4val.y, v4val.z, v4val.w); } } else return false; return true; }
//--------------------------------------------------------------------- void BillboardSetElement::updateRenderInstance( Real time, TransformInfo &info ) { if (mBillboardSet) { bool updateTexIndex = false; if (mSlices > 1 || mStacks > 1) { mCurrentTexIntervalTime += time; if (mCurrentTexIntervalTime >= mTexIntervalTime) { updateTexIndex = true; mCurrentTexIntervalTime = 0.0f; } } for ( int i = 0; i < mBillboardSet->getNumBillboards(); ++i ) { Ogre::Billboard* billboard = mBillboardSet->getBillboard(i); if (updateTexIndex) { uint16 texIndex = billboard->getTexcoordIndex(); ++texIndex; if (texIndex == mStacks * mSlices) texIndex = 0; billboard->setTexcoordIndex(texIndex); } if (mColourChanged) billboard->setColour(mBillboardColour * mAlphaValue); if (mSizeChanged) { _CurrentScaleInfo& info = mCurrentScaleInfos[i]; info.mCurrentScaleIntervalTime += time; float colourPersent = fmod( info.mCurrentScaleIntervalTime, mScaleIntervalTime ); if (info.mCurrentScaleIntervalTime > mScaleIntervalTime) { info.mCurrentScaleIntervalTime = colourPersent; ++(info.mCurrentIndex); if (info.mCurrentIndex == MAX_STAGES) info.mCurrentIndex = 0; } Ogre::Vector3 resultColour; Real rate = colourPersent / mScaleIntervalTime; int incIndex = info.mCurrentIndex + 1; if (incIndex == MAX_STAGES) incIndex = 0; resultColour = mBillboardScale[info.mCurrentIndex] * (1-rate) + mBillboardScale[incIndex] * rate; billboard->setDimensions(mBillboardWidth * resultColour.x, mBillboardHeight * resultColour.y); } } if (mColourChanged) mColourChanged = false; } }
void JumpTape::onInitialize() { auto scene = addScene(new dt::Scene("JumpTape_scene")); OgreProcedural::Root::getInstance()->sceneManager = scene->getSceneManager(); dt::ResourceManager::get()->addResourceLocation("", "FileSystem", true); Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); Ogre::FontManager::getSingleton().load("DejaVuSans", "General"); auto camnode = scene->addChildNode(new dt::Node("camnode")); camnode->setPosition(Ogre::Vector3(0, 0, 40)); camnode->addComponent(new dt::CameraComponent("cam"))->lookAt(Ogre::Vector3(0, 0, 0)); auto background = scene->addChildNode(new dt::Node("background")); background->setPosition(Ogre::Vector3(0, 0, -1)); auto background_billboard = background->addComponent( new dt::BillboardSetComponent( "background_billboard", 1, "jumptape-background.jpg")); background_billboard->getOgreBillboardSet()->setDefaultDimensions(50, 34); mField = scene->addChildNode(new dt::Node("field_node")); mField->setPosition(Ogre::Vector3(0, 0, 0)); auto billboard = mField->addComponent(new dt::BillboardSetComponent("tiles", TILES)); billboard->setTextureFromFile("jumptape-tiles.png"); mTiles = billboard->getOgreBillboardSet(); mTiles->setTextureStacksAndSlices(1, 2); mTiles->setDefaultDimensions(TILE_X, TILE_Y); mTiles->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY); // Always visible for(uint8_t i=0; i<TILES; ++i) { // Consecutive blank tiles, must be under MAX_BLANK_TILE, // otherwise the player can't reach the other tile. bool blank; if(i<5) { // Plain start. blank = false; } else { blank = _GetTileType(); } mTiles->setBillboardOrigin(Ogre::BBO_CENTER); Ogre::Billboard* tile = mTiles->createBillboard((-GAME_WITDH/2)+(TILE_X*i), 0, 0); tile->setTexcoordIndex(blank); } auto player = scene->addChildNode(new dt::Node("player")); player->setPosition(Ogre::Vector3(0, 0, 0)); auto billboard_component = player->addComponent(new dt::BillboardSetComponent("player", 1, "jumptape-jumper.png")); Ogre::BillboardSet* player_billboard = billboard_component->getOgreBillboardSet(); player_billboard->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY); player_billboard->setTextureStacksAndSlices(1, 12); player_billboard->setDefaultDimensions(2, 2); mPlayer = player_billboard->getBillboard(0); mPlayer->setPosition(mTiles->getBillboard(2)->getPosition().x-1.6, // Match edges of image. mTiles->getBillboard(2)->getPosition().y+9, 0); auto info_node = scene->addChildNode(new dt::Node("info")); info_node->setPosition(Ogre::Vector3(0, (-GAME_HEIGHT/2)+5, 2)); mGameInfo = info_node->addComponent(new dt::TextComponent("")); mGameInfo->setFont("DejaVuSans"); mGameInfo->setFontSize(20); mGameInfo->setColor(Ogre::ColourValue::White); }
void JumpTape::updateStateFrame(double simulation_frame_time) { mRuntime += simulation_frame_time; mKeyboard = dt::InputManager::get()->getKeyboard(); double field_speed = mRuntime/1000; // Move each tile. for(uint8_t i=0; i<TILES; ++i) { Ogre::Billboard* tile = mTiles->getBillboard(i); tile->setPosition(tile->getPosition() - Ogre::Vector3(field_speed, 0, 0)); } // Wheater the next tile should be blank or present. bool blank = _GetTileType(); // Move the first tile at the end of the group if it went beyond left margin. static uint8_t i = 0; // index for selecting the right tile. Ogre::Billboard* tile = mTiles->getBillboard(i); if(tile->getPosition().x < (-GAME_WITDH/2)-4) { // Get position of the last (precedent) tile. double last_tile_x = mTiles->getBillboard((i+TILES-1) % TILES)->getPosition().x; // Append billboard to end. tile->setPosition(last_tile_x+TILE_X, dt::Random::get(-3, 3), 0); tile->setTexcoordIndex(blank); i++; // Switch to next tile... i %= TILES; // ...in a cicle. } // Move player. // Player and tile variables. tile = mTiles->getBillboard((i+2) % TILES); // The tile on which the player is. static float jump_height = 0; // The height of each jump. static bool jump_allowed = false; // Jump is not allowed when the player is already on air. bool blank_tile = tile->getTexcoordIndex(); // Wheater the tile under the player is blank. Ogre::Vector3 player_pos = mPlayer->getPosition(); Ogre::Vector3 tile_pos = tile->getPosition(); // Position of the tile under the player. // Jump. if(mKeyboard->isKeyDown(OIS::KC_SPACE) && (jump_height < MAX_JUMP_HEIGHT) && jump_allowed) { player_pos += Ogre::Vector3(0, JUMP_POWER, 0); mPlayer->setPosition(player_pos); jump_height += JUMP_POWER; } else { jump_allowed = false; // Once the player release space he can't go up anymore. } float tile_top = tile_pos.y + TILE_Y * 2; // Top of the tile. // Wheater the player is running on the tile. bool on_tile = (player_pos.y <= tile_top+0.1) && (player_pos.y >= tile_top-0.1); // Gravity. if((blank_tile || !on_tile) && !jump_allowed) { player_pos -= Ogre::Vector3(0, G_POWER, 0); mPlayer->setPosition(player_pos); } // Evaluate if jump is allowed. if(on_tile && !blank_tile) { jump_height = 0; jump_allowed = true; // The player is running, change animation. mPlayer->setTexcoordIndex(static_cast<uint8_t>(mRuntime*10) % PLAYER_FRAME); } // Reset game. if(mKeyboard->isKeyDown(OIS::KC_N)) { mRuntime=0; mPlayer->setPosition(mPlayer->getPosition().x, tile_pos.y+4, 0); tile->setTexcoordIndex(0); } // Show reset game info. static QString time; if(player_pos.y < -4) { mGameInfo->setText("You ran for " + time + " seconds, press N for a new game"); } else { time = dt::Utils::toString(static_cast<uint32_t>(mRuntime)); mGameInfo->setText("You ran for " + time + " seconds"); } }
int main(int argc, char* argv[]) { std::unique_ptr<ExecutionArgs> exArgs(new ExecutionArgs()); if (!processCommandLineArgs(argc, argv, *exArgs)) { return -1; } else if (exArgs->helpPrompt) { std::cout << "Usage: sts [--help] || [--config]" << std::endl; std::cout << "Options:" << std::endl; std::cout << "\t --help - print this message;" << std::endl; std::cout << "\t --config - show config dialog." << std::endl; std::cout << std::endl; return 0; } try { Ogre::String lConfigFileName = "ogre.cfg"; Ogre::String lPluginsFileName = "plugins.cfg"; Ogre::String lLogFileName = "Ogre_STS.log"; std::unique_ptr<Ogre::Root> lRoot(new Ogre::Root(lPluginsFileName, lConfigFileName, lLogFileName)); if (exArgs->showConfigDialog) { if (!lRoot->showConfigDialog()) { return 0; } } Ogre::String lWindowTitle = "STS"; Ogre::String lCustomCapacities = ""; /* Check for the valid ogre.cfg */ bool lCreateAWindowAutomatically = lRoot->restoreConfig(); if (!lCreateAWindowAutomatically) { initSomeRenderSystem(lRoot); } Ogre::RenderWindow* lWindow = lRoot->initialise(lCreateAWindowAutomatically, lWindowTitle, lCustomCapacities); if (!lWindow) { /* ogre.cfg is not available - start with hardcoded parameters */ unsigned int lSizeX = 800; unsigned int lSizeY = 600; bool lFullscreen = false; Ogre::NameValuePairList lParams; lParams["FSAA"] = "0"; lParams["vsync"] = "true"; lWindow = lRoot->createRenderWindow(lWindowTitle, lSizeX, lSizeY, lFullscreen, &lParams); } /* Create a scene manager */ Ogre::SceneManager* lScene = lRoot->createSceneManager(Ogre::ST_GENERIC, "SceneManager"); Ogre::SceneNode* lRootSceneNode = lScene->getRootSceneNode(); /* Create camera */ Ogre::Camera* lCamera = lScene->createCamera("MyCamera"); /* Create viewport (camera <-> window) */ Ogre::Viewport* vp = lWindow->addViewport(lCamera); vp->setAutoUpdated(true); vp->setBackgroundColour(Ogre::ColourValue(1, 0, 1)); lCamera->setAspectRatio(float(vp->getActualWidth()) / vp->getActualHeight()); lCamera->setPosition(Ogre::Vector3(0, 100, -1)); lCamera->lookAt(Ogre::Vector3(0, 0, 0)); /* Set clipping*/ lCamera->setNearClipDistance(1.5f); lCamera->setFarClipDistance(3000.0f); /* Lighting */ Ogre::Light* lLight = lScene->createLight("MainLight"); lLight->setPosition(Ogre::Vector3(0, 100, 0)); /* Resource manager */ Ogre::String lRcGroupName = "Main group"; initResourceMainGroup(lRcGroupName); /* Load model */ Ogre::Entity* lShipEntity = lScene->createEntity("airship.mesh"); lShipEntity->setCastShadows(false); Ogre::SceneNode* lShipNode = lRootSceneNode->createChildSceneNode(); lShipNode->attachObject(lShipEntity); lShipNode->setScale(Ogre::Vector3(3.15f, 3.15f, 3.15f)); /* Starship start point */ Ogre::Vector3 razorSP(0, -200, -100); lShipNode->setPosition(razorSP); /* Sprite billboard */ Ogre::SceneNode* lSpriteNode = lRootSceneNode->createChildSceneNode(); Ogre::BillboardSet* lBillboardSet = lScene->createBillboardSet(); lBillboardSet->setMaterialName("enemy_01", lRcGroupName); lBillboardSet->setTextureStacksAndSlices(1, 4); Ogre::Billboard* lSpriteBillboard = lBillboardSet->createBillboard(Ogre::Vector3(0, 0, 0)); lSpriteBillboard->setDimensions(48.0f / 2.0f, 58.0f / 2.0f); lSpriteBillboard->setTexcoordIndex(1); lSpriteNode->attachObject(lBillboardSet); lSpriteNode->setPosition(Ogre::Vector3(0, -200, 100)); /* Obtain the timer pointer */ Ogre::Timer* lTimer = lRoot->getTimer(); /* Skip all the messages */ lWindow->setAutoUpdated(false); lRoot->clearEventTimes(); while (!lWindow->isClosed()) { float angle = Ogre::Math::Sin(float(lTimer->getMilliseconds()) * Ogre::Math::PI / 2000.0f) * Ogre::Math::PI / 4.0f; float diplacement = Ogre::Math::Cos(float(lTimer->getMilliseconds()) * Ogre::Math::PI / 2000.0f) * 100.0f; lShipNode->setOrientation(Ogre::Quaternion(Ogre::Radian(angle), Ogre::Vector3(0, 0, 1))); lShipNode->setPosition(razorSP + Ogre::Vector3(diplacement, 0.0f, 0.0f)); unsigned int spriteFrame = (lTimer->getMilliseconds() / 125) % 2; lSpriteBillboard->setTexcoordIndex(spriteFrame); lWindow->update(false); lWindow->swapBuffers(); lRoot->renderOneFrame(); Ogre::WindowEventUtilities::messagePump(); } Ogre::LogManager::getSingleton().logMessage("Render window closed."); } catch (Ogre::Exception &e) { std::cerr << "Ogre::Exception: " << e.what() << std::endl; } catch (std::exception &e) { std::cerr << "std::exception: " << e.what() << std::endl; } return 0; }