zoukankan      html  css  js  c++  java
  • 实验七 记两个Ogre中的小工具

    这几天整一个地形编辑器,越想越美,想了太多功能,结果自然是把自己弄的焦头烂额。。。还好目前似乎拔出几根线头,一切正在朝好的方向前进!yeah!

    静下来,好好整理一下收获,顺手先记两个Ogre中的小工具,热手:

    1,

    将数字转换为String

    StringConverter::toString( StaticEntityIndex++ ), sValue );

    (很sb地干了件事,自己去写这个类,到头来才晕晕乎乎地想起那里见过,Orz。。。)

    2

    将任意Texture保存为图片:

    盗来的,哼哼~~~

    稍微完善下吧,注意加上

    Lib:

    RenderSystem_Direct3D9_d.lib d3dx9.lib d3d9.lib

    Include:(如果编译器说找不到,自己手动到Ogre目录下拷过去)

    #include <OgreD3D9Texture.h>
    #include <OgreD3D9Mappings.h>

    详细代码如下:
    bool _saveTextureToLocal(Ogre::TexturePtr texPtr, const Ogre::String& fileName) 
      { 
          Ogre::HardwarePixelBufferSharedPtr tmpTexBuf = texPtr->getBuffer(); 
          int width = texPtr->getWidth(); 
          int height= texPtr->getHeight(); 
    
          char* tmpBuf = new char[width*height*4]; 
          Ogre::PixelBox tmpBox(width, 
              height, 
              texPtr->getDepth(), 
              texPtr->getFormat(), 
              tmpBuf); 
          tmpTexBuf->blitToMemory(tmpBox); 
    
          TexturePtr tmpTexPtr = TextureManager::getSingleton().createManual ( fileName, "General", 
              TEX_TYPE_2D, texPtr->getWidth(), texPtr->getHeight(), 1, 0, PF_A8B8G8R8 ); 
          tmpTexPtr->load(); 
    
          HRESULT hr; 
          IDirect3DSurface9 *pDstSurface = 0; 
          Ogre::D3D9Texture *d3dTex = reinterpret_cast< D3D9Texture * >(tmpTexPtr.get()); 
          RECT dstRC = {0, 0, d3dTex->getWidth(), d3dTex->getHeight()}; 
    
          if( FAILED( hr = d3dTex->getNormTexture()->GetSurfaceLevel(0, &pDstSurface) ) ) 
          { 
              return false; 
          } 
    
          size_t rowWidth; 
          if (PixelUtil::isCompressed(tmpBox.format)) 
          { 
              // D3D wants the width of one row of cells in bytes 
              if (tmpBox.format == PF_DXT1) 
              { 
                  // 64 bits (8 bytes) per 4x4 block 
                  rowWidth = (tmpBox.rowPitch / 4) * 8; 
              } 
              else 
              { 
                  // 128 bits (16 bytes) per 4x4 block 
                  rowWidth = (tmpBox.rowPitch / 4) * 16; 
              } 
          } 
          else 
          { 
              rowWidth = tmpBox.rowPitch * PixelUtil::getNumElemBytes(tmpBox.format); 
          } 
    
          if( FAILED( hr = D3DXLoadSurfaceFromMemory( 
              pDstSurface, NULL, &dstRC, tmpBox.data, 
              Ogre::D3D9Mappings::_getPF( tmpBox.format), 
              rowWidth, 0, 
              &dstRC, D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER, 0)) ) 
          { 
              SAFE_RELEASE(pDstSurface); 
              return false; 
          } 
    
          Ogre::HardwarePixelBufferSharedPtr tmpDesTexBuf = tmpTexPtr->getBuffer(); 
          char* tmpDesBuf = new char[width*height*4]; 
          Ogre::PixelBox tmpDesBox(width, 
              height, 
              tmpTexPtr->getDepth(), 
              tmpTexPtr->getFormat(), 
              tmpDesBuf); 
          tmpDesTexBuf->blitToMemory(tmpDesBox); 
          Ogre::Image image; 
          image.loadDynamicImage((Ogre::uchar*)tmpDesBox.data, tmpDesBox.getWidth(), tmpDesBox.getHeight(), tmpDesBox.format); 
          image.save(fileName); 
    
          delete [] tmpDesBuf; 
          delete [] tmpBuf; 
    
          tmpTexPtr.setNull(); 
    
          return true; 
      }

    保存文件名的时候注意,要加上后缀,支持相当广泛

    比如

    _saveTextureToLocal(layer0,"First.dds");

    感谢万能的开源组织吧!

  • 相关阅读:
    配置sqlserver端口
    HTML5本地存储之localStorage
    初始化ArrayList的方法
    like 模糊查询 mybatis写法
    关于删除 值的引用 导致入参丢失的问题。
    学生做题分析功能设计(正确率,已做)
    关于商城类项目 商品表如何设计的问题
    修改Mysql的自增Id
    java list转map的几种方式
    jxls 入门及几个比较坑的地方
  • 原文地址:https://www.cnblogs.com/Zephyroal/p/1955378.html
Copyright © 2011-2022 走看看