/* The format of the grid parameters is the following:
   *   <grid>
   *       <resolution nx=10 ny=10></resolution>
   *      <point>
   *          <coord x="0.5" y="+0.5"></coord>
   *          <texcoord s="0.0" t="1.0"></texcoord>
   *      </point>
   *      <point>
   *          <coord x="x" y="y"></coord>
   *          <texcoord s="s" t="t"></texcoord>
   *      </point>
   *  </grid>
   */
  protected void initGrid(XMLElement xml) {
    int n = xml.getChildCount();

    // if there is only one parameter line, it should contain the
    // grid dimensions.
    if (n == 1) {
      numLayers = 1;
    } else {
      numLayers = n - 1;
    }

    points = new GLTexturedPoint[numLayers];

    XMLElement child;
    String name, wStr, hStr, modeName;
    int ptLayer = 0;
    for (int i = 0; i < n; i++) {
      child = xml.getChild(i);
      name = child.getName();
      if (name.equals("resolution")) {
        wStr = child.getStringAttribute("nx");
        hStr = child.getStringAttribute("ny");

        usingSrcTexRes = false;

        if (wStr.equals("w")) resX = 0;
        else if (wStr.equals("w0")) resX = -1;
        else if (wStr.equals("w1")) resX = -2;
        else if (wStr.equals("w2")) resX = -3;
        else if (wStr.equals("w3")) resX = -4;
        else if (wStr.equals("w4")) resX = -5;
        else if (wStr.equals("w5")) resX = -6;
        else resX = child.getIntAttribute("nx");

        if (hStr.equals("h")) resY = 0;
        else if (hStr.equals("h0")) resY = -1;
        else if (hStr.equals("h1")) resY = -2;
        else if (hStr.equals("h2")) resY = -3;
        else if (hStr.equals("h3")) resY = -4;
        else if (hStr.equals("h4")) resY = -5;
        else if (hStr.equals("h5")) resY = -6;
        else resY = child.getIntAttribute("ny");

        if ((resX <= 0) || (resY <= 0))
          if (resX != resY) System.err.println("Source width and height are different!");
          else if (resX < 0) {
            usingSrcTexRes = true;
            srcTexIdx = -(resX + 1);
          }

        modeName = child.getStringAttribute("mode");
        mode = GLUtils.parsePrimitiveType(modeName);
      } else if (name.equals("point")) {
        points[ptLayer] = new GLTexturedPoint(child);
        ptLayer++;
      }
    }

    if (n == 1) {
      points[0] = new GLTexturedPoint();
      points[0].setAsUndefined();
    }
  }