gl_geometry.h
Go to the documentation of this file.
00001 #ifndef _VCG_GL_GEOMETRY_
00002 #define _VCG_GL_GEOMETRY_
00003 
00004 /* Portion of this file were more or less adapted from 
00005  * freeglut_geometry.c
00006  *
00007  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
00008  * that was Written by Pawel W. Olszta, <olszta@sourceforge.net>
00009  */
00010 
00011 #include<stdlib.h>
00012 #include<math.h>
00013 
00014 /*
00015  * Compute lookup table of cos and sin values forming a cirle
00016  *
00017  * Notes:
00018  *    It is the responsibility of the caller to free these tables
00019  *    The size of the table is (n+1) to form a connected loop
00020  *    The last entry is exactly the same as the first
00021  *    The sign of n can be flipped to get the reverse loop
00022  */
00023 
00024 static void fghCircleTable(double **sint,double **cost,const int n)
00025 {
00026     int i;
00027 
00028     /* Table size, the sign of n flips the circle direction */
00029 
00030     const int size = abs(n);
00031 
00032     /* Determine the angle between samples */
00033 
00034     const double angle = 2*M_PI/(double)( ( n == 0 ) ? 1 : n );
00035 
00036     /* Allocate memory for n samples, plus duplicate of first entry at the end */
00037 
00038     *sint = (double *) calloc(sizeof(double), size+1);
00039     *cost = (double *) calloc(sizeof(double), size+1);
00040 
00041     /* Bail out if memory allocation fails, fgError never returns */
00042 
00043     if (!(*sint) || !(*cost))
00044     {
00045         free(*sint);
00046         free(*cost);
00047         abort(); //fgError("Failed to allocate memory in fghCircleTable");
00048     }
00049 
00050     /* Compute cos and sin around the circle */
00051 
00052     (*sint)[0] = 0.0;
00053     (*cost)[0] = 1.0;
00054 
00055     for (i=1; i<size; i++)
00056     {
00057         (*sint)[i] = sin(angle*i);
00058         (*cost)[i] = cos(angle*i);
00059     }
00060 
00061     /* Last sample is duplicate of the first */
00062 
00063     (*sint)[size] = (*sint)[0];
00064     (*cost)[size] = (*cost)[0];
00065 }
00066 
00067 /*
00068  * Draws a solid sphere
00069  */
00070 inline void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks)
00071 {
00072     int i,j;
00073 
00074     /* Adjust z and radius as stacks are drawn. */
00075 
00076     double z0,z1;
00077     double r0,r1;
00078 
00079     /* Pre-computed circle */
00080 
00081     double *sint1,*cost1;
00082     double *sint2,*cost2;
00083 
00084   //  FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSolidSphere" );
00085 
00086     fghCircleTable(&sint1,&cost1,-slices);
00087     fghCircleTable(&sint2,&cost2,stacks*2);
00088 
00089     /* The top stack is covered with a triangle fan */
00090 
00091     z0 = 1.0;
00092     z1 = cost2[(stacks>0)?1:0];
00093     r0 = 0.0;
00094     r1 = sint2[(stacks>0)?1:0];
00095 
00096     glBegin(GL_TRIANGLE_FAN);
00097 
00098         glNormal3d(0,0,1);
00099         glVertex3d(0,0,radius);
00100 
00101         for (j=slices; j>=0; j--)
00102         {
00103             glNormal3d(cost1[j]*r1,        sint1[j]*r1,        z1       );
00104             glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius);
00105         }
00106 
00107     glEnd();
00108 
00109     /* Cover each stack with a quad strip, except the top and bottom stacks */
00110 
00111     for( i=1; i<stacks-1; i++ )
00112     {
00113         z0 = z1; z1 = cost2[i+1];
00114         r0 = r1; r1 = sint2[i+1];
00115 
00116         glBegin(GL_QUAD_STRIP);
00117 
00118             for(j=0; j<=slices; j++)
00119             {
00120                 glNormal3d(cost1[j]*r1,        sint1[j]*r1,        z1       );
00121                 glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius);
00122                 glNormal3d(cost1[j]*r0,        sint1[j]*r0,        z0       );
00123                 glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius);
00124             }
00125 
00126         glEnd();
00127     }
00128 
00129     /* The bottom stack is covered with a triangle fan */
00130 
00131     z0 = z1;
00132     r0 = r1;
00133 
00134     glBegin(GL_TRIANGLE_FAN);
00135 
00136         glNormal3d(0,0,-1);
00137         glVertex3d(0,0,-radius);
00138 
00139         for (j=0; j<=slices; j++)
00140         {
00141             glNormal3d(cost1[j]*r0,        sint1[j]*r0,        z0       );
00142             glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius);
00143         }
00144 
00145     glEnd();
00146 
00147     /* Release sin and cos tables */
00148 
00149     free(sint1);
00150     free(cost1);
00151     free(sint2);
00152     free(cost2);
00153 }
00154 
00155 /*
00156  * Draws a wire sphere
00157  */
00158 inline void glutWireSphere(GLdouble radius, GLint slices, GLint stacks)
00159 {
00160     int i,j;
00161 
00162     /* Adjust z and radius as stacks and slices are drawn. */
00163 
00164     double r;
00165     double x,y,z;
00166 
00167     /* Pre-computed circle */
00168 
00169     double *sint1,*cost1;
00170     double *sint2,*cost2;
00171 
00172     //FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWireSphere" );
00173 
00174     fghCircleTable(&sint1,&cost1,-slices  );
00175     fghCircleTable(&sint2,&cost2, stacks*2);
00176 
00177     /* Draw a line loop for each stack */
00178 
00179     for (i=1; i<stacks; i++)
00180     {
00181         z = cost2[i];
00182         r = sint2[i];
00183 
00184         glBegin(GL_LINE_LOOP);
00185 
00186             for(j=0; j<=slices; j++)
00187             {
00188                 x = cost1[j];
00189                 y = sint1[j];
00190 
00191                 glNormal3d(x,y,z);
00192                 glVertex3d(x*r*radius,y*r*radius,z*radius);
00193             }
00194 
00195         glEnd();
00196     }
00197 
00198     /* Draw a line loop for each slice */
00199 
00200     for (i=0; i<slices; i++)
00201     {
00202         glBegin(GL_LINE_STRIP);
00203 
00204             for(j=0; j<=stacks; j++)
00205             {
00206                 x = cost1[i]*sint2[j];
00207                 y = sint1[i]*sint2[j];
00208                 z = cost2[j];
00209 
00210                 glNormal3d(x,y,z);
00211                 glVertex3d(x*radius,y*radius,z*radius);
00212             }
00213 
00214         glEnd();
00215     }
00216 
00217     /* Release sin and cos tables */
00218 
00219     free(sint1);
00220     free(cost1);
00221     free(sint2);
00222     free(cost2);
00223 }
00224 
00225 #endif


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:31:25