Example usage for org.lwjgl.opengl GL20 glVertexAttribPointer

List of usage examples for org.lwjgl.opengl GL20 glVertexAttribPointer

Introduction

In this page you can find the example usage for org.lwjgl.opengl GL20 glVertexAttribPointer.

Prototype

public static void glVertexAttribPointer(@NativeType("GLuint") int index, @NativeType("GLint") int size,
        @NativeType("GLenum") int type, @NativeType("GLboolean") boolean normalized,
        @NativeType("GLsizei") int stride, @NativeType("void const *") FloatBuffer pointer) 

Source Link

Document

Specifies the location and organization of a vertex attribute array.

Usage

From source file:com.voxelplugineering.voxelsniper.render.buffer.BufferSection.java

License:Open Source License

public void create(int vaoId) {
    build();/*www .j a  va 2 s.  c o m*/

    GL30.glBindVertexArray(vaoId);

    // Create a new Vertex Buffer Object in memory and select it (bind)
    vboId = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesFloatBuffer, GL15.GL_STREAM_DRAW);

    // Put the position coordinates in attribute list 0
    GL20.glVertexAttribPointer(0, RenderingConstants.POSITION_ELEMENT_COUNT, GL11.GL_FLOAT, false,
            RenderingConstants.STRIDE, RenderingConstants.POSITION_BYTE_OFFSET);
    // Put the color components in attribute list 1
    GL20.glVertexAttribPointer(1, RenderingConstants.COLOUR_ELEMENT_COUNT, GL11.GL_FLOAT, false,
            RenderingConstants.STRIDE, RenderingConstants.COLOUR_BYTE_OFFSET);
    // Put the texture coordinates in attribute list 2
    GL20.glVertexAttribPointer(2, RenderingConstants.TEXTURE_ELEMENT_COUNT, GL11.GL_FLOAT, false,
            RenderingConstants.STRIDE, RenderingConstants.TEXTURE_BYTE_OFFSET);

    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

    // Deselect (bind to 0) the VAO
    GL30.glBindVertexArray(0);

    // Create a new VBO for the indices and select it (bind) - INDICES
    vboiId = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId);
    GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
}

From source file:com.xrbpowered.gl.res.shaders.InstanceBuffer.java

License:Open Source License

public void enable() {
    int offs = 0;
    for (int i = 0; i < elemCount.length; i++) {
        GL20.glEnableVertexAttribArray(attribId + i);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, iboId);
        GL20.glVertexAttribPointer(attribId + i, elemCount[i], GL11.GL_FLOAT, false, stride, offs);
        offs += 4 * elemCount[i];/*from  w ww.j a  va2 s.com*/
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        GL33.glVertexAttribDivisor(attribId + i, divisor);
        //         GL20.glGetVertexAttrib(attribId, GL33.GL_VERTEX_ATTRIB_ARRAY_DIVISOR, testParam);
        Client.checkError();
    }
}

From source file:com.xrbpowered.gl.res.shaders.VertexInfo.java

License:Open Source License

public void initAttribPointers() {
    for (int i = 0; i < getAttributeCount(); i++) {
        Attribute a = attribs.get(i);
        GL20.glVertexAttribPointer(i, a.elemCount, GL11.GL_FLOAT, false, getStride(), a.offset * 4);
    }//from w  ww.j  a  v a 2 s  . co  m
}

From source file:cuchaz.jfxgl.prism.JFXGLContext.java

License:Open Source License

