[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Public WebGL] WebGLArray iterator proposal



A crucial piece you're missing is the fact that multiple WebGLArrays can refer to the same region of memory. To do so, use the WebGL[T]Array constructor taking a WebGLArrayBuffer. You can either create the WebGLArrayBuffer separately, or fetch it from a previously created WebGLArray.

To implement your first example, you would do something like

  var numVertices = 100; // for example
  var floatSize = gl.sizeInBytes(gl.FLOAT);
  var vertexSize = 3 * floatSize + 4 * gl.sizeInBytes(gl.UNSIGNED_BYTE);
  // Round up to sizeInBytes(gl.FLOAT) -- just in case it isn't 4 on some platforms
  vertexSize = ((vertexSize + floatSize - 1) / floatSize) * floatSize;
  var vertexSizeInFloats = vertexSize / floatSize;
  var buf = new WebGLArrayBuffer(numVertices * vertexSize);
  var floatArray = new WebGLFloatArray(buf);
  var byteArray = new WebGLUnsignedByteArray(buf);
  var floatIdx = 0;
  // Set up the initial offset of the bytes within the vertex data
  var byteIdx = 3 * floatSize;
  for (var i = 0; i < numVertices; i++) {
    floatArray[floatIdx] = ...;
    floatArray[floatIdx + 1] = ...;
    floatArray[floatIdx + 2] = ...;
    byteArray[byteIdx] = ...;
    byteArray[byteIdx + 1] = ...;
    byteArray[byteIdx + 2] = ...;
    byteArray[byteIdx + 3] = ...;
    floatIdx += vertexSizeInFloats;
    byteIdx += vertexSize;
  }

We certainly need to provide examples of this. Right now I don't think there are any examples of heterogeneous interleaved data in the demos.

You should be able to implement the sort of iterators you have in mind in pure _javascript_, hiding these details from the casual programmer. Please take another look at the spec and give it a try.

-Ken