home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vectoper.zip / RTMain.cpp < prev    next >
C/C++ Source or Header  |  1997-02-17  |  9KB  |  383 lines

  1.  
  2.  
  3. #include <windows.h>
  4. //#define  _INC_WINDOWS
  5. #include <winsock.h>
  6.  
  7. #include <stdio.h>
  8.  
  9. #include "RTTypes.h"
  10. #include "RTVecOps.h"
  11.  
  12. #include "RTScreen.h"
  13. #include "RTObjOps.h"
  14.  
  15. #include "RTIMOps.h"
  16. #include "RayTrace.h"
  17.  
  18. #include "calypso.H"
  19.  
  20.  
  21.  
  22. #define MAX_HOSTNAME_LENGTH  100
  23.  
  24. sockaddr_in addr;
  25. int RT_sd;
  26. char hostname [MAX_HOSTNAME_LENGTH];
  27.  
  28.  
  29.  
  30.  
  31.  
  32. int num_T = 0;
  33. int nIter = 2;
  34.  
  35. /*
  36.  *
  37.  *
  38.  *  initialize picture region
  39.  *
  40.  *
  41.  */
  42. Region work_reg = {SX_WORLD_MIN_DEFAULT, SY_WORLD_MIN_DEFAULT,
  43.                    SX_WORLD_MAX_DEFAULT, SY_WORLD_MAX_DEFAULT};
  44.  
  45.  
  46.  
  47. Boolean  debug_flag = FALSE;
  48. Boolean  triangle_flag = FALSE;
  49. Boolean  super_sample_flag = FALSE;
  50.  
  51. Boolean XmitPixelsFlag = FALSE;
  52. Boolean WritePixelsFlag = FALSE;
  53.  
  54. void PixelDisplaySetup();
  55.  
  56. Sphere_Obj  sph = { (float) 40.0,  (float) 40.0,  (float) 40.0,
  57.                     (float) 20.0,  (float) 20.0,  (float) 20.0};
  58.  
  59.  
  60. PROCESS_INFORMATION ProcInfo;
  61. STARTUPINFO         StartStruct;
  62.  
  63.  
  64.  
  65.  
  66.  
  67. void 
  68.    CalypsoMain(int argc, char *argv[])
  69.       {
  70.        char config_file[50];
  71.        char opt;            /* gets command line option */
  72.        int i, j;
  73.  
  74.  
  75.        InitCalypso(sizeof SharedType, (char **) &shared);
  76.  
  77.        memset(config_file, 0, 50);
  78.  
  79.  
  80.        debug_flag = FALSE;
  81.        triangle_flag = FALSE;
  82.        super_sample_flag = FALSE;
  83. /*
  84.  +++++++++++++++++++++++++++++++++++++++++++++++++
  85.  *  get command line arguements
  86.  +++++++++++++++++++++++++++++++++++++++++++++++++
  87.  */ 
  88.        i = 1;
  89.  
  90.        while (i < argc)  {
  91.  
  92.            if (argv[i][0] != '-')  {
  93.                printf ("***** Error:  Command line option not preceeded by argument");
  94.                printf (" -- EXITING *****\n");
  95.                printf ("Option was:  %s\n", argv[i]);
  96.                exit(0);
  97.               }
  98.            else
  99.                opt = argv[i++][1];
  100.  
  101.            switch(opt)  {
  102.  
  103. /*
  104.  *             handle option to specify raytrace configuration files
  105.  */ 
  106.                case 'c':
  107.  
  108.                   if (argv[i][0] == '-')  {
  109.                       printf ("***** Error:  Illegal option for command line argument");
  110.                       printf (" -- EXITING *****\n");
  111.                       printf ("Argument was:  -%c   Option was:  %s\n", opt, argv[i]);
  112.                       exit(0);
  113.                      }
  114.                   else
  115.                       strcpy(config_file, argv[i++]);
  116.  
  117.                   break;
  118.  
  119. /*
  120.  *             handle option for printing debug output
  121.  */ 
  122.                case 'd':
  123.  
  124.                   debug_flag = TRUE;
  125.                   break;
  126.  
  127. /*
  128.  *             handle option to specify number of parallel iterations
  129.  */ 
  130.                case 'i':
  131.                   if (argv[i][0] == '-')  {
  132.                       printf ("***** Error:  Illegal option for command line argument");
  133.                       printf (" -- EXITING *****\n");
  134.                       printf ("Argument was:  -%c   Option was:  %s\n", opt, argv[i]);
  135.                       exit(0);
  136.                      }
  137.                   else    {
  138.                       nIter = atoi(argv[i++]);
  139.                      }
  140.  
  141.                   break;
  142.  
  143. /*
  144.  *             handle option to specify triangle flag
  145.  */ 
  146.                case 'r':
  147.  
  148.                   triangle_flag = TRUE;
  149.                   break;
  150.  
  151. /*
  152.  *             handle option to turn on super sampling
  153.  */ 
  154.                case 's':
  155.  
  156.                   super_sample_flag = TRUE;
  157.                   break;
  158.  
  159. /*
  160.  *             handle option to specify number of thread segments in each parallel section
  161.  */ 
  162.                case 't':
  163.                   if (argv[i][0] == '-')  {
  164.                       printf ("***** Error:  Illegal option for command line argument");
  165.                       printf (" -- EXITING *****\n");
  166.                       printf ("Argument was:  -%c   Option was:  %s\n", opt, argv[i]);
  167.                       exit(0);
  168.                      }
  169.                   else  {
  170.                       num_T = atoi(argv[i++]);
  171.                       printf ("***** Error:  Thread Count MUST Be Greater Than 0 -- EXITING *****\n");
  172.                       exit(0);
  173.                      }
  174.  
  175.                   break;
  176.  
  177. /*
  178.  *             handle undefined options
  179.  */ 
  180.                default:
  181.  
  182.                   printf (" ***** Error:  Invalid Command Line Argument: %c -- EXITING *****\n",
  183.                          opt);
  184.                   exit(0);
  185.                   break;
  186.  
  187.               }  /* end -- switch(opt) */
  188.  
  189.           }  /* end -- while */
  190.  
  191.  
  192. /*
  193.  *     Tests for fatal command line omissions
  194.  *        - now limited omission of configuration file
  195.  */
  196.  
  197.        if (strcmp(config_file, "") == 0)  {
  198.            printf ("***** Error:  No Configuration File Specified -- EXITING *****\n");
  199.            exit(0);
  200.           }
  201.  
  202. /*
  203.  *     initialization
  204.  */
  205.        shared->obj_num = 1;
  206.  
  207.        shared->PhongConst = (float) 15.0;
  208.  
  209.        shared->depth_limit = MAX_DEPTH;
  210.  
  211.        shared->obj_list[0].label = SPHERE;
  212.        shared->obj_list[0].ptr  = (char *) &sph;
  213.  
  214.        for (i=0; i< LIGHTS_MAX; i++)  {
  215.  
  216.            shared->lights[i].directed = FALSE;
  217.            shared->lights[i].on = FALSE;
  218.  
  219.            vset(&shared->lights[i].L_pt, (float) 0.0,  (float) 0.0,  (float) 0.0);
  220.            vset(&shared->lights[i].L_a,  (float) 0.0,  (float)-2.0,  (float) 0.0);
  221.            vset(&shared->lights[i].I,    (float) 1.0,  (float) 1.0,  (float) 1.0);
  222.  
  223.            shared->lights[i].ro = (float) 10.0;
  224.            shared->lights[i].cos_delta = (float) 0.0;
  225.           }
  226.  
  227.        shared->Sh_Heap.allocated_space = 0;
  228.        shared->Sh_Heap.free_space_ptr = shared->Sh_Heap.free_space;
  229.   
  230.        RT_Configure(config_file);
  231.  
  232. /*
  233.  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  234.  +  calculate normals for polygon mesh objects
  235.  +     - calculate triangle normals
  236.  +     - calculate approximate normals at vertices
  237.  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  238.  */
  239.        Poly_Mesh_Obj  *obj_ptr;
  240.  
  241.        for (i=0; i<shared->obj_num; i++)  {
  242.  
  243.            if (shared->obj_list[i].label == POLY_MESH)  {
  244.                obj_ptr = (Poly_Mesh_Obj *) shared->obj_list[i].ptr;
  245.  
  246.  
  247.                for (j=0; j<obj_ptr->num_surf; j++)  {
  248.  
  249.                    surf_tri_norms(&obj_ptr->surfs[j], obj_ptr->data);
  250.  
  251.                    surf_pt_norms(&obj_ptr->surfs[j], obj_ptr->surf_norms[j],
  252.                                  obj_ptr->num_vert);
  253.  
  254.                   } /* end -- for each surface in object */
  255.  
  256.               }  /* end -- if object is poly mesh object */
  257.  
  258.           } /* end -- for each object */
  259.  
  260.  
  261.        if (num_T <= 0)  {
  262.            printf ("***** Error:  Number of thread segments not specified -- ");
  263.            printf ("EXITING *****\n");
  264.            exit(0);
  265.           }
  266.  
  267.        if (nIter == 0)
  268.            nIter = 5;
  269.  
  270.        PixelDisplaySetup();
  271.  
  272.        RayTrace();
  273.  
  274.        if (WritePixelsFlag)  {
  275.            fclose(ofp);
  276.           }
  277.  
  278.        shutdown(RT_sd, 1);
  279.        closesocket(RT_sd);
  280.        Sleep(10000);
  281.        exit(0);
  282.       }
  283.  
  284.  
  285.  
  286. /*
  287.  *******************************************************************************
  288.  *
  289.  *  Name:  PixelDisplaySetup
  290.  *
  291.  *  Purpose:
  292.  *
  293.  *
  294.  *
  295.  *  Input Parameters
  296.  *
  297.  *     none
  298.  *
  299.  *
  300.  *  Output Parameters
  301.  *
  302.  *     none
  303.  *
  304.  *******************************************************************************
  305.  */
  306. void PixelDisplaySetup()
  307.    {
  308.     BOOL rv;
  309.     char CmdLine[300];
  310.     int len;
  311.     int width, height;
  312.  
  313.  
  314.     width =   shared->vp.scr_reg.max_pt.x - shared->vp.scr_reg.min_pt.x + 1;
  315.     height =  shared->vp.scr_reg.max_pt.y - shared->vp.scr_reg.min_pt.y + 1;
  316.  
  317.  
  318.     if ((RT_sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)  {
  319.         perror("Socket Creation Error:  ");
  320.         exit(1);
  321.        }
  322.  
  323.     if (gethostname(hostname, MAX_HOSTNAME_LENGTH) == -1)  {
  324.         perror("Get Hostname Error:  ");
  325.         exit(1);
  326.        }
  327.  
  328.     addr.sin_addr.s_addr = INADDR_ANY;
  329.     addr.sin_port = 0;
  330.     addr.sin_family = AF_INET;
  331.  
  332.     if (bind(RT_sd, (sockaddr *) &addr, sizeof(addr)) == -1)  {
  333.         perror("Bind Error:  ");
  334.         exit(1);
  335.        }
  336.  
  337.     if (listen(RT_sd, FD_SETSIZE) == -1)  {
  338.         perror("Listen Error:  ");
  339.         exit(1);
  340.        }
  341.  
  342.     XmitPixelsFlag = TRUE;
  343.  
  344.  
  345.     len = sizeof(addr);
  346.  
  347.     if (getsockname(RT_sd, (struct sockaddr *) &addr, &len) == SOCKET_ERROR)  {
  348.         perror("Getsockname Error:  ");
  349.         exit(1);
  350.        }
  351.  
  352.     sprintf(CmdLine, "PixDsply.exe  -h %d  -w %d  -s %s %d", height, width, hostname,
  353.                                                                ntohs(addr.sin_port) );
  354.     StartStruct.cb            = sizeof(STARTUPINFO);
  355.     StartStruct.lpReserved    = NULL;
  356.     StartStruct.lpDesktop    = NULL;
  357.     StartStruct.lpTitle        = NULL;
  358. //  StartStruct.dwFlags        = STARTF_USESHOWWINDOW;
  359.     StartStruct.cbReserved2    = 0;
  360.     StartStruct.lpReserved2    = NULL;
  361. //  StartStruct.wShowWindow    = SW_MAXIMIZE;
  362.                   
  363.     rv = CreateProcess( "PixDsply.exe",
  364.                         CmdLine,        // Commandline Arguments
  365.                         NULL,            // Process Security Attributes - none
  366.                         NULL,            // Thread Security Attributes - none
  367.                         FALSE,            // Inherit Parents Attributes - no
  368.                         0,                // Additional Attributes - none
  369.                         NULL,            // Environment Pointer
  370.                         NULL,            // Current Directory
  371.                         &StartStruct,    // Startup Information Structure
  372.                         &ProcInfo);        // Process Information Structure
  373.  
  374.     if (!rv)  {
  375.         printf ("***** Error:  Display Process Creation Failed");
  376.         printf (" -- EXITING *****\n");
  377.         exit(1);
  378.        }
  379.  
  380.    }
  381.  
  382.  
  383.