@Override
public void drawIndexedQuads(float coords[], byte colors[], int numVertices) {

    int numQuads = numVertices / 4;
    int numIndices = numQuads * 2 * 3;

    // for some reason (compatibility maybe?) JavaFX keeps all its data on the JVM heap (eg, in arrays)
    // sadly, LWJGL won't let use attribute offsets on heap buffers, so we need to copy into direct buffers
    // hopefully we're not drawing that many quads, and this won't be too slow

    // pos,tex coords
    indexedQuadsCoordsBuf = updateBuffer(indexedQuadsCoordsBuf, coords, numVertices * PosTexBytes);
    GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, PosTexBytes,
            MemoryUtil.memAddress(indexedQuadsCoordsBuf) + PosOffsetBytes);
    // index 1 is color, handled below
    GL20.glVertexAttribPointer(2, 2, GL11.GL_FLOAT, false, PosTexBytes,
            MemoryUtil.memAddress(indexedQuadsCoordsBuf) + Tex0OffsetBytes);
    GL20.glVertexAttribPointer(3, 2, GL11.GL_FLOAT, false, PosTexBytes,
            MemoryUtil.memAddress(indexedQuadsCoordsBuf) + Tex1OffsetBytes);

    // colors/*w  w w.  j av a 2s  .  c  o m*/
    indexedQuadsColorsBuf = updateBuffer(indexedQuadsColorsBuf, colors, numVertices * ColorBytes);
    GL20.glVertexAttribPointer(1, 4, GL11.GL_UNSIGNED_BYTE, true, ColorBytes, indexedQuadsColorsBuf);

    GL11.glDrawElements(GL11.GL_TRIANGLES, numIndices, GL11.GL_UNSIGNED_SHORT, 0);
}

From source file:cuchaz.jfxgl.prism.TexturedQuad.java

License:Open Source License

public TexturedQuad(int x, int y, int w, int h, int texId, Shader shader) {

    this.shader = shader;
    this.texId = texId;

    // make the vertex array
    vaoId = GL30.glGenVertexArrays();/*from   w ww. jav  a  2s.c o  m*/
    GL30.glBindVertexArray(vaoId);

    try (MemoryStack m = MemoryStack.stackPush()) {

        // make the indices
        ByteBuffer indexBuf = m.bytes(new byte[] { 0, 1, 2, 0, 2, 3 });
        iboId = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, iboId);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, indexBuf, GL15.GL_STATIC_DRAW);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, iboId);

        // make the vertices
        FloatBuffer vertexBuf = m.floats(
                new float[] { x + 0, y + 0, 0, 0, x + w, y + 0, 1, 0, x + w, y + h, 1, 1, x + 0, y + h, 0, 1 });
        vboId = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexBuf, GL15.GL_STATIC_DRAW);
        GL20.glEnableVertexAttribArray(0);
        GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, Float.BYTES * 4, 0);
        GL20.glEnableVertexAttribArray(1);
        GL20.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, Float.BYTES * 4, Float.BYTES * 2);
    }

    // unbind things
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    GL30.glBindVertexArray(0);
}

From source file:Data.Building.java

License:Apache License

@Override
public void setup() {
    // OpenGL expects vertices to be defined counter clockwise by default

    triangulize(geometry);//  w w  w. j  av a 2s .  co  m

    // Sending data to OpenGL requires the usage of (flipped) byte buffers
    FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertices.length);
    verticesBuffer.put(vertices);
    verticesBuffer.flip();

    vertexCount = vertices.length;

    // Create a new Vertex Array Object in memory and select it (bind)
    // A VAO can have up to 16 attributes (VBO's) assigned to it by default
    vaoId = GL30.glGenVertexArrays();
    GL30.glBindVertexArray(vaoId);

    // Create a new Vertex Buffer Object in memory and select it (bind)
    // A VBO is a collection of Vectors which in this case resemble the location of each vertex.
    vboId = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesBuffer, GL15.GL_STATIC_DRAW);
    // Put the VBO in the attributes list at index 0
    GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
    // Deselect (bind to 0) the VBO
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

    // Deselect (bind to 0) the VAO
    GL30.glBindVertexArray(0);

    Util.exitOnGLError("Error in setupQuad");
}

From source file:dataAccess.lwjgl.VAO_Loader.java

/**
 * Stores data in an attribute list of a VAO.
 *
 * @param vaoID The id of the VAO to which data will be added.
 * @param attributeNumber The number of the attribute list in which the data
 * will be stored.//from  ww  w.  j av  a  2s .c om
 * @param data The data that will be stored in the attribute list.
 */
