//--------------------------------------------------------------------- bool SubMesh::updateMaterialUsingTextureAliases(void) { bool newMaterialCreated = false; // if submesh has texture aliases // ask the material manager if the current summesh material exists if (hasTextureAliases() && MaterialManager::getSingleton().resourceExists(mMaterialName)) { // get the current submesh material MaterialPtr material = MaterialManager::getSingleton().getByName( mMaterialName ); // get test result for if change will occur when the texture aliases are applied if (material->applyTextureAliases(mTextureAliases, false)) { Ogre::String newMaterialName; // If this material was already derived from another material // due to aliasing, let's strip off the aliasing suffix and // generate a new one using our current aliasing table. Ogre::String::size_type pos = mMaterialName.find("?TexAlias(", 0); if( pos != Ogre::String::npos ) newMaterialName = mMaterialName.substr(0, pos); else newMaterialName = mMaterialName; newMaterialName += "?TexAlias("; // Iterate deterministically over the aliases (always in the same // order via std::map's sorted iteration nature). AliasTextureIterator aliasIter = getAliasTextureIterator(); while( aliasIter.hasMoreElements() ) { newMaterialName += aliasIter.peekNextKey(); newMaterialName += "="; newMaterialName += aliasIter.getNext(); newMaterialName += " "; } newMaterialName += ")"; // Reuse the material if it's already been created. This decreases batch // count and keeps material explosion under control. if(!MaterialManager::getSingleton().resourceExists(newMaterialName)) { Ogre::MaterialPtr newMaterial = Ogre::MaterialManager::getSingleton().create( newMaterialName, material->getGroup()); // copy parent material details to new material material->copyDetailsTo(newMaterial); // apply texture aliases to new material newMaterial->applyTextureAliases(mTextureAliases); } // place new material name in submesh setMaterialName(newMaterialName); newMaterialCreated = true; } } return newMaterialCreated; }
//--------------------------------------------------------------------- bool SubMesh::updateMaterialUsingTextureAliases(void) { bool newMaterialCreated = false; // if submesh has texture aliases // ask the material manager if the current summesh material exists if (hasTextureAliases() && MaterialManager::getSingleton().resourceExists(mMaterialName)) { // get the current submesh material MaterialPtr material = MaterialManager::getSingleton().getByName( mMaterialName ); // get test result for if change will occur when the texture aliases are applied if (material->applyTextureAliases(mTextureAliases, false)) { // material textures will be changed so copy material, // new material name is old material name + index // check with material manager and find a unique name size_t index = 0; String newMaterialName = mMaterialName + "_" + StringConverter::toString(index); while (MaterialManager::getSingleton().resourceExists(newMaterialName)) { // increment index for next name newMaterialName = mMaterialName + "_" + StringConverter::toString(++index); } Ogre::MaterialPtr newMaterial = Ogre::MaterialManager::getSingleton().create( newMaterialName, material->getGroup()); // copy parent material details to new material material->copyDetailsTo(newMaterial); // apply texture aliases to new material newMaterial->applyTextureAliases(mTextureAliases); // place new material name in submesh setMaterialName(newMaterialName); newMaterialCreated = true; } } return newMaterialCreated; }
Ogre::MaterialPtr EmberTerrainProfile::generate(const Ogre::Terrain* terrain) { const EmberTerrain* emberTerrain = static_cast<const EmberTerrain*>(terrain); const auto& index = emberTerrain->getIndex(); IPageData* pageData = mDataProvider.getPageData(index); if (!pageData) { S_LOG_WARNING("Could not find corresponding page data for OgreTerrain at " << "[" << index.first << "|" << index.second << "]"); return getOrCreateMaterialClone(mErrorMaterialTemplate, terrain->getMaterialName()); } S_LOG_INFO("Loading material for terrain page: " << "[" << index.first << "|" << index.second << "]"); Ogre::MaterialPtr mat = pageData->getMaterial(); if (mat.isNull()) { S_LOG_WARNING("Terrain material was not found."); return getOrCreateMaterialClone(mErrorMaterialTemplate, terrain->getMaterialName()); } Ogre::AliasTextureNamePairList aliases; aliases[Techniques::Shader::NORMAL_TEXTURE_ALIAS] = terrain->getTerrainNormalMap()->getName(); auto compositeMap = terrain->getCompositeMap(); if (!compositeMap.isNull()) { aliases[Techniques::Shader::COMPOSITE_MAP_ALIAS] = compositeMap->getName(); } bool success = mat->applyTextureAliases(aliases); if (!success) { S_LOG_WARNING("Could not apply alias for normal map and/or composite map for terrain material \"" << mat->getName() << "\""); } return mat; }
const Ogre::MaterialPtr& Terrain::_getGridMaterial(const TerrainData::GridInfo &gridInfo, ushort depthBias, int nTileX, int nTileZ) { assert(mData != NULL); size_t textureIds[TerrainData::NumLayers] = { 0 }; for (size_t i = 0; i < TerrainData::NumLayers; ++i) { if (gridInfo.layers[i].pixmapId) { textureIds[i] = _getPixmapAtlasId(gridInfo.layers[i].pixmapId - 1) + 1; } } assert(textureIds[0] && "Internal fault while create grid material"); bool lightmapped = mData->mLightmapImage && getLightmapQuality() != LMQ_NONE; Ogre::ulong lightmapId = lightmapped ? (nTileZ << 16) | nTileX : ~0; MaterialId id(textureIds[0], textureIds[1], lightmapId, depthBias); // find the material that already created MaterialMap::const_iterator it = mMaterials.find(id); if (it != mMaterials.end()) return it->second; Ogre::String name = "Terrain/"; if (!mData->mName.empty()) name += mData->mName + "/"; name += Ogre::StringConverter::toString(textureIds[0]) + "." + Ogre::StringConverter::toString(textureIds[1]); if (depthBias) name += "_" + Ogre::StringConverter::toString(depthBias); Ogre::String lightmapName = "<Lightmap>(" + Ogre::StringConverter::toString(nTileX / mData->mTileSize) + "," + Ogre::StringConverter::toString(nTileZ / mData->mTileSize) + ")"; if (lightmapped) name += "_" + lightmapName; Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().getByName(name); if (material.isNull()) { // get user supplied material Ogre::String templateName = textureIds[1] ? "TwoLayer" : "OneLayer"; if (lightmapped) templateName += "Lightmap"; TerrainData::MaterialTemplates::const_iterator it = mData->mMaterialTemplates.find(templateName); if (it == mData->mMaterialTemplates.end()) OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Can't found grid material template for '" + templateName + "'!", "Terrain::_getGridMaterial"); const Ogre::String& baseName = it->second; Ogre::MaterialPtr templateMaterial = Ogre::MaterialManager::getSingleton().getByName(baseName); if (templateMaterial.isNull()) OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Can't load grid material template '" + baseName + "'!", "Terrain::_getGridMaterial"); // clone the material material = templateMaterial->clone(name, true, BRUSH_RESOURCE_GROUP_NAME); // Setup texture alias list Ogre::AliasTextureNamePairList aliasList; aliasList["<layer0>"] = mAtlases[textureIds[0]-1].texture->getName(); if (textureIds[1]) aliasList["<layer1>"] = mAtlases[textureIds[1]-1].texture->getName(); if (lightmapped) aliasList["<lightmap>"] = lightmapName; // Applies texture names material->applyTextureAliases(aliasList); // Applies surface params _applySurfaceParams(material); // Applies fog _applyFogMode(material, Ogre::FOG_NONE, mCurrentFogMode); // Adjust other material attributes Ogre::Material::TechniqueIterator ti = material->getTechniqueIterator(); while (ti.hasMoreElements()) { Ogre::Technique* technique = ti.getNext(); Ogre::Technique::PassIterator pi = technique->getPassIterator(); while (pi.hasMoreElements()) { Ogre::Pass* pass = pi.getNext(); if (depthBias) { #if OGRE_VERSION >= 0x010300 pass->setDepthBias(depthBias + pass->getDepthBiasConstant(), pass->getDepthBiasSlopeScale()); #else pass->setDepthBias(depthBias + pass->getDepthBias()); #endif } } } } // The material will load on demand std::pair<MaterialMap::iterator, bool> inserted = mMaterials.insert(MaterialMap::value_type(id, material)); assert(inserted.second && "Internal fault while create grid material"); return inserted.first->second; }
bool Terrain::createTerrain() { if(mMainViewport == NULL) mMainViewport = Core::getSingleton().mCamera->getViewport(); Ogre::CompositorManager::getSingleton().addCompositor(mMainViewport, "DemoCompositor"); Ogre::CompositorManager::getSingleton().setCompositorEnabled(mMainViewport, "DemoCompositor", true); mMapData = MapDataManager::getSingletonPtr(); DataLibrary* datalib = DataLibrary::getSingletonPtr(); int terrainszie = mMapData->getMapSize() + 2 * MAPBOLDER + 1; Core::getSingleton().mSceneMgr->setSkyBox(true, "SkyBox",200); Ogre::GpuSharedParametersPtr sharedparams = Ogre::GpuProgramManager::getSingleton().getSharedParameters("TestSharedParamsName"); float border = mMapData->getMapSize() * 12.0f; sharedparams->setNamedConstant("border", border); //创建灯光 Core::getSingleton().mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5f, 0.5f, 0.5f)); mLight = Core::getSingleton().mSceneMgr->createLight("TerrainLight"); mLight->setType(Ogre::Light::LT_DIRECTIONAL); mLight->setPosition(-500.0f,500.0f, 500.0f); mLight->setDirection(1.0f, -1.0f, -1.0f); mLight->setDiffuseColour(Ogre::ColourValue(0.5f, 0.5f,0.5f)); mLight->setSpecularColour(Ogre::ColourValue(0.8f, 0.8f,0.8f)); //设置深度图投影 Ogre::TexturePtr tex = Ogre::TextureManager::getSingleton().getByName("shadowdepthmap"); if(tex.isNull()) tex = Ogre::TextureManager::getSingleton().createManual("shadowdepthmap", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, 2048, 2048, 0, Ogre::PF_FLOAT16_R, Ogre::TU_RENDERTARGET); mShadowDepthMapTarget = tex->getBuffer()->getRenderTarget(); Ogre::Viewport* vp = mShadowDepthMapTarget->addViewport(CameraContral::getSingleton().getShadowMapCamera()); vp->setSkiesEnabled(false); vp->setOverlaysEnabled(false); vp->setVisibilityMask(VISMASK_OPAQUE); vp->setMaterialScheme("WriteDepthMap"); vp->setBackgroundColour(Ogre::ColourValue(1.0f,1.0f,1.0f)); mShadowDepthMapTarget->addListener(this); //弱爆了…… Ogre::MaterialPtr mat; mat = Ogre::MaterialManager::getSingleton().getByName("TerrainTile"); Ogre::AliasTextureNamePairList texAliasList; std::string texname; datalib->getData("GameData/BattleData/MapData/Ground/G0Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); datalib->getData("GameData/BattleData/MapData/Ground/G1Tex",texname); texAliasList.insert(std::make_pair("Diffuse1",texname)); datalib->getData("GameData/BattleData/MapData/Ground/G2Tex",texname); texAliasList.insert(std::make_pair("Diffuse2",texname)); datalib->getData("GameData/BattleData/MapData/Ground/G3Tex",texname); texAliasList.insert(std::make_pair("Diffuse3",texname)); mat->applyTextureAliases(texAliasList); texAliasList.clear(); mat = Ogre::MaterialManager::getSingleton().getByName("CliffMat1"); datalib->getData("GameData/BattleData/MapData/Ground/G0Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga")); mat->applyTextureAliases(texAliasList); texAliasList.clear(); mat = Ogre::MaterialManager::getSingleton().getByName("CliffMat2"); datalib->getData("GameData/BattleData/MapData/Ground/G1Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga")); mat->applyTextureAliases(texAliasList); texAliasList.clear(); mat = Ogre::MaterialManager::getSingleton().getByName("CliffMat3"); datalib->getData("GameData/BattleData/MapData/Ground/G2Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga")); mat->applyTextureAliases(texAliasList); texAliasList.clear(); mat = Ogre::MaterialManager::getSingleton().getByName("CliffMat4"); datalib->getData("GameData/BattleData/MapData/Ground/G3Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga")); mat->applyTextureAliases(texAliasList); texAliasList.clear(); mat = Ogre::MaterialManager::getSingleton().getByName("BankMat1"); datalib->getData("GameData/BattleData/MapData/Ground/G0Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga")); mat->applyTextureAliases(texAliasList); texAliasList.clear(); mat = Ogre::MaterialManager::getSingleton().getByName("BankMat2"); datalib->getData("GameData/BattleData/MapData/Ground/G1Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga")); mat->applyTextureAliases(texAliasList); texAliasList.clear(); mat = Ogre::MaterialManager::getSingleton().getByName("BankMat3"); datalib->getData("GameData/BattleData/MapData/Ground/G2Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga")); mat->applyTextureAliases(texAliasList); texAliasList.clear(); mat = Ogre::MaterialManager::getSingleton().getByName("BankMat4"); datalib->getData("GameData/BattleData/MapData/Ground/G3Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga")); mat->applyTextureAliases(texAliasList); texAliasList.clear(); //创建地面Mesh mTerrainNode = Core::getSingleton().mSceneMgr->getRootSceneNode()->createChildSceneNode("TerrainNode"); int numVertices = terrainszie * terrainszie * VERTEX_QUAD; int numIndex = terrainszie * terrainszie * VERTEX_PREQUAD; Ogre::MeshPtr mTerrainMesh = Ogre::MeshManager::getSingleton().createManual("TerrianMesh", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); Ogre::SubMesh* subMesh = mTerrainMesh->createSubMesh(); subMesh->useSharedVertices=false; subMesh->setMaterialName("TerrainTile"); // 创建顶点数据结构 subMesh->vertexData = new Ogre::VertexData(); subMesh->vertexData->vertexStart = 0; subMesh->vertexData->vertexCount = numVertices; //顶点声明与缓冲区绑定 Ogre::VertexDeclaration* vdecl = subMesh->vertexData->vertexDeclaration; Ogre::VertexBufferBinding* vbind = subMesh->vertexData->vertexBufferBinding; //设置顶点数据结构 size_t offsetUV = 0; vdecl->addElement(VERTEX_POS_BINDING, 0, Ogre::VET_FLOAT3,Ogre::VES_POSITION);//向顶点添加一个位置元素 vdecl->addElement(VERTEX_NOM_BINDING, 0, Ogre::VET_FLOAT3,Ogre::VES_NORMAL); for(int i = 0 ; i < TEXTURE_COUNT ; i ++) { offsetUV += vdecl->addElement (VERTEX_UV_BINDING, offsetUV, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES , i).getSize(); } // 创建世界坐标顶点缓冲区 Ogre::HardwareVertexBufferSharedPtr vbufPos = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer( vdecl->getVertexSize(VERTEX_POS_BINDING), numVertices, Ogre::HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); vbind->setBinding(VERTEX_POS_BINDING, vbufPos); Ogre::HardwareVertexBufferSharedPtr vbufNOM = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer( vdecl->getVertexSize(VERTEX_NOM_BINDING), numVertices, Ogre::HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); vbind->setBinding(VERTEX_NOM_BINDING, vbufNOM); // 创建纹理坐标顶点缓冲区 Ogre::HardwareVertexBufferSharedPtr vbufUV = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer( vdecl->getVertexSize(VERTEX_UV_BINDING), numVertices, Ogre::HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); vbind->setBinding(VERTEX_UV_BINDING, vbufUV); // 创建索引缓冲区 Ogre::HardwareIndexBufferSharedPtr indexBuffer = Ogre::HardwareBufferManager::getSingleton().createIndexBuffer( Ogre::HardwareIndexBuffer::IT_16BIT , numIndex, Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY); //创建地形 float* pBufferPos = (float*)vbufPos->lock(Ogre::HardwareBuffer::HBL_DISCARD); float* pBufferUV = (float*)vbufUV->lock(Ogre::HardwareBuffer::HBL_DISCARD); float* pBufferNom = (float*)vbufNOM->lock(Ogre::HardwareBuffer::HBL_DISCARD); float startpos = - terrainszie * TILESIZE / 2; for(int y = 0 ; y < terrainszie; y ++) { for(int x = 0 ; x < terrainszie; x ++) { createTile(x, y, startpos + x * TILESIZE, startpos + y * TILESIZE, pBufferPos, pBufferUV, pBufferNom); pBufferPos += 3 * VERTEX_QUAD ; pBufferNom += 3 * VERTEX_QUAD ; pBufferUV += 2 * VERTEX_QUAD * 4; } } vbufNOM->unlock(); vbufUV->unlock(); vbufPos->unlock(); //写入索引信息 // 锁定索引缓冲区 Ogre::ushort* pIdx = (Ogre::ushort*)indexBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD); for(int y = 0 ; y < terrainszie ; y ++) { for(int x = 0 ; x < terrainszie ; x ++) { Ogre::ushort iIndexTopLeft = (x + y * terrainszie) * VERTEX_QUAD; Ogre::ushort iIndexTopRight = iIndexTopLeft + 1; Ogre::ushort iIndexBottomLeft = iIndexTopLeft + 2; Ogre::ushort iIndexBottomRight = iIndexTopLeft + 3; *pIdx++ = iIndexBottomLeft; *pIdx++ = iIndexBottomRight; *pIdx++ = iIndexTopLeft; *pIdx++ = iIndexBottomRight; *pIdx++ = iIndexTopRight; *pIdx++ = iIndexTopLeft; } } indexBuffer->unlock(); //设置模型的的索引数据 subMesh->indexData->indexBuffer = indexBuffer; subMesh->indexData->indexStart = 0; subMesh->indexData->indexCount =numIndex; Ogre::AxisAlignedBox meshBounds(startpos,0,startpos, -startpos,5,-startpos); mTerrainMesh->_setBounds(meshBounds); mTerrainEntity = Core::getSingleton().mSceneMgr->createEntity("TerrianMesh"); mTerrainNode->attachObject(mTerrainEntity); mTerrainEntity->setQueryFlags(QUERYMASK_TERRAIN); mTerrainNode->setPosition(0,0,0); //创建水面 tex = Ogre::TextureManager::getSingleton().getByName("reflection"); if(tex.isNull()) tex = Ogre::TextureManager::getSingleton().createManual("reflection", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, 512, 512, 0, Ogre::PF_R8G8B8, Ogre::TU_RENDERTARGET); mReflectionTarget = tex->getBuffer()->getRenderTarget(); mReflectionTarget->addViewport(Core::getSingleton().mCamera)->setOverlaysEnabled(false); mReflectionTarget->addListener(this); // mat = Ogre::MaterialManager::getSingleton().getByName("ReflectionWater"); // tech = mat->getTechnique(0); // pass = tech->getPass(0); // tu = pass->getTextureUnitState(1); // tu->setTextureName(tex->getName()); mWaterPlane = Ogre::Plane(Ogre::Vector3::UNIT_Y, WATERHEIGHT); mWaterNode = Core::getSingleton().mSceneMgr->getRootSceneNode()->createChildSceneNode("WaterNode"); mWaterObject = Core::getSingleton().mSceneMgr->createManualObject("WaterObject"); mWaterObject->begin("DemoWater",Ogre::RenderOperation::OT_TRIANGLE_LIST); startpos += TILESIZE/2; for(int y = 0; y < terrainszie; y++) for(int x = 0; x < terrainszie; x++) { if(mMapData->getTerrainType(x -MAPBOLDER, y -MAPBOLDER ) == Water) { mWaterObject->position(startpos + x * TILESIZE, 0.0f, startpos + y * TILESIZE); mWaterObject->colour(1.0f,1.0f,1.0f); mWaterObject->normal(0.0f,1.0f,0.0f); mWaterObject->textureCoord(0.0f,0.0f); mWaterObject->position(startpos + (x+1) * TILESIZE, 0.0f, startpos + (y+1) * TILESIZE); mWaterObject->colour(1.0f,1.0f,1.0f); mWaterObject->normal(0.0f,1.0f,0.0f); mWaterObject->textureCoord(1.0f,1.0f); mWaterObject->position(startpos + (x+1) * TILESIZE, 0.0f, startpos + y * TILESIZE); mWaterObject->colour(1.0f,1.0f,1.0f); mWaterObject->normal(0.0f,1.0f,0.0f); mWaterObject->textureCoord(1.0f,0.0f); mWaterObject->position(startpos + (x+1) * TILESIZE, 0.0f, startpos + (y+1) * TILESIZE); mWaterObject->colour(1.0f,1.0f,1.0f); mWaterObject->normal(0.0f,1.0f,0.0f); mWaterObject->textureCoord(1.0f,1.0f); mWaterObject->position(startpos + x * TILESIZE, 0.0f, startpos + y * TILESIZE); mWaterObject->colour(1.0f,1.0f,1.0f); mWaterObject->normal(0.0f,1.0f,0.0f); mWaterObject->textureCoord(0.0f,0.0f); mWaterObject->position(startpos + x * TILESIZE, 0.0f, startpos + (y+1) * TILESIZE); mWaterObject->colour(1.0f,1.0f,1.0f); mWaterObject->normal(0.0f,1.0f,0.0f); mWaterObject->textureCoord(0.0f,1.0f); } } mWaterObject->end(); mWaterNode->attachObject(mWaterObject); mWaterNode->setPosition(0,WATERHEIGHT,0); //设置摄像机移动范围 float minx = 0.0f;// = ( - (float)(terrainszie - 2 * MAPBOLDER) / 2.0f - 1.0f) * TILESIZE ; getWorldCoords(0,0,minx,minx); minx -= TILESIZE/2; CameraContral::getSingleton().setMoveRect(minx, minx); CameraContral::getSingleton().resetCamera(); //深度投影测试 // Ogre::MeshManager::getSingleton().createPlane("testplane", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, // mWaterPlane, 64, 64, 1, 1, true, 1, 1, 1, Ogre::Vector3::UNIT_Z); // Ogre::Entity* testent = Core::getSingleton().mSceneMgr->createEntity("testplaneent", "testplane"); // testent->setMaterialName("DepthTest"); // Ogre::SceneNode* testnode = Core::getSingleton().mSceneMgr->getRootSceneNode()->createChildSceneNode(); // testnode->attachObject(testent); // testnode->setPosition(0.0f,10.0f,0.0f); return true; }