ViewPort3D.H

Go to the documentation of this file.
00001 /*!@file GUI/ViewPort3D.H  3D rendering view port */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/GUI/ViewPort3D.H $
00035 // $Id: ViewPort3D.H 14542 2011-02-23 21:07:42Z dicky $
00036 //
00037 
00038 #ifndef ViewPort3D_H_DEFINED
00039 #define ViewPort3D_H_DEFINED
00040 
00041 #include "Util/Types.H"
00042 #include "Image/Image.H"
00043 #include "Image/Pixels.H"
00044 #include "Image/ImageSet.H"
00045 #include "Image/Point2D.H"
00046 #include "Image/Point3D.H"
00047 
00048 #ifdef HAVE_GL_GLEW_H
00049 #include <GL/glew.h>
00050 #endif
00051 
00052 #include <X11/Xlib.h>
00053 #include <X11/Xutil.h>
00054 #include <GL/gl.h>
00055 #include <GL/glx.h>
00056 
00057 
00058 #ifdef INVT_HAVE_LIBGLUT
00059 #include <GL/glut.h>
00060 #endif
00061 
00062 #include <vector>
00063 
00064 typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
00065 
00066 class ViewPort3D
00067 {
00068 public:
00069 
00070 
00071   struct Line
00072   {
00073     Point2D<float> p1;
00074     Point2D<float> p2;
00075   };
00076 
00077 
00078   ViewPort3D(int width, int height, 
00079       bool wireframe = false, bool useLights = true, bool useFeedback = false);
00080 
00081   ~ViewPort3D();
00082 
00083   Dims getDims() { return Dims(itsScreenWidth,  itsScreenHeight); }
00084 
00085   //! rendering options
00086   void setWireframeMode(bool val) { itsWireframe = val; }
00087   void setLightsMode(bool val) { itsUseLights = val; }
00088   void setFeedbackMode(bool val) { itsUseFeedback = val; }
00089 
00090 
00091   //! Initialize a rendering frame
00092   void initFrame();
00093 
00094   void initProjection();
00095 
00096 
00097   //! Initialize the opengl context 
00098   void initCtx();
00099 
00100   //! Render and get a the frame from the buffer
00101   Image<PixRGB<byte> > getFrame();
00102   Image<PixRGB<float> > getFrameFloat();
00103 
00104   //! Render the frame and return lines instread of a frame
00105   std::vector<ViewPort3D::Line> getFrameLines();
00106   
00107 
00108   //! Get the depth Frame
00109   Image<float> getDepthFrame();
00110 
00111   //! Get the position of an point in image space to world space
00112   Point3D<float> getPosition(Point3D<float> loc);
00113 
00114   //! Build a projection matrix based on real camera parameters
00115   void setProjectionMatrix(const double cameraParam[3][4]);
00116 
00117   //! Build a projection matrix based on precpective parameters
00118   //fovy -- field of view in the y direction
00119   //aspect -- ratio that determines the field of view in the x direction
00120   //zmin -- Distance from the viewer to the near clipping plane
00121   //zmax -- Distance from the viewer to the fax clipping plane
00122   void setProjectionMatrix(float fovy, float aspect, float zmin, float zmax);
00123       
00124   //! Decompose a matrix
00125   int  paramDecompMat(const double source[3][4],
00126       double cpara[3][4],
00127       double trans[3][4] );
00128 
00129   //! Setup the camera viewport
00130   void setCamera ( const Point3D<float> pos, const Point3D<float> rot );
00131 
00132   void setCamera( const double trans[3][4]);
00133 
00134 
00135   //! Setup the lights
00136   void setLights();
00137 
00138   //// Draw varius primities objects ////////
00139 
00140   void setColor(const PixRGB<byte> color);
00141 
00142   void drawGrid(Dims size, Dims step, float height=0);
00143   
00144   
00145   //Surfaces
00146   void drawLine(const Point3D<float>& p1, const Point3D<float>& p2);
00147 
00148   void drawRectangle(const Point3D<float> pos, //position
00149       const Point3D<float> rot, //Rotation
00150       const float width, const float height,
00151       const PixRGB<byte> color);
00152 
00153   void drawCircle(const Point3D<float> pos,
00154       const Point3D<float> rot, //Rotation
00155       const float radius,
00156       const PixRGB<byte> color);
00157 
00158   void drawDisk(const Point3D<float> pos,
00159       const Point3D<float> rot, //Rotation
00160       const float radius,
00161       const PixRGB<byte> color);
00162 
00163   void drawEllipse(const Point3D<float> pos,
00164       const Point3D<float> rot, //Rotation
00165       const float radiusX, const float radiusY,
00166       const PixRGB<byte> color);
00167   
00168 
00169 
00170   //Solids
00171   
00172   void drawBox(const Point3D<float> pos, //position
00173       const Point3D<float> rot, //Rotation
00174       const Point3D<float> size, //size
00175       const PixRGB<byte> color);
00176 
00177   //! Draw an extruded contour
00178   void drawExtrudedContour(const std::vector<Point2D<float> >& contour,
00179       const Point3D<float> pos,
00180       const Point3D<float> rot,
00181       const float thickness,
00182       const PixRGB<byte> color);
00183 
00184 
00185   //! Draw a sphere
00186   void drawSphere(const Point3D<float> pos,
00187       const Point3D<float> rot, //Rotation
00188       const Point3D<float> size,
00189       const PixRGB<byte> color);
00190 
00191   //! Draw a cylinder
00192   void drawCylinder(const Point3D<float> pos,
00193       const Point3D<float> rot, //Rotation
00194       const float radius,
00195       const float length,
00196       const PixRGB<byte> color);
00197 
00198   //! Draw a cone 
00199   void drawCone(const Point3D<float> pos,
00200       const Point3D<float> rot, //Rotation
00201       const float radius,
00202       const float length,
00203       const PixRGB<byte> color);
00204 
00205   //! Draw a Capped cylinder
00206   void drawCappedCylinder(const Point3D<float> pos,
00207       const Point3D<float> rot, //Rotation
00208       const float radius,
00209       const float length,
00210       const PixRGB<byte> color);
00211 
00212   //! Draw the ground
00213   void drawGround(const Point2D<float> size, const PixRGB<byte> color);
00214 
00215 
00216   //! Generate a unit sphere and save it in a list for faster processing
00217   uint generateSphere(int quality);
00218 
00219 
00220   //! Triangulate a polygon
00221   //Borowed from John W. Ratcliff [jratcliff@verant.com] and adapted to work with Point2D class
00222   //http://www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml
00223   std::vector<Point2D<float> > triangulate(const std::vector<Point2D<float> >& contour);
00224   bool snip(const std::vector<Point2D<float> > &contour,
00225       int u,int v,int w,int n,int *V);
00226 
00227 
00228 
00229   //! This is recursively subdivides a triangular area (vertices p1,p2,p3) into
00230   //! smaller triangles, and then draws the triangles. All triangle vertices are
00231   //! normalized to a distance of 1.0 from the origin (p1,p2,p3 are assumed
00232   //! to be already normalized). Note this is not super-fast because it draws
00233   //! triangles rather than triangle strips.
00234 
00235   void drawPatch (double p1[3], double p2[3], double p3[3], int level);
00236 
00237   
00238   //!Add a texture from Image and return its id in texture memory
00239   //Note, need to initframe first before adding
00240   uint addTexture(const Image<PixRGB<byte> >& textureImg);
00241 
00242   uint addTexture(const Image<float>& textureImg);
00243 
00244   //!Bind a texture id to current set
00245   void bindTexture(const uint texId);
00246  
00247   //!Load texture to an exsisting id
00248   void loadTexture(const Image<PixRGB<byte> >& textureImg, uint texId);
00249 
00250   
00251  
00252 #ifdef HAVE_GL_GLEW_H
00253   //! Shader support
00254   void printInfoLog(GLhandleARB obj);
00255   //! Shader support
00256   GLhandleARB createShader(const char *prog, int type);
00257 
00258   //! Render a program into a texture (-1 use current texture)
00259   void progToTexture(const GLhandleARB prog, const int texId = -1);
00260   
00261 #endif
00262   
00263 
00264 
00265 private:
00266   int itsScreenWidth; //width of viewport
00267   int itsScreenHeight;  //height of viewport
00268   Point3D<float> itsCameraPosition;
00269   Point3D<float> itsCameraRotation;
00270 
00271   bool itsShowRenderWindow; //Wether to show the render window (for debuging);
00272   bool itsWireframe; //Show wireframe only
00273   bool itsUseLights;
00274   bool itsUseFeedback;
00275   int itsSphereQuality;
00276   int itsCylinderQuality;
00277 
00278 
00279   //Objects lists
00280   uint itsSphereListNum;
00281 
00282   bool itsInitGlew; //Used to keep track if glew is init
00283 
00284   Display* itsDisplay; //Out main display
00285   Window itsWin; //the window handle
00286   GLXContext itsCtx; //the display context
00287 
00288 
00289   double itsProjMatrix[16]; //The camera projection matrix
00290   double itsCameraExParam[16]; //The camera extrinsic parameter matrix
00291   bool itsUseExParam;
00292 
00293   //For feedback buffer;
00294   int itsFeedbackBufferSize;
00295   float* itsFeedbackBuffer;
00296 
00297 
00298 
00299 
00300 };
00301 
00302 
00303 // ######################################################################
00304 /* So things look consistent in everyone's emacs... */
00305 /* Local Variables: */
00306 /* indent-tabs-mode: nil */
00307 /* End: */
00308 
00309 #endif //
Generated on Sun May 8 08:40:41 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3