private static void storeDataInAttributeList(int vaoID, int attributeNumber, int coordinateSize, float[] data) {
    // bind VAO so that it can be used.
    bindVAO(vaoID);

    // Create new VBO.
    int vboID = GL15.glGenBuffers();

    // Adds VBO to list so that it can be cleared when needed.
    vbos.add(vboID);

    // VBO has to be bound aswel.
    bindArrayBuffer(vboID);

    // Converts float array to an instance of FloatBuffer, which can
    // be stored in a VBO.
    FloatBuffer buffer = Convert.toReadableFloatBuffer(data);

    // Puts the buffer into the VBO, and GL_STATIC_DRAW tells it that it 
    // won't ever be modified.
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);

    // Specifies that this is for the Vertex Array.
    GL20.glVertexAttribPointer(attributeNumber, coordinateSize, GL11.GL_FLOAT, false, 0, 0);

    // Unbind the VBO.
    unbindArrayBuffer();

    // unbind VAO so that another may be bound.
    unbindVAO();
}

From source file:de.ikosa.mars.viewer.glviewer.engine.GLMeshBuilder.java

License:Open Source License

protected GLMesh createMesh(GLResources resourceManager, boolean fetchMaterial) {
    // create VBO buffers
    FloatBuffer interleavedBuffer = BufferUtils.createFloatBuffer(vertices.size() * 8);
    int stride = 12 + (hasNormals ? 12 : 0) + (hasTexCoords ? 8 : 0);
    int normalPosition = 12;
    int texCoordPosition = 12 + (hasNormals ? 12 : 0);

    // fill buffers
    for (GLVertex vertex : vertices) {
        interleavedBuffer.put(vertex.px);
        interleavedBuffer.put(vertex.py);
        interleavedBuffer.put(vertex.pz);
        if (hasNormals) {
            interleavedBuffer.put(vertex.nx);
            interleavedBuffer.put(vertex.ny);
            interleavedBuffer.put(vertex.nz);
        }//from  www . j  a v  a  2  s.co  m
        if (hasTexCoords) {
            interleavedBuffer.put(vertex.tu);
            interleavedBuffer.put(vertex.tv);
        }
    }

    interleavedBuffer.flip();

    // create VAO
    int vaoId = GL30.glGenVertexArrays();
    GL30.glBindVertexArray(vaoId);

    // create VBOs
    int interleavedVboId = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, interleavedVboId);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, interleavedBuffer, GL15.GL_STATIC_DRAW);

    // position should always be there
    GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, stride, 0);
    if (hasNormals)
        GL20.glVertexAttribPointer(1, 3, GL11.GL_FLOAT, false, stride, normalPosition);
    if (hasTexCoords)
        GL20.glVertexAttribPointer(2, 2, GL11.GL_FLOAT, false, stride, texCoordPosition);

    // unbind buffers
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    GL30.glBindVertexArray(0);

    // create mesh object
    GLMesh mesh = createInstance(vaoId, interleavedVboId);

    // create submeshes for material groups

    for (String material : indices.keySet()) {
        GLMaterial glMaterial = fetchMaterial ? resourceManager.getMaterial(material) : GLMaterial.nullMaterial;
        // create index buffer
        List<GLIndex> indicesForMaterial = indices.get(material);
        IntBuffer indexBuffer = BufferUtils.createIntBuffer(indicesForMaterial.size());

        for (GLIndex index : indicesForMaterial) {
            indexBuffer.put(index.index);
        }
        indexBuffer.flip();

        int indexVboId = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indexVboId);
        GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL15.GL_STATIC_DRAW);

        GLSubMesh subMesh = new GLSubMesh(indexVboId, indicesForMaterial.size(), glMaterial, mesh);
        mesh.addSubMesh(subMesh);

    }

    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);

    return mesh;
}

From source file:de.ikosa.mars.viewer.glviewer.GLHeightMapMeshBuilder.java

License:Open Source License

