コード例 #1
0
        // set rotation direction based on mouse location projected to 3D space
        public void UpdateDirection(float ix, float iy)
        {
            Vector3d mouse   = new Vector3d(ix, iy, 0);
            Vector3d horizon = new Vector3d(m_right.x, m_right.y, 0);
            Vector3d perp    = Vector3d.cross(mouse, horizon);

            m_dir = Math.Sign(perp.z);
        }
コード例 #2
0
 public void RotateRight(float deg)
 {
     Rotate(m_up, deg);
     // update target and up
     m_target = m_lookat - m_eye;    // The "look-at" unit vector.
     m_target.Normalize();
     m_right = Vector3d.cross(m_target, m_up);
     UpdateView();
 }
コード例 #3
0
        // calc a split point normal from 2 edge normal and center tanget
        Vector3d CalcCenterNormal(Vector3d norm1, Vector3d norm2, Vector3d tanget)
        {
            Vector3d tvec = (norm1 * 0.5f) + (norm2 * 0.5f);

            tvec = Vector3d.cross(tvec, tanget);
            Vector3d res = Vector3d.cross(tanget, tvec);

            res.Normalize();
            return(res);
        }
コード例 #4
0
        Vector3d GetNormalFromTanget(Vector3d norm, Vector3d tanget)
        {
            Vector3d normtang = Vector3d.cross(norm, tanget);
            Vector3d res      = Vector3d.cross(tanget, normtang);

            if ((Math.Abs(res.x) < Epsilon) && (Math.Abs(res.y) < Epsilon) && (Math.Abs(res.z) < Epsilon))
            {
                return(null);
            }
            res.Normalize();
            //res = res * dir.Mag();
            return(res);
        }
コード例 #5
0
        Vector3d GetTangetFromNormal(Vector3d norm, Vector3d dir)
        {
            Vector3d normxdir = Vector3d.cross(norm, dir);
            Vector3d res      = Vector3d.cross(normxdir, norm);

            if ((Math.Abs(res.x) < Epsilon) && (Math.Abs(res.y) < Epsilon) && (Math.Abs(res.z) < Epsilon))
            {
                res = dir.clone();
            }
            res.Normalize();
            //res = res * dir.Mag();
            return(res);
        }
コード例 #6
0
        // split edge v1-v2. v3 is the last corner in the triangle and is used for computing normals

