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

dsp.cpp

Go to the documentation of this file.
00001 /*
00002 ** ---------------------------------------------------------------
00003 ** dsp.cpp -  Display class.
00004 **
00005 ** ---------------------------------------------------------------
00006 */
00007 
00029 /*
00030 ** ---------------------------------------------------------------
00031 ** Global variables and symbols:
00032 */
00033 #include <stdlib.h>
00034 #include <math.h>
00035 
00036 #include "cd.h"
00037 
00038 #include "dsp.h"
00039 #include "t3d.h"
00040 #include "zbf.h"
00041 
00042 /*
00043 ** ---------------------------------------------------------------
00044 ** Initialization of class variables:
00045 */
00048 double Dsp::MIN_COLOR_INTENS = 0.50;
00049 
00052 int Dsp::FRAMES = 720;
00053 
00056 double Dsp::M_PI = 3.141592654;
00057 
00060 double Dsp::alpha = 0.0;
00061 
00064 double Dsp::delta = 2.0*Dsp::M_PI/Dsp::FRAMES;
00065 
00068 Dsp::Vec Dsp::view_vector = { 0.0, 0.0, 0.0 };
00069 
00072 Dsp::Vec Dsp::box[8] =
00073          { { 0.0, 0.0, 0.0 },
00074            { 1.0, 0.0, 0.0 },
00075            { 1.0, 2.0, 0.0 },
00076            { 0.0, 2.0, 0.0 },
00077            { 0.0, 0.0, 3.0 },
00078            { 1.0, 0.0, 3.0 },
00079            { 1.0, 2.0, 3.0 },
00080            { 0.0, 2.0, 3.0 } };
00081 
00084 double Dsp::xmin = 0.0;
00085 double Dsp::xmax = 1.0;
00086 double Dsp::ymin = 0.0;
00087 double Dsp::ymax = 2.0;
00088 double Dsp::zmin = 0.0;
00089 double Dsp::zmax = 3.0;
00090 
00093 double Dsp::eyex = 0.0;
00094 double Dsp::eyey = 0.0;
00095 double Dsp::eyez = 0.0;
00096 
00099 double Dsp::refx = 0.0;
00100 double Dsp::refy = 0.0;
00101 double Dsp::refz = 0.0;
00102 
00105 double Dsp::vupx = 0.0;
00106 double Dsp::vupy = 0.0;
00107 double Dsp::vupz = 0.0;
00108 
00111 double Dsp::left = 0.0;
00112 double Dsp::right = 0.0;
00113 double Dsp::bottom = 0.0;
00114 double Dsp::top = 0.0;
00115 double Dsp::front = 0.0;
00116 double Dsp::back = 0.0;
00117 
00118 /*
00119 ** ---------------------------------------------------------------
00120 ** Private class functions:
00121 */
00122 /*=======================  Dsp::ShadeColor  ========================*/
00123 
00124 long int Dsp::ShadeColor( Vec* normal, Vec* view, long int color )
00125 {
00126  unsigned char red;
00127  unsigned char green;
00128  unsigned char blue;
00129  double        fac;    /* intensity factor */
00130 
00131  DecodeColor( color, &red, &green, &blue );
00132 
00133  fac = VecDot( normal, view );
00134  fac = fabs( fac );
00135 
00136  fac = MIN_COLOR_INTENS + (1.0 - MIN_COLOR_INTENS) * fac;
00137 
00138  if( fac < 0.0 )
00139   fac = 0.0;
00140  else if( fac > 1.0 )
00141   fac = 1.0;
00142 
00143  red   = (unsigned char)(fac * (double)red);
00144  green = (unsigned char)(fac * (double)green);
00145  blue  = (unsigned char)(fac * (double)blue);
00146 
00147  return( EncodeColor( red, green, blue ) );
00148 }
00149 
00150 /*======================  Dsp::DisplaySolid  =======================*/
00151 
00152 void Dsp::DisplaySolid( void )
00153 {
00154  /* Face -x */
00155  ZbfBeginPoly( CD_GREEN, -1.0, 0.0, 0.0 );
00156  ZbfVertex( box[0].x, box[0].y, box[0].z );
00157  ZbfVertex( box[4].x, box[4].y, box[4].z );
00158  ZbfVertex( box[7].x, box[7].y, box[7].z );
00159  ZbfVertex( box[3].x, box[3].y, box[3].z );
00160  ZbfEndPoly( );
00161 
00162  /* Face -y */
00163  ZbfBeginPoly( CD_CYAN, 0.0, -1.0, 0.0 );
00164  ZbfVertex( box[0].x, box[0].y, box[0].z );
00165  ZbfVertex( box[1].x, box[1].y, box[1].z );
00166  ZbfVertex( box[5].x, box[5].y, box[5].z );
00167  ZbfVertex( box[4].x, box[4].y, box[4].z );
00168  ZbfEndPoly( );
00169 
00170  /* Face -z */
00171  ZbfBeginPoly( CD_YELLOW, 0.0, 0.0, -1.0 );
00172  ZbfVertex( box[3].x, box[3].y, box[3].z );
00173  ZbfVertex( box[2].x, box[2].y, box[2].z );
00174  ZbfVertex( box[1].x, box[1].y, box[1].z );
00175  ZbfVertex( box[0].x, box[0].y, box[0].z );
00176  ZbfEndPoly( );
00177 
00178  /* Face +x */
00179  ZbfBeginPoly( CD_GREEN, 1.0, 0.0, 0.0 );
00180  ZbfVertex( box[1].x, box[1].y, box[1].z );
00181  ZbfVertex( box[2].x, box[2].y, box[2].z );
00182  ZbfVertex( box[6].x, box[6].y, box[6].z );
00183  ZbfVertex( box[5].x, box[5].y, box[5].z );
00184  ZbfEndPoly( );
00185 
00186  /* Face +y */
00187  ZbfBeginPoly( CD_CYAN, 0.0, 1.0, 0.0 );
00188  ZbfVertex( box[2].x, box[2].y, box[2].z );
00189  ZbfVertex( box[3].x, box[3].y, box[3].z );
00190  ZbfVertex( box[7].x, box[7].y, box[7].z );
00191  ZbfVertex( box[6].x, box[6].y, box[6].z );
00192  ZbfEndPoly( );
00193 
00194  /* Face +z */
00195  ZbfBeginPoly( CD_YELLOW, 0.0, 0.0, 1.0 );
00196  ZbfVertex( box[4].x, box[4].y, box[4].z );
00197  ZbfVertex( box[5].x, box[5].y, box[5].z );
00198  ZbfVertex( box[6].x, box[6].y, box[6].z );
00199  ZbfVertex( box[7].x, box[7].y, box[7].z );
00200  ZbfEndPoly( );
00201 }
00202 
00203 /*=======================  Dsp::ZbfBeginPoly  ======================*/
00204 
00205 void Dsp::ZbfBeginPoly( long int color, double nx, double ny, double nz )
00206 {
00207  Vec normal;            /* face normal vector */
00208  long int shaded_color; /* face color with modified intensity */
00209 
00210  normal.x = nx;
00211  normal.y = ny;
00212  normal.z = nz;
00213  shaded_color = ShadeColor( &normal, &view_vector, color );
00214  Zbf::BeginPoly( shaded_color );
00215 }
00216 
00217 /*=========================  Dsp::ZbfVertex  =======================*/
00218 
00219 void Dsp::ZbfVertex( double x, double y, double z )
00220 {
00221  T3d::Transform( &x, &y, &z );
00222  Zbf::Vertex( x, y, z );
00223 }
00224 
00225 /*========================  Dsp::ZbfEndPoly  =======================*/
00226 
00227 void Dsp::ZbfEndPoly( void )
00228 {
00229  Zbf::EndPoly( );
00230 }
00231 
00232 /*
00233 ** ---------------------------------------------------------------
00234 ** Public class functions:
00235 **
00236 */
00237 /*========================  Dsp::SetupView  ========================*/
00238 
00239 void Dsp::SetupView( cdCanvas* zbuffer_canvas )
00240 {
00241  int width, height;
00242  double sizex, sizey, sizez;
00243  double max_size;
00244  double size_w, size_h;
00245  double ratio;
00246 
00247  cdCanvasGetSize( zbuffer_canvas, &width, &height, 0L, 0L );
00248 
00249  sizex = xmax - xmin;
00250  sizey = ymax - ymin;
00251  sizez = zmax - zmin;
00252  max_size = MAX( sizex, sizey );
00253  max_size = MAX( max_size, sizez );
00254 
00255 /* Place reference point at center of bounding box.
00256  */
00257  refx = (xmin + xmax) * 0.5;
00258  refy = (ymin + ymax) * 0.5;
00259  refz = (zmin + zmax) * 0.5;
00260 
00261 /* Place camera with same x coordinate of reference point, distant
00262  * 4 "max_sizes" to reference point in y direction, and distant
00263  * 2 "max_size" to reference point in z direction.
00264  */
00265  eyex = refx;
00266  eyey = refy + 4.0*max_size;
00267  eyez = refz + 2.0*max_size;
00268 
00269 /* Define camera vertical plane perpendicular to xy plane.
00270  */
00271  vupx = 0.0;
00272  vupy = 0.0;
00273  vupz = 1.0;
00274 
00275 /* Define view window with aspect ration equal to canvas 
00276  * and display entire object.
00277  */
00278  size_w = size_h = max_size;
00279  ratio = (double)height / (double)width;
00280  if( ratio < 1.0 )
00281   size_w = size_h / ratio;
00282  else
00283   size_h = size_w * ratio;
00284 
00285 /* Define view volume parameters such that:
00286  * (1) view window center coincides with canvas center;
00287  * (2) front clip plane is located at 3 "max_sizes" from camera;
00288  * (3) back  clip plane is located at 5 "max_sizes" from camera;
00289  */
00290  left = -size_w * 0.5;
00291  right = size_w * 0.5;
00292  bottom = -size_h * 0.5;
00293  top = size_h * 0.5;
00294  front = 3.0 * max_size;
00295  back = 5.0 * max_size;
00296 }
00297 
00298 /*========================  Dsp::RotateView  =======================*/
00299 
00300 void Dsp::RotateView( void )
00301 {
00302 /* Increment rotation angle.
00303  */
00304  alpha += delta;
00305 
00306 /* Define rotation radius equal to distance between camera and
00307  * reference point projected on xy plane.
00308  */
00309  double radius = sqrt( (eyex-refx)*(eyex-refx) +
00310                        (eyey-refy)*(eyey-refy) );
00311 
00312 /* Update camera position, rotation it about reference point around
00313  * z axis.
00314  */
00315  eyex = refx + radius*sin( alpha );
00316  eyey = refy + radius*cos( alpha );
00317 }
00318 
00319 /*=========================  Dsp::ZbfModel  ========================*/
00320 
00321 void Dsp::ZbfModel( cdCanvas* zbuffer_canvas )
00322 {
00323  cdCanvasActivate( zbuffer_canvas );
00324 
00325 /* Set gray background color and clear canvas.
00326  */
00327  unsigned char gray_intensity = (unsigned char)(0.75 * 255.0);
00328  cdCanvasBackground( zbuffer_canvas, 
00329                EncodeColor(gray_intensity, gray_intensity, gray_intensity) );
00330  cdCanvasClear( zbuffer_canvas );
00331 
00332 /* Compute normalized view vector.
00333  */
00334  view_vector.x = refx - eyex;
00335  view_vector.y = refy - eyey;
00336  view_vector.z = refz - eyez;
00337  VecUnit( &view_vector, &view_vector );
00338 
00339 /* Build a 3D transformation that transforms from world (object)
00340  * coordinates to normalized screen coordinates (the view frustum
00341  * is transformed into a cube of size 2 and center at origin).
00342  * Use perspective projection, placing the projection plane at
00343  * the front view volume plane (OpenGL does this way).
00344  */
00345  T3d::Identity( );
00346 /*** COMPLETE AQUI: DSP-01 ***/
00347 
00348 
00349 
00350 /*** COMPLETE AQUI: DSP-01 ***/
00351 
00352 /* Display the model in the zbuffer canvas.
00353  */
00354  DisplaySolid( );
00355 }
00356 

Generated on Mon Jun 20 18:09:52 2011 for Trab8 by  doxygen 1.4.2-20050421