home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / demos / gpc / gen_ents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-02  |  15.7 KB  |  511 lines

  1. /* $XConsortium: gen_ents.c,v 5.3 91/04/03 09:40:29 rws Exp $ */
  2. /***********************************************************
  3. Copyright(c) 1989,1990, 1991 by Sun Microsystems, Inc. and the X Consortium at M.I.T.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Sun Microsystems,
  12. the X Consortium, and MIT not be used in advertising or publicity
  13. pertaining to distribution of the software without specific, written
  14. prior permission.
  15.  
  16. SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  17. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
  18. SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  19. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22. SOFTWARE.
  23.  
  24. ******************************************************************/
  25.  
  26. /*--------------------------------------------------------------------*\
  27. |  Copyright (C) 1989,1990, 1991, National Computer Graphics Association
  28. |
  29. |  Permission is granted to any individual or institution to use, copy, or
  30. |  redistribute this software so long as it is not sold for profit, provided
  31. |  this copyright notice is retained.
  32. |
  33. |                         Developed for the
  34. |                National Computer Graphics Association
  35. |                         2722 Merrilee Drive
  36. |                         Fairfax, VA  22031
  37. |                           (703) 698-9600
  38. |
  39. |                                by
  40. |                 SimGraphics Engineering Corporation
  41. |                    1137 Huntington Drive  Unit A
  42. |                      South Pasadena, CA  91030
  43. |                           (213) 255-0900
  44. |---------------------------------------------------------------------
  45. |
  46. | Author        :    John M. Zulauf
  47. |
  48. | File        :    gen_ents.c
  49. | Date        :    Sun Jun 25 17:02:09 PDT 1989
  50. | Project       :    PLB
  51. | Description    :    Routines that take GEN_* primitive entities
  52. |            and generate BIF entities as if the generated
  53. |            data had originated from the parser.
  54. | Status        :    Version 1.0
  55. |
  56. | Revisions     :    
  57. |
  58. |      12/90            MFC Tektronix, Inc.: PEX-SI PEX5R1 Release.
  59. |
  60. \*--------------------------------------------------------------------*/
  61. #include <stdio.h>
  62. #include <math.h>
  63. #include <X11/Xosdefs.h>
  64. #ifndef X_NOT_STDC_ENV
  65. #include <stdlib.h>
  66. #else
  67. char *malloc();
  68. #endif
  69. #if defined(macII) && !defined(__STDC__)  /* stdlib.h fails to define these */
  70. char *malloc();
  71. #endif /* macII */
  72. #include "biftypes.h"
  73. #include "bifbuild.h"
  74. #include "bifparse.h"
  75. #include "bifmacro.h"
  76.  
  77.  
  78. /*--------------------------------------------------------------------*\
  79. | Procedure    :    int genSphere3Mesh(BIF_Sphere3 *);
  80. |---------------------------------------------------------------------
  81. | Description    :    The generated sphere3 is stored as a quadmesh.
  82. |            (For the reference port no "non-EXACT"
  83. |            method is supported)
  84. |            genSphere3Mesh generates points to the buffers 
  85. |            (using bif_triplet) just like the parser would.
  86. |            bif_quadmesh3(BIF_P_END) stores/executes the
  87. |            generated data as appropriate.
  88. |---------------------------------------------------------------------
  89. | Return    :    Error Code (Not Implemented)
  90. \*--------------------------------------------------------------------*/
  91. int genSphere3Mesh(ent)
  92. BIF_Sphere3 *ent;
  93.  
  94. {
  95.     int i, numCoords, numFacets;
  96.     vector3 *coords, *normals, *facet_normals;
  97.  
  98.     /*------------------------------------------------------------*\
  99.     |    Generate the data
  100.     |
  101.     |    The zero meridianal cut is repeated as the first and the
  102.     |    last row in the quadmesh.
  103.     \*------------------------------------------------------------*/
  104.     numCoords = (ent->numLat + 2) * (ent->numLong + 1);
  105.     numFacets = (ent->numLat + 1) * ent->numLong;
  106.     coords  = (vector3 *)malloc(sizeof(vector3)*numCoords);
  107.     normals = (vector3 *)malloc(sizeof(vector3)*numCoords);
  108.     facet_normals = (vector3 *)malloc(sizeof(vector3)*numFacets);
  109.  
  110.     if ( (coords == NULL) || (normals == NULL) ||
  111.         (facet_normals == NULL) )
  112.         ENT_ERROR(NULL);
  113.  
  114.     calcSphere(ent->numLong,ent->numLat+2,
  115.         ent->radius,ent->center,ent->scale,
  116.         coords,normals,facet_normals);
  117.  
  118.     /*------------------------------------------------------------*\
  119.     |    Send the quadmesh ( as if you were the parser )
  120.     \*------------------------------------------------------------*/
  121.     /* WORKING: Facets @ the pole "wink" in bad colors for flat */
  122.     /* WORKING: This could be a number of things... from pphd3 to */
  123.     /* WORKING: fxqmd3 */
  124.     /* The bug is in the vertex / facets themselves. If you disable */
  125.     /* all the normals, the hole still appears */
  126.  
  127.     bif_quad_mesh3(BIF_P_BEGIN);
  128.     bif_quadmeshorder(    (BIF_INT)ent->numLong+1,
  129.                 (BIF_INT)ent->numLat+2);
  130.  
  131.     /* Pass the Vertex coordinates */
  132.     bif_group(VERTEX_COORDINATES);
  133.     for ( i = 0 ; i < numCoords ; i++)
  134.         bif_triplet(    (BIF_REAL)coords[i][0],
  135.                 (BIF_REAL)coords[i][1],
  136.                 (BIF_REAL)coords[i][2]);
  137.     bif_group(BIF_END_OF_GROUP);
  138.  
  139.     /* Pass the Vertex normals */
  140.     bif_group(VERTEX_NORMALS);
  141.     for ( i = 0 ; i < numCoords ; i++)
  142.         bif_triplet(    (BIF_REAL)normals[i][0],
  143.                 (BIF_REAL)normals[i][1],
  144.                 (BIF_REAL)normals[i][2]);
  145.     bif_group(BIF_END_OF_GROUP);
  146.  
  147.     /* Pass the Facet normals */
  148.     bif_group(FACET_NORMALS);
  149.     for ( i = 0 ; i < numFacets ; i++)
  150.         bif_triplet(    (BIF_REAL)facet_normals[i][0],
  151.                 (BIF_REAL)facet_normals[i][1],
  152.                 (BIF_REAL)facet_normals[i][2]);
  153.     bif_group(BIF_END_OF_GROUP);
  154.     /* End the quadmesh */
  155.     bif_quad_mesh3(BIF_P_END);
  156.  
  157.     /*------------------------------------------------------------*\
  158.     |    Free the malloc'd memory
  159.     \*------------------------------------------------------------*/
  160.     free((char *)coords);
  161.     free((char *)normals);
  162.     free((char *)facet_normals);
  163.  
  164. } /* End genSphere3Mesh() */
  165.  
  166. /*--------------------------------------------------------------------*\
  167. | Procedure    :    int calcSphere(int, int, float, float, vector3,
  168. |                    vector3)
  169. |---------------------------------------------------------------------
  170. | Description    :    Calculate the vertices, normals and facets
  171. |            vertex normals for a longitudinal/latitudinal
  172. |            tesselation of a sphere.
  173. |
  174. |            The points are stored as a series of
  175. |            longitudinal cuts from north to south, east to
  176. |            west.  numLat is the number of points along  a
  177. |            longitudinal cut.
  178. |
  179. |            The zero meridianal cut is repeated at the end
  180. |            to allow closure (of a quadmesh).
  181. |
  182. |---------------------------------------------------------------------
  183. | Return    :    Error Code (not implemented)
  184. \*--------------------------------------------------------------------*/
  185. /* Inputs */
  186. int calcSphere(numLong, numLat, radius, center, scale,
  187.         coords, normals, f_norms)
  188. int numLong, numLat;         /* The number of long. and lat. cuts */
  189. float radius;            /* Overall Radius of the sphere */
  190. vector3 center;            /* The center of the calc'd sphere */
  191. vector3 scale;            /* The scale factors applied to the
  192.                     overall radius */
  193.  
  194. /* Outputs */
  195. vector3 *coords, *normals, *f_norms;
  196.  
  197. {
  198.     int longi, lat;
  199.     double along, dlong, alat, dlat;
  200.     double alongh, alath ;
  201.     double sinlong, coslong;
  202.     double sinlongh, coslongh;
  203.     double sinlat , coslat ;
  204.     double sinlath, coslath;
  205.     double radx, rady, radz;
  206.     double nscax, nscay, nscaz;
  207.     static double two_pi = 6.2831853071796;
  208.  
  209.     /* Get the steps */
  210.     dlong  = two_pi / (double)numLong;
  211.     dlat   = two_pi / (double)(numLat-1) / (double)2.;
  212.  
  213.     /* Create the scaled radii */
  214.     radx = (double)radius * (double)scale[0];
  215.     rady = (double)radius * (double)scale[1];
  216.     radz = (double)radius * (double)scale[2];
  217.  
  218.     /* Create the normal scale factors */
  219.     nscax = (double)scale[1] * (double)scale[2];
  220.     nscay = (double)scale[0] * (double)scale[2];
  221.     nscaz = (double)scale[0] * (double)scale[1];
  222.  
  223.     /* The Longitudinal Cuts */
  224.     for ( longi = 0 ; longi <= numLong ; longi++ )
  225.     {
  226.         /* Longitudinal Quantities */
  227.         along   = (double)longi * dlong;
  228.         sinlong = (double)sin(along);
  229.         coslong = (double)cos(along);
  230.  
  231.  
  232.         /* At the next half station */
  233.         alongh   = ((double)longi + 0.5) * dlong;
  234.         sinlongh = sin(alongh);
  235.         coslongh = cos(alongh);
  236. #ifdef TEST_PRINT 
  237. printf("sinlongh %6.3f coslongh %6.3f \n ", sinlongh, coslongh);
  238. #endif
  239.  
  240.         /* The Latitudinal Cuts */
  241.         for ( lat  = 0 ; lat  < numLat ; lat ++ )
  242.         {
  243.             /* Latitudinal Quantities */
  244.             alat    = (double)lat  * dlat ;
  245.             sinlat  = sin(alat );
  246.             coslat  = cos(alat );
  247.  
  248.             /* Compute the vertex */
  249.             coords[0][0] = radx * coslong * sinlat +
  250.                     (double)center[0];
  251.             coords[0][1] = rady * coslat +
  252.                     (double)center[1];
  253.             coords[0][2] = radz * sinlong * sinlat +
  254.                     (double)center[2];
  255. #ifdef TEST_PRINT
  256. printf("%6.3f %6.3f %6.3f ", coords[0][0], coords[0][1], coords[0][2]);
  257. #endif
  258.             coords++;
  259.  
  260.             /* Compute the vertex normal */
  261.             normals[0][0] = nscax * coslong * sinlat;
  262.             normals[0][1] = nscay * coslat;
  263.             normals[0][2] = nscaz * sinlong * sinlat;
  264.             unitvec3f(normals[0]);
  265. #ifdef TEST_PRINT
  266. printf("%6.3f %6.3f %6.3f ", normals[0][0], normals[0][1], normals[0][2]);
  267. #endif
  268.             normals++;
  269.  
  270.             if ( (longi < numLong)  && (lat < (numLat - 1)) ) 
  271.             {
  272.             /* Compute the facet normal */
  273.                 alath   = ((double)lat + 0.5) * dlat;
  274.                 sinlath = sin(alath);
  275.                 coslath = cos(alath);
  276.  
  277.                 f_norms[0][0] = nscax * coslongh * sinlath;
  278.                 f_norms[0][1] = nscay * coslath;
  279.                 f_norms[0][2] = nscaz * sinlongh * sinlath;
  280.                 unitvec3f(f_norms[0]);
  281.  
  282. #ifdef TEST_PRINT
  283. printf(" %6.3f %6.3f %6.3f", f_norms[0][0], f_norms[0][1], f_norms[0][2]);
  284. #endif
  285.  
  286.                 f_norms++;
  287.             }
  288. #ifdef TEST_PRINT
  289. printf("\n");
  290. #endif
  291.  
  292.         } /* End for lat */
  293.     } /* End for longi */
  294. }
  295.  
  296. /*--------------------------------------------------------------------*\
  297. | Procedure    :    int genCirclePoly(BIF_Circle *);
  298. |---------------------------------------------------------------------
  299. | Description    :    The generated circle is stored as a polygon.
  300. |            (For the reference port no "non-EXACT"
  301. |            method is supported)
  302. |            genCirclePoly generates points to the buffers 
  303. |            (using bif_pair) just like the parser would.
  304. |            bif_polygon(BIF_P_END) stores/executes the
  305. |            generated data as appropriate.
  306. |---------------------------------------------------------------------
  307. | Return    :    Error Code (Not Implemented)
  308. \*--------------------------------------------------------------------*/
  309. int genCirclePoly(ent)
  310. BIF_Circle *ent;
  311.  
  312. {
  313.     int i;
  314.     double x, y;
  315.     double ang, delang;
  316.     static double two_pi = 6.2831853071796;
  317.  
  318.     /* Compute the arc size */
  319.     delang = two_pi / (double)ent->numLong;
  320.  
  321.     /* Start the polygon */
  322.     bif_polygon(BIF_P_BEGIN);
  323.  
  324.     /* Pass the points */
  325.     for ( i=0 ; i < ent->numLong ; i++)
  326.     {
  327.         /* Compute the values */
  328.         ang = (double)i * delang;
  329.         x = (double)ent->radius * cos(ang) +
  330.             (double)ent->center[0];
  331.         y = (double)ent->radius * sin(ang) +
  332.             (double)ent->center[1];
  333.  
  334.         /* Pass the values */
  335.         bif_pair((BIF_REAL)x, (BIF_REAL)y);
  336.     }
  337.     /* End the polygon */
  338.     bif_polygon(BIF_P_END);
  339.  
  340.     
  341. } /* End genCirclePoly() */
  342.  
  343. /*--------------------------------------------------------------------*\
  344. | Procedure    :    int genCircle3Poly(BIF_Circle3 *);
  345. |---------------------------------------------------------------------
  346. | Description    :    The generated circle3 is stored as a polygon3.
  347. |            (For the reference port no "non-EXACT"
  348. |            method is supported)
  349. |            genCircle3Poly generates points to the buffers 
  350. |            (using bif_triplet) just like the parser would.
  351. |            bif_polygon3(BIF_P_END) stores/executes the
  352. |            generated data as appropriate.
  353. |---------------------------------------------------------------------
  354. | Return    :    Error Code (Not Implemented)
  355. \*--------------------------------------------------------------------*/
  356. int genCircle3Poly(ent)
  357. BIF_Circle3 *ent;
  358.  
  359. {
  360.     int i;
  361.     double ang, delang;
  362.     static double two_pi = 6.2831853071796;
  363.     matrix4 genMat;
  364.     vector4 genPoint, xfPoint;
  365.  
  366.     /* Make the generation space coordinate system */
  367.     makeCircle3Mat(ent->norm,ent->center,genMat);
  368.  
  369.     /* Compute the arc size */
  370.     delang = two_pi / (double)ent->numLong;
  371.  
  372.     /* Start the polygon3 */
  373.     bif_polygon3(BIF_P_BEGIN);
  374.  
  375.     /* Pass the coordinates */
  376.     bif_group(VERTEX_COORDINATES);
  377.     genPoint[2] = 0;
  378.     genPoint[3] = 1;
  379.     for ( i=0 ; i < ent->numLong ; i++)
  380.     {
  381.         /* Compute the values */
  382.         ang = (double)i * delang;
  383.         genPoint[0] = (double)ent->radius * cos(ang) ;
  384.         genPoint[1] = (double)ent->radius * sin(ang) ;
  385.  
  386.         /* Transform the point */
  387.         mx_44mult(genMat,genPoint,xfPoint);
  388.  
  389.         /* Pass the values */
  390.         bif_triplet((BIF_REAL)xfPoint[0], 
  391.             (BIF_REAL)xfPoint[1],
  392.             (BIF_REAL)xfPoint[2]);
  393.     }
  394.     bif_group(BIF_END_OF_GROUP);
  395.  
  396.     /* Pass the Vertex Normals */
  397.     bif_group(VERTEX_NORMALS);
  398.  
  399.     genPoint[0] = 0;
  400.     genPoint[1] = 0;
  401.     genPoint[2] = 1;
  402.     genPoint[4] = 1;
  403.     mx_44mult(genMat,genPoint,xfPoint);
  404.     for ( i=0 ; i < ent->numLong ; i++)
  405.         bif_triplet((BIF_REAL)xfPoint[0], 
  406.             (BIF_REAL)xfPoint[1],
  407.             (BIF_REAL)xfPoint[2]);
  408.  
  409.     bif_group(BIF_END_OF_GROUP);
  410.  
  411.     /* Pass the Facet Normal */
  412.     bif_group(FACET_NORMALS);
  413.     bif_triplet((BIF_REAL)xfPoint[0], (BIF_REAL)xfPoint[1],
  414.             (BIF_REAL)xfPoint[2]);
  415.     bif_group(BIF_END_OF_GROUP);
  416.  
  417.     /* End the polygon3 */
  418.     bif_polygon3(BIF_P_END);
  419.  
  420. } /* End genCircle3Poly() */
  421.  
  422. /*--------------------------------------------------------------------*\
  423. | Procedure    :    int makeCircle3Mat(vector3,vector3,matrix4)
  424. |---------------------------------------------------------------------
  425. | Description    :    Form the transformation needed for generating
  426. |            the CIRCLE3 primitive as spec'd.
  427. |
  428. |            normal maps to +z in the generation coord.
  429. |            system.  center maps to the orientation.  The
  430. |            directions of the inplane vectors are arbitrary.
  431. |---------------------------------------------------------------------
  432. | Return    :    Error Code (not Implemented)
  433. \*--------------------------------------------------------------------*/
  434. int makeCircle3Mat(normal,center,mat)
  435. vector3 normal, center;
  436. matrix4 mat;
  437.  
  438. {
  439.     static vector3 x_axe = { 1., 0., 0.};
  440.     static vector3 y_axe = { 0., 1., 0.};
  441.     static vector3 z_axe = { 0., 0., 1.};
  442.     double dotvec3f();
  443.  
  444.     vector3 x_dir, y_dir, z_dir, other;
  445.     float xDot, yDot, zDot;
  446.  
  447.     /*------------------------------------------------------------*\
  448.     |    find the basis vectors for the generation space
  449.     \*------------------------------------------------------------*/
  450.     /* Find the most "normal" axis */
  451.     xDot = (float)fabs(dotvec3f(normal,x_axe));
  452.     yDot = (float)fabs(dotvec3f(normal,y_axe));
  453.     zDot = (float)fabs(dotvec3f(normal,z_axe));
  454.     if     (( xDot > yDot ) || ( xDot > zDot ))
  455.         copyvec3f(x_axe,other);
  456.     else if    (( yDot > xDot ) || ( yDot > zDot ))
  457.         copyvec3f(y_axe,other);
  458.     else
  459.         copyvec3f(z_axe,other);
  460.  
  461.         /* Cover the cases where the normal runs on an axis -- new */
  462.         if (normal[0] == 0 && normal[1] == 0) copyvec3f(x_axe,other); /*new*/
  463.         if (normal[0] == 0 && normal[2] == 0) copyvec3f(x_axe,other); /*new*/
  464.         if (normal[1] == 0 && normal[2] == 0) copyvec3f(y_axe,other); /*new*/
  465.  
  466.     /* Generate the basis vectors */
  467.     copyvec3f(normal,z_dir);
  468.     crossvec3f(other,normal,x_dir);
  469.     crossvec3f(z_dir,x_dir,y_dir);
  470.  
  471.     /* Normalize them suckers.... */
  472.     unitvec3f(x_dir);
  473.     unitvec3f(y_dir);
  474.     unitvec3f(z_dir);
  475.  
  476.     /*------------------------------------------------------------*\
  477.     |    Load the rows of the matrix with the basis vectors
  478.     \*------------------------------------------------------------*/
  479.     mat[0][0] = x_dir[0];
  480.     mat[0][1] = x_dir[1];
  481.     mat[0][2] = x_dir[2];
  482.     mat[0][3] = 0.0;
  483.  
  484.     mat[1][0] = y_dir[0];
  485.     mat[1][1] = y_dir[1];
  486.     mat[1][2] = y_dir[2];
  487.     mat[1][3] = 0.0;
  488.  
  489. #ifdef EXTERNALNOTES
  490.     The following matrices were originally coded as ...
  491.  
  492.     mat[1][0] = z_dir[0];
  493.     mat[1][1] = z_dir[1];
  494.     mat[1][2] = z_dir[2];
  495.     mat[1][3] = 0.0;
  496.  
  497. #endif
  498.  
  499.     mat[2][0] = z_dir[0];
  500.     mat[2][1] = z_dir[1];
  501.     mat[2][2] = z_dir[2];
  502.     mat[2][3] = 0.0;
  503.  
  504.     /* The centering offset */
  505.     mat[3][0] = center[0];
  506.     mat[3][1] = center[1];
  507.     mat[3][2] = center[2];
  508.     mat[3][3] = 1.0;
  509.  
  510. } /* End makeCircle3Mat */
  511.