Example #1
0
R3Mesh* Terrain::Patch(R3Point center, R2Point size, R2Point dps) { // dps: dots per side
  R3Mesh *patch = new R3Mesh();
  for (unsigned int r = 0; r < dps.Y(); r++) {
    for (unsigned int c = 0; c < dps.X(); c++) {
      double dx = (double)c / dps.X() * size.X() - size.X() / 2;
      double dy = (r + c) % 2 * Util::UnitRandom(); // default

      if (params.heightMap) {
        int x = ((double)c / dps.X()) * params.heightMap->Width();
        int y = ((double)r / dps.Y()) * params.heightMap->Height();
        dy = 25 * params.heightMap->Pixel(x,y).Luminance();
      }

      double dz = (double)r / dps.Y() * size.Y() - size.Y() / 2;
      R3Point position(center.X() + dx, center.Y() + dy, center.Z() + dz);

      double tx = position.X() / 80;
      double ty = position.Z() / 80;

      //R2Point texcoord = R2Point(tx - ((int) tx), ty - ((int) ty));
      //if (texcoord.X() < 0)
          //texcoord.SetX(1 + texcoord.X());
      //if (texcoord.Y() < 0)
          //texcoord.SetY(1 + texcoord.Y());

      patch->CreateVertex(position, R3Vector(0,1,0), R2Point(tx, ty)); // id's go up along x axis, then along y
    }
  }
  
  for (unsigned int r = 0; r < dps.Y() - 1; r++) {
    for (unsigned int c = 0; c < dps.X() - 1; c++) {
      unsigned int cols = (unsigned int)dps.X();
      unsigned int k = c + r * cols;
      vector<R3MeshVertex*> vertices;
      vertices.push_back(patch->Vertex(k+cols));
      vertices.push_back(patch->Vertex(k+1));
      vertices.push_back(patch->Vertex(k));
      patch->CreateFace(vertices);

      vector<R3MeshVertex*> vertices2;
      vertices2.push_back(patch->Vertex(k+cols));
      vertices2.push_back(patch->Vertex(k+1+cols));
      vertices2.push_back(patch->Vertex(k+1));
      patch->CreateFace(vertices2);
    }
  }

  patch->Update();

  return patch;
}