Ejemplo n.º 1
0
  /** Initialize the WebGL buffers for our shapes. */
  private void buildBuffers(Shape[] shapes) {
    if (shapes != null) {
      vertices = new WebGLBuffer[shapes.length];
      colors = new WebGLBuffer[shapes.length];
      indices = new WebGLBuffer[shapes.length];

      for (int i = 0; i < shapes.length; ++i) {
        vertices[i] = gl.createBuffer();
        gl.bindBuffer(BufferTarget.ARRAY_BUFFER, vertices[i]);
        gl.bufferData(
            BufferTarget.ARRAY_BUFFER,
            Float32Array.create(shapes[i].vertices),
            BufferUsage.STATIC_DRAW);
        gl.vertexAttribPointer(vertexPositionAttribute, 3, DataType.FLOAT, false, 0, 0);

        colors[i] = gl.createBuffer();
        gl.bindBuffer(BufferTarget.ARRAY_BUFFER, colors[i]);
        gl.bufferData(
            BufferTarget.ARRAY_BUFFER,
            Float32Array.create(shapes[i].colors),
            BufferUsage.STATIC_DRAW);
        gl.vertexAttribPointer(vertexColorAttribute, 4, DataType.FLOAT, false, 0, 0);

        if (shapes[i].indexes != null) {
          indices[i] = gl.createBuffer();
          gl.bindBuffer(BufferTarget.ELEMENT_ARRAY_BUFFER, indices[i]);
          gl.bufferData(
              BufferTarget.ELEMENT_ARRAY_BUFFER,
              Uint16Array.create(shapes[i].indexes),
              BufferUsage.STATIC_DRAW);
        }
      }
    } else {
      Window.alert("No shapes found");
    }
  }