1 /* 2 ----------------------------------------------------------------------------- 3 This source file is part of OGRE 4 (Object-oriented Graphics Rendering Engine) 5 For the latest info, see http://www.ogre3d.org 6 7 Copyright (c) 2000-2014 Torus Knot Software Ltd 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ----------------------------------------------------------------------------- 26 */ 27 #include "OgreVolumeTextureSource.h" 28 #include "OgreTextureManager.h" 29 #include "OgreHardwarePixelBuffer.h" 30 #include "OgreColourValue.h" 31 #include "OgreMemoryAllocatorConfig.h" 32 #include "OgreLogManager.h" 33 #include "OgreTimer.h" 34 35 namespace Ogre { 36 namespace Volume { 37 getVolumeGridValue(size_t x,size_t y,size_t z) const38 float TextureSource::getVolumeGridValue(size_t x, size_t y, size_t z) const 39 { 40 x = x >= mWidth ? mWidth - 1 : x; 41 y = y >= mHeight ? mHeight - 1 : y; 42 z = z >= mDepth ? mDepth - 1 : z; 43 return mData[(mDepth - z - 1) * mWidthTimesHeight + y * mWidth + x]; 44 } 45 46 //----------------------------------------------------------------------- 47 setVolumeGridValue(int x,int y,int z,float value)48 void TextureSource::setVolumeGridValue(int x, int y, int z, float value) 49 { 50 mData[(mDepth - z - 1) * mWidthTimesHeight + y * mWidth + x] = value; 51 } 52 53 //----------------------------------------------------------------------- 54 TextureSource(const String & volumeTextureName,const Real worldWidth,const Real worldHeight,const Real worldDepth,const bool trilinearValue,const bool trilinearGradient,const bool sobelGradient)55 TextureSource::TextureSource(const String &volumeTextureName, const Real worldWidth, const Real worldHeight, const Real worldDepth, const bool trilinearValue, const bool trilinearGradient, const bool sobelGradient) : 56 GridSource(trilinearValue, trilinearGradient, sobelGradient) 57 { 58 59 Timer t; 60 //we to load then read a 3d texture. we cannot load it directly as that will mean we might 61 //not be able to read it. we need to change it's usage from dynamic (default) to a readable static. 62 Ogre::ResourceManager::ResourceCreateOrRetrieveResult res = 63 TextureManager::getSingleton().createOrRetrieve(volumeTextureName, 64 Ogre::ResourceGroupManager::getSingleton().getWorldResourceGroupName(), 65 false, 0, 0, Ogre::TEX_TYPE_3D, 0); 66 Ogre::TexturePtr tex = static_pointer_cast<Texture>(res.first); 67 tex->setUsage(TU_DYNAMIC); 68 tex->load(); 69 70 LogManager::getSingleton().stream() << "Loaded texture in " << t.getMilliseconds() << "ms."; 71 t.reset(); 72 73 mWidth = tex->getSrcWidth(); 74 mHeight= tex->getSrcHeight(); 75 mWidthTimesHeight = mWidth * mHeight; 76 mDepth = tex->getSrcDepth(); 77 78 mPosXScale = (Real)1.0 / (Real)worldWidth * (Real)mWidth; 79 mPosYScale = (Real)1.0 / (Real)worldHeight * (Real)mHeight; 80 mPosZScale = (Real)1.0 / (Real)worldDepth * (Real)mDepth; 81 82 mVolumeSpaceToWorldSpaceFactor = (Real)worldWidth * (Real)mWidth; 83 84 HardwarePixelBufferSharedPtr buffer = tex->getBuffer(0, 0); 85 buffer->lock(HardwareBuffer::HBL_READ_ONLY); 86 const PixelBox &pb = buffer->getCurrentLock(); 87 float *pbptr = reinterpret_cast<float*>(pb.data); 88 mData = OGRE_ALLOC_T(float, mWidth * mHeight * mDepth, MEMCATEGORY_GENERAL); 89 float * dataRunner = mData; 90 ColourValue cv; 91 size_t x, y; 92 size_t zEnd = pb.back; 93 size_t yStart = pb.top; 94 size_t yEnd = pb.bottom; 95 size_t xStart = pb.left; 96 size_t xEnd = pb.right; 97 size_t sliceSkip = pb.getSliceSkip(); 98 for (size_t z = pb.front; z < zEnd; ++z) 99 { 100 for (y = yStart; y < yEnd; ++y) 101 { 102 for (x = xStart; x < xEnd; ++x) 103 { 104 *dataRunner++ = pbptr[x]; 105 } 106 pbptr += pb.rowPitch; 107 } 108 pbptr += sliceSkip; 109 } 110 buffer->unlock(); 111 112 TextureManager::getSingleton().remove(tex->getHandle()); 113 114 LogManager::getSingleton().stream() << "Processed texture in " << t.getMilliseconds() << "ms."; 115 } 116 117 //----------------------------------------------------------------------- 118 ~TextureSource(void)119 TextureSource::~TextureSource(void) 120 { 121 OGRE_FREE(mData, MEMCATEGORY_GENERAL); 122 } 123 124 } 125 } 126