public GLHeightMapMesh createMesh() {
    ML.d(String.format("Writing mesh \"%s\" to buffer...", name));
    // create VBO buffers
    FloatBuffer interleavedBuffer = BufferUtils.createFloatBuffer(matrixSizeX * matrixSizeY * 9);
    int stride = 12 + 12 + 12;
    int normalPosition = 12;
    int texCoordPosition = 24;

    // fill buffers
    for (int y = 0; y < matrixSizeY; y++) {
        for (int x = 0; x < matrixSizeX; x++) {
            GLWorldVertex vertex = vMatrix[x][y];
            interleavedBuffer.put(vertex.px);
            interleavedBuffer.put(vertex.py);
            interleavedBuffer.put(vertex.pz);
            interleavedBuffer.put(vertex.nx);
            interleavedBuffer.put(vertex.ny);
            interleavedBuffer.put(vertex.nz);
            interleavedBuffer.put(vertex.tu);
            interleavedBuffer.put(vertex.tv);
            interleavedBuffer.put(vertex.tw);
        }/*from  w ww. j  a  va 2s  .co  m*/
    }

    interleavedBuffer.flip();

    ML.d("Done.");

    // create VAO
    int vaoId = GL30.glGenVertexArrays();
    GL30.glBindVertexArray(vaoId);

    // create VBOs
    int interleavedVboId = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, interleavedVboId);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, interleavedBuffer, GL15.GL_STATIC_DRAW);

    // attribs
    GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, stride, 0);
    GL20.glVertexAttribPointer(1, 3, GL11.GL_FLOAT, false, stride, normalPosition);
    GL20.glVertexAttribPointer(2, 3, GL11.GL_FLOAT, false, stride, texCoordPosition);

    // unbind buffers
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    GL30.glBindVertexArray(0);

    // create index buffer
    iBuffer.flip();

    int indexVboId = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indexVboId);
    GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, iBuffer, GL15.GL_STATIC_DRAW);
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);

    GLTextureArray terrainTexture = (GLTextureArray) GLResources.global.getTexture("terrain");

    // create mesh object
    GLHeightMapMesh mesh = new GLHeightMapMesh(name, vaoId, interleavedVboId, indexVboId, indices,
            terrainTexture);
    return mesh;
}

From source file:engine.render.TexturedQuad.java

private void setupQuad(Vector2f size) {
    TexturedVertex v0 = new TexturedVertex();
    v0.setXYZ(-0.5f * size.x, 0.5f * size.y, 0);
    v0.setRGB(1, 0, 0);//w w w  . j a  v a 2 s.co  m
    v0.setST(0, 0);
    TexturedVertex v1 = new TexturedVertex();
    v1.setXYZ(-0.5f * size.x, -0.5f * size.x, 0);
    v1.setRGB(0, 1, 0);
    v1.setST(0, 1);
    TexturedVertex v2 = new TexturedVertex();
    v2.setXYZ(0.5f * size.y, -0.5f * size.x, 0);
    v2.setRGB(0, 0, 1);
    v2.setST(1, 1);
    TexturedVertex v3 = new TexturedVertex();
    v3.setXYZ(0.5f * size.x, 0.5f * size.y, 0);
    v3.setRGB(1, 1, 1);
    v3.setST(1, 0);

    TexturedVertex[] vertices = new TexturedVertex[] { v0, v1, v2, v3 };

    FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertices.length * TexturedVertex.elementCount);
    for (int i = 0; i < vertices.length; i++) {

        verticesBuffer.put(vertices[i].getElements());
    }
    verticesBuffer.flip();

    byte[] indices = { 0, 1, 2, 2, 3, 0 };
    indicesCount = indices.length;
    ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indicesCount);
    indicesBuffer.put(indices);
    indicesBuffer.flip();

    vaoId = GL30.glGenVertexArrays();
    GL30.glBindVertexArray(vaoId);

    vboId = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesBuffer, GL15.GL_STATIC_DRAW);

    GL20.glVertexAttribPointer(0, TexturedVertex.positionElementCount, GL11.GL_FLOAT, false,
            TexturedVertex.stride, TexturedVertex.positionByteOffset);

    GL20.glVertexAttribPointer(1, TexturedVertex.colorElementCount, GL11.GL_FLOAT, false, TexturedVertex.stride,
            TexturedVertex.colorByteOffset);

    GL20.glVertexAttribPointer(2, TexturedVertex.textureElementCount, GL11.GL_FLOAT, false,
            TexturedVertex.stride, TexturedVertex.textureByteOffset);

    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

    GL30.glBindVertexArray(0);

    vboiId = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId);
    GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);

    //this.exitOnGLError("setupQuad");
}