home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / IRIT / POLY3DS.ZIP / POLY3D.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-05  |  11.1 KB  |  324 lines

  1. /*****************************************************************************
  2. *   Program to draw 3D object as wireframe from    any ortographic view         *
  3. * Options:                                     *
  4. * 1. -c : Object is closed. If object is closed each edge is shared by two   *
  5. *         polygons and therefore can bedrawn only once.                 *
  6. * 2. -e #Edges : If the edges are order is specific way (like in DrawFn3D)   *
  7. *         and only the k first edges of each polygons are to be displayed    *
  8. *      use this option as -e k.                         *
  9. * 3. -i : Internal edges. IRIT solid modeller may generate edges, which one  *
  10. *      may not want to see (default). -i will draw all of them.         *
  11. * 4. -m : More flag, to print more imformation on input/errors.             *
  12. *         Note all messages goes to STDOUT (not stderr!) as this program     *
  13. *      works in graphic mode, and we can redirect stdout to a file.       *
  14. * 5. -n : Draw vertices normals if the data has them, otherwise ignore.      *
  15. * 6. -o Objects : list of objects to be displayed from the input data files. *
  16. *      This option should not be last and Object list terminates with     *
  17. *      next option (- as first char). ALL objects are displayed otherwise.*
  18. * 7. -v ViewFile : optional view to start with.                     *
  19. * 8. -z : Print current version, and some helpfull data.             *
  20. *                                         *
  21. * Note some of those options way be permanently set to a different default   *
  22. * using the configuration file "Poly3D.cfg"                     *
  23. *                                         *
  24. * Usage:                                     *
  25. *   Poly3D [-c] [-m] [-i] [-e #Edges] [-n] [-N] [-o Objects] [-v ViewFile]   *
  26. *                                [-z] Files   *
  27. *                                         *
  28. * Written by:  Gershon Elber                Ver 2.2, Apr 1990    *
  29. *****************************************************************************/
  30.  
  31. #ifdef __MSDOS__
  32. #include <stdlib.h>
  33. #include <conio.h>
  34. #include <dos.h>
  35. #include <graphics.h>
  36. #include <alloc.h>
  37. #endif /* __MSDOS__ */
  38.  
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <time.h>
  42. #include "program.h"
  43. #include "getarg.h"
  44. #include "genmat.h"
  45. #include "parser.h"
  46. #include "graphgng.h"
  47. #include "viewobjg.h"
  48. #include "config.h"
  49.  
  50. #ifdef __MSDOS__
  51. #include "mousedrv.h"
  52. #include "matherr.h"
  53. #endif /* __MSDOS__ */
  54.  
  55. #ifdef __MSDOS__
  56. extern unsigned int _stklen = 32766;         /* Increase default stack size. */
  57. #endif /* __MSDOS__ */
  58.  
  59. #ifdef SYSV
  60. static char *VersionStr =
  61.     "Poly3D        version 2.2,    Gershon Elber,\n\
  62.      (C) Copyright 1989 Gershon Elber, Non commercial use only.";
  63. #else
  64. static char *VersionStr = "Poly3D    version 2.2,    Gershon Elber,    "
  65.         __DATE__ ",   " __TIME__ "\n"
  66.         "(C) Copyright 1989 Gershon Elber, Non commercial use only.";
  67. #endif /* SYSV */
  68.  
  69. static char *CtrlStr =
  70.     "poly3d c%- m%- i%- e%-#Edges!d n%- N%- o%-Objects!*s v%-ViewFile!s z%- DFiles!*s";
  71. static BinTree *FreeBinTreeList    = NULL;        /* List of freed bin tree nodes. */
  72.  
  73. static long SaveTotalTime;
  74.  
  75. MatrixType GlblViewMat;                  /* Current view of object. */
  76. MatrixType GlblPerspMat =         /* Perspective optional transformation. */
  77.     {    { 1.0,   0.0,   0.0,   0.0 },
  78.     { 0.0,   1.0,   0.0,   0.0 },
  79.     { 0.0,   0.0,   0.1,  -0.35 },
  80.     { 0.0,   0.0,   0.35,  1.0 }
  81.     };
  82. MatrixType CrntViewMat;                /* This is the current view! */
  83. int GlblTransformMode = TRANS_SCREEN; /* Screen, Object coords. trans. mode. */
  84. int GlblViewMode = VIEW_ORTHOGRAPHIC;       /* Perspective, Orthographic etc. */
  85. int GlblDepthCue = TRUE;               /* Activate depth cueing. */
  86. int GlblNormalLen = NORMAL_DEFAULT_LENGTH;   /* Scaler for normals if drawn. */
  87.  
  88. /* The following are setable variables (via configuration file poly3d.cfg).  */
  89. #ifdef __MSDOS__
  90. int MouseExists = FALSE, GraphDriver = DETECT;
  91. #else
  92. int MouseExists = TRUE;
  93. #endif /* __MSDOS__ */
  94.  
  95. int InterFlag = FALSE, MoreFlag = FALSE, TextFlag = FALSE, NumEdges = 0,
  96.     DrawVNormalsFlag = FALSE, DrawPNormalsFlag = FALSE, ViewFlag = FALSE,
  97.     ClosedObject = FALSE;
  98.  
  99. static ConfigStruct SetUp[] =
  100. {
  101. #ifdef __MSDOS__
  102.   { "Mouse",        (VoidPtr) &MouseExists,        SU_BOOLEAN_TYPE },
  103.   { "GraphDriver",    (VoidPtr) &GraphDriver,        SU_INTEGER_TYPE },
  104. #endif /* __MSDOS__ */
  105.   { "ViewMode",        (VoidPtr) &GlblViewMode,    SU_INTEGER_TYPE },
  106.   { "TransMode",    (VoidPtr) &GlblTransformMode,    SU_INTEGER_TYPE },
  107.   { "ClosedObject",    (VoidPtr) &ClosedObject,    SU_BOOLEAN_TYPE },
  108.   { "DepthCue",        (VoidPtr) &GlblDepthCue,    SU_BOOLEAN_TYPE },
  109.   { "Internal",        (VoidPtr) &InterFlag,        SU_BOOLEAN_TYPE },
  110.   { "More",        (VoidPtr) &MoreFlag,        SU_BOOLEAN_TYPE },
  111.   { "NormalLength",    (VoidPtr) &GlblNormalLen,    SU_INTEGER_TYPE },
  112.   { "NumOfEdges",    (VoidPtr) &NumEdges,        SU_INTEGER_TYPE } };
  113.  
  114. #define NUM_SET_UP    (sizeof(SetUp) / sizeof(ConfigStruct))
  115.  
  116. static void MainGetViewFile(char *ViewFileName);
  117. static FileDescription **MainGetDataFiles(char **DataFileNames,
  118.                             int NumOfDataFiles);
  119. static void FreeBinTree(BinTree *Pbin);
  120.  
  121. /*****************************************************************************
  122. * Main routine - Read Parameter    line and do what you need...             *
  123. *****************************************************************************/
  124. void main(int argc, char **argv)
  125. {
  126.     int EdgesFlag = 0, ObjFlag = 0, NumObjs = 0, VerFlag = 0, NumFiles = 0,
  127.     Error;
  128.     char **Objects = NULL, *ViewName = NULL, **FileNames = NULL, *ErrorMsg;
  129.     FileDescription **FD;
  130.  
  131.     SaveTotalTime = time(NULL);
  132.  
  133. #ifdef __MSDOS__
  134.     ctrlbrk((int (*)()) MyExit);              /* Kill process if ^C. */
  135. #endif /* __MSDOS__ */
  136.  
  137. #ifdef __MSDOS__
  138.     MathErrorSetUp(ME_KILL, NULL);     /* Kill process if math error occurs! */
  139.  
  140.     MouseExists = MouseDetect();       /* Auto test for mouse existance. */
  141. #endif /* __MSDOS__ */
  142.  
  143.     Config("poly3d", SetUp, NUM_SET_UP);     /* Read config. file if exists. */
  144.  
  145.     if ((Error = GAGetArgs (argc, argv, CtrlStr,
  146.         &ClosedObject, &MoreFlag, &InterFlag, &EdgesFlag,
  147.         &NumEdges, &DrawVNormalsFlag, &DrawPNormalsFlag, &ObjFlag,
  148.         &NumObjs, &Objects, &ViewFlag, &ViewName,
  149.         &VerFlag, &NumFiles, &FileNames)) != 0) {
  150.     GAPrintErrMsg(Error);
  151.     GAPrintHowTo(CtrlStr);
  152.     MyExit(1);
  153.     }
  154.  
  155.     if (VerFlag) {
  156.     printf("\n%s\n\n", VersionStr);
  157.     GAPrintHowTo(CtrlStr);
  158.     ConfigPrint(SetUp, NUM_SET_UP);
  159.     MyExit(0);
  160.     }
  161.  
  162.     if (!NumFiles) {
  163.     fprintf(stderr, "No data file names where given, exit\n");
  164.     GAPrintHowTo(CtrlStr);
  165.     MyExit(1);
  166.     }
  167.  
  168.     MainGetViewFile(ViewName);                   /* Into GlblViewMat. */
  169.  
  170.     /* Get the data files: */
  171.     FD = MainGetDataFiles(FileNames, NumFiles);
  172.  
  173. #ifdef __MSDOS__
  174.     GGInitGraph();                 /* Initiate the graphic driver. */
  175. #else
  176.     GGInitGraph(argc, argv);             /* Initiate the graphic driver. */
  177. #endif /* __MSDOS__ */
  178.  
  179. #ifdef __MSDOS__
  180.     if (MouseExists)
  181.     if ((ErrorMsg = MouseInit()) != 0) {
  182.         /* Must be called before any usage (but after InitGraph routine)!*/
  183.         printf("\n%s\n\n\tPress any key to continue:", ErrorMsg);
  184.         MouseExists = FALSE;
  185.         getch();
  186.     }
  187. #endif /* __MSDOS__ */
  188.  
  189.     InteractGeomObject(FD, NumObjs, Objects);
  190.  
  191.     MyExit(0);
  192. }
  193.  
  194. /*****************************************************************************
  195. * Main routine to read the view    description file:                 *
  196. *****************************************************************************/
  197. static void MainGetViewFile(char *ViewFileName)
  198. {
  199.     FILE *f;
  200.  
  201.     if (ViewFlag) {
  202.     f = fopen(ViewFileName, "rt");               /* Open the file. */
  203.     if (!f)
  204.     {
  205.          printf("Can't open view file %s\n", ViewFileName);
  206.          MyExit(1);
  207.     }
  208.     }
  209.  
  210.     GetViewFile(f, ViewFlag);        /* Get the view data - into GlblViewMat. */
  211.  
  212.     if (ViewFlag) fclose(f);                   /* Close the file. */
  213. }
  214.  
  215. /*****************************************************************************
  216. * Main routine to read the data    description files:                 *
  217. * Returns pointer to pointers on FileDescription structures (one per file).  *
  218. *****************************************************************************/
  219. static FileDescription **MainGetDataFiles(char **DataFileNames,
  220.                             int NumOfDataFiles)
  221. {
  222.     int    i;
  223.     FILE *f;
  224.     FileDescription **FDbase, **FDinc; /* Used to save memory pointers loc. */
  225.  
  226.     FDbase = FDinc = (FileDescription **)   /* Allocate the pointers block. */
  227.      MyMalloc((unsigned) sizeof(FileDescription *) * (NumOfDataFiles + 1));
  228.  
  229.     for    (i=0; i<NumOfDataFiles;    i++) {
  230.     if (MoreFlag) printf("Reading data file %s\n", *DataFileNames);
  231.     f = fopen(*DataFileNames, "rt");          /* Open the file. */
  232.     if (!f)
  233.     {
  234.         printf("Can't open data file %s\n", *DataFileNames);
  235.         MyExit(1);
  236.     }
  237.  
  238.     *FDinc = GetDataFile(f);              /* Get the data file. */
  239.     FreeBinTree((*FDinc) ->    VertexPointer);/* We dont need these trees. */
  240.     FreeBinTree((*FDinc) ->    PolygonPointer); /* objects are all loaded. */
  241.     (*FDinc) -> VertexPointer = (*FDinc) ->    PolygonPointer =
  242.                             (BinTree *) NULL;
  243.     *FDinc++;
  244.  
  245.     (void) fclose(f);                 /* Close the file. */
  246.  
  247.     DataFileNames++;             /* Skip to next file name. */
  248.     }
  249.  
  250.     *FDinc = (FileDescription *) NULL;
  251.     return FDbase;
  252. }
  253.  
  254. /*****************************************************************************
  255. * My Routine to    allocate dynamic memory. All program requests must call this *
  256. * routine (no direct call to malloc). Dies if no memory.             *
  257. *****************************************************************************/
  258. char *MyMalloc(unsigned size)
  259. {
  260.     static int Count = 0;
  261.     char *p;
  262.     BinTree *Pbin;
  263.  
  264.     if ((size == sizeof(BinTree)) && (FreeBinTreeList))    {
  265.     Pbin = FreeBinTreeList;
  266.     FreeBinTreeList    = FreeBinTreeList -> right;
  267.     return (char *)    Pbin;
  268.     }
  269.  
  270. #ifdef __MSDOS__
  271.     if (Count++ == 50)
  272.     {
  273.     Count = 0;
  274.     printf("Core left: %ldk   \r", coreleft() / 1024);
  275.     }
  276. #endif /* __MSDOS__ */
  277.  
  278.     if ((p = malloc(size)) != NULL) return p;
  279.  
  280.     printf("Not enough memory, exit\n");
  281.     MyExit(2);
  282.  
  283.     return NULL;                    /* Make warnings silent. */
  284. }
  285.  
  286. /*****************************************************************************
  287. * Routine to free a binary tree, which is not needed any more.             *
  288. * Saves    the freed nodes    in a linear list to be used by malloc.             *
  289. *****************************************************************************/
  290. static void FreeBinTree(BinTree *Pbin)
  291. {
  292.     if (Pbin) {
  293.     FreeBinTree(Pbin -> right);
  294.     FreeBinTree(Pbin -> left);
  295.     Pbin ->    right =    FreeBinTreeList;
  296.     FreeBinTreeList    = Pbin;
  297.     }
  298. }
  299.  
  300. /*****************************************************************************
  301. * MyExit routine. Note it might call to CloseGraph without calling         *
  302. * InitGraph(), or call MouseClose() without MouseInit() etc. and it is the   *
  303. * responsibility of the individual modules to do nothing in these cases.     *
  304. *****************************************************************************/
  305. void MyExit(int ExitCode)
  306. {
  307.     GGCloseGraph();                /* Close the graphic driver. */
  308. #ifdef __MSDOS__
  309.     MouseClose();                /* Disable mouse interrupts. */
  310. #endif /* __MSDOS__ */
  311.  
  312. #ifdef __MSDOS__
  313.     fprintf(stderr,
  314.         "\nPoly3D: Total RealTime %ld seconds, Core left %ldk.\n",
  315.         time(NULL) - SaveTotalTime, coreleft() / 1024);
  316. #else
  317.     fprintf(stderr,
  318.         "\nPoly3D: Total RealTime %ld seconds.\n",
  319.         time(NULL) - SaveTotalTime);
  320. #endif /* __MSDOS__ */
  321.  
  322.     exit(ExitCode);
  323. }
  324.