// ================================================================================================ // // This file is part of the CAMPVis Software Framework. // // If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved, // Christian Schulte zu Berge // Chair for Computer Aided Medical Procedures // Technische Universität München // Boltzmannstr. 3, 85748 Garching b. München, Germany // For a full list of authors and contributors, please refer to the file "AUTHORS.txt". // // The licensing of this softare is not yet resolved. Until then, redistribution in source or // binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form. // However, the names of the original authors and the above copyright notice must retain in its // original state in any case. // // Legal disclaimer provided by the BSD license: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. // // ================================================================================================ #ifndef IMAGEDATAGL_H__ #define IMAGEDATAGL_H__ #include "tgt/tgt_gl.h" #include "tgt/texture.h" #include "tgt/textureunit.h" #include "tgt/vector.h" #include "core/datastructures/imagedata.h" #include "core/datastructures/genericimagedatalocal.h" #include "core/tools/weaklytypedpointer.h" namespace tgt { class Shader; } namespace campvis { /** * Stores image data as OpenGL texture. * Can bei instantiated either by a WeaklyTypedPointer or strongly typed by an ImageDataRAM. */ class ImageDataGL : public ImageData { public: /** * Creates and returns new ImageDataGL representation from a tgt::Texture. * The caller takes ownership of the returned pointer. * * \note This static factory method serves mainly for convenience to automatically * calculate the dimensionality and size of the texture. * \param texture OpenGL texture to use, must not be 0, ImageDataGL will take ownership of this texture. */ static ImageDataGL* createFromTexture(tgt::Texture* texture); /** * Creates a new ImageDataGL representation from a tgt::Texture. * * \param dimensionality Dimensionality of data * \param size Size of this image (number of elements per dimension) * \param texture OpenGL texture to use, must not be 0, ImageDataGL will take ownership of this texture. */ ImageDataGL(size_t dimensionality, const tgt::svec3& size, tgt::Texture* texture); /** * Creates a new ImageDataGL representation. * * \param dimensionality Dimensionality of data * \param size Size of this image (number of elements per dimension) * \param wtp WeaklyTypedPointer to the image data, must not be 0, ImageDataGL does \b not take ownership of that pointer. */ ImageDataGL(size_t dimensionality, const tgt::svec3& size, const WeaklyTypedPointer& wtp); /** * Creates a new ImageDataGL representation from GenericImageDataLocal. * * \param dimensionality Dimensionality of data * \param size Size of this image (number of elements per dimension) * \param data Pointer to the GenericImageDataLocal instance, must not be 0 * \tparam BASETYPE Base type of image data * \tparam NUMCHANNELS Number of channels per element */ template ImageDataGL(size_t dimensionality, const tgt::svec3& size, const GenericImageDataLocal* data); /** * Destructor */ virtual ~ImageDataGL(); /** * \see AbstractData::clone() **/ virtual ImageDataGL* clone() const; /// \see AbstractData::getLocalMemoryFootprint() virtual size_t getLocalMemoryFootprint() const; /// \see AbstractData::getVideoMemoryFootprint() virtual size_t getVideoMemoryFootprint() const; /** * \see ImageData::getSubImage */ virtual ImageDataGL* getSubImage(const tgt::svec3& llf, const tgt::svec3& urb) const; /** * Binds the texture without activating a texture unit. */ void bind() const; /** * Activates the texture unit \a texUnit and binds the texture. * \param texUnit Texture unit to activate */ void bind(const tgt::TextureUnit& texUnit) const; void bind( tgt::Shader* shader, const tgt::TextureUnit& texUnit, const std::string& texUniform = "_texture") const; /** * Gets the OpenGL texture. * \return _texture */ const tgt::Texture* getTexture() const; protected: /** * Creates the OpenGL texture from the given pointer \a wtp. * \param wtp WeaklyTypedPointer with source image data */ void createTexture(const WeaklyTypedPointer& wtp); /** * Creates the OpenGL texture from the given ImageDataRAM \a data. * \param data Pointer to the GenericImageDataLocal instance, must not be 0 * \tparam BASETYPE Base type of image data * \tparam NUMCHANNELS Number of channels per element */ template void createTexture(const GenericImageDataLocal* data); tgt::Texture* _texture; //< OpenGL texture static const std::string loggerCat_; }; // = Template definition ========================================================================== template campvis::ImageDataGL::ImageDataGL(size_t dimensionality, const tgt::svec3& size, const GenericImageDataLocal* data) : ImageData(dimensionality, size) { createTexture(data); } template void campvis::ImageDataGL::createTexture(const GenericImageDataLocal* data) { tgtAssert(data != 0, "Pointer to image must not be 0!"); _texture = new tgt::Texture( reinterpret_cast(data->getImageData()), _size, TypeTraits::glFormat, TypeTraits::glInternalFormat, TypeTraits::glDataType, tgt::Texture::LINEAR); switch (_dimensionality) { case 1: _texture->setType(GL_TEXTURE_1D); break; case 2: _texture->setType(GL_TEXTURE_2D); break; case 3: _texture->setType(GL_TEXTURE_3D); break; default: tgtAssert(false, "Unsupported dimensionality of image."); break; } _texture->bind(); _texture->uploadTexture(); _texture->setWrapping(tgt::Texture::CLAMP); // revoke ownership of local pixel data from the texture _texture->setPixelData(0); LGL_ERROR; } } #endif // IMAGEDATAGL_H__