Пример #1
0
  public void drawShapes() {
    gl.clear(ClearBufferMask.COLOR_BUFFER_BIT, ClearBufferMask.DEPTH_BUFFER_BIT);

    MODELVIEW.push();
    float[] gt = getState().translation;
    if (gt != null) {
      MODELVIEW.translate(gt[0], gt[1], gt[2]);
    }

    for (int i = 0; i < vertices.length; ++i) {
      WebGLState s = getState();

      MODELVIEW.push();
      MODELVIEW.translate(s.shapes[i].tx, s.shapes[i].ty, s.shapes[i].tz);

      MODELVIEW.rotateX(s.shapes[i].rx);
      MODELVIEW.rotateY(s.shapes[i].ry);
      MODELVIEW.rotateZ(s.shapes[i].rz);

      setMatrixUniforms();
      MODELVIEW.pop();

      gl.bindBuffer(BufferTarget.ARRAY_BUFFER, vertices[i]);
      gl.vertexAttribPointer(vertexPositionAttribute, 3, DataType.FLOAT, false, 0, 0);

      gl.bindBuffer(BufferTarget.ARRAY_BUFFER, colors[i]);
      gl.vertexAttribPointer(vertexColorAttribute, 4, DataType.FLOAT, false, 0, 0);

      if (indices[i] != null) {
        gl.bindBuffer(BufferTarget.ELEMENT_ARRAY_BUFFER, indices[i]);
        gl.drawElements(BeginMode.TRIANGLES, 36, DrawElementsType.UNSIGNED_SHORT, 0);
      } else {
        gl.drawArrays(BeginMode.TRIANGLE_STRIP, 0, 3);
      }
    }

    MODELVIEW.pop();
  }
Пример #2
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");
    }
  }