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 "hugin_utils/shared_ptr.h"
00044 #include <huginapp/ImageCache.h>
00045 #include "panodata/Panorama.h"
00046 
00047 //class GLViewer;
00048 class ViewState;
00049 
00050 class TextureManager
00051 {
00052 public:
00053     TextureManager(HuginBase::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     // update the texture when image cache has finished loading
00074     void LoadingImageFinished(int min, int max,
00075         bool texture_photometric_correct,
00076         const HuginBase::PanoramaOptions &dest_img,
00077         const HuginBase::SrcPanoImage &state);
00078 
00079 protected:
00080     HuginBase::Panorama  * m_pano;
00081     ViewState *view_state;
00082     float viewer_exposure;
00083     // remove textures for deleted images.
00084     void CleanTextures();
00085     class TextureInfo
00086     {
00087     public:
00088         explicit TextureInfo(ViewState *new_view_state);
00089         // specify log2(width) and log2(height) for the new texture's size.
00090         // this is the size of mip level 0, which may or may not be defined.
00091         TextureInfo(ViewState *new_view_state, unsigned int width_p, unsigned int height_p);
00092         ~TextureInfo();
00093         // width and height are the size of the texture. This can be different
00094         // to the image size, we have to scale to powers of two. The texture
00095         // size is the biggest we can use for the image without scaling up
00096         // (unless the hardware doesn't support textures that big)
00097         // we generally have only lower mip levels defined though.
00098         unsigned int width, height;
00099         // log base 2 of the above, cached.
00100         unsigned int width_p, height_p;
00101         // min_lod is the most detailed mipmap level defined
00102         int min_lod;
00103         
00104         void DefineLevels(int min, int max,
00105                           bool photometric_correct,
00106                           const HuginBase::PanoramaOptions &dest_img,
00107                           const HuginBase::SrcPanoImage &state);
00108         void DefineMaskTexture(const HuginBase::SrcPanoImage &srcImg);
00109         void UpdateMask(const HuginBase::SrcPanoImage &srcImg);
00110         void SetMaxLevel(int level);
00111         void Bind();
00112         void BindImageTexture();
00113         void BindMaskTexture();
00114         unsigned int GetNumber() {return num;};
00115         // if the image has a mask, we want to use alpha blending to draw it.
00116         bool GetUseAlpha() {return has_mask;};
00117         bool GetHasActiveMasks() {return has_active_masks;};
00118     private:
00119         unsigned int num;     // the openGL texture name
00120         unsigned int numMask; // the openGL texture name for the mask
00121         bool has_mask; // this is the alpha channel
00122         bool has_active_masks; // has active masks
00123         ViewState *m_viewState;
00125         HuginBase::ImageCache::RequestPtr m_imageRequest;
00126         // this binds a new texture in openGL and sets the various parameters
00127         // we need for it.
00128         void CreateTexture();
00129         void SetParameters();
00130     };
00131     
00132     // A TextureKey uniquely identifies a texture. It contains the filename
00133     // of the image and the photometric correction properties used to make it.
00134     class TextureKey
00135     {
00136     public:
00137         TextureKey();
00138         TextureKey(const HuginBase::SrcPanoImage * source, bool *photometric_correct_ptr);
00139             
00140         // TODO all of this lot should probably be made read only
00141         std::string filename;
00142         double exposure, white_balance_red, white_balance_blue;
00143         std::vector<float> EMoR_params;
00144         std::vector<double> radial_vig_corr_coeff;
00145         hugin_utils::FDiff2D radial_vig_corr_center_shift;
00146         int vig_corr_mode;
00147         HuginBase::SrcPanoImage::ResponseType response_type;
00148         std::vector<double> radial_distortion_red;
00149         std::vector<double> radial_distortion_blue;
00150         double gamma;
00151         std::string masks;
00152                 
00153         // when stored in the textures map, this should be set to something that
00154         // always indicates if photometric correction comparisons should be made
00155         bool *photometric_correct;
00156         // we need to specify our own comparison function as the photometric
00157         // correction parameters do not need to be compared when we are not
00158         // using photometric correction.
00159         const bool operator==(const TextureKey& comp) const;
00160         // we need to be able to order the keys
00161         const bool operator<(const TextureKey& comp) const;
00162     private:
00163         void SetOptions(const HuginBase::SrcPanoImage *source);
00164     };
00165     // we map filenames to TexturesInfos, so we can keep track of
00166     // images' textures when the numbers change.
00167     typedef std::map<TextureKey, sharedPtrNamespace::shared_ptr<TextureInfo> > TexturesMap;
00168     TexturesMap textures;
00169     // Our pixel budget for all textures.
00170     unsigned int GetMaxTotalTexels();
00171     // this is the maximum size a single texture is supported on the hardware.
00172     unsigned int GetMaxTextureSizePower(); 
00173     float texel_density;          // multiply by angles to get optimal size.
00174     bool photometric_correct;
00175 };
00176 
00177 #endif
00178    

Generated on 4 Sep 2015 for Hugintrunk by  doxygen 1.4.7