home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 543a.lha / Nebula / source.LZH / source / 3d.c next >
Encoding:
C/C++ Source or Header  |  1991-06-10  |  10.8 KB  |  542 lines

  1. /*
  2.  
  3. ------------------------------------------------------------------
  4.  
  5. Black Nebula
  6.  
  7. File :                3d.c
  8. Programmer:        Colin Adams
  9. Date:                4/3/91
  10. Last Modified :    10/6/91
  11.  
  12. ------------------------------------------------------------------
  13.  
  14.             +y
  15.             |
  16.             |
  17.             |
  18.             |
  19.             |
  20.             |
  21.             |
  22.             |
  23.             |
  24.             ------------ +x
  25.           /
  26.          /
  27.         /    
  28.     /
  29.    /
  30.      +z
  31.  
  32. */
  33.  
  34.  
  35. /*    ---------------------------------------------------------------
  36.         Defines/Includes
  37.         ---------------------------------------------------------------
  38. */
  39.  
  40. #define MAINCODE
  41. #define AMIGA_INCLUDES
  42.  
  43. #include "3d.h"
  44.  
  45. #include <hardware/custom.h>
  46. #include <hardware/cia.h>
  47.  
  48. #define CIAAPRA 0xBFE001 
  49.  
  50. #define FIRE   1
  51. #define RIGHT  2
  52. #define LEFT   4
  53. #define DOWN   8
  54. #define UP    16
  55.  
  56. extern struct Custom far custom;
  57. struct CIA *cia = (struct CIA *) CIAAPRA;
  58.  
  59. extern struct RastPort my_rast_port, back_rast_port;
  60. extern struct View my_view, back_view;
  61. extern struct Screen *screen;
  62. extern struct Window *window1;
  63. extern void build_table(void), SetUpExplosions(void);
  64. extern short U_rot, W_rot;
  65.  
  66. /*    ------------------------------------------------------------------
  67.         General Variables
  68.         ------------------------------------------------------------------
  69. */
  70.  
  71. char filename[80];
  72. object myship;
  73. short swapflag = 1;
  74.  
  75. /*    ------------------------------------------------------------------
  76.         Amiga Variables
  77.         ------------------------------------------------------------------
  78. */    
  79.  
  80. UWORD Palette2[] =
  81. {
  82.     0x0000,
  83.     0x000F,
  84.     0x00D7,
  85.     0x0098,
  86.     0x0078,
  87.     0x0047,
  88.     0x0016,
  89.     0x0024,
  90.     0x0FF0,
  91.     0x0FB1,
  92.     0x0F72,
  93.     0x0C21,
  94.     0x0803,
  95.     0x0503,
  96.     0x0F0F,
  97.     0x0847 
  98. };
  99.  
  100.  
  101. /*    ------------------------------------------------------------------
  102.         EXTERNAL ROUTINES
  103.         ------------------------------------------------------------------
  104. */
  105.  
  106. extern object *CreateObject();
  107.  
  108.  
  109. /*    ------------------------------------------------------------------
  110.         START OF ROUTINES
  111.         ------------------------------------------------------------------
  112. */
  113.  
  114. void CleanUpandExit(void)
  115. /*
  116. Frees resources and quits.  Unfortunately fails to free some memory 
  117. somewhere, if the OS was done properly wouldn't be a problem, but I'll
  118. try and track it down sometime.
  119. */
  120. {
  121.     EndSound();
  122.     
  123.     if(window1)
  124.         CloseWindow(window1);
  125.     if(screen)
  126.         CloseScreen(screen);
  127.   if(IntuitionBase)
  128.      CloseLibrary(IntuitionBase);
  129.   if(GfxBase)
  130.      CloseLibrary((struct Library *) GfxBase);
  131.   exit(1);
  132. }
  133.  
  134. UBYTE Joystick(void)
  135. /* 
  136. Reads the joystick port. Goes straight to the metal...
  137. All flames will be ignored.
  138. */
  139. {
  140.   UBYTE data = 0;
  141.   UWORD joy;
  142.   
  143.     joy = custom.joy1dat;
  144.   data += !( cia->ciapra & 0x0080 ) ? FIRE : 0;
  145.   data += joy & 0x0002 ? RIGHT : 0;
  146.   data += joy & 0x0200 ? LEFT : 0;
  147.   data += (joy >> 1 ^ joy) & 0x0001 ? DOWN : 0;
  148.   data += (joy >> 1 ^ joy) & 0x0100 ? UP : 0;
  149.  
  150.   return data;
  151. }
  152.  
  153. UBYTE Keyboard(void)
  154. /*
  155. Returns the keycode of a key pressed. Goes straight to the
  156. hardware, which isn't very nice to the OS, but is fast.
  157. */
  158. {
  159.   UBYTE code;
  160.   code = cia->ciasdr ^ 0xFF;
  161.   code = code & 0x01 ? (code>>1)+0x80 : code>>1;
  162.   return code;
  163. }
  164.  
  165. void get2d(short px, short py, short pz)
  166. /*
  167. Converts a point onto a 2d plane from 3d.  This code is optimized
  168. to be fast, not readable.  The details of the algorithm are in
  169. 3d.h.  Though naturally you should be able to understand this :-)
  170. */
  171. {
  172.     register int temp;
  173.     register int rx, ry, rz;
  174.     
  175.     rx = px - ex;
  176.     ry = py - ey;
  177.     rz = pz - ez;
  178.     
  179.     d = getmultC(ux, rx) + getmultC(uy, ry) + getmultC(uz,rz);
  180.  
  181.     temp = getmultC(vx, rx) + getmultC(vy, ry) + getmultC(vz,rz);
  182.     y = d ? - (temp*571) / d : 0;
  183.  
  184.     temp = getmultC(wx, rx) + getmultC(wy, ry) + getmultC(wz,rz);
  185.     x = (G/2) + (d ? (temp*571) / d : 0);
  186. }
  187.  
  188. void SetUp3d(void)
  189. /* must initialize viewing vectors */
  190. {
  191.     k.numer = 571; /* this variable is not used, just here for reference */
  192.     k.denom = 1; /* because this is 1, it simplifies the calculations */
  193.     
  194.     ex = 4000;                     /* eye location */
  195.     ey = 500;
  196.     ez = 1000;
  197.     
  198.     setconst(vx,0);             /* top of skull unit vector */
  199.     setconst(vy,1);
  200.     setconst(vz,0);
  201.     
  202.     setconst(wy,0);             /* out of right ear vector */
  203.     SetFixed(wx, 1000,1000);
  204.     SetFixed(wz, 0, 1000);
  205.     
  206.     setconst(uy,0);             /* view vector */
  207.     SetFixed(ux, 0, 1000);
  208.     SetFixed(uz, -1000, 1000);
  209.     view_mode = 0;
  210.     View_Angle = 270;
  211.     U_rot = 270; W_rot = 180;
  212. }
  213.  
  214. /*    ------------------------------------------------------------------
  215.         Routines for allowing objects to be defined
  216.         ------------------------------------------------------------------
  217. */
  218.  
  219. void EnterObject(void)
  220. {
  221.     object *obj;
  222.     polygon *pg;
  223.     int sides, i;
  224.     int convert;
  225.     int x,y,z;
  226.     
  227.     obj = CreateObject();
  228.     
  229.     printf("ENTER:\n");
  230.     printf("Enter the name of the object : ");
  231.     scanf("%s",filename);
  232.     strcat(filename, ".object");
  233.     
  234.     fflush(stdin);
  235.     printf("Object Types:\n");
  236.     printf("Enter the type of object : ");
  237.     
  238.     scanf("%d",&convert);
  239.     obj->type = convert;
  240.     
  241.     printf("How many points comprise the object ? ");
  242.     scanf("%d",&sides); /* get number points in object */
  243.     obj->numpoints = sides;
  244.     
  245.     for(i=1; i<=sides; i++)
  246.     {
  247.         printf("Enter point %d: x, y, z = ",i);
  248.         scanf("%d %d %d",&x,&y,&z);
  249.         obj->objpoints[i].x = x;
  250.         obj->objpoints[i].y = y;
  251.         obj->objpoints[i].z = z;
  252.     }
  253.     
  254.     printf("Enter the centre point of the object (x,y,z) ? ");
  255.     scanf("%d %d %d",&x, &y, &z);
  256.     obj->start_x = x;
  257.     obj->start_y = y;
  258.     obj->start_z = z;
  259.     
  260.     printf("Enter the approx. radius squared of the object ? ");
  261.     scanf("%d",&x);
  262.     obj->radius = x;
  263.     
  264.     printf("Enter the number of polygons : ");
  265.     scanf("%d",&sides);
  266.     
  267.     for(i=0; i<sides; i++)
  268.     {
  269.         int numpoints, j;
  270.         
  271.         printf("Enter data for polygon %d :\n",i+1);
  272.         do
  273.         {
  274.             printf("How many points are in this polygon ? ");
  275.             scanf("%d",&numpoints);
  276.         } while(numpoints>MAX_POINTS);
  277.         
  278.         pg = (polygon *) malloc(sizeof(polygon));
  279.  
  280.         printf("Colours :\n");
  281.         printf("0-black, 1-blue, 2-green, 3-lt.green 4-grey, 5-med.blue\n");
  282.         printf("6-matt.blue,7-blue/black,8-yellow,9-lt.orange,10-orange\n");
  283.         printf("11-red,12-d.red,13-v.d.red,14-d.laven,15-lavender\n");
  284.  
  285.         printf("What colour is it ? ");
  286.         scanf("%d", &j);
  287.         pg->colour = j;
  288.         
  289.         printf("Enter %d Point Numbers : ", numpoints);
  290.         
  291.         for(j=0; j<numpoints; j++)
  292.         {
  293.             scanf("%d",&convert);
  294.             pg->p[j] = convert;    
  295.         }
  296.         
  297.         printf("Enter the centre of the polygon (x,y,z) ? ");
  298.         scanf("%d %d %d",&x, &y, &z);
  299.         pg->centre.x = x;
  300.         pg->centre.y = y;
  301.         pg->centre.z = z;
  302.         
  303.         pg->numpoints = numpoints;
  304.         AddPoly(obj, pg);
  305.     }
  306.     
  307.     Save_Object(obj, filename);
  308.     Destroy_Object(obj);
  309. }
  310.  
  311. void ChangeObjectFile(void)
  312. {
  313.     object *obj;
  314.     polygon *p;
  315.     int i, count = 1;
  316.     int x,y,z;
  317.         
  318.     printf("CHANGE :\n");
  319.     printf("Enter the object name (NO .object) - ");
  320.     scanf("%s",filename);
  321.     
  322.     printf("\nObject - %s\n",filename);
  323.     strcat(filename,".object");
  324.     
  325.     obj = CreateObject();
  326.     if(!(Load_Object(obj, filename)))
  327.         return;
  328.     
  329.     printf("What would you like to change ?\n");
  330.     printf("(2=details, 1=point, 0=poly) ?");
  331.     scanf("%d",&i);
  332.     
  333.     if(i==2)
  334.     {
  335.         printf("Current values :\n");
  336.         printf("Type = %d\n",obj->type);
  337.         printf("Numpoints = %d\n",obj->numpoints);
  338.         printf("Radius = %d\n",obj->radius);
  339.         
  340.         printf("\nEnter type : ");
  341.         scanf("%d", &i);
  342.         obj->type = i;
  343.         
  344.         printf("Enter numpoints : ");
  345.         scanf("%d",&i);
  346.         obj->numpoints = i;
  347.         
  348.         printf("Enter radius^2 : ");
  349.         scanf("%d",&i);
  350.         obj->radius = i;
  351.     
  352.         printf("Ok.\n");
  353.         Save_Object(obj, filename);
  354.         Destroy_Object(obj);
  355.         return;
  356.     }
  357.     
  358.     if(i)
  359.     {
  360.         printf("Enter point to change (max is %d) ?",obj->numpoints);
  361.         scanf("%d", &i);
  362.         printf("Enter new value for point (x,y,z) ? ");
  363.         scanf("%d %d %d", &x, &y, &z);
  364.         obj->objpoints[i].x = x;
  365.         obj->objpoints[i].y = y;
  366.         obj->objpoints[i].z = z;
  367.         printf("Ok.\n");
  368.         Save_Object(obj, filename);
  369.         Destroy_Object(obj);
  370.         return;
  371.     }
  372.     
  373.     printf("Enter number of polygon to change ? ");
  374.     scanf("%d",&i);
  375.     p = obj->poly;
  376.     
  377.     while(p && count!=i)
  378.     {
  379.         p = (polygon *) p->next;
  380.         count++;
  381.     }
  382.     
  383.     if(!p)
  384.     {
  385.         printf("That polygon doesn't exist!\n");
  386.         Destroy_Object(obj);
  387.         return;
  388.     }
  389.     
  390.     printf("Old Values :\n");
  391.     printf("  Number of points - %d\n",p->numpoints);
  392.     printf("  Colour - %d\n",p->colour);
  393.     printf("  Centre %d %d %d\n",p->centre.x,p->centre.y,
  394.         p->centre.z);
  395.         
  396.     for(i=0; i<p->numpoints; i++)
  397.         printf("  Point %d : %d\n",i+1,p->p[i]);
  398.         
  399.     printf("Enter no. of points in polygon ?");
  400.     scanf("%d", &i);
  401.     p->numpoints = i;
  402.     
  403.     printf("Colours :\n");
  404.     printf("0-black, 1-blue, 2-green, 3-lt.green 4-grey, 5-med.blue\n");
  405.     printf("6-matt.blue,7-blue/black,8-yellow,9-lt.orange,10-orange\n");
  406.     printf("11-red,12-d.red,13-v.d.red,14-d.laven,15-lavender\n");
  407.     printf("What colour is it ? ");
  408.     scanf("%d", &i);
  409.     p->colour = i;
  410.     
  411.     printf("Enter the centre of the polygon (x,y,z) ? ");
  412.     scanf("%d %d %d", &x, &y, &z);
  413.     p->centre.x = x;
  414.     p->centre.y = y;
  415.     p->centre.z = z;
  416.     
  417.     printf("Enter the points : ");
  418.     
  419.     for(i=0; i<p->numpoints; i++)
  420.     {
  421.             scanf("%d", &x);
  422.             p->p[i] = x;
  423.     }
  424.     
  425.     printf("Object Changed.\n");
  426.     Save_Object(obj, filename);
  427.     Destroy_Object(obj);
  428. }
  429.  
  430. void PrintObjValues(void)
  431. {
  432.     object *obj;
  433.     polygon *p;
  434.     int i, counter = 1;
  435.         
  436.     printf("PRINT :\n");
  437.     printf("Enter the object name (NO .object) - ");
  438.     scanf("%s",filename);
  439.     
  440.     printf("\nObject - %s\n",filename);
  441.     strcat(filename,".object");
  442.     
  443.     obj = CreateObject();
  444.     if(!(Load_Object(obj, filename)))
  445.         return;
  446.  
  447.     printf("Type - %d\n",obj->type);
  448.     p = obj->poly;
  449.     
  450.     printf("Centre - %d %d %d\n",obj->start_x, obj->start_y,
  451.         obj->start_z);
  452.  
  453.     printf("Radius - %d\n", obj->radius);
  454.     
  455.     printf("Points :\n");
  456.     for(i=1; i<=obj->numpoints; i++)
  457.     {
  458.         printf("Point %d = %d, %d, %d\n",i,obj->objpoints[i].x,
  459.             obj->objpoints[i].y, obj->objpoints[i].z);
  460.     }
  461.     
  462.     while(p)
  463.     {
  464.         printf("Polygon %d :\n",counter++);
  465.         printf("  Number of points - %d\n",p->numpoints);
  466.         printf("  Colour - %d\n",p->colour);
  467.         printf("  Centre %d %d %d\n",p->centre.x,p->centre.y,
  468.             p->centre.z);
  469.         
  470.         for(i=0; i<p->numpoints; i++)
  471.             printf("  Point %d : %d\n",i+1,p->p[i]);
  472.         
  473.         p = (polygon *) p->next;
  474.     }
  475.     Destroy_Object(obj);
  476. }
  477.  
  478. void OptionMenu(void)
  479. {
  480.     int c;
  481.     
  482.     while(1)
  483.     {
  484.         printf("\nF-79 OBJECT CREATION\n");
  485.         printf("Options :\n");
  486.         printf("1 - Enter an Object\n");
  487.         printf("2 - List Objects\n");
  488.         printf("3 - Change Object\n");
  489.         printf("4 - Print Object values\n");
  490.         printf("5 - Exit\n");
  491.         printf("Enter option : ");
  492.         
  493.         fflush(stdin);
  494.         scanf("%d",&c);
  495.         printf("\n");
  496.         
  497.         switch(c)
  498.         {
  499.             case 1:    EnterObject(); break;
  500.             case 2:    system("dir #?.object"); break;
  501.             case 3:    ChangeObjectFile(); break;
  502.             case 4:    PrintObjValues(); break;
  503.             case 5:  CleanUpandExit(); break;
  504.             default:    printf("Unknown Option!\n");
  505.         }        
  506.     }
  507. }
  508.  
  509. int main(char argc, char *argv[])
  510. {
  511.     IntuitionBase = (struct IntutionBase *)
  512.         OpenLibrary("intuition.library",0);
  513.     
  514.     if(IntuitionBase==NULL)
  515.         exit(FALSE);
  516.     
  517.     GfxBase = (struct GfxBase *)
  518.         OpenLibrary("graphics.library",0);
  519.     
  520.     if(GfxBase==NULL)
  521.         CleanUpandExit();
  522.  
  523.     TrigSetUp();    /* set up fast sin/cos table */
  524.     build_table(); /* set up fast square root table */
  525.     Setupangles(); /* set up fast asin table */
  526.  
  527.     
  528.     if(argc>=2)
  529.         OptionMenu();
  530.     else
  531.     {
  532.         SoundSetUp();  /* load sound effects */
  533.         SetRandom();
  534.         SetUpExplosions();
  535.         DoIntro();
  536.     }
  537.     
  538.     CleanUpandExit();
  539. }
  540.  
  541. /* end of module 3d.c */
  542.