Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

circ.c

Go to the documentation of this file.
00001 /*
00002 ** ----------------------------------------------------------------
00003 ** circ.c  -  Circle primitive module.
00004 **
00005 ** ----------------------------------------------------------------
00006 */
00007 
00038 /*
00039 ** ---------------------------------------------------------------
00040 ** Global variables and symbols:
00041 */
00042 #ifdef _WIN32
00043 #include <windows.h>
00044 #endif
00045 #include <stdlib.h>
00046 #include <stdio.h>
00047 #include <math.h>
00048 #include <GL/gl.h>
00049 #include <GL/glu.h>
00050 
00051 #include "prm.h"
00052 #include "circ.h"
00053 #include "cd.h"
00054 #include "wd.h"
00055 #include "dsp.h"
00056 
00057 /*
00058 ** ---------------------------------------------------------------
00059 ** Local variables and symbols:
00060 */
00061 
00062 #define CIRC_SEGS 32
00063 
00064 #ifndef M_PI
00065 #define M_PI 3.141592654
00066 #endif
00067 
00070 struct _circ {
00072   Coord         c;
00074   double        r;
00075  };
00076 
00077 
00078 
00079 /*
00080 ** ---------------------------------------------------------------
00081 ** Private functions:
00082 */
00083 
00084 static void circGetLateralNormal( Circ *circ, Coord *p, Coord *normal );
00085 
00086 /*====================  circGetLateralNormal  ===================*/
00087 
00088 static void circGetLateralNormal( Circ *circ, Coord *p, Coord *normal )
00089 {
00090  double len;
00091  normal->x = p->x - circ->c.x;
00092  normal->y = p->y - circ->c.y;
00093  len = sqrt( normal->x*normal->x + normal->y*normal->y );
00094  normal->x /= len;
00095  normal->y /= len;
00096 }
00097 
00098 /*
00099 ** ---------------------------------------------------------------
00100 ** Public functions:
00101 */
00102 
00103 /*=========================  circCreate  ========================*/
00104 
00105 Circ *circCreate( void )
00106 {
00107  Circ *circ;
00108 
00109 /* Allocate memory for a new primitive.
00110  */
00111  circ = malloc( sizeof( Circ ) );
00112  if( circ == NULL )
00113   return( NULL );
00114 
00115 /* Set default values for new primitive.
00116  */
00117  circ->c.x = 0.0;
00118  circ->c.y = 0.0;
00119  circ->r = 1.0;
00120 
00121  return( circ );
00122 }
00123 
00124 /*=========================  circDelete  ========================*/
00125 
00126 void circDelete( Circ *circ )
00127 {
00128  free( circ );
00129 }
00130 
00131 /*==========================  circRead  =========================*/
00132 
00133 void circRead( Circ *circ, FILE *fd )
00134 {
00135 /*** COMPLETE HERE: 30 ***/
00136 }
00137 
00138 /*=========================  circWrite  =========================*/
00139 
00140 void circWrite( Circ *circ, FILE *fd )
00141 {
00142 /*** COMPLETE HERE: 31 ***/
00143 }
00144 
00145 /*========================  circGetNPts  ========================*/
00146 
00147 int circGetNPts( Circ *circ )
00148 {
00149  return( 4 );
00150 }
00151 
00152 /*=======================  circSetCoords  =======================*/
00153 
00154 void circSetCoords( Circ *circ, int id, double x, double y )
00155 {
00156 /*** COMPLETE HERE: 32 ***/
00157 }
00158 
00159 /*=======================  circGetCoords  =======================*/
00160 
00161 void circGetCoords( Circ *circ, int id, double *x, double *y )
00162 {
00163 /*** COMPLETE HERE: 33 ***/
00164 }
00165 
00166 /*=======================  circSet1stPt  ========================*/
00167 
00168 void circSet1stPt( Circ *circ, double x, double y )
00169 {
00170 /*** COMPLETE HERE: 34 ***/
00171 }
00172 
00173 /*=======================  circSet2ndPt  ========================*/
00174 
00175 void circSet2ndPt( Circ *circ, double x, double y )
00176 {
00177 /*** COMPLETE HERE: 35 ***/
00178 }
00179 
00180 /*=======================  circPickArea  ========================*/
00181 
00182 int circPickArea( Circ *circ, double x, double y )
00183 {
00184 /*** COMPLETE HERE: 36 ***/
00185 
00186  return( 0 );
00187 }
00188 
00189 /*======================  circPickVertex  =======================*/
00190 
00191 int circPickVertex( Circ *circ, double x, double y, double tol,
00192                     int *id )
00193 {
00194 /*** COMPLETE HERE: 37 ***/
00195 
00196  return( 0 );
00197 }
00198 
00199 /*=======================  circPickSide  ========================*/
00200 
00201 int circPickSide( Circ *circ, double x, double y, double tol,
00202                   int *id )
00203 {
00204  return( 0 );
00205 }
00206 
00207 /*=======================  circTranslate  =======================*/
00208 
00209 void circTranslate( Circ *circ, double dx, double dy )
00210 {
00211 /*** COMPLETE HERE: 38 ***/
00212 }
00213 
00214 /*====================  circTranslateVertex  ====================*/
00215 
00216 void circTranslateVertex( Circ *circ, int id, double dx, double dy )
00217 {
00218 /*** COMPLETE HERE: 39 ***/
00219 }
00220 
00221 /*=====================  circTranslateSide  =====================*/
00222 
00223 void circTranslateSide( Circ *circ, int id, double dx, double dy )
00224 {
00225 }
00226 
00227 /*=========================  circGetBox  ========================*/
00228 
00229 void circGetBox( Circ *circ, double *xmin, double *xmax, 
00230                              double *ymin, double *ymax )
00231 {
00232 /*** COMPLETE HERE: 40 ***/
00233 }
00234 
00235 /*====================  circDisplayBoundary  ====================*/
00236 
00237 void circDisplayBoundary( Circ *circ )
00238 {
00239 /*** COMPLETE HERE: 41 ***/
00240 }
00241 
00242 /*====================  circDisplayInterior  ====================*/
00243 
00244 void circDisplayInterior( Circ *circ )
00245 {
00246 /*** COMPLETE HERE: 42 ***/
00247 }
00248 
00249 /*=====================  circDisplaySolid  ======================*/
00250 
00251 void circDisplaySolid( Circ *circ, double height )
00252 {
00253 /*** COMPLETE HERE: 42-A ***/
00254 /*** Study function circDisplayZbuffer below ***/
00255 /*** Use OpenGL functions ***/
00256 }
00257 
00258 /*======================  circHighltSolid  ======================*/
00259 
00260 void circHighltSolid( Circ *circ, double height )
00261 {
00262 /*** COMPLETE HERE: 42-B ***/
00263 /*** Use OpenGL functions ***/
00264 }
00265 
00266 /*====================  circDisplayZbuffer  =====================*/
00267 
00268 void circDisplayZbuffer( Circ *circ, double height, long int color )
00269 {
00270  Coord p[32];                     /* circle points */
00271  Coord normal;                    /* face normal vector */
00272  double alpha;                    /* circle angle */
00273  double delta = 2*M_PI/CIRC_SEGS; /* increment of circle angle */
00274  Coord  midpt;                    /* auxiliar lateral face mid point */
00275  int i;
00276  
00277 /* Build circle points.
00278  */
00279  alpha = 0.0;
00280  for( i = 0; i < CIRC_SEGS; i++ )
00281  {
00282   p[i].x = circ->c.x + circ->r * cos( alpha );
00283   p[i].y = circ->c.y + circ->r * sin( alpha );
00284   alpha += delta;
00285  }
00286 
00287 /* Display base circle.
00288  * To display means to project the face and send it to the zbuffer module.
00289  */
00290  for( i = 0; i < CIRC_SEGS; i++ )
00291  {
00292   dspZbfBeginPoly( color, 0.0, 0.0, -1.0 );
00293   dspZbfVertex( circ->c.x, circ->c.y, 0.0 );
00294   dspZbfVertex( p[(i+1)%CIRC_SEGS].x, p[(i+1)%CIRC_SEGS].y, 0.0 );
00295   dspZbfVertex( p[i].x, p[i].y, 0.0 );
00296   dspZbfEndPoly( );
00297  }
00298 
00299 /* If the height of the primitive is null, return;
00300  */
00301  if( height == 0.0 )
00302  {
00303   return;
00304  }
00305 
00306 /* Display top circle (send to zbuffer module).
00307  */
00308  for( i = 0; i < CIRC_SEGS; i++ )
00309  {
00310   dspZbfBeginPoly( color, 0.0, 0.0, 1.0 );
00311   dspZbfVertex( circ->c.x, circ->c.y, height );
00312   dspZbfVertex( p[(i+1)%CIRC_SEGS].x, p[(i+1)%CIRC_SEGS].y, height );
00313   dspZbfVertex( p[i].x, p[i].y, height );
00314   dspZbfEndPoly( );
00315  }
00316 
00317 /* Display lateral faces (send to zbuffer module).
00318  */
00319  for( i = 0; i < CIRC_SEGS; i++ )
00320  {
00321   midpt.x = (p[i].x + p[(i+1)%CIRC_SEGS].x) * 0.5;
00322   midpt.y = (p[i].y + p[(i+1)%CIRC_SEGS].y) * 0.5;
00323   circGetLateralNormal( circ, &midpt, &normal );
00324   dspZbfBeginPoly( color, normal.x, normal.y, 0.0 );
00325   dspZbfVertex( p[i].x, p[i].y, height );
00326   dspZbfVertex( p[i].x, p[i].y, 0.0 );
00327   dspZbfVertex( p[(i+1)%CIRC_SEGS].x, p[(i+1)%CIRC_SEGS].y, 0.0 );
00328   dspZbfVertex( p[(i+1)%CIRC_SEGS].x, p[(i+1)%CIRC_SEGS].y, height );
00329   dspZbfEndPoly( );
00330  }
00331 }
00332 
00334 midpt, &normal );
00335   dspZbfBeginPoly( color, normal.x, normal.y, 0.0 );
00336   dspZbfVertex( p[i].x, p[i].y, height );
00337   dspZbfVertex( p[i].x, p[i].y, 0.0 );
00338   dspZbfVertex( p[(i+1)%CIRC_SEGS].x, p[(i+1)%CIRC_SEGS].y, 0.0 );
00339   dspZbfVertex( p[(i+1)%CIRC_SEGS].x, p[(i+1)%CIRC_SEGS].y, height );
00340   dspZbfEndPoly( );
00341  }
00342 }
00343 

Generated on Tue Nov 8 10:57:59 2005 for Trab3 by doxygen1.2.18