菜鸟学习OGRE和天龙八部之十七: 修正部分地图载入的通用性问题,附源码
有的地图不是tile的整数倍,有的地图没有WCollision文件,有的地图有centre等
有的地图没有光照图等等
比如武当的地图不是tile的整数倍,那么就需要调整tile大小,其实只需要稍微改动
源码没有经过优化的,比如不需要更改的函数参数可以全部改成const引用,提高效率的
自己去优化了,
- void TLBBTerrain::createTerrain()
- {
- // 生成材质, 看是否有光照图
- if(mHasLightMap){
- createLightMaterial();
- }else{
- createMaterial();
- }
- // 判断地图大小是否能被tile(32)整除,不能整除的就矫正tile大小
- bool isAdjustCol = false; // 是否矫正tile的大小
- bool isAdjustRow = false;
- if(mXSize%mTileSize != 0){
- isAdjustCol = true;
- }
- if(mZSize%mTileSize != 0){
- isAdjustRow = true;
- }
- // tile的行数和列数,不管能否被整除都适用
- mTileCol = mXSize/mTileSize;
- mTileRow = mZSize/mTileSize;
- // WCollision
- createWCollision();
- // 地形mesh
- for(size_t matIndex = 0; matIndex < mMaterialNum; ++ matIndex){
- // 同材质的mesh按tile区域生成
- for(int tileIndex =0; tileIndex < mTileCol*mTileRow; ++ tileIndex){
- if(createTileMesh(tileIndex, matIndex, isAdjustCol, isAdjustRow)){
- Entity* entity = mSceneMgr->createEntity("tile"+StringConverter::toString(tileIndex + matIndex*mTileCol*mTileRow), "GridMesh" + StringConverter::toString(tileIndex + matIndex*mTileCol*mTileRow));
- entity->setCastShadows(false);
- mSceneMgr->getRootSceneNode()->createChildSceneNode(mCentre)->attachObject(entity);
- }
- }
- }
- }
- // 分别用mesh和ManualObject生成地形
- bool TLBBTerrain::createTileMesh(int tileIndex, int materialIndex, bool isAdjustCol, bool isAdjustRow)
- {
- // 方法1, ManualObject
- const Real width = 1;
- ManualObject mo("GridObject" + StringConverter::toString(tileIndex + materialIndex*mMaterialNum));
- mo.begin("material"+StringConverter::toString(materialIndex), RenderOperation::OT_TRIANGLE_LIST);
- int k = 0;
- bool hasMesh = false;
- int tileSizeX = mTileSize;
- int tileSizeZ = mTileSize;
- int tileIndex_x = tileIndex%(mXSize/mTileSize);
- int tileIndex_z = tileIndex/(mXSize/mTileSize);
- // 如果地图大小不能被tile整除,就需要矫正
- if(isAdjustCol && tileIndex_x == mXSize/mTileSize - 1){
- tileSizeX = mTileSize + mXSize%mTileSize;
- }
- if(isAdjustRow && tileIndex_z == mZSize/mTileSize - 1){
- tileSizeZ = mTileSize + mZSize%mTileSize;
- }
- // 地图可以被tile整除
- for(int j = 0; j < tileSizeX*tileSizeZ; ++ j){
- // 把每个tile的坐标转换成全图坐标
- int row = j/tileSizeX + tileIndex_z*mTileSize;
- int col = j%tileSizeX + tileIndex_x*mTileSize;
- int mapIndex = col + row*mXSize;
- Ogre::String str;
- if(mHasLightMap){
- str = mLightMaterialData[mapIndex];
- }else{
- str = mMaterialData[mapIndex];
- }
- if(str == "material"+StringConverter::toString(materialIndex) && mGridData[mapIndex].nFirstLayer >= 0){
- // 此tile含有此材质的mesh
- hasMesh = true;
- // 高度图坐标转换
- int heightMapIndex = mapIndex + mapIndex/mXSize;
- // 第一层纹理坐标
- Real left1 = mPixMapData[mGridData[mapIndex].nFirstLayer].left;
- Real right1 = mPixMapData[mGridData[mapIndex].nFirstLayer].right;
- Real top1 = mPixMapData[mGridData[mapIndex].nFirstLayer].top;
- Real bottom1 = mPixMapData[mGridData[mapIndex].nFirstLayer].bottom;
- Vector2 left_top_1(left1, top1);
- Vector2 right_top_1(right1, top1);
- Vector2 right_bottom_1(right1, bottom1);
- Vector2 left_bottom_1(left1, bottom1);
- // 图片翻转等操作
- flipPicture(mGridData[mapIndex].nFirstLayerOp, left_top_1, right_top_1, left_bottom_1, right_bottom_1, mGridData[mapIndex].IndexOrder);
- // 第二层纹理坐标
- Vector2 left_top_2 = Vector2::ZERO;
- Vector2 right_top_2 = Vector2::ZERO;
- Vector2 right_bottom_2 = Vector2::ZERO;
- Vector2 left_bottom_2 = Vector2::ZERO;
- if(mGridData[mapIndex].nSecondLayer >= 0){
- Real left2 = mPixMapData[mGridData[mapIndex].nSecondLayer].left;
- Real right2 = mPixMapData[mGridData[mapIndex].nSecondLayer].right;
- Real top2 = mPixMapData[mGridData[mapIndex].nSecondLayer].top;
- Real bottom2 = mPixMapData[mGridData[mapIndex].nSecondLayer].bottom;
- left_top_2 = Vector2(left2, top2);
- right_top_2 = Vector2(right2, top2);
- right_bottom_2 = Vector2(right2, bottom2);
- left_bottom_2 = Vector2(left2, bottom2);
- flipPicture(mGridData[mapIndex].nSecondLayerOp, left_top_2, right_top_2, left_bottom_2, right_bottom_2, mGridData[mapIndex].IndexOrder);
- }
- // 光照图纹理坐标
- Vector2 left_top_3 = Vector2::ZERO;
- Vector2 right_top_3 = Vector2::ZERO;
- Vector2 right_bottom_3 = Vector2::ZERO;
- Vector2 left_bottom_3 = Vector2::ZERO;
- if(mHasLightMap){
- Real left3 = (mapIndex%mXSize)/(Real)mXSize;
- Real right3 = left3 + 1/(Real)mXSize;
- Real top3 = (mapIndex/mXSize)/(Real)mZSize;
- Real bottom3 = top3 + 1/(Real)mZSize;
- left_top_3 = Vector2(left3, top3);
- right_top_3 = Vector2(right3, top3);
- right_bottom_3 = Vector2(right3, bottom3);
- left_bottom_3 = Vector2(left3, bottom3);
- }
- // 点0
- mo.position((mapIndex%mXSize)*mScaleX, mHeightMapData[heightMapIndex]*mScaleY, (mapIndex/mXSize)*mScaleZ);
- mo.normal(getNormalAt(mapIndex%mXSize, mapIndex/mXSize));
- mo.textureCoord(left_top_1);
- if(mGridData[mapIndex].nSecondLayer >= 0){
- mo.textureCoord(left_top_2);
- }
- if(mHasLightMap){
- mo.textureCoord(left_top_3); // 光照图纹理坐标
- }
- // 点1
- mo.position((mapIndex%mXSize+width)*mScaleX, mHeightMapData[heightMapIndex+1]*mScaleY, (mapIndex/mXSize)*mScaleZ);
- mo.normal(getNormalAt(mapIndex%mXSize+width, mapIndex/mXSize));
- mo.textureCoord(right_top_1);
- if(mGridData[mapIndex].nSecondLayer >= 0){
- mo.textureCoord(right_top_2);
- }
- if(mHasLightMap){
- mo.textureCoord(right_top_3); // 光照图纹理坐标
- }
- // 点2
- mo.position((mapIndex%mXSize+width)*mScaleX, mHeightMapData[heightMapIndex+mXSize+2]*mScaleY, (width+mapIndex/mXSize)*mScaleZ);
- mo.normal(getNormalAt(mapIndex%mXSize+width, mapIndex/mXSize+width));
- mo.textureCoord(right_bottom_1);
- if(mGridData[mapIndex].nSecondLayer >= 0){
- mo.textureCoord(right_bottom_2);
- }
- if(mHasLightMap){
- mo.textureCoord(right_bottom_3); // 光照图纹理坐标
- }
- // 点3
- mo.position((mapIndex%mXSize)*mScaleX, mHeightMapData[heightMapIndex+mXSize+1]*mScaleY, (width+mapIndex/mXSize)*mScaleZ);
- mo.normal(getNormalAt(mapIndex%mXSize, mapIndex/mXSize+width));
- mo.textureCoord(left_bottom_1);
- if(mGridData[mapIndex].nSecondLayer >= 0){
- mo.textureCoord(left_bottom_2);
- }
- if(mHasLightMap){
- mo.textureCoord(left_bottom_3); // 光照图纹理坐标
- }
- // 三角形索引顺序
- int offset = k * 4;
- if(mGridData[mapIndex].IndexOrder == 0){ // 正常顺序
- mo.triangle(offset+1, offset, offset+3);
- mo.triangle(offset+1, offset+3, offset+2);
- }else{
- mo.triangle(offset, offset+3, offset+2);
- mo.triangle(offset, offset+2, offset+1);
- }
- ++ k;
- }
- }
- mo.end();
- if(hasMesh){
- mo.convertToMesh("GridMesh" + StringConverter::toString(tileIndex + materialIndex*mTileCol*mTileRow));
- return true;
- }
- return false;
- }
如图,武当正确载入
老是看峨眉那图,都看烦了!换几个新鲜的
菜鸟学习OGRE和天龙八部之十七: 修正部分地图载入的通用性问题,附源码相关推荐
- android flv 编码器,Android 音视频深入 十七 FFmpeg 获取 RTMP 流保存为 flv (附源码下载)...
Android 音视频深入 十七 FFmpeg 获取 RTMP 流保存为 flv (附源码下载) 项目地址 https://github.com/979451341/RtmpSave 这个项目主要代码 ...
- 菜鸟学习OGRE和天龙八部之十八: 获得档案(Archive)文件列表
要获得档案文件的文件列表,只要获得Archive的指针,就可以调用list()函数获得文件列表 但是如何获得Archive的指针呢,先看看资源的载入过程: 先从resources.cfg文件获取资源的 ...
- 【深度强化学习】神经网络、爬山法优化控制倒立摆问题实战(附源码)
需要源码请点赞关注收藏后评论区留言私信~~~ 直接优化策略 直接优化策略强化学习算法中,根据采用的是确定性策略还是随机性策略,又分为确定性策略搜索和随机性策略搜索两类.随机性策略搜索算法有策略梯度法和 ...
- 【ML-SVM案例学习】案例一:对鸢尾花数据进行SVM分类(附源码)
文章目录 前言 一.完整源码分步实现 1.引入库 2.读入数据 3.编码数据 4.数据分割 5.数据SVM分类器构建 6.计算模型的准确率/精度 7.计算决策函数的结构值以及预测值 8.画图 总结 前 ...
- 深度强化学习之gym扫地机器人环境的搭建(持续更新算法,附源码,python实现)
想要源码可以点赞关注收藏后评论区留下QQ邮箱 本次利用gym搭建一个扫地机器人环境,描述如下: 在一个5×5的扫地机器人环境中,有一个垃圾和一个充电桩,到达[5,4]即图标19处机器人捡到垃圾,并结束 ...
- 菜鸟学习OGRE和天龙八部之二: 天龙八部AXP数据格式搞定
站位编辑,过段时间再写...因为现在有大侠写了资源提取工具,我暂时不用自己去写了哇 ---------------------------------------------------------- ...
- 通过自定义组件学习Vue系列(二)【时间轴】(附源码)
需求: 用于升级日志的显示 效果图: 实现原理: 主要区域分为两块,时间区和内容区,时间区是画一个圆点和显示一个时间,内容区左边一个竖线和文字显示 然后做一下循环,将每个日期的数据显示出来 布局采用f ...
- 菜鸟学习OGRE和天龙八部之四: 地表贴图的实现
地表我已经成功载入,也走了不少弯路...犯了很多低级错误...不然半天都可以搞定的事情,郁闷 天龙八部的地表算是比较难搞的了,至少我这样认为,由于自己没多少OGRE经验,不清楚地形的载入过程,于是看T ...
- 前海征信“好信杯”大数据算法竞赛 - HM队【附源码】 原创 2017-06-17 高铭 科赛Kesci 赛题回顾 自2006年Hinton等人提出“深度学习”概念至今,深度学习在海量数据的挖
前海征信"好信杯"大数据算法竞赛 - H&M队[附源码] 原创 2017-06-17 高铭 科赛Kesci 赛题回顾 自2006年Hinton等人提出"深度学习& ...
最新文章
- PostgreSQL 10.1 手册_部分 II. SQL 语言_第 9 章 函数和操作符_9.15. JSON 函数和操作符...
- Java基础概念(一)
- Python学习笔记——文件写入和读取
- 机器学习基础(一)——人工神经网络与简单的感知器
- hashmap 判断key是否存在
- centos国内yum源
- 2013年人人校园招聘笔试题
- 字符串函数参数传入传出(字符串反转)
- 自己动手写事件总线(EventBus)
- Duplicate entry ‘XXX‘ for key
- 如何运行python代码
- android手机设置固定dns,安卓手机修改WiFi DNS设置的方法
- Crashing Balloon
- 在印度与软件相关的发明可不可以申请专利?
- TCP/IP详解卷1:第十四章 DNS 域名系统
- 华为交换机端口安全配置
- [读书笔记] 代码整洁之道(一)
- lougu3906 Geodetic
- 【产品实操】手把手教你安装 TDH8.1.0
- 时间与日期插件 -- laydate 使用方法(摘自官网)