00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
00057 #define RAD_TO_DEG (180.0/M_PI)
00058 #define DEG_TO_RAD (M_PI/180.0)
00059
00060
00061 #define LIGHTX (0.0f)
00062 #define LIGHTY (0.0f)
00063
00064
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
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 class ViewPort {
00084
00085
00086 public:
00087 enum TEXTURES {NONE,OTHER, SKY, GROUND, WOOD, TREE};
00088
00089
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
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
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;
00155 int writeframes;
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;
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
00178 Texture *sky_texture;
00179 Texture *ground_texture;
00180 Texture *wood_texture;
00181 Texture *tree_texture;
00182 Texture *other_texture;
00183
00184 GLuint listnum;
00185 bool itsDrawWorld;
00186
00187
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
00193 void initCamera();
00194 double view_xyz[3];
00195 double view_hpr[3];
00196 double itsProjMatrix[16];
00197
00198
00199
00200 double color[4];
00201 int tnum;
00202
00203 double ground_scale;
00204 double ground_ofsx;
00205 double ground_ofsy;
00206 double sky_scale;
00207 double sky_height;
00208
00209
00210
00211 int sphere_quality;
00212 int capped_cylinder_quality;
00213
00214
00215 int use_textures;
00216 int use_shadows;
00217
00218
00219
00220
00221
00222
00223 void dsSetViewpoint (double xyz[3], double hpr[3]);
00224 void dsGetViewpoint (double xyz[3], double hpr[3]);
00225
00226
00227 void unProjectPoint(const int x, const int y, double objLoc[3]);
00228
00229
00230
00231
00232
00233
00234
00235
00236
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
00244
00245
00246
00247
00248
00249
00250
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
00263
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
00278
00279
00280
00281 void dsSetSphereQuality (int n);
00282 void dsSetCappedCylinderQuality (int n);
00283
00284
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
00317 typedef struct
00318 {
00319 unsigned int biSize;
00320 int biWidth;
00321 int biHeight;
00322 unsigned short biPlanes;
00323 unsigned short biBitCount;
00324 unsigned int biCompression;
00325 unsigned int biSizeImage;
00326 int biXPelsPerMeter;
00327 int biYPelsPerMeter;
00328 unsigned int biClrUsed;
00329 unsigned int biClrImportant;
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;
00344
00345
00346 };
00347
00348 #endif