SHARE
TWEET

Graphics Assignment

a guest Nov 8th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export function ColoredSphere(gl: WebGL2RenderingContext, verticalResolution: number=32, horizontalResolution: number=32): Mesh{
  2.     // TODO: Create a colored sphere mesh and return it
  3.     let mesh = createMesh(gl);
  4.     let data = new Array(horizontalResolution);
  5.     let color_data = new Array(horizontalResolution);
  6.     let elements_data = new Array(horizontalResolution);
  7.     for(var i: number; i<horizontalResolution; i++) {
  8.         var longSeg = Math.PI * (-1+2*i/horizontalResolution);         //Mapping the longSeg from -PI to PI
  9.         //data[i] = new Array(verticalResolution);
  10.         for(var j=0; j<verticalResolution; j++) {
  11.             var latSeg = Math.PI * (-1+2*i/horizontalResolution)/2;      //Mapping latSeg to (-PI/2 to PI/2)
  12.             var x:number = Math.sin(latSeg) * Math.cos(longSeg);
  13.             var y:number = Math.sin(latSeg) * Math.sin(longSeg);
  14.             var z:number = Math.cos(latSeg);
  15.             data[i].push([x,y,z,1.0]);
  16.             color_data[i].push(colorArr[i%8]);
  17.         }
  18.     }
  19.  
  20.     for (let i:number = 0; i < latSeg; ++i) {
  21.         for (let j = 0; j < longSeg; ++j) {
  22.           let first = (i * (j + 1)) + j;
  23.           let second = first + longSeg + 1;
  24.  
  25.           elements_data.push(first);
  26.           elements_data.push(second);
  27.           elements_data.push(first + 1);
  28.           elements_data.push(second);
  29.           elements_data.push(second + 1);
  30.           elements_data.push(first + 1);
  31.         }
  32.     }
  33.  
  34.     let positions: Array<number> = data.flat();
  35.     let colors: Array<number>  = color_data.flat();
  36.     let elements: Array<number>  = elements_data.flat();
  37.     mesh.setBufferData("positions", new Float32Array(positions), gl.STATIC_DRAW);
  38.     mesh.setBufferData("colors", new Uint8Array(colors), gl.STATIC_DRAW);
  39.     mesh.setElementsData(new Uint32Array(elements), gl.STATIC_DRAW);
  40.     return mesh;
  41. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top