Example usage for org.lwjgl.opengl GL15 GL_ARRAY_BUFFER

List of usage examples for org.lwjgl.opengl GL15 GL_ARRAY_BUFFER

Introduction

In this page you can find the example usage for org.lwjgl.opengl GL15 GL_ARRAY_BUFFER.

Prototype

int GL_ARRAY_BUFFER

To view the source code for org.lwjgl.opengl GL15 GL_ARRAY_BUFFER.

Click Source Link

Document

Accepted by the target parameters of BindBuffer, BufferData, BufferSubData, MapBuffer, UnmapBuffer, GetBufferSubData, GetBufferParameteriv, and GetBufferPointerv.

Usage

From source file:org.spout.engine.renderer.vertexbuffer.VertexBufferImplByte.java

License:Open Source License

public void bind() {
    if (vboId == -1)
        throw new IllegalStateException("Cannot bind a vertex buffer without data!");
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
    GL20.glVertexAttribPointer(getLayout(), getElements(), GL11.GL_BYTE, false, 0, 0);
}

From source file:org.spout.renderer.lwjgl.gl20.GL20VertexArray.java

License:Open Source License

@Override
public void setData(VertexData vertexData) {
    checkCreated();//from  www.j a  v  a  2  s.  c  o  m
    // Generate a new indices buffer if we don't have one yet
    if (indicesBufferID == 0) {
        indicesBufferID = GL15.glGenBuffers();
    }
    // Bind the indices buffer
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBufferID);
    // Get the new count of indices
    final int newIndicesCount = vertexData.getIndicesCount();
    // If the new count is greater than or 50% smaller than the old one, we'll reallocate the memory
    // In the first case because we need more space, in the other to save space
    if (newIndicesCount > indicesCount || newIndicesCount <= indicesCount * 0.5) {
        GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, vertexData.getIndicesBuffer(), GL15.GL_STATIC_DRAW);
    } else {
        // Else, we replace the data with the new one, but we don't resize, so some old data might be left trailing in the buffer
        GL15.glBufferSubData(GL15.GL_ELEMENT_ARRAY_BUFFER, 0, vertexData.getIndicesBuffer());
    }
    // Unbind the indices buffer
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
    // Update the count to the new one
    indicesCount = newIndicesCount;
    indicesDrawCount = indicesCount;
    // Ensure that the indices offset and count fits inside the valid part of the buffer
    indicesOffset = Math.min(indicesOffset, indicesCount - 1);
    indicesDrawCount = indicesDrawCount - indicesOffset;
    // Bind the vao
    if (extension.has()) {
        extension.glBindVertexArray(id);
    }
    // Create a new array of attribute buffers ID of the correct size
    final int attributeCount = vertexData.getAttributeCount();
    final int[] newAttributeBufferIDs = new int[attributeCount];
    // Copy all the old buffer IDs that will fit in the new array so we can reuse them
    System.arraycopy(attributeBufferIDs, 0, newAttributeBufferIDs, 0,
            Math.min(attributeBufferIDs.length, newAttributeBufferIDs.length));
    // Delete any buffers that we don't need (new array is smaller than the previous one)
    for (int i = newAttributeBufferIDs.length; i < attributeBufferIDs.length; i++) {
        GL15.glDeleteBuffers(attributeBufferIDs[i]);
    }
    // Create new buffers if necessary (new array is larger than the previous one)
    for (int i = attributeBufferIDs.length; i < newAttributeBufferIDs.length; i++) {
        newAttributeBufferIDs[i] = GL15.glGenBuffers();
    }
    // Copy the old valid attribute buffer sizes
    final int[] newAttributeBufferSizes = new int[attributeCount];
    System.arraycopy(attributeBufferSizes, 0, newAttributeBufferSizes, 0,
            Math.min(attributeBufferSizes.length, newAttributeBufferSizes.length));
    // If we don't have a vao, we have to save the properties manually
    if (!extension.has()) {
        attributeSizes = new int[attributeCount];
        attributeTypes = new int[attributeCount];
        attributeNormalizing = new boolean[attributeCount];
    }
    // Upload the new vertex data
    for (int i = 0; i < attributeCount; i++) {
        final VertexAttribute attribute = vertexData.getAttribute(i);
        final ByteBuffer attributeData = attribute.getData();
        // Get the current buffer size
        final int bufferSize = newAttributeBufferSizes[i];
        // Get the new buffer size
        final int newBufferSize = attributeData.remaining();
        // Bind the target buffer
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, newAttributeBufferIDs[i]);
        // If the new count is greater than or 50% smaller than the old one, we'll reallocate the memory
        if (newBufferSize > bufferSize || newBufferSize <= bufferSize * 0.5) {
            GL15.glBufferData(GL15.GL_ARRAY_BUFFER, attributeData, GL15.GL_STATIC_DRAW);
        } else {
            // Else, we replace the data with the new one, but we don't resize, so some old data might be left trailing in the buffer
            GL15.glBufferSubData(GL15.GL_ARRAY_BUFFER, 0, attributeData);
        }
        // Update the buffer size to the new one
        newAttributeBufferSizes[i] = newBufferSize;
        // Next, we add the pointer to the data in the vao
        if (extension.has()) {
            // As a float, normalized or not
            GL20.glVertexAttribPointer(i, attribute.getSize(), attribute.getType().getGLConstant(),
                    attribute.getUploadMode().normalize(), 0, 0);
            // Enable the attribute
            GL20.glEnableVertexAttribArray(i);
        } else {
            // Else we save the properties for rendering
            attributeSizes[i] = attribute.getSize();
            attributeTypes[i] = attribute.getType().getGLConstant();
            attributeNormalizing[i] = attribute.getUploadMode().normalize();
        }
    }
    // Unbind the last vbo
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    // Unbind the vao
    if (extension.has()) {
        extension.glBindVertexArray(0);
    }
    // Update the attribute buffer IDs to the new ones
    attributeBufferIDs = newAttributeBufferIDs;
    // Update the attribute buffer sizes to the new ones
    attributeBufferSizes = newAttributeBufferSizes;
    // Check for errors
    LWJGLUtil.checkForGLError();
}

