core/vgui/vgui_image_renderer.cxx
Go to the documentation of this file.
00001 // This is core/vgui/vgui_image_renderer.cxx
00002 #include "vgui_image_renderer.h"
00003 //:
00004 // \file
00005 // \author fsm
00006 // \brief  See vgui_image_renderer.h for a description of this file.
00007 //
00008 // \verbatim
00009 //  Modifications
00010 //   15-AUG-2000 Marko Bacic, Oxford RRG -- Now uses new routines for image
00011 //                                          rendering via textures
00012 //   23-AUG-2000 Marko Bacic, Oxford RRG -- Now uses vgui_cache_wizard
00013 //   08-AUG-2000 Marko Bacic, Oxford RRG -- Minor changes
00014 //   06-AUG-2003 Amitha Perera -- Remove texture mapping.
00015 //   27-DEC-2004 J.L Mundy     -- Added range mapping to control image display
00016 // \endverbatim
00017 
00018 #include <vgui/vgui_gl.h>
00019 #include <vgui/vgui_section_buffer.h>
00020 #include <vgui/vgui_range_map_params.h>
00021 
00022 //-----------------------------------------------------------------------------
00023 //: Constructor - create an empty image renderer.
00024 vgui_image_renderer::vgui_image_renderer()
00025   : buffer(0), buffer_params(0), valid_buffer(false), use_texture_mapping(false)
00026 {
00027 }
00028 
00029 //-----------------------------------------------------------------------------
00030 //: Destructor - delete image buffer.
00031 vgui_image_renderer::~vgui_image_renderer()
00032 {
00033   if (buffer)
00034     delete buffer;
00035 }
00036 
00037 //-----------------------------------------------------------------------------
00038 void vgui_image_renderer::need_resection() const
00039 {
00040   // Not implemented since we use only one section buffer at the moment.
00041   // Ideally, the set_image() method should be implemented by :
00042   // {
00043   //   the_image = image_;
00044   //   need_resection();
00045   // }
00046   // Next time the render() method asks for a section_buffer, the buffer
00047   // would then be resectioned first.
00048 }
00049 
00050 //-----------------------------------------------------------------------------
00051 //: Attach the renderer to a new vil1_image.
00052 void vgui_image_renderer::set_image(vil1_image const &image_)
00053 {
00054   if (image_ == the_image)
00055     return; // same image -- do nothing.
00056 
00057   // delete old buffer. we could try to reuse it.
00058   if (buffer)
00059     delete buffer;
00060   buffer = 0;
00061 
00062   the_image = image_;
00063   valid_buffer = false;
00064 }
00065 
00066 //-----------------------------------------------------------------------------
00067 //: Tell the image renderer that the image has been changed, and should be re-read.
00068 void vgui_image_renderer::reread_image()
00069 {
00070   delete buffer;
00071   buffer = 0;
00072   valid_buffer = false;
00073 }
00074 
00075 void vgui_image_renderer::
00076 create_buffer(vgui_range_map_params_sptr const& rmp)
00077 {
00078   delete buffer;
00079 
00080   buffer = new vgui_section_buffer(0, 0,
00081                                    the_image.width(), the_image.height(),
00082                                    GL_NONE, GL_NONE);
00083   buffer->apply( the_image, rmp );
00084 
00085   buffer_params = rmp;
00086   valid_buffer = true;
00087 }
00088 
00089 void vgui_image_renderer::
00090 draw_pixels()
00091 {
00092   buffer->draw_as_image() || buffer->draw_as_rectangle();
00093 }
00094 
00095 bool vgui_image_renderer::
00096 render_directly(vgui_range_map_params_sptr const& rmp)
00097 {
00098   //hardware mapping not currently supported for vil1 images
00099   if (rmp)//test needed to eliminate warning of unused parameter
00100     vcl_cout << "No hardware mapping support for vil1 images\n";
00101   return false;
00102 }
00103 
00104 //-----------------------------------------------------------------------------
00105 // draw the image :
00106 void vgui_image_renderer::render(vgui_range_map_params_sptr const& rmp)
00107 {
00108   if (!the_image)
00109     return;
00110 
00111   //If the image can be mapped then there is no point in having a
00112   //GL buffer.  The image can be directly rendered by the hardware
00113   //using the range map.
00114   if (rmp&&rmp->use_glPixelMap_&&this->render_directly(rmp))
00115     return;
00116 
00117   // Delay sectioning until first render time. This allows the section
00118   // buffer to decide on a cache format which depends on the current GL
00119   // rendering context.
00120 
00121   if (!this->old_range_map_params(rmp)||!valid_buffer)
00122     this->create_buffer(rmp);
00123 
00124   this->draw_pixels();
00125 }
00126 
00127 //: Are the range map params associated with the current buffer out of date?
00128 //  If so we have to refresh the buffer.
00129 bool vgui_image_renderer::
00130 old_range_map_params(vgui_range_map_params_sptr const& rmp)
00131 {
00132   //Cases
00133 
00134   //1) Both the current params and the new params are null
00135   if (!buffer_params&&!rmp)
00136     return true;
00137 
00138   //2) The current params are null and the new params are not
00139   if (!buffer_params&&rmp)
00140     return false;
00141 
00142   //3) The current params are not null and the new params are
00143   if (buffer_params&&!rmp)
00144     return false;
00145 
00146   //4) Both current params and the new params are not null.
00147   // Are they equal?
00148   if (buffer_params&&rmp)
00149     return *buffer_params==*rmp;
00150 
00151   return false;
00152 }