home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Exec 4 / CD_Magazyn_EXEC_nr_4.iso / Recent / dev / c / GSys.lha / gsys / g3d / G3DObject.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-01  |  6.6 KB  |  290 lines

  1.  
  2. /* Author Anders Kjeldsen */
  3.  
  4. #ifndef G3DOBJECT_CPP
  5. #define G3DOBJECT_CPP
  6.  
  7. #include "g3d/G3DObject.h"
  8. #include "ggraphics/GScreen.cpp"
  9. #include "g3d/G3DPolygon.cpp"
  10. #include "g3d/G3DQuat.cpp"
  11.  
  12. int teller, teller2 = NULL;
  13.  
  14. GObject::GObject(STRPTR FileName)
  15. {
  16.     class GBuffer *OldObject = new GBuffer(FileName);
  17.  
  18.     APTR ObjectBuffer = OldObject->LockBuf();
  19.     
  20.     if ( ((ULONG *)ObjectBuffer)[2] == (ULONG) ((ULONG *)"LWOB")[0] )
  21.     {
  22.         /* LIGHTWAVE */
  23.  
  24.         class GChunkHandler *LwHandler = new GChunkHandler(ObjectBuffer, SIZELONG, SIZELONG, FALSE);
  25.  
  26.         float *PNTS;
  27.         WORD *POLS;
  28.         ULONG POLS_SIZE;
  29.         WORD *POLS_END;
  30.  
  31.         if (LwHandler)
  32.         {
  33.             LwHandler->EnterChunk();
  34.             LwHandler->Adjust(4);
  35.             LwHandler->FindChunk("PNTS", SIZELONG);
  36.             PNTS = (float *) LwHandler->EnterChunk();
  37.             LwHandler->ParentChunk();
  38.  
  39.             LwHandler->ParentChunk();
  40.             LwHandler->EnterChunk();
  41.             LwHandler->Adjust(4);
  42.             LwHandler->FindChunk("POLS", SIZELONG);
  43.  
  44.             POLS_SIZE = LwHandler->GetSIZE();
  45.             POLS_END = (WORD *)LwHandler->GetEND();
  46.             POLS = (WORD *)LwHandler->EnterChunk();
  47.  
  48. //            printf("POLS (first) %x\n", (ULONG)POLS );
  49. //            printf("Size 2: %d\n", (ULONG)POLS_END );
  50.  
  51.             class G3DPolygon *CurrentPoly = new G3DPolygon;
  52.             GPolygons = CurrentPoly;    // first entry in poly-linked-list
  53.             class G3DPolygon *PrevPoly = NULL;
  54.             BOOL out = FALSE;    
  55.  
  56.             while ( (POLS < POLS_END) && !out )
  57.             {
  58.                 UWORD Point1 = POLS[1];
  59.                 UWORD Point2, Point3;
  60.                 UWORD Loop = POLS[0] - 2;
  61.                 POLS+=2;
  62.                 if (Loop>1)
  63.                 {
  64. //                    printf("POLS %x\t POLS_END %x\n", (int)POLS, (int)POLS_END );
  65. //                    printf("Hey, LOOP>1 !\n");
  66.                     out = TRUE;
  67.                 }
  68.                 while (Loop==1)
  69.                 {
  70. //                    printf("%d", Loop);
  71.                     Point2 = POLS[0];
  72.                     Point3 = POLS[1];
  73.  
  74. //                    printf("Polygon #%x\n", teller/(sizeof (class G3DPolygon)) );
  75.                     CurrentPoly->GVertex1.X = PNTS[(Point1*3)];
  76.                     CurrentPoly->GVertex1.Y = PNTS[(Point1*3)+1];
  77.                     CurrentPoly->GVertex1.Z = (double)PNTS[(Point1*3)+2];
  78.                     CurrentPoly->GVertex2.X = PNTS[(Point2*3)];
  79.                     CurrentPoly->GVertex2.Y = PNTS[(Point2*3)+1];
  80.                     CurrentPoly->GVertex2.Z = (double)PNTS[(Point2*3)+2];
  81.                     CurrentPoly->GVertex3.X = PNTS[(Point3*3)];
  82.                     CurrentPoly->GVertex3.Y = PNTS[(Point3*3)+1];
  83.                     CurrentPoly->GVertex3.Z = (double)PNTS[(Point3*3)+2];
  84.  
  85. //                    CurrentPoly->GVertex1.PrintfAll();
  86. //                    CurrentPoly->GVertex2.PrintfAll();
  87. //                    CurrentPoly->GVertex3.PrintfAll();
  88. //                    printf("\n");
  89.                     POLS+=1;
  90.  
  91.                     PrevPoly = CurrentPoly;
  92.                     CurrentPoly = new G3DPolygon();
  93.                     PrevPoly->NextGPolygon = CurrentPoly;
  94.  
  95.                     teller+= sizeof (class G3DPolygon);
  96.                     Loop--;
  97.                 }
  98.                 POLS+=1;
  99.  
  100.                 if (POLS[0] > 0) // then no DETAIL POLYGON(s)
  101.                 {    
  102.                     POLS+=1;
  103.                 }
  104.                 else
  105.                 {
  106.                     POLS+=1;
  107.                     UWORD DPOLS = POLS[0];
  108.                     POLS+=1;    // ON A DPOL or on next
  109.                     while (DPOLS)
  110.                     {
  111.                         POLS+=POLS[0]+1;    // x+surface data
  112.                         DPOLS--;
  113.                     }
  114.                 }
  115.  
  116.                 teller2++;
  117.             }
  118.             if (PrevPoly) PrevPoly->NextGPolygon = NULL;
  119.             if (CurrentPoly) delete CurrentPoly;    // Made one too many
  120.  
  121.             printf("Rakk å telle %d polys\n", teller2);
  122.  
  123.             LwHandler->ParentChunk();
  124.             LwHandler->ParentChunk();
  125.             LwHandler->EnterChunk();
  126.             LwHandler->Adjust(4);    // back at start
  127.         }
  128.     }
  129.     OldObject->UnLockBuf();
  130.  
  131.     printf("GPolygon size %d\n", (int) sizeof (class G3DPolygon));
  132.     printf("total %d\n", teller);
  133.     printf("total %d\n", teller2);
  134. }
  135.  
  136. void G3DObject::ClipPolygons(class GScreen *GScreen)
  137. {
  138.     class G3DPolygon *CurrentGPolygon = G3DPolygons;
  139.     while (CurrentGPolygon)
  140.     {
  141.         CurrentGPolygon->ClipZ(1.0, HIGHER);
  142. //draw lines
  143.  
  144.         if (CurrentGPolygon->GPolygonA && CurrentGPolygon->ClippedA)
  145.         {
  146.             CurrentGPolygon->GPolygonA->DrawLines(GScreen);
  147.         }
  148.         if (CurrentGPolygon->GPolygonB  && CurrentGPolygon->ClippedB)
  149.         {
  150.             CurrentGPolygon->GPolygonB->DrawLines(GScreen);
  151.         }
  152.         if ( !(CurrentGPolygon->ClippedA | CurrentGPolygon->ClippedB) )
  153.         {
  154.             CurrentGPolygon->DrawLines(GScreen);
  155.         }
  156.  
  157.         CurrentGPolygon = CurrentGPolygon->NextGPolygon;
  158.     }
  159. }
  160.  
  161. void G3DObject::SetDirection(float Ax, float Ay, float Az)
  162. {
  163.     ObjQuat.Set(Ax, Ay, Az);
  164. }
  165.  
  166. void G3DObject::SetPosition(double px, double py, double pz)
  167. {
  168.     AxisX = px;
  169.     AxisY = py;
  170.     AxisZ = pz;
  171. }
  172.  
  173. void G3DObject::AddPosition(double px, double py, double pz)
  174. {
  175.     AxisX += px;
  176.     AxisY += py;
  177.     AxisZ += pz;
  178. }
  179.  
  180. void G3DObject::AddDirection(float Ax, float Ay, float Az)
  181. {
  182.     DiffQuat.Set(Ax, Ay, Az);
  183.     ObjQuat = DiffQuat * ObjQuat;
  184. }
  185.  
  186.  
  187. void G3DObject::Rotate(class G3DCamera *GCamera)
  188. {
  189.     class G3DPolygon *CurrentGPolygon = GPolygons;
  190.  
  191.     class G3DMatrix ObjMatrix, YMatrix, ZMatrix, TMatrix;
  192. //    ObjMatrix = ObjQuat;
  193.  
  194.     ObjMatrix.SetAsX(Anglex);
  195.     YMatrix.SetAsY(Angley);
  196.     ZMatrix.SetAsZ(Anglez);
  197.     TMatrix.SetPosition(AxisX, AxisY, AxisZ);
  198.  
  199. //    printf("angles %f %f %f\n", Anglex, Angley, Anglez);
  200.     ObjMatrix = TMatrix * ObjMatrix * YMatrix * ZMatrix;
  201.  
  202. //    class G3DMatrix IdMatrix;
  203. //    IdMatrix.SetIdentity(320.0);    // 320
  204. //    ObjMatrix = ObjMatrix * IdMatrix;
  205.  
  206. //    printf("Object Quat\n");
  207. //    ObjQuat.PrintfAll();
  208.  
  209. //    printf("Object Matrix \n");
  210. //    ObjMatrix.PrintfAll();
  211.  
  212.     ObjMatrix.SetPosition(AxisX-GCamera->GetAxisX(), G3DCamera->GetAxisY(), G3DCamera->GetAxisZ() );
  213.     class G3DMatrix EyeMatrix = ObjMatrix; //* G3DCamera->CamMatrix;
  214.  
  215. //    printf("Eye Matrix \n");
  216. //    EyeMatrix.PrintfAll();
  217.  
  218.  
  219.     while (CurrentGPolygon)
  220.     {
  221.         CurrentGPolygon->Rotate(&EyeMatrix);
  222. //        CurrentGPolygon->DrawPoints(GScreen);
  223. //        CurrentGPolygon->DrawLines(GScreen);
  224.         CurrentGPolygon = CurrentGPolygon->NextGPolygon;
  225.     }
  226. }
  227.  
  228. void G3DObject::SortPolygons()
  229. {
  230.     class G3DPolygon *CurrentGPolygon = GPolygons->NextGPolygon;
  231.     GPolygons->SetSortWeight();
  232.  
  233. /*
  234.     if (GPolygons->GPolygonA && GPolygons->ClippedA)
  235.     {
  236.         GPolygons->GPolygonA->DrawLines(GScreen);
  237.     }
  238.     if (GPolygons->GPolygonB  && GPolygons->ClippedB)
  239.     {
  240.         GPolygons->GPolygonB->DrawLines(GScreen);
  241.     }
  242.     if ( !(GPolygons->ClippedA | GPolygons->ClippedB) )
  243.     {
  244.         GPolygons->DrawLines(GScreen);
  245.     }
  246. */
  247.  
  248.     while (CurrentGPolygon)
  249.     {
  250.         if (CurrentGPolygon->Visible)
  251.         {
  252.             CurrentGPolygon->SetSortWeight();
  253.             float w = CurrentGPolygon->GetSortWeight();
  254. //            GPolygons->GSortNode.InsertNode( &CurrentGPolygon->GSortNode );
  255.  
  256. /*
  257.             if (CurrentGPolygon->GPolygonA && CurrentGPolygon->ClippedA)
  258.             {
  259.                 CurrentGPolygon->GPolygonA->DrawLines(GScreen);
  260.             }
  261.             if (CurrentGPolygon->GPolygonB  && CurrentGPolygon->ClippedB)
  262.             {
  263.                 CurrentGPolygon->GPolygonB->DrawLines(GScreen);
  264.             }
  265.             if ( !(CurrentGPolygon->ClippedA | CurrentGPolygon->ClippedB) )
  266.             {
  267.                 CurrentGPolygon->DrawLines(GScreen);
  268.             }
  269. */
  270.         }
  271.         CurrentGPolygon = CurrentGPolygon->NextGPolygon;
  272.     }
  273. }
  274.  
  275. void G3DObject::DrawPolygons()
  276. {
  277.     class G3DPolygon *CurrentGPolygon = GPolygons->NextGPolygon;
  278.  
  279.     while (CurrentGPolygon)
  280.     {
  281.         if (CurrentGPolygon->Visible)
  282.         {
  283.             CurrentGPolygon->DrawPolygon(GScreen);
  284.         }
  285.         CurrentGPolygon = CurrentGPolygon->NextGPolygon;
  286.     }
  287. }
  288.  
  289.  
  290. #endif /* G3DOBJECT_CPP */