From source file:org.spout.renderer.lwjgl.gl20.GL20VertexArray.java

License:Open Source License

@Override
public void draw() {
    checkCreated();//from w  w w.jav  a2  s .  c  o m
    if (extension.has()) {
        // Bind the vao
        extension.glBindVertexArray(id);
    } else {
        // Enable the vertex attributes
        for (int i = 0; i < attributeBufferIDs.length; i++) {
            // Bind the buffer
            GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, attributeBufferIDs[i]);
            // Define the attribute
            GL20.glVertexAttribPointer(i, attributeSizes[i], attributeTypes[i], attributeNormalizing[i], 0, 0);
            // Enable it
            GL20.glEnableVertexAttribArray(i);
        }
        // Unbind the last buffer
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    }
    // Bind the index buffer
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBufferID);
    // Draw all indices with the provided mode
    GL11.glDrawElements(drawingMode.getGLConstant(), indicesDrawCount, GL11.GL_UNSIGNED_INT,
            indicesOffset * DataType.INT.getByteSize());
    // Unbind the indices buffer
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
    // Check for errors
    LWJGLUtil.checkForGLError();
}

From source file:org.spout.renderer.lwjgl.gl30.GL30VertexArray.java

License:Open Source License

@Override
public void setData(VertexData vertexData) {
    checkCreated();// ww w . j  a v  a  2 s  . c  om
    // Generate a new indices buffer if we don't have one yet
    if (indicesBufferID == 0) {
        indicesBufferID = GL15.glGenBuffers();
    }
    // Bind the indices buffer
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBufferID);
    // Get the new count of indices
    final int newIndicesCount = vertexData.getIndicesCount();
    // If the new count is greater than or 50% smaller than the old one, we'll reallocate the memory
    // In the first case because we need more space, in the other to save space
    if (newIndicesCount > indicesCount || newIndicesCount <= indicesCount * 0.5) {
        GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, vertexData.getIndicesBuffer(), GL15.GL_STATIC_DRAW);
    } else {
        // Else, we replace the data with the new one, but we don't resize, so some old data might be left trailing in the buffer
        GL15.glBufferSubData(GL15.GL_ELEMENT_ARRAY_BUFFER, 0, vertexData.getIndicesBuffer());
    }
    // Unbind the indices buffer
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
    // Update the count to the new one
    indicesCount = newIndicesCount;
    indicesDrawCount = indicesCount;
    // Ensure that the indices offset and count fits inside the valid part of the buffer
    indicesOffset = Math.min(indicesOffset, indicesCount - 1);
    indicesDrawCount = indicesDrawCount - indicesOffset;
    // Bind the vao
    GL30.glBindVertexArray(id);
    // Create a new array of attribute buffers ID of the correct size
    final int attributeCount = vertexData.getAttributeCount();
    final int[] newAttributeBufferIDs = new int[attributeCount];
    // Copy all the old buffer IDs that will fit in the new array so we can reuse them
    System.arraycopy(attributeBufferIDs, 0, newAttributeBufferIDs, 0,
            Math.min(attributeBufferIDs.length, newAttributeBufferIDs.length));
    // Delete any buffers that we don't need (new array is smaller than the previous one)
    for (int i = newAttributeBufferIDs.length; i < attributeBufferIDs.length; i++) {
        GL15.glDeleteBuffers(attributeBufferIDs[i]);
    }
    // Create new buffers if necessary (new array is larger than the previous one)
    for (int i = attributeBufferIDs.length; i < newAttributeBufferIDs.length; i++) {
        newAttributeBufferIDs[i] = GL15.glGenBuffers();
    }
    // Copy the old valid attribute buffer sizes
    final int[] newAttributeBufferSizes = new int[attributeCount];
    System.arraycopy(attributeBufferSizes, 0, newAttributeBufferSizes, 0,
            Math.min(attributeBufferSizes.length, newAttributeBufferSizes.length));
    // Upload the new vertex data
    for (int i = 0; i < attributeCount; i++) {
        final VertexAttribute attribute = vertexData.getAttribute(i);
        final ByteBuffer attributeData = attribute.getData();
        // Get the current buffer size
        final int bufferSize = newAttributeBufferSizes[i];
        // Get the new buffer size
        final int newBufferSize = attributeData.remaining();
        // Bind the target buffer
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, newAttributeBufferIDs[i]);
        // If the new count is greater than or 50% smaller than the old one, we'll reallocate the memory
        if (newBufferSize > bufferSize || newBufferSize <= bufferSize * 0.5) {
            GL15.glBufferData(GL15.GL_ARRAY_BUFFER, attributeData, GL15.GL_STATIC_DRAW);
        } else {
            // Else, we replace the data with the new one, but we don't resize, so some old data might be left trailing in the buffer
            GL15.glBufferSubData(GL15.GL_ARRAY_BUFFER, 0, attributeData);
        }
        // Update the buffer size to the new one
        newAttributeBufferSizes[i] = newBufferSize;
        // Next, we add the pointer to the data in the vao
        // We have three ways to interpret integer data
        if (attribute.getType().isInteger() && attribute.getUploadMode() == UploadMode.KEEP_INT) {
            // Directly as an int
            GL30.glVertexAttribIPointer(i, attribute.getSize(), attribute.getType().getGLConstant(), 0, 0);
        } else {
            // Or as a float, normalized or not
            GL20.glVertexAttribPointer(i, attribute.getSize(), attribute.getType().getGLConstant(),
                    attribute.getUploadMode().normalize(), 0, 0);
        }
        // Finally enable the attribute
        GL20.glEnableVertexAttribArray(i);
    }
    // Unbind the last vbo
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    // Unbind the vao
    GL30.glBindVertexArray(0);
    // Update the attribute buffer IDs to the new ones
    attributeBufferIDs = newAttributeBufferIDs;
    // Update the attribute buffer sizes to the new ones
    attributeBufferSizes = newAttributeBufferSizes;
    // Check for errors
    LWJGLUtil.checkForGLError();
}

