ViewPort.H

00001 /*!@file GUI/Viewport.H test opengl viewport */
00002 
00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003   //
00004 // by the University of Southern California (USC) and the iLab at USC.  //
00005 // See http://iLab.usc.edu for information about this project.          //
00006 // //////////////////////////////////////////////////////////////////// //
00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00009 // in Visual Environments, and Applications'' by Christof Koch and      //
00010 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00011 // pending; application number 09/912,225 filed July 23, 2001; see      //
00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00013 // //////////////////////////////////////////////////////////////////// //
00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00015 //                                                                      //
00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00017 // redistribute it and/or modify it under the terms of the GNU General  //
00018 // Public License as published by the Free Software Foundation; either  //
00019 // version 2 of the License, or (at your option) any later version.     //
00020 //                                                                      //
00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00024 // PURPOSE.  See the GNU General Public License for more details.       //
00025 //                                                                      //
00026 // You should have received a copy of the GNU General Public License    //
00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00029 // Boston, MA 02111-1307 USA.                                           //
00030 // //////////////////////////////////////////////////////////////////// //
00031 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu>
00032 // Originally obtained from ode
00033 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/GUI/ViewPort.H $
00034 // $Id: ViewPort.H 12962 2010-03-06 02:13:53Z irock $
00035 
00036 
00037 #ifndef ViewPort_H
00038 #define ViewPort_H
00039 
00040 #include <X11/Xlib.h>
00041 #include <X11/Xatom.h>
00042 #include <X11/keysym.h>
00043 #include <pthread.h>
00044 #include <GL/glx.h>
00045 #include "GUI/Texture.H"
00046 #include "Image/Image.H"
00047 #include "Image/Pixels.H"
00048 #include "Util/log.H"
00049 
00050 #include <vector>
00051 
00052 #ifndef M_PI
00053 #define M_PI (3.14159265358979323846)
00054 #endif
00055 
00056 // constants to convert degrees to radians and the reverse
00057 #define RAD_TO_DEG (180.0/M_PI)
00058 #define DEG_TO_RAD (M_PI/180.0)
00059 
00060 // light vector. LIGHTZ is implicitly 1
00061 #define LIGHTX (0.0f)
00062 #define LIGHTY (0.0f)
00063 
00064 // ground and sky
00065 #define SHADOW_INTENSITY (0.65f)
00066 #define GROUND_R (1.0f)         // ground color for when there's no texture
00067 #define GROUND_G (1.0f)
00068 #define GROUND_B (1.0f)
00069 
00070 //***************************************************************************
00071 // misc mathematics stuff
00072 
00073 /*
00074 #ifndef dCROSS
00075 #define dCROSS(a,op,b,c) \
00076 (a)[0] op ((b)[1]*(c)[2] - (b)[2]*(c)[1]); \
00077 (a)[1] op ((b)[2]*(c)[0] - (b)[0]*(c)[2]); \
00078 (a)[2] op ((b)[0]*(c)[1] - (b)[1]*(c)[0]);
00079 #endif
00080 */
00081 
00082 
00083 class ViewPort {
00084 
00085 
00086   public:
00087     enum TEXTURES {NONE,OTHER, SKY, GROUND, WOOD, TREE}; //NONE always need to be 0
00088 
00089     // Our vertex type
00090     struct vertex_type {
00091 
00092       vertex_type(float _x = 0, float _y = 0, float _z = 0):
00093         x(_x), y(_y), z(_z)
00094       {}
00095 
00096       float x,y,z;
00097     };
00098 
00099     // The polygon (triangle), 3 numbers that aim 3 vertices
00100     struct polygon_type {
00101       polygon_type(int _a = 0,int _b = 0, int _c=0) :
00102         a(_a), b(_b), c(_c)
00103       {}
00104       int a,b,c;
00105     };
00106 
00107     // The mapcoord type, 2 texture coordinates for each vertex
00108     struct mapcoord_type {
00109 
00110       mapcoord_type(float _u=0, float _v=0) :
00111         u(_u), v(_v)
00112       {}
00113       float u,v;
00114     };
00115 
00116     typedef struct {
00117       char name[20];
00118       int vertices_qty;
00119       int polygons_qty;
00120       std::vector<vertex_type> vertex;
00121       std::vector<polygon_type> polygon;
00122       std::vector<mapcoord_type> mapcoord;
00123       Texture* texture;
00124       float scale;
00125     } DSObject;
00126 
00127     ViewPort(const char *winname,
00128         const char* ground = "ground.ppm",
00129         const char*sky = "sky.ppm",
00130         bool useFog = false,
00131         bool drawWorld=true,
00132         int w=320, int h=240,
00133         const double cameraParam[3][4] = NULL);
00134     ~ViewPort();
00135     static void mainWindowThread(void *t);
00136     XEvent initFrame(const double* cameraParam=NULL);
00137     int updateFrame();
00138     void getFrame(unsigned char *img);
00139     Image<PixRGB<byte> > getFrame();
00140 
00141     void setTextures(bool val);
00142 
00143     void setShadows(bool val);
00144 
00145     Dims getDims(){return Dims(itsWidth,itsHeight);}
00146 
00147     void setWireframe(bool val) { itsWireframe = val; }
00148     void setDrawWorld(bool val) { itsDrawWorld = val; }
00149     void setZoom(float val) { itsZoomFactor = val; }
00150 
00151   public:
00152     int itsWidth;
00153     int itsHeight;
00154     int run;                                //is the window running
00155     int writeframes;         //do we need to write the frames to the disk?
00156 
00157     pthread_t win_thread;
00158 
00159     Display *display;
00160     XVisualInfo *visual;
00161     Colormap colormap;
00162     Window win;
00163     GLXContext glx_context;
00164     int last_key_pressed;        //the last key pressed in the window
00165 
00166     Atom wm_protocols_atom;
00167     Atom wm_delete_window_atom;
00168 
00169     int screen;
00170     void createMainWindow(const char *winname);
00171     void destroyMainWindow();
00172     void handleEvent (XEvent &event);
00173 
00174     void startGraphics (const char *prefix, const char *ground, const char *sky);
00175     void stopGraphics();
00176 
00177     //3d grapics stuff
00178     Texture *sky_texture;
00179     Texture *ground_texture;
00180     Texture *wood_texture;
00181     Texture *tree_texture;
00182     Texture *other_texture;
00183 
00184     GLuint listnum;   //store a list for processing spheres
00185     bool itsDrawWorld;
00186 
00187     //Build the projection matrix from a camera parameter
00188     void buildProjectionMatrix(const double cameraParam[3][4], double projMatrix[16]);
00189     int  paramDecompMat(const double source[3][4], double cpara[3][4], double trans[3][4] );
00190 
00191 
00192     // current camera position and orientation
00193     void initCamera();
00194     double view_xyz[3];        // position x,y,z
00195     double view_hpr[3];        // heading, pitch, roll (degrees)
00196     double itsProjMatrix[16]; //The camera projection matrix
00197 
00198     //***************************************************************************
00199     // the current drawing state (for when the user's step function is drawing)
00200     double color[4];        // current r,g,b,alpha color
00201     int tnum;                        // current texture number
00202 
00203     double ground_scale;        // ground texture scale (1/size)
00204     double ground_ofsx;                // offset of ground texture
00205     double ground_ofsy;
00206     double sky_scale;        // sky texture scale (1/size)
00207     double sky_height;                // sky height above viewpoint
00208 
00209 
00210 
00211     int sphere_quality;
00212     int capped_cylinder_quality;
00213 
00214     // textures and shadows
00215     int use_textures;                // 1 if textures to be drawn
00216     int use_shadows;                // 1 if shadows to be drawn
00217 
00218     /* set and get the camera position. xyz is the cameria position (x,y,z).
00219      * hpr contains heading, pitch and roll numbers in degrees. heading=0
00220      * points along the x axis, pitch=0 is looking towards the horizon, and
00221      * roll 0 is "unrotated".
00222      */
00223     void dsSetViewpoint (double xyz[3], double hpr[3]);
00224     void dsGetViewpoint (double xyz[3], double hpr[3]);
00225 
00226     //! get the 3D location of an x,y pos on the screen
00227     void unProjectPoint(const int x, const int y, double objLoc[3]);
00228 
00229 
00230     /* change the way objects are drawn. these changes will apply to all further
00231      * dsDrawXXX() functions. the texture number must be a DS_xxx texture
00232      * constant. the red, green, and blue number are between 0 and 1.
00233      * alpha is between 0 and 1 - if alpha is not specified it's assubed to be 1.
00234      * the current texture is colored according to the current color.
00235      * at the start of each frame, the texture is reset to none and the color is
00236      * reset to white.
00237      */
00238     void dsSetTexture (TEXTURES texture_number, Texture* texturePtr = NULL);
00239     void dsSetColor (double red, double green, double blue);
00240     void dsSetColorAlpha (double red, double green, double blue, double alpha);
00241 
00242 
00243     /* draw objects.
00244      *   - pos[] is the x,y,z of the center of the object.
00245      *   - R[] is a 3x3 rotation matrix for the object, stored by row like this:
00246      *        [ R11 R12 R13 0 ]
00247      *        [ R21 R22 R23 0 ]
00248      *        [ R31 R32 R33 0 ]
00249      *   - sides[] is an array of x,y,z side lengths.
00250      *   - all cylinders are aligned along the z axis.
00251      */
00252     void dsDrawBox (const double pos[3], const double R[12], const double sides[3]);
00253     void dsDrawSphere (const double pos[3], const double R[12], double radius);
00254     void dsDrawTriangle (const double pos[3], const double R[12],
00255         const double *v0, const double *v1, const double *v2, int solid);
00256     void dsDrawCylinder (const double pos[3], const double R[12],
00257         double length, double radius);
00258     void dsDrawCappedCylinder (const double pos[3], const double R[12],
00259         double length, double radius);
00260     void dsDrawLine (const double pos1[3], const double pos2[3]);
00261 
00262     /* these drawing functions are identical to the ones above, except they take
00263      * double arrays for `pos' and `R'.
00264      */
00265     void dsDrawBoxD (const double pos[3], const double R[12],
00266         const double sides[3]);
00267     void dsDrawSphereD (const double pos[3], const double R[12],
00268         const double radius);
00269     void dsDrawTriangleD (const double pos[3], const double R[12],
00270         const double *v0, const double *v1, const double *v2, int solid);
00271     void dsDrawCylinderD (const double pos[3], const double R[12],
00272         double length, double radius);
00273     void dsDrawCappedCylinderD (const double pos[3], const double R[12],
00274         double length, double radius);
00275     void dsDrawLineD (const double pos1[3], const double pos2[3]);
00276 
00277     /* Set the drawn quality of the objects. Higher numbers are higher quality,
00278      * but slower to draw. This must be set before the first objects are drawn to
00279      * be effective.
00280      */
00281     void dsSetSphereQuality (int n);                /* default = 1 */
00282     void dsSetCappedCylinderQuality (int n);        /* default = 3 */
00283 
00284     //util
00285     void normalizeVector3(float*);
00286     double dDOT(const double*, const double*);
00287     void setCamera(double, double, double, double, double, double);
00288     void setColor(double, double, double, double);
00289     void setTransform(const double*, const double*);
00290     void setShadowTransform();
00291     void drawBox(const double*);
00292     void drawPatch(double*, double*, double*,int);
00293 
00294     void drawSphere();
00295     void drawSphereShadow(double, double, double, double);
00296     void drawTriangle(const double*, const double*, const double*, int);
00297     void drawTriangleD(const double*, const double*, const double*, int);
00298     void drawCappedCylinder(double, double);
00299     void drawCylinder(double, double, double);
00300     void initMotionModel();
00301     void wrapCameraAngles();
00302     void dsMotion(int, int, int);
00303     void drawSky(double*);
00304     void drawGround();
00305     void drawPyramidGrid();
00306 
00307     void dsDrawFrame(const double cameraParam[16]=NULL);
00308     int dsGetShadows();
00309     void dsSetShadows(int);
00310     int dsGetTextures();
00311     void dsSetTextures(int);
00312     void setupDrawingMode();
00313     void setShadowDrawingMode();
00314 
00315 
00316     //3ds object support
00317     typedef struct                       /**** BMP file info structure ****/
00318     {
00319       unsigned int   biSize;           /* Size of info header */
00320       int            biWidth;          /* Width of image */
00321       int            biHeight;         /* Height of image */
00322       unsigned short biPlanes;         /* Number of color planes */
00323       unsigned short biBitCount;       /* Number of bits per pixel */
00324       unsigned int   biCompression;    /* Type of compression to use */
00325       unsigned int   biSizeImage;      /* Size of image data */
00326       int            biXPelsPerMeter;  /* X pixels per meter */
00327       int            biYPelsPerMeter;  /* Y pixels per meter */
00328       unsigned int   biClrUsed;        /* Number of colors used */
00329       unsigned int   biClrImportant;   /* Number of important colors */
00330       char *data;
00331     } BITMAPINFOHEADER;
00332 
00333 
00334     DSObject load3DSObject(const char* filename, const char* textureFile = NULL);
00335     int loadBitmap(const char*filename);
00336     void dsDraw3DSObject(const double pos[3], const double R[12], DSObject &object);
00337     void draw3dsObject(DSObject& object);
00338 
00339 
00340   private:
00341     bool itsUseFog;
00342     bool itsWireframe;
00343     float itsZoomFactor; //1 = +/- 45 degrees
00344 
00345 
00346 };
00347 
00348 #endif
Generated on Sun May 8 08:40:41 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3