home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Jeux / demos / crystalPPC.lha / polyplan.cpp < prev    next >
C/C++ Source or Header  |  1998-02-05  |  6KB  |  248 lines

  1. #include <math.h>
  2. #include <time.h>
  3. #include "system.h"
  4.  
  5. #ifndef DEF_H
  6. #include "def.h"
  7. #endif
  8.  
  9. #ifndef POLYPLANE_H
  10. #include "polyplan.h"
  11. #endif
  12.  
  13. #ifndef VERTEX_H
  14. #include "vertex.h"
  15. #endif
  16.  
  17. #ifndef TOKEN_H
  18. #include "token.h"
  19. #endif
  20.  
  21. //---------------------------------------------------------------------------
  22.  
  23. PolyPlane::PolyPlane (char* name)
  24. {
  25.   strcpy (PolyPlane::name, name);
  26. }
  27.  
  28. PolyPlane::~PolyPlane ()
  29. {
  30. }
  31.  
  32. void PolyPlane::set_texture_space (Vertex& v_orig, Vertex& v1, float len1, Vertex& v2, float len2)
  33. {
  34.   float xo = v_orig.get_ox ();
  35.   float yo = v_orig.get_oy ();
  36.   float zo = v_orig.get_oz ();
  37.   float x1 = v1.get_ox ();
  38.   float y1 = v1.get_oy ();
  39.   float z1 = v1.get_oz ();
  40.   float x2 = v2.get_ox ();
  41.   float y2 = v2.get_oy ();
  42.   float z2 = v2.get_oz ();
  43.   set_texture_space (xo, yo, zo, x1, y1, z1, len1, x2, y2, z2, len2);
  44. }
  45.  
  46. void PolyPlane::set_texture_space (
  47.     float xo, float yo, float zo,
  48.     float x1, float y1, float z1,
  49.     float len1,
  50.     float x2, float y2, float z2,
  51.     float len2)
  52. {
  53.   float l1 = sqrt ((xo-x1)*(xo-x1) + (yo-y1)*(yo-y1) + (zo-z1)*(zo-z1));
  54.   float l2 = sqrt ((xo-x2)*(xo-x2) + (yo-y2)*(yo-y2) + (zo-z2)*(zo-z2));
  55.   x1 = (x1-xo) * len1 / l1;
  56.   y1 = (y1-yo) * len1 / l1;
  57.   z1 = (z1-zo) * len1 / l1;
  58.   x2 = (x2-xo) * len2 / l2;
  59.   y2 = (y2-yo) * len2 / l2;
  60.   z2 = (z2-zo) * len2 / l2;
  61.  
  62.   set_texture_space (xo, yo, zo, x1, y1, z1, x2, y2, z2, 1, 1, 1);
  63. }
  64.  
  65. void PolyPlane::set_texture_space (Vertex& v_orig, Vertex& v_u, Vertex& v_v)
  66. {
  67.   float xo = v_orig.get_ox ();
  68.   float yo = v_orig.get_oy ();
  69.   float zo = v_orig.get_oz ();
  70.   float x1 = v_u.get_ox ();
  71.   float y1 = v_u.get_oy ();
  72.   float z1 = v_u.get_oz ();
  73.   float x2 = v_v.get_ox ();
  74.   float y2 = v_v.get_oy ();
  75.   float z2 = v_v.get_oz ();
  76.   set_texture_space (xo, yo, zo, x1, y1, z1, x2, y2, z2);
  77. }
  78.  
  79. void PolyPlane::set_texture_space (float xo, float yo, float zo,
  80.                 float xu, float yu, float zu,
  81.                 float xv, float yv, float zv)
  82. {
  83.   set_texture_space (xo, yo, zo, xu-xo, yu-yo, zu-zo,
  84.       xv-xo, yv-yo, zv-zo, 1, 1, 1);
  85. }
  86.  
  87. void PolyPlane::set_texture_space (
  88.     float xo, float yo, float zo,
  89.     float xu, float yu, float zu,
  90.     float xv, float yv, float zv,
  91.     float xw, float yw, float zw)
  92. {
  93.   Matrix3 m_tex2obj;
  94.  
  95.   m_obj2tex.m11 = xu;
  96.   m_obj2tex.m12 = xv;
  97.   m_obj2tex.m13 = xw;
  98.   m_obj2tex.m21 = yu;
  99.   m_obj2tex.m22 = yv;
  100.   m_obj2tex.m23 = yw;
  101.   m_obj2tex.m31 = zu;
  102.   m_obj2tex.m32 = zv;
  103.   m_obj2tex.m33 = zw;
  104.   m_obj2tex.inverse ();
  105.  
  106.   v_obj2tex.x = xo;
  107.   v_obj2tex.y = yo;
  108.   v_obj2tex.z = zo;
  109.  
  110.   m_world2tex = m_obj2tex;
  111.   v_world2tex = v_obj2tex;
  112. }
  113.  
  114. void PolyPlane::set_texture_space (Matrix3& tx_matrix, Vector3& tx_vector)
  115. {
  116.   m_obj2tex = tx_matrix;
  117.   m_world2tex = tx_matrix;
  118.   v_obj2tex = tx_vector;
  119.   v_world2tex = tx_vector;
  120. }
  121.  
  122. void PolyPlane::transform_world2cam (Matrix3& m_w2c, Matrix3& m_c2w, Vector3& v_w2c)
  123. {
  124.   // Create the matrix to transform camera space to texture space.
  125.   // From: T = Mwt * (W - Vwt)
  126.   //       C = Mwc * (W - Vwc)
  127.   // To:   T = Mct * (C - Vct)
  128.  
  129.   // Mcw * C + Vwc = W
  130.   // T = Mwt * (Mcw * C + Mcw * Mwc * (Vwc - Vwt))
  131.   // T = Mwt * Mcw * (C - Mwc * (Vwt-Vwc))
  132.   // ===>
  133.   // Mct = Mwt * Mcw
  134.   // Vct = Mwc * (Vwt - Vwc)
  135.  
  136.   m_cam2tex = m_world2tex;
  137.   m_cam2tex *= m_c2w;
  138.  
  139.   Vector3 v3 = v_world2tex;
  140.   v3 -= v_w2c;
  141.   m_w2c.transform (v3, v_cam2tex);
  142. }
  143.  
  144. void PolyPlane::object_to_world (Matrix3& m_o2w, Matrix3& m_w2o, Vector3& v_o2w)
  145. {
  146.   // From: T = Mot * (O - Vot)
  147.   //       W = Mow * O - Vow
  148.   // To:   T = Mwt * (W - Vwt)
  149.  
  150.   // Mwo * (W + Vow) = O
  151.   // T = Mot * (Mwo * (W + Vow) - (Mwo * Mow) * Vot)
  152.   // T = Mot * Mwo * (W + Vow - Mow * Vot)
  153.   // ===>
  154.   // Mwt = Mot * Mwo
  155.   // Vwt = Mow * Vot - Vow
  156.  
  157.   m_world2tex = m_obj2tex;
  158.   m_world2tex *= m_w2o;
  159.   m_o2w.transform (v_obj2tex, v_world2tex);
  160.   v_world2tex -= v_o2w;
  161. }
  162.  
  163. void PolyPlane::save (FILE* fp, int indent)
  164. {
  165.   char sp[100]; strcpy (sp, spaces); sp[indent] = 0;
  166.   fprintf (fp, "%sPLANE '%s' (\n", sp, name);
  167.   m_world2tex.save (fp, indent+2);
  168.   v_world2tex.save (fp, indent+2);
  169.   fprintf (fp, "%s)\n", sp);
  170. }
  171.  
  172. void PolyPlane::load (char** buf)
  173. {
  174.   char* t;
  175.   char* old_buf;
  176.  
  177.   skip_token (buf, "PLANE");
  178.   t = get_token (buf);
  179.   strcpy (name, t);
  180.   skip_token (buf, "(", "Expected '%s' instead of '%s' after the name of a PLANE!\n");
  181.  
  182.   int tx1_given = FALSE, tx2_given = FALSE;
  183.   Vector3 tx1_orig, tx1, tx2;
  184.   float tx1_len = 0, tx2_len = 0;
  185.   Matrix3 tx_matrix;
  186.   Vector3 tx_vector;
  187.  
  188.   while (TRUE)
  189.   {
  190.     old_buf = *buf;
  191.     t = get_token (buf);
  192.     if (*t == ')' || *t == 0) break;
  193.     if (!strcmp (t, "ORIG"))
  194.     {
  195.       skip_token (buf, "=", "Expected '%s' instead of '%s' after PLANE/ORIG!\n");
  196.       tx1_given = TRUE;
  197.       tx1_orig.load (buf);
  198.     }
  199.     else if (!strcmp (t, "FIRST"))
  200.     {
  201.       skip_token (buf, "=", "Expected '%s' instead of '%s' after PLANE/FIRST!\n");
  202.       tx1_given = TRUE;
  203.       tx1.load (buf);
  204.     }
  205.     else if (!strcmp (t, "FIRST_LEN"))
  206.     {
  207.       skip_token (buf, "=", "Expected '%s' instead of '%s' after PLANE/FIRST_LEN!\n");
  208.       tx1_len = get_token_float (buf);
  209.       tx1_given = TRUE;
  210.     }
  211.     else if (!strcmp (t, "SECOND"))
  212.     {
  213.       skip_token (buf, "=", "Expected '%s' instead of '%s' after PLANE/SECOND!\n");
  214.       tx2_given = TRUE;
  215.       tx2.load (buf);
  216.     }
  217.     else if (!strcmp (t, "SECOND_LEN"))
  218.     {
  219.       skip_token (buf, "=", "Expected '%s' instead of '%s' after PLANE/SECOND_LEN!\n");
  220.       tx2_len = get_token_float (buf);
  221.       tx2_given = TRUE;
  222.     }
  223.     else if (!strcmp (t, "MATRIX"))
  224.     {
  225.       *buf = old_buf;
  226.       tx_matrix.load (buf);
  227.     }
  228.     else if (!strcmp (t, "("))
  229.     {
  230.       *buf = old_buf;
  231.       tx_vector.load (buf);
  232.     }
  233.     else
  234.     {
  235.       printf ("What is '%s' doing in a PLANE statement?\n", t);
  236.     }
  237.   }
  238.  
  239.   if (tx1_given)
  240.     if (tx2_given) set_texture_space (tx1_orig.x, tx1_orig.y, tx1_orig.z,
  241.                       tx1.x, tx1.y, tx1.z, tx1_len,
  242.                       tx2.x, tx2.y, tx2.z, tx2_len);
  243.     else { printf ("Not supported!\n"); exit (0); }
  244.   else set_texture_space (tx_matrix, tx_vector);
  245. }
  246.  
  247. //---------------------------------------------------------------------------
  248.