From source file:org.terasology.logic.manager.VertexBufferObjectManager.java

License:Apache License

public void bufferVboData(int id, ByteBuffer buffer, int drawMode) {
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, id);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, drawMode);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}

From source file:org.terasology.rendering.assets.skeletalmesh.SkeletalMesh.java

License:Apache License

public void preRender() {
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);

    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboUVBuffer);
    GL13.glClientActiveTexture(GL13.GL_TEXTURE0);
    glTexCoordPointer(2, GL11.GL_FLOAT, TEX_COORD_SIZE * 4, 0);

    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboIndexBuffer);
}

From source file:org.terasology.rendering.assets.skeletalmesh.SkeletalMesh.java

License:Apache License

public void postRender() {
    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);

    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
}

From source file:org.terasology.rendering.assets.skeletalmesh.SkeletalMesh.java

License:Apache License

public void doRender(List<Vector3f> verts, List<Vector3f> normals) {
    FloatBuffer vertBuffer = BufferUtils.createFloatBuffer(verts.size() * 6);
    for (int i = 0; i < verts.size(); ++i) {
        Vector3f vert = verts.get(i);
        vertBuffer.put(vert.x);//w w  w .jav a2s  .  c  om
        vertBuffer.put(vert.y);
        vertBuffer.put(vert.z);
        Vector3f norm = normals.get(i);
        vertBuffer.put(norm.x);
        vertBuffer.put(norm.y);
        vertBuffer.put(norm.z);
    }
    vertBuffer.flip();
    VertexBufferObjectManager.getInstance().bufferVboData(vboPosNormBuffer, vertBuffer, GL15.GL_DYNAMIC_DRAW);

    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboPosNormBuffer);
    glVertexPointer(VECTOR3_SIZE, GL_FLOAT, STRIDE, 0);
    glNormalPointer(GL_FLOAT, STRIDE, NORMAL_OFFSET);

    GL11.glDrawElements(GL11.GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);

    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}

