Ogre 编辑器二(用Ogre的地形组件加载天龙八部地形)

  主界面如上文设计完成后,场景刚开始添加了是Ogre例子里的,发现场景里实物太少,于是想到直接把天龙的场景拿下来,天龙网上有源码,参考了下,把天龙的地形用Ogre的地形组件完成了下,如下是效果图:  

  因为主要是加载地形,然后只是简单加载了静态模型,因此场景看着比较简陋,再者因为上传的图片限制,场景复杂后根本传不上来.

  天龙的地形还是比较简单的,如下是天龙的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,这样达到有限的合并网格渲染的目的.

  然后是scale,这个就是对应的在游戏中的实际位置,如xsize是128,那么x轴的实际长度应该是12800,其中y是地形高度的缩放,然后是高度图,网格信息文件,光照图.然后是所有网格要用的纹理,然后是网格要用的分块纹理pixmaps.简单来说,一个纹理分成几块pixmap,而pixmap才是每个网格用到的纹理部分.其中最重要的gridInfo文件是网格信息文件,存放的是每个网格用的二层pixmap id和显示方式.

  天龙的这种地形方式,一般来说会分成十几块(如上面是(128/32)^2=16),每块差不多用到所有纹理的一半左右,综合下来,每次渲染要一百多个单元,不算多也不算少,不过没有LOD,也不会合成远处显示,故天龙的场景一般要开很大雾.

  Ogre地形组件比较复杂,实现的功能比较多,在这 Ogre 1.7的地形系统 简单说了下,其中和天龙不一样的是,天龙中的地形材质针对是每个网格,而Ogre中地形材质一般针对是整个地形,那么我们需要拿到天龙整个地形的纹理,这个纹理要拿到,我们只需想下这个地形是如何组成的就行,由zsize行xsize列个网格组成,每成对应的显示在对应gridInfo上的pixmaps上,这样我们只需要借助RTT把一块纹理分成zsize行xsize列,然后把每个pixmap渲染到对应位置.请看如下代码:

        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;
        }

RTT 天龙地图

  如下是pingpan中对应的二张图:  

  这个方法过程就是生成一张Ogre::TU_RENDERTARGET纹理,调用scene->manualRender方法渲染到这张渲染上就行了,这个方法manualRender是我在看Ogre源码时,他的注释让我特别关注了下,发现用在这个地方完美,最开始我想到这个方法,但是发现生成的图片上啥都没,如果用正常的方法,生成多个Rectangle2D,多个Material,并挂在SceneNode下,然后调用rtt->pdate ,想想就变态,因为我只是暂时用下,生成这么多,我update后要清理多个,并且速度也不见的比这个快,网上找manualRender发现很多人也在说这个渲染不出来,也没人说为啥并给出解决方法,还好,我也是忽然想到了下,在调用manualRender 之前,我先让rtt->update一下,这样就能正常的输出到纹理了,不知这个地方算不算BUG.

  有了这二张纹理后,我们就让正常的流程加载高度图,然后生成TerrainGroup,在原来的地形组件中,Ogre会自动给我们生成对应的着色器代码,包含多层混合,灯光,雾设置,远景Technique等等.请看如下地形加载的代码. 

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);
    }

TLTerrainMaterial

  简单来说,就是我们生成我们自己的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;
            }
        }
    };

TLTerrain文件加载

  其中我用MyGUI里提供的XML读取类来读取相关的文件,注意如果是中文,并且要拿到Ogre中去查找资料的字符串,应该调用utf8togbk,开始这个位置直接用的utf8编码,然后在前面生成天龙的地形时,调用manualRender非常慢,怎么说了,一分钟都还没走完,我很奇怪怎么会这么慢,用VS的性能和诊断分析了下,定位到加载纹理是最要时间的,然后跑去看,才发现所有纹理都没加载上,这里插一句,VS的性能和诊断这个工具太牛了.然后比较同一字符串,在Ogre中和Mygui读出来的char比对才发现是不一样的,但是还想着宽字符啥的,但是Ogre本身也是用std::string存的,所以应该是字符编码的问题,转成gbk后,二者的char就一样了,这样用前面的manualRender渲染128*128个网格不到一秒了.

  到这整个天龙的地形就用Ogre的地形组件加载上去了.

  

时间: 2024-10-28 21:23:03

Ogre 编辑器二(用Ogre的地形组件加载天龙八部地形)的相关文章

Struts2的国际化(二)-利用超链接实现动态加载国际化资源文件

