Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

prj.cpp

Go to the documentation of this file.
00001 /*
00002 ** ---------------------------------------------------------------
00003 ** prj.c - Project manager class.
00004 **
00005 ** ---------------------------------------------------------------
00006 */
00007 
00042 /*
00043 ** ---------------------------------------------------------------
00044 ** Global variables and symbols
00045 */
00046 #ifdef _WIN32
00047 #include <windows.h>
00048 #endif
00049 #include <stdio.h>
00050 #include <stdlib.h>
00051 #include <string.h>
00052 #include <GL/glew.h>
00053 #include <GL/gl.h>
00054 #include <GL/glu.h>
00055 
00056 #include "iup.h"
00057 #include "cd.h"
00058 #include "cdiup.h"
00059 #include "prj.h"
00060 #include "prm.h"
00061 #include "prmio.h"
00062 #include "dsp.h"
00063 #include "zbf.h"
00064 
00065 /*
00066 ** ---------------------------------------------------------------
00067 ** Initialization of class variables:
00068 */
00071 char *Prj::trab_info =
00072   "\n"
00073   "\n"
00074   "                   CIV-2802  -- 2010.1               \n"
00075   "\n"
00076   "            Sistemas Graficos para Engenharia        \n"
00077   "\n"
00078   "           Departamento de Engenharia Civil          \n"
00079   "                        PUC-Rio                      \n"
00080   "\n"
00081   "            Trabalho No.: 4                          \n"
00082   "            Aluno:       \n"
00083   "            Data:        \n"
00084   "\n";
00085 
00088 double Prj::max_height = 50.0;
00089 
00093 Prm* Prj::sel_prm = NULL;
00094 
00097 int Prj::list_updated = 0;
00098 
00101 Ihandle* Prj::Ozbufferdialog = NULL;
00102 
00105 cdCanvas* Prj::Gzbuffercanvas = NULL;
00106 
00109 Ihandle* Prj::Odatascreen = NULL;
00110 
00113 VglCanvas* Prj::vglcanvas = NULL; 
00114 
00115 
00116 /*
00117 ** ---------------------------------------------------------------
00118 ** Private class functions: 
00119 */
00120 
00121 /*======================  Prj::ResetLocalParams  ====================*/
00122 
00123 void Prj::ResetLocalParams( void )
00124 {
00125  max_height = 50.0;
00126  sel_prm = NULL;
00127  list_updated = 0;
00128 }
00129 
00130 /*======================  Prj::BuildDisplayList  ====================*/
00131 
00132 void Prj::BuildDisplayList( void )
00133 {
00134  glNewList( 1, GL_COMPILE );
00135  if( Prm::First( ) )
00136  {
00137   Dsp::Model( );
00138   list_updated = 1;
00139  }
00140  glEndList( );
00141 }
00142 
00143 /*=========================  Prj::PckPrm  ==========================*/
00144 
00145 Prm* Prj::PckPrm( double x, double y )
00146 {
00147  Prm* prm;
00148 
00149  for( prm = Prm::First( ); prm != NULL; prm = prm->Next( ) )
00150  {
00151   if( prm->PickArea( x, y ) )
00152   {
00153    return( prm );
00154   }
00155  }
00156  return( NULL );
00157 }
00158 
00159 /*====================  Prj::DspZbufferCanvas  =====================*/
00160 
00161 int Prj::DspZbufferCanvas( Ihandle* cv )
00162 {
00163  if( Gzbuffercanvas )
00164   Dsp::ZbfModel( Gzbuffercanvas );
00165  return( IUP_DEFAULT );
00166 }
00167 
00168 /*
00169 ** ---------------------------------------------------------------
00170 ** Public class functions:
00171 */
00172 
00173 /*===========================  Prj::Init  ===========================*/
00174 
00175 void Prj::Init( void )
00176 {
00177 }
00178 
00179 /*===========================  Prj::Quit  ===========================*/
00180 
00181 void Prj::Quit( void )
00182 {
00183  Prm::DeleteList( );
00184 
00185  if( Gzbuffercanvas )
00186   cdKillCanvas( Gzbuffercanvas );
00187 
00188  if( Ozbufferdialog )
00189   IupDestroy( Ozbufferdialog );
00190 }
00191 
00192 /*==========================  Prj::Resize  ==========================*/
00193 
00194 void Prj::Resize( int width, int height )
00195 {
00196  Dsp::Init( );
00197 }
00198 
00199 /*========================  Prj::Redisplay  =========================*/
00200 
00201 void Prj::Redisplay( void )
00202 {
00203  glClearColor( 0.75, 0.75, 0.75, 1.0 ); /* gray background color */
00204  glEnable( GL_LIGHTING );
00205  glEnable( GL_LIGHT0 );
00206  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00207  if( ! list_updated )
00208  {
00209   BuildDisplayList( );
00210  }
00211  glCallList( 1 );
00212  IupGLSwapBuffers( Odatascreen ); 
00213 }
00214 
00215 /*===========================  Prj::Info  ===========================*/
00216 
00217 void Prj::Info( void )
00218 {
00219  IupMessage( "  Informacoes do Projeto  ", trab_info );
00220 }
00221 
00222 /*===========================  Prj::Help  ===========================*/
00223 
00224 void Prj::Help( void )
00225 {
00226  Dsp::Help( );
00227 }
00228 
00229 /*=========================  Prj::OpenModel  ========================*/
00230 
00231 int Prj::OpenModel( FILE *fd )
00232 {
00233  double xmin;
00234  double xmax;
00235  double ymin;
00236  double ymax;
00237  double zmin;
00238  double zmax;
00239  int    status;
00240 
00241 /* Delete current model (if there is one).
00242  */
00243  Prm::DeleteList( );
00244  ResetLocalParams( );
00245 
00246 /* Read new data file
00247  */
00248  status = PrmIO::Interpret( fd );
00249 
00250  if( status )
00251  {
00252 /* Compute new model bounding box and adjust visualization volume
00253  * to fit new bounding box.
00254  * Set maximum primitive height equal to maximum size in XY plane.
00255  */
00256   if( Prm::GetGlobalBox( &xmin, &xmax, &ymin, &ymax ) )
00257   {
00258    max_height = MAX( xmax-xmin, ymax-ymin );
00259    zmin = 0.0;
00260    zmax = max_height;
00261    vglSetBox( vglcanvas, (float)xmin, (float)xmax, (float)ymin, 
00262                          (float)ymax, (float)zmin, (float)zmax );
00263    Persp( );
00264   }
00265  }
00266 
00267  return( status );
00268 }
00269 
00270 /*======================  Prj::ZbufferSnapShot  =====================*/
00271 
00272 void Prj::ZbufferSnapShot( void )
00273 {   
00274  Ihandle  *Ozbuffercanvas;   /* IUP handle of zbuffer canvas */
00275  int      viewport[4];       /* OpenGL canvas viewport */
00276  int      width, height;     /* main data screen sizes */
00277  static char size_string[32];
00278 
00279  if( Prm::First( ) == NULL )
00280   return;
00281 
00282  if( Gzbuffercanvas )
00283   cdKillCanvas( Gzbuffercanvas );
00284 
00285  if( Ozbufferdialog )
00286   IupDestroy( Ozbufferdialog );
00287 
00288 /* Get main data screen (canvas) sizes.
00289  */
00290  glGetIntegerv( GL_VIEWPORT, viewport );
00291  width  = viewport[2] - viewport[0];
00292  height = viewport[3] - viewport[1];
00293 
00294 /* Initialize zbuffer for the given canvas sizes.
00295  */
00296  if( ! Zbf::Init( width, height ) )
00297   return;
00298 
00299 /* Create a CD canvas with the same sizes.
00300  * Specifies function "Prj::DspZbufferCanvas" as the redisplay callback.
00301  */
00302  IupSetFunction( "Prj::DspZbufferCanvas", (Icallback)Prj::DspZbufferCanvas );
00303  Ozbuffercanvas = IupCanvas( "Prj::DspZbufferCanvas" );
00304  sprintf( size_string, "%dx%d", width, height );
00305  IupSetAttribute( Ozbuffercanvas, IUP_RASTERSIZE, size_string );
00306  IupSetAttribute( Ozbuffercanvas, IUP_SCROLLBAR, IUP_NO );
00307 
00308 /* Create a dialog for the canvas with no resize option.
00309  */
00310  Ozbufferdialog = IupDialog( Ozbuffercanvas );
00311  IupSetAttribute( Ozbufferdialog, IUP_MAXBOX, IUP_NO );
00312  IupSetAttribute( Ozbufferdialog, IUP_MINBOX, IUP_NO );
00313  IupSetAttribute( Ozbufferdialog, IUP_RESIZE, IUP_NO );
00314  IupSetAttribute( Ozbufferdialog, IUP_TITLE,  "Zbuffer Snap Shot" );
00315 
00316  IupMap( Ozbufferdialog );
00317  Gzbuffercanvas = cdCreateCanvas( CD_IUP, Ozbuffercanvas );
00318  IupShow( Ozbufferdialog );
00319 }
00320 
00321 /*======================  Prj::Raster2WorldXY  ======================*/
00322 
00323 int Prj::Raster2WorldXY( int x, int y, double* xw, double* yw )
00324 {
00325  double frontx, fronty, frontz;    /* pick point at front view volume plane */
00326  double backx,  backy,  backz;     /* pick point at back view volume plane */
00327  double t;
00328 
00329  Dsp::GetPickRay( x, y, &frontx, &fronty, &frontz, &backx, &backy, &backz );
00330 
00331  if( frontz == backz )
00332   return( 0 );
00333 
00334 /* Get intersection of pick ray line with XY plane.
00335  */
00336  t = frontz / (frontz - backz );
00337 
00338  if( (t < 0.0) && (t > 1.0) )
00339   return( 0 );
00340 
00341  *xw = (1.0-t) * frontx + t * backx;
00342  *yw = (1.0-t) * fronty + t * backy;
00343 
00344  return( 1 );
00345 }
00346 
00347 /*=========================  Prj::SelectPrm  ========================*/
00348 
00349 int Prj::SelectPrm( double x, double y )
00350 {
00351  Prm* prm;
00352  int status = 0;
00353 
00354 /* Try to select a primitive based on the given point.
00355  */
00356  Prm::UnselectAll( );
00357  sel_prm = NULL;
00358 
00359  if( (prm = PckPrm( x, y )) != NULL )
00360  {
00361   sel_prm = prm;
00362   sel_prm->Select( );
00363   status = 1;
00364  }
00365 
00366 /* Rebuild display list.
00367  */
00368  BuildDisplayList( );
00369 
00370  return( status );
00371 }
00372 
00373 /*=======================  Prj::SetHeightFac  =======================*/
00374 
00375 void Prj::SetHeightFac( double fac )
00376 {
00377  if( (Prm::First( ) == NULL) || (sel_prm == NULL) )
00378   return;
00379 
00380 /* Does not allow height factors outside the range 0 to 1.
00381  */
00382  if( fac < 0.01 )
00383   fac = 0.0;
00384 
00385  if( fac > 0.99 )
00386   fac = 1.0;
00387 
00388 /* Set the height of currently selected primitive
00389  * according to given factor.
00390  */
00391  sel_prm->SetHeight( max_height * fac );
00392 
00393 /* Rebuild display list and redisplay the model.
00394  */
00395  BuildDisplayList( );
00396  vglRedraw(vglcanvas);
00397 }
00398 
00399 /*=======================  Prj::GetHeightFac  =======================*/
00400 
00401 double Prj::GetHeightFac( void )
00402 {
00403  double fac;
00404 
00405  if( (Prm::First( ) == NULL) || (sel_prm == NULL) )
00406   return( 0.0 );
00407 
00408  fac = sel_prm->GetHeight( ) / max_height;
00409 
00410  return( fac );
00411 }
00412 
00413 /*=========================  Prj::ResetView  ========================*/
00414 
00415 void Prj::ResetView( void )
00416 {
00417  vglReset( vglcanvas );
00418  vglRedraw( vglcanvas );
00419 }
00420 
00421 /*===========================  Prj::Fit  ============================*/
00422 
00423 void Prj::Fit( void )
00424 {
00425  vglReset( vglcanvas );
00426  vglRedraw( vglcanvas );
00427 }
00428 
00429 /*==========================  Prj::Persp  ===========================*/
00430 
00431 void Prj::Persp( void )
00432 {
00433  vglReset( vglcanvas );
00434  vglSetOrtho( vglcanvas, 0 );
00435  vglSetAngle( vglcanvas, 10 );
00436  vglSetItrMode( vglcanvas, VGL_MANIP );
00437  vglEnableAutoFit( vglcanvas, 0, 0, -1, 1.3f );
00438  vglSetUp( vglcanvas, 0, 1, 0 );
00439  vglPostRedraw( vglcanvas );  
00440 }
00441 
00442 /*==========================  Prj::Ortho  ===========================*/
00443 
00444 void Prj::Ortho( void )
00445 {
00446  vglReset( vglcanvas );
00447  vglSetOrtho( vglcanvas, 1 );
00448  vglRedraw( vglcanvas );
00449 }
00450 
00451 /*==========================  Prj::ProjX  ===========================*/
00452 
00453 void Prj::ProjX( void )
00454 {
00455  vglReset( vglcanvas);
00456  vglSetOrtho( vglcanvas, 1 );
00457  vglSetDOP( vglcanvas, -1.0f, 0.0f, 0.0f );
00458  vglSetUp( vglcanvas, 0.0, 0.0, 1.0 );
00459  vglRedraw( vglcanvas );
00460 }
00461 
00462 /*==========================  Prj::ProjY  ===========================*/
00463 
00464 void Prj::ProjY( void )
00465 {
00466  vglReset( vglcanvas );
00467  vglSetOrtho( vglcanvas, 1 );
00468  vglSetDOP( vglcanvas, 0.0f, -1.0f, 0.0f );
00469  vglSetUp( vglcanvas, 0.0, 0.0, 1.0 );
00470  vglRedraw( vglcanvas );
00471 }
00472 
00473 /*==========================  Prj::ProjZ  ===========================*/
00474 
00475 void Prj::ProjZ( void )
00476 {
00477  vglReset( vglcanvas );
00478  vglSetOrtho( vglcanvas, 1 );
00479  vglSetDOP( vglcanvas, 0.0f, 0.0f, -1.0f );
00480  vglSetUp( vglcanvas, 0.0, 1.0, 0.0 );
00481  vglRedraw( vglcanvas );
00482 }
00483 
00484 /*========================  Prj::ManipMode  =========================*/
00485 
00486 void Prj::ManipMode( void )
00487 {
00488  vglSetItrMode( vglcanvas, VGL_MANIP );
00489 }
00490 
00491 /*========================  Prj::NavigMode  =========================*/
00492 
00493 void Prj::NavigMode( void )
00494 {
00495  vglSetItrMode( vglcanvas, VGL_NAVIG );
00496 }
00497 
00498 /*========================  Prj::ZoomMode  ==========================*/
00499 
00500 void Prj::ZoomMode( void )
00501 {
00502  vglSetItrMode( vglcanvas, VGL_SINGLE_ZOOM );
00503 }
00504 

Generated on Mon Jun 21 12:45:12 2010 for Trab4 by  doxygen 1.4.2-20050421