From source file:org.terasology.rendering.opengl.OpenGLMesh.java

License:Apache License

public void preRender() {
    if (!isDisposed()) {
        glEnableClientState(GL_VERTEX_ARRAY);
        if (hasTexCoord0 || hasTexCoord1) {
            glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        }/*from w  ww.ja v a  2s  .c  o m*/
        if (hasColor) {
            glEnableClientState(GL_COLOR_ARRAY);
        }
        if (hasNormal) {
            glEnableClientState(GL_NORMAL_ARRAY);
        }

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboVertexBuffer);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboIndexBuffer);

        glVertexPointer(VERTEX_SIZE, GL11.GL_FLOAT, stride, vertexOffset);

        if (hasTexCoord0) {
            GL13.glClientActiveTexture(GL13.GL_TEXTURE0);
            glTexCoordPointer(TEX_COORD_0_SIZE, GL11.GL_FLOAT, stride, texCoord0Offset);
        }

        if (hasTexCoord1) {
            GL13.glClientActiveTexture(GL13.GL_TEXTURE1);
            glTexCoordPointer(TEX_COORD_1_SIZE, GL11.GL_FLOAT, stride, texCoord1Offset);
        }

        if (hasColor) {
            glColorPointer(COLOR_SIZE, GL11.GL_FLOAT, stride, colorOffset);
        }
        if (hasNormal) {
            glNormalPointer(GL11.GL_FLOAT, stride, normalOffset);
        }
    } else {
        logger.error("Attempted to render disposed mesh: {}", getURI());
    }
}

From source file:org.terasology.rendering.opengl.OpenGLMesh.java

License:Apache License

public void postRender() {
    if (!isDisposed()) {
        if (hasNormal) {
            glDisableClientState(GL_NORMAL_ARRAY);
        }/*from w w  w  .ja va2  s.  c o  m*/
        if (hasColor) {
            glDisableClientState(GL_COLOR_ARRAY);
        }
        if (hasTexCoord0 || hasTexCoord1) {
            glDisableClientState(GL_TEXTURE_COORD_ARRAY);
        }
        glDisableClientState(GL_VERTEX_ARRAY);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
    } else {
        logger.error("Attempted to render disposed mesh: {}", getURI());
    }
}