/*        int SplitEdge(int v1, int v2, int v3)
 *      {
 *          Vector3d norm1 = CalcNormal(v1, v2, v3);
 *          Vector3d norm2 = CalcNormal(v2, v3, v1);
 *          EdgeAmf edge = m_pointList[v1].FindEdge(v2);
 *          if (edge == null)
 *          {
 *              edge = m_pointList[v2].FindEdge(v1);
 *              if (edge != null)
 *              {
 *                  // swap verteces tomatch edge
 *                  int tv = v1;
 *                  v1 = v2;
 *                  v2 = tv;
 *                  Vector3d tnorm = norm1;
 *                  norm1 = norm2;
 *                  norm2 = tnorm;
 *              }
 *          }
 *
 *          Vector3d t1, t2;
 *          PointAmf pamf1 = m_pointList[v1];
 *          PointAmf pamf2 = m_pointList[v2];
 *          Point3d pt1 = pamf1.pt;
 *          Point3d pt2 = pamf2.pt;
 *          PointAmf pamf;
 *          float x, y, z;
 *
 *          // calculate edge vector
 *          x = pt2.x - pt1.x;
 *          y = pt2.y - pt1.y;
 *          z = pt2.z - pt1.z;
 *          Vector3d edgeDir = new Vector3d(x, y, z);
 *
 *          // first see if we have an edge for this segment
 *          if (edge != null)
 *          {
 *              // if this edge was already split, return result
 *              if (edge.v12 >= 0)
 *                  return edge.v12;
 *              t1 = edge.t1;
 *              t2 = edge.t2;
 *          }
 *          /*else if ((pamf1.normal == null) && (pamf2.normal == null))
 *          {
 *              // its a linear line, return the center
 *              x = (pamf1.pt.x + pamf2.pt.x) / 2.0f;
 *              y = (pamf1.pt.y + pamf2.pt.y) / 2.0f;
 *              z = (pamf1.pt.z + pamf2.pt.z) / 2.0f;
 *              pamf = new PointAmf();
 *              pamf.pt = new Point3d(x, y, z);
 *              m_pointList.Add(pamf);
 *              return m_pointList.Count - 1;
 *          }
 *          else
 *          {
 *              // calculate tangets from normals.
 *              //edgeDir.Normalize();
 *              t1 = GetTangetFromNormal(norm1, edgeDir);
 *              t2 = GetTangetFromNormal(norm2, edgeDir);
 *              /*if (pamf1.normal == null)
 *                  pamf1.normal = norm1;
 *              if (pamf2.normal == null)
 *                  pamf2.normal = norm2;
 *          }
 *
 *          float d = edgeDir.Mag();
 *
 *          // calculate mid point using Hermite interpolation
 *          x = 0.5f * pt1.x + 0.125f * t1.x * d + 0.5f * pt2.x - 0.125f * t2.x * d;
 *          y = 0.5f * pt1.y + 0.125f * t1.y * d + 0.5f * pt2.y - 0.125f * t2.y * d;
 *          z = 0.5f * pt1.z + 0.125f * t1.z * d + 0.5f * pt2.z - 0.125f * t2.z * d;
 *
 *          pamf = new PointAmf();
 *          pamf.pt = new Point3d(x, y, z);
 *          int v = m_pointList.Count;
 *          m_pointList.Add(pamf);
 *
 *          // calculate new tanget and new normal
 *          x = -1.5f * pt1.x - 0.25f * t1.x * d + 1.5f * pt2.x - 0.25f * t2.x * d;
 *          y = -1.5f * pt1.y - 0.25f * t1.y * d + 1.5f * pt2.y - 0.25f * t2.y * d;
 *          z = -1.5f * pt1.z - 0.25f * t1.z * d + 1.5f * pt2.z - 0.25f * t2.z * d;
 *          Vector3d tanget = new Vector3d(x, y, z);
 *          tanget.Normalize();
 *
 *          Vector3d tvec = (norm1 * 0.5f) + (norm2 * 0.5f);
 *          tvec = Vector3d.cross(tvec, tanget);
 *          pamf.normal = Vector3d.cross(tanget, tvec);
 *
 *          if (edge == null)
 *          {
 *              //pamf.normal = GetNormalFromTanget(norm1, tanget);
 *              // create an edge for this segment
 *              edge = new EdgeAmf();
 *              edge.v1 = v1;
 *              edge.v2 = v2;
 *              edge.t1 = t1;
 *              edge.t2 = t2;
 *              pamf1.AddEdge(edge);
 *          }
 *          edge.v12 = m_pointList.Count - 1; // saves double computation
 *
 *          //tanget.Normalize();
 *          // save 2 split edges
 *          EdgeAmf edge1 = new EdgeAmf();
 *          edge1.v1 = v1;
 *          edge1.v2 = v;
 *          edge1.t1 = t1;
 *          edge1.t2 = tanget;
 *          pamf1.AddEdge(edge1);
 *
 *          EdgeAmf edge2 = new EdgeAmf();
 *          edge2.v1 = v;
 *          edge2.v2 = v2;
 *          edge2.t1 = tanget;
 *          edge2.t2 = t2;
 *          pamf.AddEdge(edge2);
 *
 *          return v;
 *      }*/

        // calc normal at corner v (looking at the triangle when corner v is at the bottom, v1 at top right, and v2 is at to left
        Vector3d CalcNormal(int v, int v1, int v2)
        {
            PointAmf pamf = m_pointList[v];

            if (pamf.normal != null)
            {
                return(pamf.normal);
            }
            Vector3d t1     = GetTanget(v, v1);
            Vector3d t2     = GetTanget(v, v2);
            Vector3d normal = Vector3d.cross(t1, t2);

            normal.Normalize();
            return(normal);
        }
コード例 #7
0
        void AddTriangle(int v1, int v2, int v3)
        {
            Point3d pt1 = m_pointList[v1].pt;
            Point3d pt2 = m_pointList[v2].pt;
            Point3d pt3 = m_pointList[v3].pt;
            Polygon p   = new Polygon();

            p.m_points = new Point3d[] { pt1, pt2, pt3 };
            // calculate normal
            Vector3d edge1 = new Vector3d(pt1.x - pt2.x, pt1.y - pt2.y, pt1.z - pt2.z);
            Vector3d edge2 = new Vector3d(pt3.x - pt2.x, pt3.y - pt2.y, pt3.z - pt2.z);

            p.m_normal = Vector3d.cross(edge1, edge2);
            p.m_normal.Normalize();
            m_curObject.m_lstpolys.Add(p);
        }
コード例 #8
0
        public void ResetView(float x, float y, float z, float updeg, float lookz)
        {
            m_eye    = new Vector3d(x, y, z);
            m_lookat = new Vector3d(0, 0, 0);
            m_up     = new Vector3d(0, 0, 1);
            m_dz     = lookz;
            m_dx     = m_dy = 0;
            //m_right = new Vector3d(1, 0, 0);
            m_target = m_lookat - m_eye;    // The "look-at" unit vector.
            m_target.Normalize();
            m_right = Vector3d.cross(m_target, m_up);
            RotateUp(updeg);
            UpdateView();

            //Vector3d xaxis = Vector3d.cross(up, zaxis);// The "right" vector.
            //xaxis.Normalize();
            //Vector3d yaxis = Vector3d.cross(zaxis, xaxis);     // The "up" vector.
        }