TextureManager.h

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00023 /* a TextureManager sets up openGL textures for each of the input images in a
00024  * panorama. It scales images down so they should fit in graphics memory.
00025  */
00026 
00027 /* Texture sizes must be powers of two. The texture sizes are the biggest size
00028  * that does not scale up the input image, or the biggest size supported by the
00029  * hardware, whichever is smaller. However, not all of the texture is defined
00030  * at any time, so the memory consumption is limited.
00031  * The textures are mipmapped, i.e. they use progressivly smaller versions.
00032  * The mipmaps are defined in a range from the smallest 1*1 pixel version to
00033  * the largest mipmap that doesn't go over the texture space budget. OpenGL
00034  * uses mip level 0 to be the original image, and log2(max(width, height)) + 1
00035  * to be the smallest 1 by 1 pixel image. We use this convention too.
00036  */
00037 
00038 #ifndef _TextureManager_h
00039 #define _TextureManager_h
00040 
00041 #include <string>
00042 #include <map>
00043 #include <boost/signals/trackable.hpp>
00044 #include <huginapp/ImageCache.h>
00045 #include "PT/Panorama.h"
00046 
00047 //class GLViewer;
00048 class ViewState;
00049 
00050 class TextureManager
00051 {
00052 public:
00053     TextureManager(PT::Panorama *pano, ViewState *view);
00054     virtual ~TextureManager();
00055     // selct the texture for the requested image in opengl
00056     void DrawImage(unsigned int image_number, unsigned int display_list);
00057     // react to the images & fields of view changing. We can update the
00058     // textures here.
00059     void CheckUpdate();
00060     // change the OpenGL state for rendering the textures.
00061     void Begin();
00062     void End();
00063     // set to true if we are doing photmetric correction
00064     void SetPhotometricCorrect(bool state);
00065     // return true if we are doing photometric correction.
00066     bool GetPhotometricCorrect() {return photometric_correct;}
00067     // get the OpneGL texture name for a given image
00068     unsigned int GetTextureName(unsigned int image_number);
00069     // binds the texture for a given image
00070     void BindTexture(unsigned int image_number);
00071     // disables the image textures
00072     void DisableTexture(bool maskOnly=false);
00073 
00074 protected:
00075     PT::Panorama  * m_pano;
00076     ViewState *view_state;
00077     float viewer_exposure;
00078     // remove textures for deleted images.
00079     void CleanTextures();
00080     class TextureInfo
00081         // If the TextureInfo is deleted while the image is loading, we want to
00082         // avoid a segfault when the image loaded event happens. So we let the
00083         // signal react to destruction of a TextureInfo.
00084         : public boost::signals::trackable
00085     {
00086     public:
00087         TextureInfo(ViewState *new_view_state);
00088         // specify log2(width) and log2(height) for the new texture's size.
00089         // this is the size of mip level 0, which may or may not be defined.
00090         TextureInfo(ViewState *new_view_state, unsigned int width_p, unsigned int height_p);
00091         ~TextureInfo();
00092         // width and height are the size of the texture. This can be different
00093         // to the image size, we have to scale to powers of two. The texture
00094         // size is the biggest we can use for the image without scaling up
00095         // (unless the hardware doesn't support textures that big)
00096         // we generally have only lower mip levels defined though.
00097         unsigned int width, height;
00098         // log base 2 of the above, cached.
00099         unsigned int width_p, height_p;
00100         // min_lod is the most detailed mipmap level defined
00101         int min_lod;
00102         
00103         void DefineLevels(int min, int max,
00104                           bool photometric_correct,
00105                           const HuginBase::PanoramaOptions &dest_img,
00106                           const HuginBase::SrcPanoImage &state);
00107         void DefineMaskTexture(const HuginBase::SrcPanoImage &srcImg);
00108         void UpdateMask(const HuginBase::SrcPanoImage &srcImg);
00109         void SetMaxLevel(int level);
00110         void Bind();
00111         void BindImageTexture();
00112         void BindMaskTexture();
00113         unsigned int GetNumber() {return num;};
00114         // if the image has a mask, we want to use alpha blending to draw it.
00115         bool GetUseAlpha() {return has_mask;};
00116         bool GetHasActiveMasks() {return has_active_masks;};
00117     private:
00118         unsigned int num;     // the openGL texture name
00119         unsigned int numMask; // the openGL texture name for the mask
00120         bool has_mask; // this is the alpha channel
00121         bool has_active_masks; // has active masks
00122         ViewState *m_viewState;
00124         HuginBase::ImageCache::RequestPtr m_imageRequest;
00125         // this binds a new texture in openGL and sets the various parameters
00126         // we need for it.
00127         void CreateTexture();
00128         void SetParameters();
00129     };
00130     
00131     // A TextureKey uniquely identifies a texture. It contains the filename
00132     // of the image and the photometric correction properties used to make it.
00133     class TextureKey
00134     {
00135     public:
00136         TextureKey();
00137         TextureKey(HuginBase::SrcPanoImage * source, bool *photometric_correct_ptr);
00138             
00139         // TODO all of this lot should probably be made read only
00140         std::string filename;
00141         double exposure, white_balance_red, white_balance_blue;
00142         std::vector<float> EMoR_params;
00143         std::vector<double> radial_vig_corr_coeff;
00144         hugin_utils::FDiff2D radial_vig_corr_center_shift;
00145         int vig_corr_mode;
00146         HuginBase::SrcPanoImage::ResponseType response_type;
00147         std::vector<double> radial_distortion_red;
00148         std::vector<double> radial_distortion_blue;
00149         double gamma;
00150         std::string masks;
00151                 
00152         // when stored in the textures map, this should be set to something that
00153         // always indicates if photometric correction comparisons should be made
00154         bool *photometric_correct;
00155         // we need to specify our own comparison function as the photometric
00156         // correction parameters do not need to be compared when we are not
00157         // using photometric correction.
00158         const bool operator==(const TextureKey comp) const;
00159         // we need to be able to order the keys
00160         const bool operator<(const TextureKey comp) const;
00161     private:
00162         void SetOptions(HuginBase::SrcPanoImage *source);
00163     };
00164     // we map filenames to TexturesInfos, so we can keep track of
00165     // images' textures when the numbers change.
00166     std::map<TextureKey, TextureInfo> textures;
00167     // Our pixel budget for all textures.
00168     unsigned int GetMaxTotalTexels();
00169     // this is the maximum size a single texture is supported on the hardware.
00170     unsigned int GetMaxTextureSizePower(); 
00171     float texel_density;          // multiply by angles to get optimal size.
00172     bool photometric_correct;
00173 };
00174 
00175 #endif
00176    

Generated on Tue May 21 01:25:38 2013 for Hugintrunk by  doxygen 1.3.9.1