主界面如上文設計完成後,場景剛開始添加了是Ogre例子裏的,發現場景裏實物太少,因而想到直接把天龍的場景拿下來,天龍網上有源碼,參考了下,把天龍的地形用Ogre的地形組件完成了下,以下是效果圖: node
由於主要是加載地形,而後只是簡單加載了靜態模型,所以場景看着比較簡陋,再者由於上傳的圖片限制,場景複雜後根本傳不上來.app
天龍的地形仍是比較簡單的,以下是天龍的pingpan.terrain簡化後的內容.編輯器
<?xml version="1.0" encoding="UTF-8" standalone="no" ?> <Terrain name="_pingpan_1" tileSize="32" xsize="128" zsize="128"> <scale x="100" y="100" z="100"/> <heightmap filename="_pingpan_1.Heightmap" type="standard"/> <gridInfo filename="_pingpan_1.GridInfo" type="standard"/> <lightmap filename="_pingpan_1.Lightmap" type="standard"/> <textures> <texture filename="08大理/大理方磚地.tga" type="image"/> <texture filename="08大理/大理石臺階.tga" type="image"/> <texture filename="08大理/臺基座矮.tga" type="image"/> <texture filename="08大理/臺基座高.tga" type="image"/> <texture filename="13鏡湖/鏡湖桃花瓣.tga" type="image"/> <texture filename="江南/花2.tga" type="image"/> <texture filename="江南/花3.tga" type="image"/> <texture filename="江南/花4.tga" type="image"/> <texture filename="西南/淺草地底層.jpg" type="image"/> <texture filename="西南/淺草地上層.tga" type="image"/> <texture filename="西南/山b01.jpg" type="image"/> <texture filename="西南/山b02.jpg" type="image"/> <texture filename="西南/山b03.jpg" type="image"/> <texture filename="西南/深草地底層.jpg" type="image"/> <texture filename="西南/深草地上層.tga" type="image"/> <texture filename="西南/西南碎石地.tga" type="image"/> <texture filename="西南/沼澤.jpg" type="image"/> <texture filename="西南/磚地.tga" type="image"/> </textures> <pixmaps> <pixmap bottom="0.2480469" left="0.00390625" right="0.4960938" textureId="0" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.00390625" right="0.4960938" textureId="0" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.00390625" right="0.4960938" textureId="0" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.00390625" right="0.4960938" textureId="0" top="0.7519531"/> <pixmap bottom="0.748047" left="0.503906" right="0.996094" textureId="0" top="0.501953"/> <pixmap bottom="0.2480469" left="0.5039063" right="0.9960938" textureId="0" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.5039063" right="0.9960938" textureId="0" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.5039063" right="0.9960938" textureId="0" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.5039063" right="0.9960938" textureId="0" top="0.7519531"/> <pixmap bottom="0.4960938" left="0.00390625" right="0.4960938" textureId="1" top="0.00390625"/> <pixmap bottom="0.9960938" left="0.00390625" right="0.4960938" textureId="1" top="0.5039063"/> <pixmap bottom="0.4960938" left="0.5039063" right="0.9960938" textureId="1" top="0.00390625"/> <pixmap bottom="0.9960938" left="0.5039063" right="0.9960938" textureId="1" top="0.5039063"/> <pixmap bottom="0.9960938" left="0.00390625" right="0.4960938" textureId="2" top="0.00390625"/> <pixmap bottom="0.996094" left="0.00390625" right="0.496094" textureId="2" top="0.00390625"/> <pixmap bottom="0.996094" left="0.503906" right="0.996094" textureId="2" top="0.00390625"/> <pixmap bottom="0.9960938" left="0.5039063" right="0.9960938" textureId="2" top="0.00390625"/> <pixmap bottom="0.9980469" left="0.00390625" right="0.4960938" textureId="3" top="0.001953125"/> <pixmap bottom="0.998047" left="0.00390625" right="0.496094" textureId="3" top="0.00195313"/> <pixmap bottom="0.998047" left="0.503906" right="0.996094" textureId="3" top="0.00195313"/> <pixmap bottom="0.9980469" left="0.5039063" right="0.9960938" textureId="3" top="0.001953125"/> <pixmap bottom="0.2480469" left="0.00390625" right="0.4960938" textureId="4" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.00390625" right="0.4960938" textureId="4" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.00390625" right="0.4960938" textureId="4" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.00390625" right="0.4960938" textureId="4" top="0.7519531"/> <pixmap bottom="0.2480469" left="0.5039063" right="0.9960938" textureId="4" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.5039063" right="0.9960938" textureId="4" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.5039063" right="0.9960938" textureId="4" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.5039063" right="0.9960938" textureId="4" top="0.7519531"/> <pixmap bottom="0.4960938" left="0.00390625" right="0.4960938" textureId="5" top="0.00390625"/> <pixmap bottom="0.9960938" left="0.00390625" right="0.4960938" textureId="5" top="0.5039063"/> <pixmap bottom="0.4960938" left="0.5039063" right="0.9960938" textureId="5" top="0.00390625"/> <pixmap bottom="0.9960938" left="0.5039063" right="0.9960938" textureId="5" top="0.5039063"/> <pixmap bottom="0.2480469" left="0.00390625" right="0.4960938" textureId="6" top="0.001953125"/> <pixmap bottom="0.7480469" left="0.00390625" right="0.4960938" textureId="6" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.00390625" right="0.4960938" textureId="6" top="0.7519531"/> <pixmap bottom="0.2480469" left="0.00390625" right="0.4960938" textureId="7" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.00390625" right="0.4960938" textureId="7" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.00390625" right="0.4960938" textureId="7" top="0.5019531"/> <pixmap bottom="0.2480469" left="0.5039063" right="0.9960938" textureId="7" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.5039063" right="0.9960938" textureId="7" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.5039063" right="0.9960938" textureId="7" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.5039063" right="0.9960938" textureId="7" top="0.7519531"/> <pixmap bottom="0.2480469" left="0.2519531" right="0.4980469" textureId="8" top="0.001953125"/> <pixmap bottom="0.2480469" left="0.001953125" right="0.2480469" textureId="9" top="0.001953125"/> <pixmap bottom="0.9980469" left="0.4980469" right="0.2519531" textureId="9" top="0.7519531"/> <pixmap bottom="0.4980469" left="0.7480469" right="0.5019531" textureId="9" top="0.2519531"/> <pixmap bottom="0.250977" left="0.00195313" right="0.250977" textureId="10" top="0.00195313"/> <pixmap bottom="0.5" left="0.00195313" right="0.250977" textureId="10" top="0.250977"/> <pixmap bottom="0.749023" left="0.00195313" right="0.250977" textureId="10" top="0.5"/> <pixmap bottom="0.998047" left="0.00195313" right="0.250977" textureId="10" top="0.749023"/> <pixmap bottom="0.250977" left="0.250977" right="0.5" textureId="10" top="0.00195313"/> <pixmap bottom="0.5" left="0.250977" right="0.5" textureId="10" top="0.250977"/> <pixmap bottom="0.749023" left="0.250977" right="0.5" textureId="10" top="0.5"/> <pixmap bottom="0.998047" left="0.250977" right="0.5" textureId="10" top="0.749023"/> <pixmap bottom="0.250977" left="0.5" right="0.749023" textureId="10" top="0.00195313"/> <pixmap bottom="0.5" left="0.5" right="0.749023" textureId="10" top="0.250977"/> <pixmap bottom="0.749023" left="0.5" right="0.749023" textureId="10" top="0.5"/> <pixmap bottom="0.998047" left="0.5" right="0.749023" textureId="10" top="0.749023"/> <pixmap bottom="0.250977" left="0.749023" right="0.998047" textureId="10" top="0.00195313"/> <pixmap bottom="0.5" left="0.749023" right="0.998047" textureId="10" top="0.250977"/> <pixmap bottom="0.749023" left="0.749023" right="0.998047" textureId="10" top="0.5"/> <pixmap bottom="0.998047" left="0.749023" right="0.998047" textureId="10" top="0.749023"/> <pixmap bottom="0.25" left="0.00195313" right="0.25" textureId="11" top="0.00195313"/> <pixmap bottom="0.498047" left="0.00195313" right="0.25" textureId="11" top="0.25"/> <pixmap bottom="0.75" left="0.00195313" right="0.250977" textureId="11" top="0.501953"/> <pixmap bottom="0.998047" left="0.00195313" right="0.250977" textureId="11" top="0.75"/> <pixmap bottom="0.25" left="0.25" right="0.498047" textureId="11" top="0.00195313"/> <pixmap bottom="0.498047" left="0.25" right="0.498047" textureId="11" top="0.25"/> <pixmap bottom="0.75" left="0.250977" right="0.5" textureId="11" top="0.501953"/> <pixmap bottom="0.998047" left="0.250977" right="0.5" textureId="11" top="0.75"/> <pixmap bottom="0.75" left="0.5" right="0.749023" textureId="11" top="0.501953"/> <pixmap bottom="0.998047" left="0.5" right="0.749023" textureId="11" top="0.75"/> <pixmap bottom="0.25" left="0.501953" right="0.75" textureId="11" top="0.00195313"/> <pixmap bottom="0.498047" left="0.501953" right="0.75" textureId="11" top="0.25"/> <pixmap bottom="0.75" left="0.749023" right="0.998047" textureId="11" top="0.501953"/> <pixmap bottom="0.998047" left="0.749023" right="0.998047" textureId="11" top="0.75"/> <pixmap bottom="0.25" left="0.75" right="0.998047" textureId="11" top="0.00195313"/> <pixmap bottom="0.498047" left="0.75" right="0.998047" textureId="11" top="0.25"/> <pixmap bottom="0.5" left="0.00390625" right="0.5" textureId="12" top="0.00390625"/> <pixmap bottom="0.996094" left="0.00390625" right="0.5" textureId="12" top="0.5"/> <pixmap bottom="0.5" left="0.5" right="0.996094" textureId="12" top="0.00390625"/> <pixmap bottom="0.996094" left="0.5" right="0.996094" textureId="12" top="0.5"/> <pixmap bottom="0.25" left="0" right="0.5" textureId="13" top="0"/> <pixmap bottom="0.5" left="0" right="0.5" textureId="13" top="0.25"/> <pixmap bottom="0.75" left="0" right="0.5" textureId="13" top="0.5"/> <pixmap bottom="1" left="0" right="0.5" textureId="13" top="0.75"/> <pixmap bottom="0.2480469" left="0.00390625" right="0.4960938" textureId="13" top="0.001953125"/> <pixmap bottom="0.248047" left="0.00390625" right="0.496094" textureId="13" top="0.00195313"/> <pixmap bottom="0.498047" left="0.00390625" right="0.496094" textureId="13" top="0.251953"/> <pixmap bottom="0.4980469" left="0.00390625" right="0.4960938" textureId="13" top="0.2519531"/> <pixmap bottom="0.748047" left="0.00390625" right="0.496094" textureId="13" top="0.501953"/> <pixmap bottom="0.7480469" left="0.00390625" right="0.4960938" textureId="13" top="0.5019531"/> <pixmap bottom="0.998047" left="0.00390625" right="0.496094" textureId="13" top="0.751953"/> <pixmap bottom="0.9980469" left="0.00390625" right="0.4960938" textureId="13" top="0.7519531"/> <pixmap bottom="0.25" left="0.5" right="1" textureId="13" top="0"/> <pixmap bottom="0.5" left="0.5" right="1" textureId="13" top="0.25"/> <pixmap bottom="0.75" left="0.5" right="1" textureId="13" top="0.5"/> <pixmap bottom="1" left="0.5" right="1" textureId="13" top="0.75"/> <pixmap bottom="0.248047" left="0.503906" right="0.996094" textureId="13" top="0.00195313"/> <pixmap bottom="0.498047" left="0.503906" right="0.996094" textureId="13" top="0.251953"/> <pixmap bottom="0.748047" left="0.503906" right="0.996094" textureId="13" top="0.501953"/> <pixmap bottom="0.998047" left="0.503906" right="0.996094" textureId="13" top="0.751953"/> <pixmap bottom="0.2480469" left="0.5039063" right="0.9960938" textureId="13" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.5039063" right="0.9960938" textureId="13" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.5039063" right="0.9960938" textureId="13" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.5039063" right="0.9960938" textureId="13" top="0.7519531"/> <pixmap bottom="0.2480469" left="0.001953125" right="0.2480469" textureId="14" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.001953125" right="0.2480469" textureId="14" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.001953125" right="0.2480469" textureId="14" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.001953125" right="0.2480469" textureId="14" top="0.7519531"/> <pixmap bottom="0.248047" left="0.00195313" right="0.248047" textureId="14" top="0.00195313"/> <pixmap bottom="0.498047" left="0.00195313" right="0.248047" textureId="14" top="0.251953"/> <pixmap bottom="0.748047" left="0.00195313" right="0.248047" textureId="14" top="0.501953"/> <pixmap bottom="0.998047" left="0.00195313" right="0.248047" textureId="14" top="0.751953"/> <pixmap bottom="0.2480469" left="0.2519531" right="0.4980469" textureId="14" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.4980469" right="0.2519531" textureId="14" top="0.2519531"/> <pixmap bottom="0.5019531" left="0.4980469" right="0.2519531" textureId="14" top="0.7480469"/> <pixmap bottom="0.9980469" left="0.4980469" right="0.2519531" textureId="14" top="0.7519531"/> <pixmap bottom="0.498047" left="0.498047" right="0.251953" textureId="14" top="0.251953"/> <pixmap bottom="0.501953" left="0.498047" right="0.251953" textureId="14" top="0.748047"/> <pixmap bottom="0.998047" left="0.498047" right="0.251953" textureId="14" top="0.751953"/> <pixmap bottom="0.001953125" left="0.7480469" right="0.5019531" textureId="14" top="0.2480469"/> <pixmap bottom="0.4980469" left="0.7480469" right="0.5019531" textureId="14" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.7480469" right="0.5019531" textureId="14" top="0.5019531"/> <pixmap bottom="0.00195313" left="0.748047" right="0.501953" textureId="14" top="0.248047"/> <pixmap bottom="0.498047" left="0.748047" right="0.501953" textureId="14" top="0.251953"/> <pixmap bottom="0.248047" left="0.751953" right="0.998047" textureId="14" top="0.00195313"/> <pixmap bottom="0.498047" left="0.751953" right="0.998047" textureId="14" top="0.251953"/> <pixmap bottom="0.748047" left="0.751953" right="0.998047" textureId="14" top="0.501953"/> <pixmap bottom="0.2480469" left="0.7519531" right="0.9980469" textureId="14" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.7519531" right="0.9980469" textureId="14" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.7519531" right="0.9980469" textureId="14" top="0.5019531"/> <pixmap bottom="0.2480469" left="0.00390625" right="0.4960938" textureId="15" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.00390625" right="0.4960938" textureId="15" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.00390625" right="0.4960938" textureId="15" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.00390625" right="0.4960938" textureId="15" top="0.7519531"/> <pixmap bottom="0.2480469" left="0.5039063" right="0.9960938" textureId="15" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.5039063" right="0.9960938" textureId="15" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.5039063" right="0.9960938" textureId="15" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.5039063" right="0.9960938" textureId="15" top="0.7519531"/> <pixmap bottom="0.2480469" left="0.001953125" right="0.2480469" textureId="16" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.001953125" right="0.2480469" textureId="16" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.001953125" right="0.2480469" textureId="16" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.001953125" right="0.2480469" textureId="16" top="0.7519531"/> <pixmap bottom="0.2480469" left="0.2519531" right="0.4980469" textureId="16" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.2519531" right="0.4980469" textureId="16" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.2519531" right="0.4980469" textureId="16" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.2519531" right="0.4980469" textureId="16" top="0.7519531"/> <pixmap bottom="0.2480469" left="0.5019531" right="0.7480469" textureId="16" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.5019531" right="0.7480469" textureId="16" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.5019531" right="0.7480469" textureId="16" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.5019531" right="0.7480469" textureId="16" top="0.7519531"/> <pixmap bottom="0.2480469" left="0.7519531" right="0.9980469" textureId="16" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.7519531" right="0.9980469" textureId="16" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.7519531" right="0.9980469" textureId="16" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.7519531" right="0.9980469" textureId="16" top="0.7519531"/> <pixmap bottom="0.2480469" left="0.001953125" right="0.2480469" textureId="17" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.001953125" right="0.2480469" textureId="17" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.001953125" right="0.2480469" textureId="17" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.001953125" right="0.2480469" textureId="17" top="0.7519531"/> <pixmap bottom="0.2480469" left="0.2519531" right="0.4980469" textureId="17" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.2519531" right="0.4980469" textureId="17" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.2519531" right="0.4980469" textureId="17" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.2519531" right="0.4980469" textureId="17" top="0.7519531"/> <pixmap bottom="0.2480469" left="0.5019531" right="0.7480469" textureId="17" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.5019531" right="0.7480469" textureId="17" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.5019531" right="0.7480469" textureId="17" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.5019531" right="0.7480469" textureId="17" top="0.7519531"/> <pixmap bottom="0.2480469" left="0.7519531" right="0.9980469" textureId="17" top="0.001953125"/> <pixmap bottom="0.4980469" left="0.7519531" right="0.9980469" textureId="17" top="0.2519531"/> <pixmap bottom="0.7480469" left="0.7519531" right="0.9980469" textureId="17" top="0.5019531"/> <pixmap bottom="0.9980469" left="0.7519531" right="0.9980469" textureId="17" top="0.7519531"/> </pixmaps> <materials> <template material="Terrain/OneLayer" name="OneLayer"/> <template material="Terrain/OneLayerLightmap" name="OneLayerLightmap"/> <template material="Terrain/TwoLayer" name="TwoLayer"/> <template material="Terrain/TwoLayerLightmap" name="TwoLayerLightmap"/> <fog_replacement exp="Terrain/OneLayer_ps%fog_exp" exp2="Terrain/OneLayer_ps%fog_exp2" linear="Terrain/OneLayer_ps%fog_linear" none="Terrain/OneLayer_ps"/> <fog_replacement exp="Terrain/TwoLayer_ps%fog_exp" exp2="Terrain/TwoLayer_ps%fog_exp2" linear="Terrain/TwoLayer_ps%fog_linear" none="Terrain/TwoLayer_ps"/> <fog_replacement exp="Terrain/OneLayerLightmap_ps%fog_exp" exp2="Terrain/OneLayerLightmap_ps%fog_exp2" linear="Terrain/OneLayerLightmap_ps%fog_linear" none="Terrain/OneLayerLightmap_ps"/> <fog_replacement exp="Terrain/TwoLayerLightmap_ps%fog_exp" exp2="Terrain/TwoLayerLightmap_ps%fog_exp2" linear="Terrain/TwoLayerLightmap_ps%fog_linear" none="Terrain/TwoLayerLightmap_ps"/> </materials> </Terrain>
天龍的地形代碼我沒仔細看,大體看了下,以下有不對的地方請指明,前面的titleSize與xsize與zsize主要是指明天龍是如何分塊,xsize*zsize是全部的網格,而每titleSize指明多個網格合成一個MovableObject,如這個,一共有128*128塊,而後每32*32塊合成一個MovableObject(天龍中是TerrainTile),而後TerrainTile根據每一個網格的紋理決定生成多少個Renderable,這樣達到有限的合併網格渲染的目的.ide
而後是scale,這個就是對應的在遊戲中的實際位置,如xsize是128,那麼x軸的實際長度應該是12800,其中y是地形高度的縮放,而後是高度圖,網格信息文件,光照圖.而後是全部網格要用的紋理,而後是網格要用的分塊紋理pixmaps.簡單來講,一個紋理分紅幾塊pixmap,而pixmap纔是每一個網格用到的紋理部分.其中最重要的gridInfo文件是網格信息文件,存放的是每一個網格用的二層pixmap id和顯示方式.性能
天龍的這種地形方式,通常來講會分紅十幾塊(如上面是(128/32)^2=16),每塊差很少用到全部紋理的一半左右,若是不設置霧與攝像機在遠處全看到,綜合下來,每次渲染要一百多個單元,不算多也不算少,不過沒有LOD,也不會合成遠處顯示,故天龍的場景通常要開很大霧.ui
Ogre地形組件比較複雜,實現的功能比較多,在這 Ogre 1.7的地形系統 簡單說了下,其中和天龍不同的是,天龍中的地形材質針對是每一個網格,而Ogre中地形材質通常針對是整個地形,那麼咱們須要拿到天龍整個地形的紋理,這個紋理要拿到,咱們只需想下這個地形是如何組成的就行,由zsize行xsize列個網格組成,每成對應的顯示在對應gridInfo上的pixmaps上,這樣咱們只須要藉助RTT把一塊紋理分紅zsize行xsize列,而後把每一個pixmap渲染Rectangle2D與manualRender方法渲染到對應位置.請看以下代碼:this
void createLayerTexture(int width, int height, string& textureName, bool firstLay) { auto layName = firstLay ? "firstLay" : "secondLay"; auto fullName = name + layName; textureName = fullName; auto texture = Ogre::TextureManager::getSingleton().getByName(fullName); if (!texture.isNull()) { return; } auto scene = Ogre::Root::getSingleton().getSceneManager(DSceneName); auto mRtTexture = Ogre::TextureManager::getSingleton().createManual(fullName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, width, height, 0, Ogre::PF_A8R8G8B8, Ogre::TU_RENDERTARGET); Ogre::RenderTexture* renderTaget = mRtTexture->getBuffer()->getRenderTarget(); auto mRtCamera = scene->createCamera(fullName); renderTaget->addViewport(mRtCamera); renderTaget->getViewport(0)->setClearEveryFrame(false); renderTaget->getViewport(0)->setBackgroundColour(ColourValue(1.0f, 1.0f, 1.0f, 1.0f)); renderTaget->getViewport(0)->setOverlaysEnabled(true); renderTaget->setAutoUpdated(false); renderTaget->setActive(true); MaterialPtr matPtr = MaterialManager::getSingleton().create (fullName, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); Ogre::Pass* pass = matPtr->getTechnique(0)->getPass(0); pass->setLightingEnabled(false); pass->createTextureUnitState(); pass->getTextureUnitState(0)->addFrameTextureName(""); auto tUnit = pass->getTextureUnitState(0); Ogre::Rectangle2D* rect = new Ogre::Rectangle2D(true); //opengl 不知是不是BUG,須要先update一下,才能用manualRender. renderTaget->update(); auto getLocation = [&](int t, float &aleft, float &atop, float& aright, float& abottom){ int width = gridInfos->m_width; int height = gridInfos->m_height; int row = t / width; int col = t % width; aleft = (1.0f / width) * col * 2.0f - 1.0f; atop = 1.0f - (1.0f / height) * row * 2.0f; aright = aleft + 2.0f / width; abottom = atop - 2.0f / height; }; int i = 0; for (auto grid : gridInfos->m_data) { int gridIndex = i++; int index = firstLay ? grid.nFirstLayer : grid.nSecondLayer; if (index < 0 && index >= pixmaps.size()) continue; auto pixmap = pixmaps[index]; int textureIndex = pixmap->textureId; auto left = pixmap->left; auto top = pixmap->top; auto right = pixmap->right; auto bottom = pixmap->bottom; //auto texture = Ogre::TextureManager::getSingletonPtr()->getByName(textures[textureIndex]); tUnit->setFrameTextureName(textures[textureIndex], 0); int op = firstLay ? grid.nFirstLayerOp : grid.nSecondLayerOp; auto leftTop = Vector2(left, top); auto leftBottom = Vector2(left, bottom); auto rightTop = Vector2(right, top); auto rightBottom = Vector2(right, bottom); if (op > 0) { if (op & 1) { swap(leftTop, rightTop); swap(leftBottom, rightBottom); } if (op & 2) { swap(leftTop, leftBottom); swap(rightTop, rightBottom); } if (op & 4) { swap(leftTop, rightTop); swap(leftBottom, rightTop); swap(rightBottom, rightTop); } if (op & 8) { // 非正常索引 if (grid.IndexOrder == 0) { leftBottom.x = rightTop.x; leftBottom.y = rightTop.y; } // 正常索引 else { rightBottom.x = leftTop.x; rightBottom.y = leftTop.y; } } } rect->setUVs(leftTop, leftBottom, rightTop, rightBottom); //獲得當前網格在整個屏幕上的位置(-1,1) getLocation(gridIndex, left, top, right, bottom); rect->setCorners(left, top, right, bottom); Ogre::RenderOperation renderOp; rect->getRenderOperation(renderOp); scene->manualRender(&renderOp, pass, renderTaget->getViewport(0), Ogre::Matrix4::IDENTITY, Ogre::Matrix4::IDENTITY, Ogre::Matrix4::IDENTITY, false); } //std::string saveFile = "d:\\" + fullName + ".png"; //renderTaget->writeContentsToFile(saveFile); renderTaget->removeAllViewports(); scene->destroyCamera(mRtCamera); MaterialManager::getSingleton().remove(fullName); delete rect; }
以下是pingpan中對應的二張圖: 編碼
這個方法過程就是生成一張Ogre::TU_RENDERTARGET紋理,調用scene->manualRender方法渲染到這張渲染上就好了,這個方法manualRender是我在看Ogre源碼時,他的註釋讓我特別關注了下,發現用在這個地方完美,最開始我想到這個方法,可是發現生成的圖片上啥都沒,若是用正常的方法,生成多個Rectangle2D,多個Material,並掛在SceneNode下,而後調用rtt->pdate ,想一想就變態,由於我只是暫時用下,生成這麼多,我update後要清理多個,而且速度也不見的比這個快,網上找manualRender發現不少人也在說這個渲染不出來,也沒人說爲啥並給出解決方法,還好,我也是突然想到了下,在調用manualRender 以前,我先讓rtt->update一下,這樣就能正常的輸出到紋理了,不知這個地方算不算BUG.spa
有了這二張紋理後,咱們就讓正常的流程加載高度圖,而後生成TerrainGroup,在原來的地形組件中,Ogre會自動給咱們生成對應的着色器代碼,包含多層混合,燈光,霧設置,遠景Technique等等.請看以下地形加載的代碼. .net
class TLTerrainManager : public TerrainManager { protected: bool autoMaterialGen = true; SharedPtr<TLTerrainMaterial> tlMaterial; public: TLTerrainManager(SceneManager* scene, Ogre::Camera* camera) :TerrainManager(scene, camera) { tlMaterial = SharedPtr<TLTerrainMaterial>(new TLTerrainMaterial()); //loadTerrainFile("_pingpan_1.Terrain"); } ~TLTerrainManager() { } void initBlendMaps(Terrain* terrain,string towTexture) { int blendMapSize = terrain->getLayerBlendMapSize(); auto texture2 = Ogre::TextureManager::getSingleton().getByName(towTexture); Image image2; tool::TextureToImage(texture2, image2); int scale = image2.getWidth() / blendMapSize; TerrainLayerBlendMap* blendMap1 = terrain->getLayerBlendMap(1); float* pBlend1 = blendMap1->getBlendPointer(); for (Ogre::uint16 y = 0; y < blendMapSize; ++y) { for (Ogre::uint16 x = 0; x < blendMapSize; ++x) { auto color2 = image2.getColourAt(x * scale + scale / 2, y*scale + scale / 2, 0); float blendA = color2.a; *pBlend1++ = blendA; } } blendMap1->dirty(); blendMap1->update(); } virtual void loadTerrainFile(const string& fileName) { TLTerrain tlTerrain; tlTerrain.openTerrain(fileName); TerrainInfo terrainInfo; tlTerrain.grenate(4096, 4096, terrainInfo); loadInfo(terrainInfo); } float getHeight(Vector3 pos) { return mTerrainGroup->getHeightAtWorldPosition(pos); } private: void loadInfo(TerrainInfo& info) { mTerrainGroup = OGRE_NEW TerrainGroup(mScene, Terrain::ALIGN_X_Z, info.TerrainSize, info.WorldSize); mTerrainGroup->setFilenameConvention(info.FileName, info.FileSuffix); mTerrainGroup->setOrigin(info.Center); mTerrainGroup->setResourceGroup(info.ResourceGroup); mTerrainGlobals->setMaxPixelError(8); mTerrainGlobals->setCompositeMapDistance(3000); Vector3 lightdir(0.0, -1000, 0.0); Ogre::Light* l = mScene->createLight(info.FileName + "tstLight"); l->setType(Light::LT_DIRECTIONAL); l->setDirection(lightdir); l->setDiffuseColour(ColourValue::White); l->setSpecularColour(ColourValue::White); mTerrainGlobals->setLightMapDirection(l->getDerivedDirection()); mTerrainGlobals->setCompositeMapAmbient(mScene->getAmbientLight()); mTerrainGlobals->setCompositeMapDiffuse(l->getDiffuseColour()); Terrain::ImportData& defaultimp = mTerrainGroup->getDefaultImportSettings(); defaultimp.terrainSize = info.TerrainSize; defaultimp.worldSize = info.WorldSize; defaultimp.inputScale = info.InputScale; defaultimp.minBatchSize = info.MinBatchSize; defaultimp.maxBatchSize = info.MaxBatchSize; if (autoMaterialGen) { defaultimp.layerList = info.LayerList; auto matProfile = static_cast<TerrainMaterialGeneratorA::SM2Profile*>(mTerrainGlobals->getDefaultMaterialGenerator()->getActiveProfile()); matProfile->setLayerNormalMappingEnabled(false); matProfile->setLayerParallaxMappingEnabled(false); matProfile->setLayerSpecularMappingEnabled(false); matProfile->setGlobalColourMapEnabled(false); //matProfile->setCompositeMapEnabled(false); //matProfile->setLightmapEnabled(false); } else { mTerrainGlobals->setDefaultMaterialGenerator(tlMaterial); tlMaterial->setLight(l); tlMaterial->setLayerName(info.LayerList[0].textureNames[0], info.LayerList[1].textureNames[0]); } if (mPaging) { mPageManager = new PageManager(); mPageManager->setPageProvider(&mDummyPageProvider); mPageManager->addCamera(mCamera); mPageManager->setDebugDisplayLevel(0); mTerrainPaging = new TerrainPaging(mPageManager); mPagedWorld = mPageManager->createWorld(); mTerrainPagedWorldSection = mTerrainPaging->createWorldSection(mPagedWorld, mTerrainGroup, 400, 500, 0, 0, 1, 1); mPageDefiner = new SimpleTerrainDefiner(info.HeightImage); mTerrainPagedWorldSection->setDefiner(mPageDefiner); } else { mTerrainGroup->defineTerrain(0, 0, info.HeightImage); mTerrainGroup->loadTerrain(0, 0, true); if (autoMaterialGen) initBlendMaps(mTerrainGroup->getTerrain(0, 0), info.LayerList[1].textureNames[0]); } mTerrainGroup->freeTemporaryResources(); } };
這段代碼Ogre例子中有差很少的,天龍的地圖就用二層,而且每層只有一個散射光紋理,因此咱們把每層的鏡面光,法線,視差紋理所有關閉,若是咱們使用光照圖,須要設置terrainglobal的光照方向.
代碼參照Ogre地形例子裏的就行,須要注意咱們設置混合值.咱們若是有超過一層的紋理,須要設置混合值,不然只有第一層的顏色,這個混合值在地形中有紋理專門來保存,若有1-5層紋理,那麼對應一個混合紋理的argb四個通道,其中1-2層散射光紋理的混合值對應a,2-3對應g,若是超過5層,就生成第二個混合紋理.在這咱們只有二層,那麼只須要設置第一層的混合值就行,若是第二張圖的Alpha爲0,咱們則全用第一層的.在天龍的地形中,第二層是專門用來混合的,在這,咱們取對應第二層的Alpha就好.
可是這樣有個問題,咱們發現顏色和天龍本身的編輯器加載仍是有些區別,以下是地形組件自動生成的場景.
經過分析,主要是兩者的片段着色器代碼有些區別.
#version 440 vec4 expand(vec4 v) { return v * 2.0 - 1.0; } vec4 lit(float NdotL, float NdotH, float m) { float ambient = 1.0; float diffuse = max(0.0, NdotL); float specular = step(0.0, NdotL) * max(NdotH, 0.0); return vec4(ambient, diffuse, specular, 1.0); } in vec4 oPosObj; in vec4 oUVMisc; out vec4 fragColour; in vec4 layerUV0; uniform vec4 lightPosObjSpace; uniform vec3 lightDiffuseColour; uniform vec3 lightSpecularColour; uniform vec3 eyePosObjSpace; uniform vec4 ambient; uniform vec4 scaleBiasSpecular; uniform sampler2D globalNormal; uniform sampler2D blendTex0; uniform sampler2D difftex0; uniform sampler2D normtex0; uniform sampler2D difftex1; uniform sampler2D normtex1; void main(void) { float shadow = 1.0; vec2 uv = oUVMisc.xy; fragColour = vec4(0,0,0,1); vec3 normal = expand(texture(globalNormal, uv)).rgb; vec3 lightDir = lightPosObjSpace.xyz - (oPosObj.xyz * lightPosObjSpace.w); vec3 eyeDir = eyePosObjSpace - oPosObj.xyz; vec3 diffuse = vec3(0,0,0); float specular = 0.0; vec4 blendTexVal0 = texture(blendTex0, uv); lightDir = normalize(lightDir); eyeDir = normalize(eyeDir); vec3 halfAngle = normalize(lightDir + eyeDir); vec4 litRes = lit(dot(normal, lightDir), dot(normal, halfAngle), scaleBiasSpecular.z); vec2 uv0 = layerUV0.xy; vec4 diffuseSpecTex0 = texture(difftex0, uv0); diffuse = diffuseSpecTex0.rgb; vec2 uv1 = layerUV0.zw; vec4 diffuseSpecTex1 = texture(difftex1, uv1); diffuse = mix(diffuse, diffuseSpecTex1.rgb, blendTexVal0.r); fragColour.rgb += ambient.rgb * diffuse + litRes.y * lightDiffuseColour * diffuse * shadow; specular = 1.0; fragColour.a = shadow; }
void TwoLayerLightmap_ps( in float2 uv0 : TEXCOORD0, in float2 uvLightmap : TEXCOORD2, in uniform sampler2D layer0, in uniform sampler2D layer1, in uniform sampler2D lightmap, in float4 diffuse : COLOR0, in float4 specular : COLOR1, out float4 oColour : COLOR) { float4 c0 = tex2D(layer0, uv0); float4 c1 = tex2D(layer1, uv0); float3 texturedColour = lerp(c0.rgb, c1.rgb, c1.a); float4 lightmapColour = tex2D(lightmap, uvLightmap); float4 baseColour = diffuse * lightmapColour; float3 finalColour = baseColour.rgb * texturedColour + specular.rgb * (1-c0.a) * (1-c1.a) * lightmapColour.a; float3 resultColour = Fogging(finalColour); oColour = float4(resultColour, baseColour.a); } void TwoLayer_ps( in float2 uv0 : TEXCOORD0, in uniform sampler2D layer0, in uniform sampler2D layer1, in float4 diffuse : COLOR0, in float4 specular : COLOR1, out float4 oColour : COLOR) { float4 c0 = tex2D(layer0, uv0); float4 c1 = tex2D(layer1, uv0); float3 texturedColour = lerp(c0.rgb, c1.rgb, c1.a); float4 baseColour = diffuse; float3 finalColour = baseColour.rgb * texturedColour + specular.rgb * (1-c0.a) * (1-c1.a); float3 resultColour = Fogging(finalColour); oColour = float4(resultColour, baseColour.a); }
經過咱們前面設置的混合紋理initBlendMaps這個方法,兩者紋理都是第二層的Alpha進行線性混合,主要是天龍在最後還會用到這二個紋理的Alpha的值形成一些顏色上的區別.那麼咱們能不能就用天龍的着色器代碼了,不用地形組件自動生成的,答案是能夠.Ogre自己的地形組件提供足夠咱們的擴展,看以下代碼:
class TLTerrainMaterial : public TerrainMaterialGenerator { public: TLTerrainMaterial(); std::string getMaterialTemplate(); void setLight(Ogre::Light* light) { mCompositeMapLight = light; } void setLayerName(std::string oneLayer, std::string twoLayer = ""); void setLightMapName(std::string lightMap); class TLProfile : public Ogre::TerrainMaterialGenerator::Profile { public: TLProfile(Ogre::TerrainMaterialGenerator* parent, const Ogre::String& name, const Ogre::String& desc); ~TLProfile(); bool isVertexCompressionSupported() const { return false; } Ogre::MaterialPtr generate(const Ogre::Terrain* terrain); Ogre::MaterialPtr generateForCompositeMap(const Ogre::Terrain* terrain); void setLightmapEnabled(bool enabled); Ogre::uint8 getMaxLayers(const Ogre::Terrain* terrain) const; void updateParams(const Ogre::MaterialPtr& mat, const Ogre::Terrain* terrain); void updateParamsForCompositeMap(const Ogre::MaterialPtr& mat, const Ogre::Terrain* terrain); void requestOptions(Ogre::Terrain* terrain); }; protected: bool bTwoLayer = false; bool bLightMap = false; std::string oneLayerName; std::string twoLayerName; std::string lightMapName; }; TLTerrainMaterial::TLTerrainMaterial() { mProfiles.push_back(OGRE_NEW TLProfile(this, "TLTerrainMaterial", "Profile for rendering Ogre standard material")); setActiveProfile("TLTerrainMaterial"); } std::string TLTerrainMaterial::getMaterialTemplate() { std::string materialName = "Terrain/"; if (bTwoLayer) materialName += "TwoLayer"; else materialName += "OneLayer"; if (bLightMap) materialName += "Lightmap"; return materialName; } void TLTerrainMaterial::setLayerName(std::string oneLayer, std::string twoLayer) { oneLayerName = oneLayer; bTwoLayer = twoLayer.size() > 0; if (bTwoLayer) { twoLayerName = twoLayer; } } void TLTerrainMaterial::setLightMapName(std::string lightMap) { bLightMap = lightMap.size() > 0; if (bLightMap) { lightMapName = lightMap; } } // ----------------------------------------------------------------------------------------------------------------------- TLTerrainMaterial::TLProfile::TLProfile(Ogre::TerrainMaterialGenerator* parent, const Ogre::String& name, const Ogre::String& desc) : Ogre::TerrainMaterialGenerator::Profile(parent, name, desc) { }; TLTerrainMaterial::TLProfile::~TLProfile() { } Ogre::MaterialPtr TLTerrainMaterial::TLProfile::generate(const Ogre::Terrain* terrain) { auto mGenerator = ((TLTerrainMaterial*)getParent()); const String& matName = terrain->getMaterialName(); MaterialPtr mat = terrain->_getMaterial(); if (mat.isNull()) { MaterialManager& matMgr = MaterialManager::getSingleton(); mat = matMgr.create(matName, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); } mat->removeAllTechniques(); auto templateName = mGenerator->getMaterialTemplate(); Ogre::MaterialPtr templateMaterial = Ogre::MaterialManager::getSingleton().getByName(templateName); *mat = *(templateMaterial->clone(matName)); mat->setDiffuse(mGenerator->mCompositeMapLight->getDiffuseColour()); mat->setSpecular(mGenerator->mCompositeMapLight->getSpecularColour()); // Setup texture alias list Ogre::AliasTextureNamePairList aliasList; aliasList["<layer0>"] = mGenerator->oneLayerName; if (mGenerator->bTwoLayer) aliasList["<layer1>"] = mGenerator->twoLayerName; if (mGenerator->bLightMap) aliasList["<lightmap>"] = mGenerator->lightMapName; mat->applyTextureAliases(aliasList); updateParams(mat, terrain); mat->load(); return mat; } Ogre::MaterialPtr TLTerrainMaterial::TLProfile::generateForCompositeMap(const Ogre::Terrain* terrain) { return terrain->_getCompositeMapMaterial(); } void TLTerrainMaterial::TLProfile::setLightmapEnabled(bool enabled) { } Ogre::uint8 TLTerrainMaterial::TLProfile::getMaxLayers(const Ogre::Terrain* terrain) const { return 2; } void TLTerrainMaterial::TLProfile::updateParams(const Ogre::MaterialPtr& mat, const Ogre::Terrain* terrain) { } void TLTerrainMaterial::TLProfile::updateParamsForCompositeMap(const Ogre::MaterialPtr& mat, const Ogre::Terrain* terrain) { } void TLTerrainMaterial::TLProfile::requestOptions(Ogre::Terrain* terrain) { terrain->_setMorphRequired(true); terrain->_setNormalMapRequired(true); // enable global normal map terrain->_setLightMapRequired(false); terrain->_setCompositeMapRequired(false); }
簡單來講,就是咱們生成咱們本身的TerrainMaterialGenerator與Profile,而後在地形設置中調用以下mTerrainGlobals->setDefaultMaterialGenerator(tlMaterial);這樣地形組件就是用的咱們的着色器代碼,在這咱們只是簡單把設置下,因此原先裏有不少設置咱們用不上,直接關掉就可.
以下是加載天龍地形文件裏全部信息的完整代碼,最主要的就是前面的第一段代碼,已經發過的:
class TLTerrain { private: std::string name; int tileSize = 1; int xsize = 1; int zsize = 1; Vector3 scale; std::string heightMap = ""; std::string gridInfo = ""; std::string lightMap = ""; typedef std::vector<MyGUI::UString> VectorUString; std::vector<Ogre::String> textures; std::vector<Pixmap*> pixmaps; std::vector<float> heightMapData; GridInfos* gridInfos; SharedPtr<Image> imagePtr; public: TLTerrain() { gridInfos = new GridInfos(); imagePtr = SharedPtr<Image>(new Image()); } ~TLTerrain() { clear(); } void clear() { heightMap = ""; gridInfo = ""; lightMap = ""; gridInfos->close(); textures.clear(); heightMapData.clear(); if (pixmaps.size() > 0) { for (auto p : pixmaps) { delete p; } pixmaps.clear(); } } void openTerrain(const std::string& fileName) { clear(); DataStreamHolder data = MyGUI::DataManager::getInstance().getData(fileName); xml::Document doc; if (!doc.open(data.getData())) { throw exception(""); } xml::ElementPtr root = doc.getRoot(); name = root->findAttribute("name"); tileSize = MyGUI::utility::parseFloat(root->findAttribute("tileSize")); xsize = MyGUI::utility::parseFloat(root->findAttribute("xsize")); zsize = MyGUI::utility::parseFloat(root->findAttribute("zsize")); auto node = root->getElementEnumerator(); while (node.next()) { if (node->getName() == "scale") { float x = MyGUI::utility::parseFloat(node->findAttribute("x")); float y = MyGUI::utility::parseFloat(node->findAttribute("y")); float z = MyGUI::utility::parseFloat(node->findAttribute("z")); scale.x = x; scale.y = y; scale.z = z; } else if (node->getName() == "heightmap") { heightMap = node->findAttribute("filename"); loadHightMap(heightMap); } else if (node->getName() == "gridInfo") { gridInfo = node->findAttribute("filename"); gridInfos->open(gridInfo, Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); } else if (node->getName() == "lightmap") { lightMap = node->findAttribute("filename"); } else if (node->getName() == "textures") { auto xtexts = node->getElementEnumerator(); while (xtexts.next()) { //08大理/大理方磚地.tga auto textName = tool::UTF8ToGBK(xtexts->findAttribute("filename").c_str()); textures.push_back(textName); } } else if (node->getName() == "pixmaps") { auto xpixs = node->getElementEnumerator(); while (xpixs.next()) { float left = MyGUI::utility::parseFloat(xpixs->findAttribute("left")); float top = MyGUI::utility::parseFloat(xpixs->findAttribute("top")); float right = MyGUI::utility::parseFloat(xpixs->findAttribute("right")); float bottom = MyGUI::utility::parseFloat(xpixs->findAttribute("bottom")); int textureId = MyGUI::utility::parseInt(xpixs->findAttribute("textureId")); Pixmap* map = new Pixmap(textureId, left, top, right, bottom); pixmaps.push_back(map); } } } } void loadHightMap(const string& fileName) { //DataStreamHolder data = MyGUI::DataManager::getInstance().getData(fileName); auto stream = ResourceGroupManager::getSingletonPtr()->openResource(fileName); if (stream.isNull()) { return; } stream->seek(8); int* size = new int[2]; stream->read(reinterpret_cast<char*>(size), 8); int height = size[0]; int width = size[1]; if (heightMapData.size() > 0) heightMapData.clear(); int dataSize = height * width * 4; heightMapData.resize(dataSize); stream->read(reinterpret_cast<char*>(heightMapData.data()), dataSize * 4); imagePtr = SharedPtr<Image>(new Image()); DataStreamPtr imgstream(new MemoryDataStream(heightMapData.data(), width * height * 4)); imagePtr->loadRawData(imgstream, width, height, PF_FLOAT32_R); } void grenate(int width, int height, TerrainInfo& importData) { importData.WorldSize = xsize * scale.x; importData.TerrainSize = 257; importData.MaxBatchSize = 65; importData.MinBatchSize = 33; importData.InputScale = scale.y; importData.HeightImage = imagePtr.get(); importData.FileName = name; importData.ResourceGroup = DTLGroupName; auto bLoadHight = createLightTexture(lightMap); importData.LayerList.resize(2); string firstLayName; createLayerTexture(width, height, firstLayName, true); importData.LayerList[0].worldSize = xsize * scale.x; importData.LayerList[0].textureNames.push_back(firstLayName); string secondLayName; createLayerTexture(width, height, secondLayName, false); importData.LayerList[1].worldSize = xsize * scale.x; importData.LayerList[1].textureNames.push_back(secondLayName); } void createLayerTexture(int width, int height, string& textureName, bool firstLay) { auto layName = firstLay ? "firstLay" : "secondLay"; auto fullName = name + layName; textureName = fullName; auto texture = Ogre::TextureManager::getSingleton().getByName(fullName); if (!texture.isNull()) { return; } auto scene = Ogre::Root::getSingleton().getSceneManager(DSceneName); auto mRtTexture = Ogre::TextureManager::getSingleton().createManual(fullName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, width, height, 0, Ogre::PF_A8R8G8B8, Ogre::TU_RENDERTARGET); Ogre::RenderTexture* renderTaget = mRtTexture->getBuffer()->getRenderTarget(); auto mRtCamera = scene->createCamera(fullName); renderTaget->addViewport(mRtCamera); renderTaget->getViewport(0)->setClearEveryFrame(false); renderTaget->getViewport(0)->setBackgroundColour(ColourValue(1.0f, 1.0f, 1.0f, 1.0f)); renderTaget->getViewport(0)->setOverlaysEnabled(true); renderTaget->setAutoUpdated(false); renderTaget->setActive(true); MaterialPtr matPtr = MaterialManager::getSingleton().create (fullName, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); Ogre::Pass* pass = matPtr->getTechnique(0)->getPass(0); pass->setLightingEnabled(false); pass->createTextureUnitState(); pass->getTextureUnitState(0)->addFrameTextureName(""); auto tUnit = pass->getTextureUnitState(0); Ogre::Rectangle2D* rect = new Ogre::Rectangle2D(true); //opengl 不知是不是BUG,須要先update一下,才能用manualRender. renderTaget->update(); auto getLocation = [&](int t, float &aleft, float &atop, float& aright, float& abottom){ int width = gridInfos->m_width; int height = gridInfos->m_height; int row = t / width; int col = t % width; aleft = (1.0f / width) * col * 2.0f - 1.0f; atop = 1.0f - (1.0f / height) * row * 2.0f; aright = aleft + 2.0f / width; abottom = atop - 2.0f / height; }; int i = 0; for (auto grid : gridInfos->m_data) { int gridIndex = i++; int index = firstLay ? grid.nFirstLayer : grid.nSecondLayer; if (index < 0 && index >= pixmaps.size()) continue; auto pixmap = pixmaps[index]; int textureIndex = pixmap->textureId; auto left = pixmap->left; auto top = pixmap->top; auto right = pixmap->right; auto bottom = pixmap->bottom; //auto texture = Ogre::TextureManager::getSingletonPtr()->getByName(textures[textureIndex]); tUnit->setFrameTextureName(textures[textureIndex], 0); int op = firstLay ? grid.nFirstLayerOp : grid.nSecondLayerOp; auto leftTop = Vector2(left, top); auto leftBottom = Vector2(left, bottom); auto rightTop = Vector2(right, top); auto rightBottom = Vector2(right, bottom); if (op > 0) { if (op & 1) { swap(leftTop, rightTop); swap(leftBottom, rightBottom); } if (op & 2) { swap(leftTop, leftBottom); swap(rightTop, rightBottom); } if (op & 4) { swap(leftTop, rightTop); swap(leftBottom, rightTop); swap(rightBottom, rightTop); } if (op & 8) { // 非正常索引 if (grid.IndexOrder == 0) { leftBottom.x = rightTop.x; leftBottom.y = rightTop.y; } // 正常索引 else { rightBottom.x = leftTop.x; rightBottom.y = leftTop.y; } } } rect->setUVs(leftTop, leftBottom, rightTop, rightBottom); //獲得當前網格在整個屏幕上的位置(-1,1) getLocation(gridIndex, left, top, right, bottom); rect->setCorners(left, top, right, bottom); Ogre::RenderOperation renderOp; rect->getRenderOperation(renderOp); scene->manualRender(&renderOp, pass, renderTaget->getViewport(0), Ogre::Matrix4::IDENTITY, Ogre::Matrix4::IDENTITY, Ogre::Matrix4::IDENTITY, false); } //std::string saveFile = "d:\\" + fullName + ".png"; //renderTaget->writeContentsToFile(saveFile); renderTaget->removeAllViewports(); scene->destroyCamera(mRtCamera); MaterialManager::getSingleton().remove(fullName); delete rect; } bool createLightTexture(const string& textureName) { auto haveLight = lightMap.size() != 0; if (!haveLight) return false; try { auto texture = Ogre::TextureManager::getSingleton().load(textureName, DTLGroupName); return !texture.isNull(); } catch (...) { return false; } } };
其中我用MyGUI裏提供的XML讀取類來讀取相關的文件,注意若是是中文,而且要拿到Ogre中去查找資料的字符串,應該調用utf8togbk,開始這個位置直接用的utf8編碼,而後在前面生整天龍的地形時,調用manualRender很是慢,怎麼說了,一分鐘都還沒走完,我很奇怪怎麼會這麼慢,用VS的性能和診斷分析了下,定位到加載紋理是最要時間的,而後跑去看,才發現全部紋理都沒加載上.而後比較同一字符串,在Ogre中和Mygui讀出來的char比對才發現是不同的,可是還想着寬字符啥的,可是Ogre自己也是用std::string存的,因此應該是字符編碼的問題,轉成gbk後,兩者的char就同樣了,這樣用前面的manualRender渲染128*128個網格不到一秒了.
到這整個天龍的地形就用Ogre的地形組件加載上去了.