proto


  
  PROTO asphere [
     field SFInt32 nlatitude 16
     field SFInt32 nlongitude 16
     field SFFloat creaseAngle 3.14
  ]
  {
  DEF object Transform {
  children [
     DEF IFS IndexedFaceSet {
        coord DEF C Coordinate { }
        texCoord DEF TC TextureCoordinate { }
        creaseAngle IS creaseAngle
     }
   ]}
     DEF S Script {
        field SFInt32 nlat IS nlatitude
        field SFInt32 nl IS nlongitude
        field SFInt32 tick 0
        field SFInt32 state 0
        field SFInt32 phase 1
        ##eventOut MFVec3f c
        eventOut MFVec3f c
        eventOut MFVec2f tc
        eventOut MFInt32 ci
        eventIn SFFloat update
        eventIn SFFloat change
        url [
           "javascript:
           function initialize() {
              var r, phi, x, y, z;
              var i, j, idx;
              // Compute coordinates, texture coordinates:
              if (1) for (i = 0; i < nlat ; i++) {
                 //y = 2 * ( i / (nlat-1) ) - 1;
                 y = 2 * ( i / (nlat-1) ) - 1;
                 r = Math.sqrt( 1 - y*y );
                 for (j = 0; j < nl; j++) {
                    phi = 2 * Math.PI * j / nl;
                    x = -Math.sin(phi)*r;
                    z = -Math.cos(phi)*r;
                    c[i*nl+j] = new SFVec3f(x-1.0,y,z);
                    tc[i*nl+j] = new SFVec2f( j/nl, i/(nlat-1) );
                 }
               }
                 
               if (1) for (i = 0; i < nlat ; i++) {
                 //y = 2 * ( i / (nlat-1) ) - 1;
                 y = 2 * ( i / (nlat-1) ) - 1;
                 r = Math.sqrt( 1 - y*y );
                 for (j = 0; j < nl; j++) {
                    phi = 2 * Math.PI * j / nl;
                    x =  -Math.sin(phi)*r;
                    z =  -Math.cos(phi)*r;
                    c[nlat*nl + i*nl+j] = new SFVec3f(x+1.0,y,z);
                    tc[nlat*nl + i*nl+j] = new SFVec2f( j/nl, i/(nlat-1) );
                 }
              }
              // And compute indices:
              dfac = 3;
              if (1) for (i = 0; i < (nlat-1)/dfac; i++) {
                 for (j = 0; j < nl; j++) {
                    idx = 5*(i*nl+j);
                    ci[idx+0] = dfac*i*nl+(j)\%nl;
                    ci[idx+1] = dfac*i*nl+(j+1)\%nl;
                    ci[idx+2] = (dfac*i+1)*nl+(j+1)\%nl;
                    ci[idx+3] = (dfac*i+1)*nl+(j)\%nl;
                    ci[idx+4] = -1;  // end-of-polygon
                 }
              }
              if (1) for (i = 0; i < (nlat-1)/dfac; i++) {
              var offset;
              offset =  5* ((nlat-1)/dfac)*nl;
                 for (j = 0; j < nl; j++) {
                    idx = offset + 5*(i*nl+j);
                    ci[idx+0] = nlat*nl + dfac*i*nl+(j)\%nl;
                    ci[idx+1] = nlat*nl + dfac*i*nl+(j+1)\%nl;
                    ci[idx+2] = nlat*nl + (dfac*i+1)*nl+(j+1)\%nl;
                    ci[idx+3] = nlat*nl + (dfac*i+1)*nl+(j)\%nl;
                    ci[idx+4] = -1;  // end-of-polygon
                 }
              }
           }