void TestGame::DrawPlanetMap(Logic::PlanetJustGenerateEvent *evnt){ std::map<Logic::BaseCountry*,std::vector<Logic::CountryTile>>::iterator CntrItr; std::vector<Logic::CountryTile>::iterator TileItr; int tilesize=TILESIZE; bool why; Ogre::Vector3 tempVect; int maxX = (int)Logic::BaseAsset::GetSigleton("MAP_X"),maxY=(int)Logic::BaseAsset::GetSigleton("MAP_Y"); Ogre::SceneNode* node,*capitalNode,*mapnode =mSceneMgr->getRootSceneNode()->createChildSceneNode("Map"); mapnode->translate(-maxX/2*tilesize,-maxY/2*tilesize,0); Ogre::Entity* entNinja = 0; Ogre::FontPtr font = Ogre::FontManager::getSingleton().getResourceIterator().begin()->second; // Make sure the texture is not WRITE_ONLY, we need to read the buffer to do the blending with the font (get the alpha for example) int j =0; for(CntrItr=evnt->allTiles.begin();CntrItr!=evnt->allTiles.end(); CntrItr++){ Ogre::ManualObject* manual = mSceneMgr->createManualObject("CountryObject"+CntrItr->first->mName); Ogre::Texture* texture = Ogre::TextureManager::getSingleton().createManual("CountryText"+CntrItr->first->mName,"General",Ogre::TEX_TYPE_2D,512, 512, 0, Ogre::PF_FLOAT32_RGBA , Ogre::TU_DEFAULT).getPointer(); Ogre::MaterialPtr mat = Ogre::MaterialManager::getSingleton().create("CountryMaterial"+CntrItr->first->mName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); //Draw the background to the new texture Ogre::HardwarePixelBufferSharedPtr pixelBuffer = texture->getBuffer(); // Lock the pixel buffer and get a pixel box pixelBuffer->lock(Ogre::HardwareBuffer::HBL_NORMAL); // for best performance use HBL_DISCARD! const Ogre::PixelBox& pixelBox = pixelBuffer->getCurrentLock(); float *pDest = static_cast<float*>(pixelBox.data); Ogre::ColourValue *temp = &CntrItr->first->mColor; // Fill in some pixel data. This will give a semi-transparent blue, // but this is of course dependent on the chosen pixel format. for (size_t k = 0; k < 512; k++) for(size_t q = 0; q < 512; q++) { *pDest++ =temp->b; // B *pDest++ = temp->g; *pDest++ =temp->r; *pDest++ = 0; // A*/ /* *pDest++ = 255; // B *pDest++ = 0; // G *pDest++ = 0; // R *pDest++ = 127; // A*/ } // Unlock the pixel buffer pixelBuffer->unlock(); ///WriteToTexture(Logic::LogicStd::IntToString(j),Ogre::TexturePtr(texture),Ogre::Image::Box(25,275,370,500),font,Ogre::ColourValue(1.0,1.0,1.0,1.0),'c'); mat->getTechnique(0)->getPass(0)->createTextureUnitState("CountryText"+CntrItr->first->mName); // mat->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA); j++; // specify the material (by name) and rendering type manual->begin("CountryMaterial"+CntrItr->first->mName, Ogre::RenderOperation::OT_TRIANGLE_LIST); int i=0; for(TileItr= CntrItr->second.begin();TileItr!= CntrItr->second.end();TileItr++){ tempVect = calculateActualPointFromCenter(CntrItr->first->mCapital.mPosition,(*TileItr).mPosition); tempVect =tempVect*tilesize; manual->position(tempVect.x-tilesize/2,tempVect.y+tilesize/2,tempVect.z); //manual->colour(CntrItr->first->mColor); manual->position(tempVect.x+tilesize/2,tempVect.y+tilesize/2,tempVect.z); //manual->colour(CntrItr->first->mColor); manual->position(tempVect.x+tilesize/2,tempVect.y-tilesize/2,tempVect.z); //manual->colour(CntrItr->first->mColor); manual->position(tempVect.x-tilesize/2,tempVect.y-tilesize/2,tempVect.z); //manual->colour(CntrItr->first->mColor); manual->triangle(i*4+0,i*4+2,i*4+1); manual->triangle(i*4+0,i*4+3,i*4+2); //manual->quad(i*4+0,i*4+2,i*4+3,i*4+1); i++; } // tell Ogre, your definition has finished manual->end(); // add ManualObject to the RootSceneNode (so it will be visible) node =mapnode->createChildSceneNode(); entNinja= mSceneMgr->createEntity("Capital/"+CntrItr->first->mName, "capital.mesh"); entNinja->setCastShadows(true); entNinja->setMaterialName("CountryMaterial"+CntrItr->first->mName); Control::ClickHelper* helpr = new Control::ClickHelper(Logic::CHT_COUNTRY); helpr->target = CntrItr->first; entNinja->setUserAny(Ogre::Any(helpr)); CntrItr->first->mNode = node; capitalNode = node->createChildSceneNode(); capitalNode->attachObject(entNinja); capitalNode->pitch(Ogre::Degree(90)); node->attachObject(manual); // I move the SceneNode so that it is visible to the camera. tempVect = Ogre::Vector3(CntrItr->first->mCapital.mPosition.x*tilesize,CntrItr->first->mCapital.mPosition.y*tilesize,-10); node->translate(tempVect); } mapnode->pitch(Ogre::Degree(-45)); }
//! //! Clones an Ogre::MovableObject. //! //! Is needed because OGRE does not provide clone functions for cameras and //! lights. //! //! \param movableObject The object to clone. //! \param name The name to use for the object. //! \param sceneManager The scene manager to use for creating the object. //! \return The cloned object. //! Ogre::MovableObject * OgreTools::cloneMovableObject ( Ogre::MovableObject *movableObject, const QString &name, Ogre::SceneManager *sceneManager /* = 0 */ ) { // make sure the given object is valid if (!movableObject) { Log::error("The given movable object is invalid.", "OgreTools::cloneMovableObject"); return 0; } // make sure a valid scene manager is available if (!sceneManager) sceneManager = movableObject->_getManager(); if (!sceneManager) { Log::error("No valid scene manager available.", "OgreTools::cloneMovableObject"); return 0; } Ogre::MovableObject *result = 0; Ogre::String typeName = movableObject->getMovableType(); if (typeName == "Entity") { // clone entity Ogre::Entity *entity = dynamic_cast<Ogre::Entity *>(movableObject); //movableObjectCopy = entity->clone(name.toStdString()); Ogre::Entity *entityCopy = sceneManager->createEntity(name.toStdString(), entity->getMesh()->getName()); Ogre::AnimationStateSet *animationStateSet = entity->getAllAnimationStates(); Ogre::AnimationStateSet *animationStateSetCopy = entityCopy->getAllAnimationStates(); // set the same blend mode on entity copy if (entity && entityCopy) { if (entity->hasSkeleton() && entityCopy->hasSkeleton()) { Ogre::Skeleton *skeleton = entity->getSkeleton(); Ogre::Skeleton *skeletonCopy = entityCopy->getSkeleton(); skeletonCopy->setBlendMode(skeleton->getBlendMode()); } } // copy all animation states if (animationStateSet && animationStateSetCopy) { Ogre::AnimationStateIterator animationStateIter = animationStateSet->getAnimationStateIterator(); Ogre::AnimationStateIterator animationStateCopyIter = animationStateSetCopy->getAnimationStateIterator(); while (animationStateIter.hasMoreElements()) { if (!animationStateCopyIter.hasMoreElements()) break; Ogre::AnimationState *animationState = animationStateIter.getNext(); Ogre::AnimationState *animationStateCopy = animationStateCopyIter.getNext(); animationStateCopy->setLoop(animationState->getLoop()); //bool enabled = animationState->getEnabled(); //animationStateCopy->setEnabled(animationState->getEnabled()); animationStateCopy->setEnabled(true); animationStateCopy->setTimePosition(animationState->getTimePosition()); } } // create a new container for the cloned entity OgreContainer *entityCopyContainer = new OgreContainer(entityCopy); entityCopy->setUserAny(Ogre::Any(entityCopyContainer)); if (!entity->getUserAny().isEmpty()) { OgreContainer *entityContainer = Ogre::any_cast<OgreContainer *>(entity->getUserAny()); if (entityContainer) { QObject::connect(entityContainer, SIGNAL(animationStateUpdated(const QString &, double)), entityCopyContainer, SLOT(updateAnimationState(const QString &, double))); QObject::connect(entityContainer, SIGNAL(boneTransformUpdated(const QString &, double, double, double, double, double, double)), entityCopyContainer, SLOT(updateBoneTransform(const QString &, double, double, double, double, double, double))); } } result = dynamic_cast<Ogre::MovableObject *>(entityCopy); } else if (typeName == "Light") { // clone light Ogre::Light *light = dynamic_cast<Ogre::Light *>(movableObject); Ogre::Light *lightCopy = sceneManager->createLight(name.toStdString()); lightCopy->setType(light->getType()); lightCopy->setDiffuseColour(light->getDiffuseColour()); lightCopy->setSpecularColour(light->getSpecularColour()); lightCopy->setAttenuation(light->getAttenuationRange(), light->getAttenuationConstant(), light->getAttenuationLinear(), light->getAttenuationQuadric()); lightCopy->setPosition(light->getPosition()); lightCopy->setDirection(light->getDirection()); if (lightCopy->getType() == Ogre::Light::LT_SPOTLIGHT) lightCopy->setSpotlightRange(light->getSpotlightInnerAngle(), light->getSpotlightOuterAngle(), light->getSpotlightFalloff()); lightCopy->setPowerScale(light->getPowerScale()); lightCopy->setCastShadows(light->getCastShadows()); // create a new container for the cloned light OgreContainer *lightCopyContainer = new OgreContainer(lightCopy); lightCopy->setUserAny(Ogre::Any(lightCopyContainer)); if (!light->getUserAny().isEmpty()) { OgreContainer *lightContainer = Ogre::any_cast<OgreContainer *>(light->getUserAny()); if (lightContainer) QObject::connect(lightContainer, SIGNAL(sceneNodeUpdated()), lightCopyContainer, SLOT(updateLight())); } result = dynamic_cast<Ogre::MovableObject *>(lightCopy); } else if (typeName == "Camera") { // clone camera Ogre::Camera *camera = dynamic_cast<Ogre::Camera *>(movableObject); Ogre::Camera *cameraCopy = sceneManager->createCamera(name.toStdString()); //cameraCopy->setCustomParameter(0, camera->getCustomParameter(0)); cameraCopy->setAspectRatio(camera->getAspectRatio()); cameraCopy->setAutoAspectRatio(camera->getAutoAspectRatio()); //cameraCopy->setAutoTracking(...); cameraCopy->setCastShadows(camera->getCastsShadows()); //cameraCopy->setCullingFrustum(camera->getCullingFrustum()); //cameraCopy->setCustomParameter(...); //cameraCopy->setCustomProjectionMatrix(..); //cameraCopy->setCustomViewMatrix(..); //cameraCopy->setDebugDisplayEnabled(...); //cameraCopy->setDefaultQueryFlags(...); //cameraCopy->setDefaultVisibilityFlags(...); cameraCopy->setDirection(camera->getDirection()); //cameraCopy->setFixedYawAxis(...); cameraCopy->setFocalLength(camera->getFocalLength()); cameraCopy->setFOVy(camera->getFOVy()); //Ogre::Real left; //Ogre::Real right; //Ogre::Real top; //Ogre::Real bottom; //camera->getFrustumExtents(left, right, top, bottom); //cameraCopy->setFrustumExtents(left, right, top, bottom); //cameraCopy->setFrustumOffset(camera->getFrustumOffset()); //cameraCopy->setListener(camera->getListener()); cameraCopy->setLodBias(camera->getLodBias()); //cameraCopy->setLodCamera(camera->getLodCamera()); cameraCopy->setNearClipDistance(camera->getNearClipDistance()); cameraCopy->setFarClipDistance(camera->getFarClipDistance()); cameraCopy->setOrientation(camera->getOrientation()); //cameraCopy->setOrthoWindow(...); //cameraCopy->setOrthoWindowHeight(...); //cameraCopy->setOrthoWindowWidth(...); cameraCopy->setPolygonMode(camera->getPolygonMode()); cameraCopy->setPolygonModeOverrideable(camera->getPolygonModeOverrideable()); cameraCopy->setPosition(camera->getPosition()); cameraCopy->setProjectionType(camera->getProjectionType()); cameraCopy->setQueryFlags(camera->getQueryFlags()); cameraCopy->setRenderingDistance(camera->getRenderingDistance()); cameraCopy->setRenderQueueGroup(camera->getRenderQueueGroup()); //cameraCopy->setRenderSystemData(camera->getRenderSystemData()); cameraCopy->setUseIdentityProjection(camera->getUseIdentityProjection()); cameraCopy->setUseIdentityView(camera->getUseIdentityView()); //cameraCopy->setUserAny(camera->getUserAny()); cameraCopy->setUseRenderingDistance(camera->getUseRenderingDistance()); //cameraCopy->setUserObject(camera->getUserObject()); cameraCopy->setVisibilityFlags(camera->getVisibilityFlags()); cameraCopy->setVisible(camera->getVisible()); //cameraCopy->setWindow(...); if (!movableObject->getUserAny().isEmpty()) { CameraInfo *sourceCameraInfo = Ogre::any_cast<CameraInfo *>(movableObject->getUserAny()); if (sourceCameraInfo) { CameraInfo *targetCameraInfo = new CameraInfo(); targetCameraInfo->width = sourceCameraInfo->width; targetCameraInfo->height = sourceCameraInfo->height; dynamic_cast<Ogre::MovableObject *>(cameraCopy)->setUserAny(Ogre::Any(targetCameraInfo)); } } //// Setup connections for instances //SceneNode *targetSceneNode = new SceneNode(cameraCopy); //((Ogre::MovableObject *)cameraCopy)->setUserAny(Ogre::Any(targetSceneNode)); //if (!((Ogre::MovableObject *)camera)->getUserAny().isEmpty()) { // SceneNode *sourceSceneNode = Ogre::any_cast<SceneNode *>(((Ogre::MovableObject *)camera)->getUserAny()); // if (sourceSceneNode) { // QObject::connect(sourceSceneNode, SIGNAL(sceneNodeUpdated()), targetSceneNode, SLOT(updateSceneNode())); // } //} result = dynamic_cast<Ogre::MovableObject *>(cameraCopy); } if (!result) Log::error(QString("Could not clone movable object \"%1\" of type \"%2\".").arg(movableObject->getName().c_str()).arg(typeName.c_str()), "OgreTools::cloneMovableObject"); return result; }
Ogre::SceneNode *TutorialApplication::loadBSP(std::shared_ptr<l2p::UModel> m, bool ignoreNonVisible) { l2p::Name name = m->package->name; std::vector<float> vertex_data; std::vector<uint32_t> index_buf; l2p::Box bounds; // Build vertex and index buffer. for (auto ni = m->nodes.begin(), ne = m->nodes.end(); ni != ne; ++ni) { l2p::BSPNode &n = *ni; l2p::BSPSurface &s = m->surfaces[n.surface]; if (ignoreNonVisible && ignoreNode(m.get(), n, s)) continue; uint32_t vert_start = vertex_data.size() / 8; const Ogre::Vector3 uvec = ogre_cast(m->vectors[s.U]); const Ogre::Vector3 vvec = ogre_cast(m->vectors[s.V]); const Ogre::Vector3 base = ogre_cast(m->points[s.base]); int usize = 0; int vsize = 0; std::shared_ptr<l2p::UTexture> mat = s.material; if (mat) { usize = mat->USize; vsize = mat->VSize; } if (usize == 0 || vsize == 0) usize = vsize = 64; // Vertex buffer. if (n.num_verticies > 0) { l2p::Vector Normal = m->vectors[s.normal]; for (uint32_t vert_index = 0; vert_index < n.num_verticies; ++vert_index) { const l2p::Vector &pos = m->points[m->vertexes[n.vert_pool + vert_index].vertex]; const Ogre::Vector3 dist(ogre_cast(pos) - base); const Ogre::Vector2 tcoord((dist | uvec) / float(usize), (dist | vvec) / float(vsize)); bounds += pos; vertex_data.push_back(pos.X); vertex_data.push_back(pos.Y); vertex_data.push_back(pos.Z); vertex_data.push_back(Normal.X); vertex_data.push_back(Normal.Y); vertex_data.push_back(Normal.Z); vertex_data.push_back(tcoord.x); vertex_data.push_back(tcoord.y); } if (s.flags & l2p::PF_TwoSided) { for (uint32_t vert_index = 0; vert_index < n.num_verticies; ++vert_index) { const l2p::Vector &pos = m->points[m->vertexes[n.vert_pool + vert_index].vertex]; const Ogre::Vector3 dist(ogre_cast(pos) - base); const Ogre::Vector2 tcoord((dist | uvec) / float(usize), (dist | vvec) / float(vsize)); vertex_data.push_back(pos.X); vertex_data.push_back(pos.Y); vertex_data.push_back(pos.Z); vertex_data.push_back(Normal.X); vertex_data.push_back(Normal.Y); vertex_data.push_back(-Normal.Z); vertex_data.push_back(tcoord.x); vertex_data.push_back(tcoord.y); } } } // Index buffer. for (int verti = 2; verti < n.num_verticies; ++verti) { index_buf.push_back(vert_start); index_buf.push_back(vert_start + verti - 1); index_buf.push_back(vert_start + verti); } if (s.flags & l2p::PF_TwoSided) { for (int verti = 2; verti < n.num_verticies; ++verti) { index_buf.push_back(vert_start); index_buf.push_back(vert_start + verti); index_buf.push_back(vert_start + verti - 1); } } } if (vertex_data.size() == 0 || index_buf.size() == 0) return nullptr; Ogre::MeshPtr mesh = Ogre::MeshManager::getSingleton().createManual(Ogre::String(name) + Ogre::String(m->name), "General"); Ogre::VertexData *data = new Ogre::VertexData(); mesh->sharedVertexData = data; data->vertexCount = vertex_data.size() / 8; Ogre::VertexDeclaration *decl = data->vertexDeclaration; uint32_t offset = 0; decl->addElement(0, offset, Ogre::VET_FLOAT3, Ogre::VES_POSITION); offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3); decl->addElement(0, offset, Ogre::VET_FLOAT3, Ogre::VES_NORMAL); offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3); decl->addElement(0, offset, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES); offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT2); Ogre::HardwareVertexBufferSharedPtr vbuf = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer( offset, data->vertexCount, Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY); vbuf->writeData(0, vbuf->getSizeInBytes(), &vertex_data.front(), true); data->vertexBufferBinding->setBinding(0, vbuf); // Setup index buffer. Ogre::HardwareIndexBufferSharedPtr ibuf = Ogre::HardwareBufferManager::getSingleton().createIndexBuffer( Ogre::HardwareIndexBuffer::IT_32BIT, index_buf.size(), Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY); ibuf->writeData(0, ibuf->getSizeInBytes(), &index_buf.front(), true); Ogre::SubMesh *subMesh = mesh->createSubMesh(); subMesh->useSharedVertices = true; subMesh->indexData->indexBuffer = ibuf; subMesh->indexData->indexCount = index_buf.size(); subMesh->indexData->indexStart = 0; mesh->_setBounds(Ogre::AxisAlignedBox(bounds.min.X, bounds.min.Y, bounds.min.Z, bounds.max.X, bounds.max.Y, bounds.max.Z)); mesh->_setBoundingSphereRadius((std::max(bounds.max.X - bounds.min.X, std::max(bounds.max.Y - bounds.min.Y, bounds.max.Z - bounds.min.Z))) / 2.0); mesh->load(); Ogre::Entity *ent = mSceneMgr->createEntity(Ogre::String(name) + Ogre::String(m->name) + "E", Ogre::String(name) + Ogre::String(m->name)); ent->setUserAny(Ogre::Any(static_cast<l2p::UObject*>(m.get()))); ent->setMaterialName("StaticMesh/Default"); Ogre::SceneNode *node = mUnrealCordNode->createChildSceneNode(); node->attachObject(ent); return node; }