Esempio n. 1
0
  /**
   * Constructor that creates a Vertex Buffer Object with the specified GLSL attributes. (typically
   * location, texture coordinates, normals, etc.)
   *
   * @param gl The global openGL instance.
   * @param attribs One or more attributes that represent this VBO, @see GLSLAttrib
   */
  public VBO(GL3 gl, GLSLAttrib... attribs) {
    this.attribs = attribs;

    // Generate a new internal OpenGL VBO pointer
    this.vboPointer = Buffers.newDirectIntBuffer(1);
    gl.glGenVertexArrays(1, this.vboPointer);
    gl.glBindVertexArray(this.vboPointer.get(0));

    // Generate a new internal OpenGL Array Buffer pointer
    this.bufferPointer = Buffers.newDirectIntBuffer(1);
    gl.glGenBuffers(1, this.bufferPointer);
    gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, this.bufferPointer.get(0));

    // Allocate enough memory
    int size = 0;
    for (final GLSLAttrib attrib : attribs) {
      size += attrib.buffer.capacity() * Buffers.SIZEOF_FLOAT;
    }

    gl.glBufferData(GL3.GL_ARRAY_BUFFER, size, (Buffer) null, GL3.GL_STATIC_DRAW);

    // Copy the GLSL Attribute data into the internal OpenGL buffer
    int nextStart = 0;
    for (final GLSLAttrib attrib : attribs) {
      gl.glBufferSubData(
          GL3.GL_ARRAY_BUFFER,
          nextStart,
          attrib.buffer.capacity() * Buffers.SIZEOF_FLOAT,
          attrib.buffer);
      nextStart += attrib.buffer.capacity() * Buffers.SIZEOF_FLOAT;
    }
  }