core/vgui/vgui_blender_tableau.cxx
Go to the documentation of this file.
00001 // This is core/vgui/vgui_blender_tableau.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author Philip C. Pritchett, RRG, University of Oxford
00008 // \date   27 Oct 99
00009 // \brief  See vgui_blender_tableau.h for a description of this file.
00010 
00011 #include "vgui_blender_tableau.h"
00012 
00013 #include <vcl_iostream.h>
00014 
00015 #include <vil/vil_load.h>
00016 #include <vil/vil_new.h>
00017 
00018 #include <vgui/vgui_gl.h>
00019 #include <vgui/vgui_glu.h>
00020 #include <vgui/vgui_event.h>
00021 #include <vgui/vgui_matrix_state.h>
00022 #include <vgui/vgui_image_renderer.h>
00023 #include <vgui/vgui_vil_image_renderer.h>
00024 
00025 //-----------------------------------------------------------------------------
00026 //: Constructor - don't use this, use vgui_blender_tableau_new.
00027 //  Creates a blender with the given image and alpha_ value.
00028 vgui_blender_tableau::
00029 vgui_blender_tableau(char const* file,
00030                      vgui_range_map_params_sptr const& rmp,
00031                      float a)
00032   : renderer_(0),
00033     vil_renderer_(0),
00034     rmp_(rmp),
00035     alpha_(a)
00036 {
00037   vil_renderer_ = new vgui_vil_image_renderer;
00038   vil_renderer_->set_image_resource(vil_load_image_resource(file));
00039   filename_ = vcl_string(file);
00040 }
00041 
00042 //-----------------------------------------------------------------------------
00043 //: Constructor - don't use this, use vgui_blender_tableau_new.
00044 //  Creates a blender with the given image and alpha_ value.
00045 vgui_blender_tableau::
00046 vgui_blender_tableau(vil1_image const& img,
00047                      vgui_range_map_params_sptr const& rmp,
00048                      float a)
00049   : renderer_(0),
00050     vil_renderer_(0),
00051     rmp_(rmp),
00052     alpha_(a)
00053 {
00054   renderer_ = new vgui_image_renderer;
00055   renderer_->set_image(img);
00056   filename_ = vcl_string("unknown");
00057 }
00058 
00059 //-----------------------------------------------------------------------------
00060 //: Constructor - don't use this, use vgui_blender_tableau_new.
00061 //  Creates a blender with the given image and alpha_ value.
00062 vgui_blender_tableau::
00063 vgui_blender_tableau(vil_image_resource_sptr const& img,
00064                      vgui_range_map_params_sptr const& rmp,
00065                      float a)
00066   : renderer_(0),
00067     vil_renderer_(0),
00068     rmp_(rmp),
00069     alpha_(a)
00070 {
00071   vil_renderer_ = new vgui_vil_image_renderer;
00072   vil_renderer_->set_image_resource(img);
00073   filename_ = vcl_string("unknown");
00074 }
00075 
00076 //-----------------------------------------------------------------------------
00077 //: Constructor - don't use this, use vgui_blender_tableau_new.
00078 //  Creates a blender with the given image and alpha_ value.
00079 vgui_blender_tableau::
00080 vgui_blender_tableau(vil_image_view_base const& img,
00081                      vgui_range_map_params_sptr const& rmp,
00082                      float a)
00083   : renderer_(0),
00084     vil_renderer_(0),
00085     rmp_(rmp),
00086     alpha_(a)
00087 {
00088   vil_renderer_ = new vgui_vil_image_renderer;
00089   vil_renderer_->set_image_resource( vil_new_image_resource_of_view( img ) );
00090   filename_ = vcl_string("unknown");
00091 }
00092 
00093 //-----------------------------------------------------------------------------
00094 //: Destructor - called by vgui_blender_tableau_sptr.
00095 vgui_blender_tableau::~vgui_blender_tableau()
00096 {
00097   delete renderer_;
00098   delete vil_renderer_;
00099 }
00100 
00101 //-----------------------------------------------------------------------------
00102 //: Returns the filename_ of the loaded image (if it was loaded from file).
00103 vcl_string vgui_blender_tableau::file_name() const
00104 {
00105   return filename_.c_str();
00106 }
00107 
00108 //-----------------------------------------------------------------------------
00109 // Returns the type of this tableau ('vgui_blender_tableau').
00110 vcl_string vgui_blender_tableau::type_name() const
00111 {
00112   return "vgui_blender_tableau";
00113 }
00114 
00115 //-----------------------------------------------------------------------------
00116 //: Tell the blender that the image pixels have been changed.
00117 void vgui_blender_tableau::reread_image()
00118 {
00119   if ( renderer_ )      renderer_->reread_image();
00120   if ( vil_renderer_ )  vil_renderer_->reread_image();
00121 }
00122 
00123 //-----------------------------------------------------------------------------
00124 //: Handle all events sent to this tableau.
00125 //  In particular, use draw events to draw the blended image.
00126 //  Use '*' and '/' key-press events to change alpha_.
00127 bool vgui_blender_tableau::handle(vgui_event const &e)
00128 {
00129   if (vgui_matrix_state::gl_matrices_are_cleared()) {
00130     GLint vp[4];
00131     glGetIntegerv(GL_VIEWPORT, vp);
00132     int width = vp[2];
00133     int height = vp[3];
00134 
00135     glMatrixMode(GL_PROJECTION);
00136     glLoadIdentity();
00137     gluOrtho2D(0, width, 0, height);
00138 
00139     glMatrixMode(GL_MODELVIEW);
00140     glLoadIdentity();
00141     glScalef(1.0,-1.0,1.0);
00142     glTranslatef(0.0, -height, 0.0);
00143   }
00144 
00145   if (e.type == vgui_DRAW) {
00146 
00147     glEnable(GL_BLEND);
00148     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00149     glPixelTransferf(GL_ALPHA_SCALE, alpha_);
00150 
00151     if ( renderer_ )     renderer_->render(rmp_);
00152     if ( vil_renderer_ ) vil_renderer_->render(rmp_);
00153 
00154     glPixelTransferf(GL_ALPHA_SCALE, 1.0);
00155     glBlendFunc(GL_ONE, GL_ZERO);
00156     glDisable(GL_BLEND);
00157 
00158     return true;
00159   }
00160 
00161   if (e.type == vgui_KEY_PRESS) {
00162     switch (e.key)
00163     {
00164      case '/':
00165       alpha_ -= 0.1f;
00166       if (alpha_ <= 0.0f) alpha_ = 0.0f;
00167 #ifdef DEBUG
00168       vcl_cerr << "blender : alpha_ = " << alpha_ << vcl_endl;
00169 #endif
00170       post_redraw();
00171       return true;
00172      case '*':
00173       alpha_ += 0.1f;
00174       if (alpha_ >= 1.0f) alpha_ = 1.0f;
00175 #ifdef DEBUG
00176       vcl_cerr << "blender : alpha_ = " << alpha_ << vcl_endl;
00177 #endif
00178       post_redraw();
00179       return true;
00180      default:
00181       break;
00182     }
00183   }
00184   return false;
00185 }