private void initBuffers(GL3 gl) { // IDs for 2 buffers int[] buffArray = new int[2]; gl.glGenBuffers(buffArray.length, buffArray, 0); vbo = buffArray[0]; assert vbo > 0; // Bind buffer and upload data gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbo); gl.glBufferData( GL3.GL_ARRAY_BUFFER, vertexColorData.length * Buffers.SIZEOF_FLOAT, vertexColorDataBuffer, GL3.GL_STATIC_DRAW); gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0); // Buffer with the 3 indices required for one triangle ibo = buffArray[1]; assert ibo > 0; gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, ibo); gl.glBufferData( GL3.GL_ELEMENT_ARRAY_BUFFER, indices.length * Buffers.SIZEOF_SHORT, indicesBuffer, GL3.GL_STATIC_DRAW); gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, 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; } }
/** * A utility method to load vertex data into an OpenGL "vertex array object" (VAO) for efficient * rendering. The VAO stores several "vertex buffer objects" (VBOs) that contain the vertex * attribute data. * * @param data reference to the vertex data to be loaded into a VAO */ private void initArrayBuffer(GLVertexData data) { // Make a vertex array object (VAO) for this vertex data // and store a reference to it GLVertexArrayObject vao = new GLVertexArrayObject(gl, data.getElements().size() + 1); data.setVAO(vao); // Bind (activate) the VAO for the vertex data in OpenGL. // The subsequent OpenGL operations on VBOs will be recorded (stored) // in the VAO. vao.bind(); // Store all vertex attributes in vertex buffer objects (VBOs) ListIterator<VertexData.VertexElement> itr = data.getElements().listIterator(0); data.getVAO().rewindVBO(); while (itr.hasNext()) { VertexData.VertexElement e = itr.next(); // Bind the vertex buffer object (VBO) gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, data.getVAO().getNextVBO()); // Upload vertex data gl.glBufferData( GL3.GL_ARRAY_BUFFER, e.getData().length * 4, FloatBuffer.wrap(e.getData()), GL3.GL_DYNAMIC_DRAW); } // Bind the default vertex buffer objects gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0); // Store the vertex data indices into the last vertex buffer gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, data.getVAO().getNextVBO()); gl.glBufferData( GL3.GL_ELEMENT_ARRAY_BUFFER, data.getIndices().length * 4, IntBuffer.wrap(data.getIndices()), GL3.GL_DYNAMIC_DRAW); // Bind the default vertex array object. This "deactivates" the VAO // of the vertex data gl.glBindVertexArray(0); }
private void initializeVertexBuffer(GL3 gl3) { gl3.glGenBuffers(1, IntBuffer.wrap(vertexBufferObject)); gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferObject[0]); { FloatBuffer buffer = GLBuffers.newDirectFloatBuffer(vertexData); gl3.glBufferData(GL3.GL_ARRAY_BUFFER, vertexData.length * 4, buffer, GL3.GL_STATIC_DRAW); } gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0); }