示例#1
0
//--------------------------------------------------------------
// figure the normal for a point in grid
void Planet::pointNormal(
  mgVertex& v,
  double* heights,
  mgPoint3* points,
  int index,
  int rowSize,
  BOOL outward)
{
  double ht = heights[index];
  mgPoint3* npt = &points[index];
  mgPoint3 pt(ht*npt->x, ht*npt->y, ht*npt->z);

  v.m_px = (float) pt.x;
  v.m_py = (float) pt.y;
  v.m_pz = (float) pt.z;

  ht = heights[index-rowSize];
  npt = &points[index-rowSize];
  mgPoint3 westPt(ht*npt->x, ht*npt->y, ht*npt->z);

  ht = heights[index+rowSize];
  npt = &points[index+rowSize];
  mgPoint3 eastPt(ht*npt->x, ht*npt->y, ht*npt->z);

  ht = heights[index-1];
  npt = &points[index-1];
  mgPoint3 northPt(ht*npt->x, ht*npt->y, ht*npt->z);

  ht = heights[index+1];
  npt = &points[index+1];
  mgPoint3 southPt(ht*npt->x, ht*npt->y, ht*npt->z);

  northPt.subtract(pt);
  southPt.subtract(pt);
  westPt.subtract(pt);
  eastPt.subtract(pt);

  mgPoint3 normal(northPt);
  normal.cross(westPt);
  normal.normalize();

  v.m_nx = (float) normal.x;

  v.m_ny = (float) normal.y;
  v.m_nz = (float) normal.z;

  normal = westPt;
  normal.cross(southPt);
  normal.normalize();

  v.m_nx += (float) normal.x;
  v.m_ny += (float) normal.y;
  v.m_nz += (float) normal.z;

  normal = southPt;
  normal.cross(eastPt);
  normal.normalize();

  v.m_nx += (float) normal.x;
  v.m_ny += (float) normal.y;
  v.m_nz += (float) normal.z;

  normal = eastPt;
  normal.cross(northPt);
  normal.normalize();

  v.m_nx += (float) normal.x;
  v.m_ny += (float) normal.y;
  v.m_nz += (float) normal.z;

  float len = sqrt(v.m_nx * v.m_nx + v.m_ny * v.m_ny + v.m_nz * v.m_nz);
  if (!outward)
    len = -len;
  v.m_nx /= len;
  v.m_ny /= len;
  v.m_nz /= len;
}
示例#2
0
//-----------------------------------------------------------------------------
// set normals of points on grid
void Wreck::setNormals(
  mgVertexTA* points,
  int rows,
  int cols,
  BOOL outward)
{
  for (int i = 0; i < rows; i++)
  {
    for (int j = 0; j < cols; j++)
    {
      // get the point
      int index = i*cols + j;
      mgPoint3 pt(points[index].m_px, points[index].m_py, points[index].m_pz);

      int northIndex = i*cols + ((j == cols-1) ? 1 : j+1);
      mgPoint3 northPt(points[northIndex].m_px, points[northIndex].m_py, points[northIndex].m_pz);
      northPt.subtract(pt);

      int southIndex = i*cols + ((j == 0) ? cols-2 : j-1);
      mgPoint3 southPt(points[southIndex].m_px, points[southIndex].m_py, points[southIndex].m_pz);
      southPt.subtract(pt);

      // normal is average of orthogonal vectors to each neighbor point
      mgVertexTA* v = &points[index];
      v->m_nx = v->m_ny = v->m_nz = 0.0f;
      mgPoint3 normal;
      if (i > 0)
      {
        int westIndex = (i-1)*cols + j;
        mgPoint3 westPt(points[westIndex].m_px, points[westIndex].m_py, points[westIndex].m_pz);
        westPt.subtract(pt);

        normal = northPt;
        normal.cross(westPt);
        normal.normalize();

        v->m_nx += (float) normal.x;
        v->m_ny += (float) normal.y;
        v->m_nz += (float) normal.z;

        normal = westPt;
        normal.cross(southPt);
        normal.normalize();

        v->m_nx += (float) normal.x;
        v->m_ny += (float) normal.y;
        v->m_nz += (float) normal.z;
      }

      if (i < rows-1)
      {
        // get vector to each neighbor point on grid
        int eastIndex = (i+1)*cols + j;
        mgPoint3 eastPt(points[eastIndex].m_px, points[eastIndex].m_py, points[eastIndex].m_pz);
        eastPt.subtract(pt);

        normal = southPt;
        normal.cross(eastPt);
        normal.normalize();

        v->m_nx += (float) normal.x;
        v->m_ny += (float) normal.y;
        v->m_nz += (float) normal.z;

        normal = eastPt;
        normal.cross(northPt);
        normal.normalize();

        v->m_nx += (float) normal.x;
        v->m_ny += (float) normal.y;
        v->m_nz += (float) normal.z;
      }

      float len = sqrt(v->m_nx*v->m_nx + v->m_ny*v->m_ny + v->m_nz*v->m_nz);
      if (outward)
        len = -len;

      v->m_nx /= len;
      v->m_ny /= len;
      v->m_nz /= len;
    }
  }
}