core/vgui/internals/vgui_rasterpos.cxx
Go to the documentation of this file.
00001 // This is core/vgui/internals/vgui_rasterpos.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author fsm
00008 
00009 #include "vgui_rasterpos.h"
00010 #include <vnl/vnl_vector_fixed.h>
00011 #include <vnl/vnl_matrix_fixed.h>
00012 #include <vgui/vgui_matrix_state.h>
00013 
00014 // Purpose : a RasterPos() routine which sets the raster position,
00015 // even if GL thinks it is invalid.
00016 void vgui_rasterpos4dv(double const X[4])
00017 {
00018   // This class will restore the matrix state on exit of this
00019   // function.
00020   vgui_matrix_state matrix_state;
00021 
00022   GLint vp[4]; // x,y, w,h
00023   glGetIntegerv(GL_VIEWPORT, vp);
00024 
00025   vnl_matrix_fixed<double,4,4> T = vgui_matrix_state::total_transformation();
00026   vnl_vector_fixed<double,4> tmp = T * vnl_vector_fixed<double,4>(X);
00027   double rx = tmp[0]/tmp[3];
00028   double ry = tmp[1]/tmp[3];
00029 
00030   glMatrixMode(GL_PROJECTION);
00031   glLoadIdentity();
00032 
00033   glMatrixMode(GL_MODELVIEW);
00034   glLoadIdentity();
00035 
00036   //           To set a valid raster position outside the viewport, first
00037   //          set a valid raster position inside the viewport, then call
00038   //          glBitmap with NULL as the bitmap parameter and with xmove
00039   //          and ymove set to the offsets of the new raster position.
00040   //          This technique is useful when panning an image around the
00041   //          viewport.
00042   // (from http://www.opengl.org/developers/faqs/technical/clipping.htm#0070)
00043   glRasterPos2f(0,0);
00044   glBitmap(0,0,
00045            0,0,
00046            float(rx*vp[2]/2), float(ry*vp[3]/2),
00047            0);
00048 }
00049 
00050 
00051 void vgui_rasterpos2f(float x, float y)
00052 {
00053   double X[4]={x,y,0,1};
00054   vgui_rasterpos4dv(X);
00055 }
00056 
00057 
00058 void vgui_rasterpos2i(int x, int y)
00059 {
00060   double X[4]={double(x),double(y),0.0,1.0};
00061   vgui_rasterpos4dv(X);
00062 }
00063 
00064 
00065 bool vgui_rasterpos_valid()
00066 {
00067   GLboolean params;
00068   glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &params);
00069   return params != GL_FALSE;
00070 }