原理:程序是根据Locale来确定国际化资源文件,因此关键之处在于知道 Struts2 框架是如何确定 Local 对象的 ! 由于Struts2 使用 i18n 拦截器处理国际化,并且将其注册在默认的拦截器中,因此,可以通过阅读 I18N 拦截器知道. 具体确定 Locale 对象的过程: > Struts2 使用 i18n 拦截器 处理国际化,并且将其注册在默认的拦截器栈中 > i18n拦截器在执行Action方法前,自动查找请求中一个名为 request_locale 的参数. 如果该参

跟王老师学反射(二):Java类的加载、连接和初始化

跟王老师学反射(二):Java类的加载.连接和初始化 主讲教师:王少华   QQ群号:483773664 学习内容: 了解类的加载.连接和初始化 一.类的生命周期 当我们编写一个java的源文件后,经过编译会生成一个后缀名为class的文件,这种文件叫做字节码文件,只有这种字节码文件才能够在java虚拟机中运行,java类的生命周期就是指一个class文件从加载到卸载的全过程.一个java类的完整的生命周期会经历加载.连接.初始化.使用.和卸载五个阶段,当然也有在加载或者连接之后没有被初始化就直

Android插件化(二):使用DexClassLoader动态加载assets中的apk

Android插件化(二):使用DexClassLoader动态加载assets中的apk 简介 上一篇博客讲到,我们可以使用MultiDex.java加载离线的apk文件.需要注意的是,apk中的类是加载到当前的PathClassLoader当中的,如果apk文件过多,可能会出现ANR的情况.那么,我们能不能使用DexClassLoader加载apk呢?当然是可以的!首先看一下Doc文档. A class loader that loads classes from .jar and .apk

web.xml 组件加载顺序

<web-app> <display-name></display-name>WEB应用的名字 <description></description> WEB应用的描述 <context-param></context-param> context-param元素声明应用范围内的初始化参数 <!– 指定spring配置文件位置 –> <context-param> <param-name>

在Java Web Project中实现Vue异步组件加载

背景 最近看上了ElementUI(Vue实现)用来实现一个管理系统Demo,其中一个最常见的需求就是左侧导航不动,右侧主页块在点击导航菜单时动态更新,如下图所示:之前的实现方案是右边嵌入一个iframe,动态更改iframe的url即可,现在既然用了Vue咱也试试单页,是不是显得更优雅.接着就接触到了vue-router.组件.异步组件这些关键字,本以为把页面定义为xxx.vue放到webapp下,然后告诉vue-router去加载就好了,最后发现自己想简单了,思维模式还停留在Java Web

react 动态组件加载器

在进行react项目,特别是后台管理的项目开发中,表单.提示框等弹出组件的应用是相当广泛的. 而这些弹框组件的状态比如开启.关闭以及内部显示的内容通常都要依靠父级组件的state进行管理. 但是当在一个页面组件中大量的使用到这些弹框的时候,对他们的状态管理会有非常严重的问题,每一个弹框组件都需要相应的state状态控制,这些状态信息一个一个的积累下来会造成state对象的异常臃肿,对代码的维护管理有很大的负面影响. 为了解决这个问题,最好不要将这些组件写死在父级组件里面,不用state状态控制打

Ogre 编辑器一(MyGUI+Ogre配置与主界面)

在查看Ogre例子时,想看材质要里的纹理,着色器代码都需要每个去查找,非常麻烦.也想看更新每个Ogre里的对象后有什么效果.然后看到Compositor组件与粒子组件时,想到能实时编辑着色器代码实时更新渲染. 开始想着C++做界面麻烦,用C#的winForm做,后面发现首先结合层比较麻烦,然后C#与C++一起调试也会比较麻烦,还有一些比较奇怪的异常也会麻烦.好吧,不如全用C++做,在学习能用在Ogre中的UI时,主要了解了包括Ogre自己的Overlay, CEGUI, MyGUI等等,最终选择

EasyUI - DataGrid 组建 - [ 组件加载和排序 ]

效果: 原理:通过POST传递到数据后台字段. 此时上传的参数,page:当前页数,rows:每页显示的页数. 有此两项参数,计算取出数据条数. 通过后台接受参数,进行处理并返回抽取的数据. html代码: <table id="tab"></table> JS代码: 此时显示的列名称,对应的是JSON数据中的列名称. $(function () { $('#tab').datagrid({ width: 500,//宽度 title: '信息列表',//标题名

Angualr2动态组件加载器

在Angular2中,组件是我们建立并指定页面上的元素和逻辑的主要途径.而我们通常加载它在其中包含了一些条件,使其加载动态标记的标签. 但也有情况下,我们不知道在编译时DOM结构.在这种情况下,我们必须对任意位置的需求添加组件,因此我们使用DynamicComponentLoader动态加载的组件.在这种情况下,我们将能够在指定的位置来实例化一个组件,它连接到一个视图. beta16之前,DynamicComponentLoader有三种方法,升级后删除了一种方法 loadAsRoot: 创建了