Example usage for org.lwjgl.opengl GL15 glBufferData

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

Introduction

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

Prototype

public static void glBufferData(@NativeType("GLenum") int target, @NativeType("void const *") double[] data,
        @NativeType("GLenum") int usage) 

Source Link

Document

Array version of: #glBufferData BufferData

Usage

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  w  w w  .  j a v a 2 s .  c  o  m*/
 * @param data The data that will be stored in the attribute list.
 */
private static void bindIndicesBuffer(int vaoID, int[] 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.
    bindElementArrayBuffer(vboID);

    // Converts int array to an instance of IntBuffer, which can
    // be stored in a VBO.
    IntBuffer buffer = Convert.toReadableIntBuffer(data);

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

    // Unbind the VBO.
    //unbindElementArrayBuffer();
    // unbind VAO so that another may be bound.
    unbindVAO();
}

From source file:de.codesourcery.flocking.LWJGLRenderer.java

License:Apache License

private void renderWorld(World world) {
    final double modelMax = world.getSimulationParameters().modelMax;
    xInc = Display.getWidth() / modelMax;
    yInc = Display.getHeight() / modelMax;

    GL11.glEnable(GL11.GL_DEPTH_TEST);//from  w  w  w  .  java 2s. c  o  m
    GL11.glDepthMask(true);

    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

    GL11.glDisable(GL11.GL_DEPTH_TEST);
    GL11.glDepthMask(false);

    GL11.glColor3f(0.5f, 0.5f, 1.0f);

    GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);

    final int triangleCount = world.getPopulationCount();

    // setup vertex data       
    final int vertexArrayLen = triangleCount * 3 * 2; // one triangle = 3 vertices * 2 int's per vertex
    final MyIntBuffer vertexBuffer = getVertexBuffer(vertexArrayLen);

    final IntBuffer vertexIntBuffer = vertexBuffer.getBuffer();
    final IBoidVisitor visitor = new IBoidVisitor() {

        @Override
        public void visit(Boid boid) {
            drawBoid(boid, vertexIntBuffer);
        }
    };
    world.visitAllBoids(visitor);

    vertexBuffer.rewind();

    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBuffer.getHandle());
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexBuffer.getBuffer(), GL15.GL_DYNAMIC_DRAW);
    GL11.glVertexPointer(2, GL11.GL_INT, 0, 0);

    // setup index data
    MyIntBuffer indexBuffer = getIndexBuffer(triangleCount * 3);
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indexBuffer.getHandle());
    GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indexBuffer.getBuffer(), GL15.GL_STATIC_DRAW);

    GL11.glDrawElements(GL11.GL_TRIANGLES, triangleCount * 3, GL11.GL_UNSIGNED_INT, 0);

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

    GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);

    GL11.glEnable(GL11.GL_DEPTH_TEST);
    GL11.glDepthMask(true);
}

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  w ww. j  a  v a 2  s. c o  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 w  w  .  ja v a  2  s  .  c o  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.LightArea.java

private void bindIndices(IntBuffer indicesBuffer) {
    vbos.add(indicesVboId);/*ww  w.j av  a 2s .  c  o m*/
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesVboId);
    GL15.glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_DYNAMIC_DRAW);
}

From source file:engine.render.LightArea.java

private void bindPositions(FloatBuffer positions, int attributeNumber) {
    vbos.add(positionsVboId);//w w w.j  a  v a 2  s . com
    glBindBuffer(GL_ARRAY_BUFFER, positionsVboId);
    GL15.glBufferData(GL_ARRAY_BUFFER, positions, GL_DYNAMIC_DRAW);
    glVertexAttribPointer(attributeNumber, 2, GL_FLOAT, false, 0, 0);
    glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}

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);/*from   w  w  w .jav a  2s .  c  om*/
    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");
}

From source file:eu.over9000.veya.rendering.ChunkVAO.java

License:Open Source License

public void create() {
    // create objects

    if (holdsSolid) {
        this.ibo_handle_solid = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.ibo_handle_solid);
        GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, this.ibo_buffer_solid, GL15.GL_STATIC_DRAW);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);

        this.vbo_handle_solid = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.vbo_handle_solid);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, this.vbo_buffer_solid, GL15.GL_STATIC_DRAW);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

        this.vao_handle_solid = GL30.glGenVertexArrays();
        GL30.glBindVertexArray(this.vao_handle_solid);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.vbo_handle_solid);
        GL20.glEnableVertexAttribArray(0);
        GL20.glVertexAttribPointer(0, Vertex.positionElementCount, GL11.GL_FLOAT, false, Vertex.stride,
                Vertex.positionByteOffset);

        GL20.glEnableVertexAttribArray(1);
        GL20.glVertexAttribPointer(1, Vertex.colorElementCount, GL11.GL_FLOAT, false, Vertex.stride,
                Vertex.colorByteCount);/*from  w  w  w. j ava  2s  .  c om*/

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

        GL20.glEnableVertexAttribArray(3);
        GL20.glVertexAttribPointer(3, Vertex.normalElementCount, GL11.GL_FLOAT, false, Vertex.stride,
                Vertex.normalByteOffset);

        GL20.glEnableVertexAttribArray(4);
        GL20.glVertexAttribPointer(4, Vertex.aoElementCount, GL11.GL_FLOAT, false, Vertex.stride,
                Vertex.aoByteOffset);

        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.ibo_handle_solid);

        GL30.glBindVertexArray(0);
    }

    if (holdsTransparent) {
        this.ibo_handle_transparent = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.ibo_handle_transparent);
        GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, this.ibo_buffer_transparent, GL15.GL_STATIC_DRAW);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);

        this.vbo_handle_transparent = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.vbo_handle_transparent);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, this.vbo_buffer_transparent, GL15.GL_STATIC_DRAW);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

        this.vao_handle_transparent = GL30.glGenVertexArrays();
        GL30.glBindVertexArray(this.vao_handle_transparent);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.vbo_handle_transparent);
        GL20.glEnableVertexAttribArray(0);
        GL20.glVertexAttribPointer(0, Vertex.positionElementCount, GL11.GL_FLOAT, false, Vertex.stride,
                Vertex.positionByteOffset);

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

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

        GL20.glEnableVertexAttribArray(3);
        GL20.glVertexAttribPointer(3, Vertex.normalElementCount, GL11.GL_FLOAT, false, Vertex.stride,
                Vertex.normalByteOffset);

        GL20.glEnableVertexAttribArray(4);
        GL20.glVertexAttribPointer(4, Vertex.aoElementCount, GL11.GL_FLOAT, false, Vertex.stride,
                Vertex.aoByteOffset);

        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.ibo_handle_transparent);

        GL30.glBindVertexArray(0);
    }

    // System.out.println("created ChunkVAO with " + this.vertexData.length + " vertices");
}

From source file:eu.over9000.veya.rendering.Shadow.java

License:Open Source License

public void renderQuad() {
    if (quadVAO == 0) {
        final float[] quadVertices = {
                // Positions        // Texture Coords
                -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
                1.0f, -1.0f, 0.0f, 1.0f, 0.0f, };
        // Setup plane VAO

        final FloatBuffer fb = BufferUtils.createFloatBuffer(quadVertices.length);

        fb.put(quadVertices);/*from ww w. j a va  2 s . c om*/
        fb.flip();

        quadVAO = GL30.glGenVertexArrays();
        quadVBO = GL15.glGenBuffers();
        GL30.glBindVertexArray(quadVAO);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, quadVBO);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, fb, GL15.GL_STATIC_DRAW);
        GL20.glEnableVertexAttribArray(0);
        GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 5 * Float.BYTES, 0);
        GL20.glEnableVertexAttribArray(1);
        GL20.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, 5 * Float.BYTES, 3 * Float.BYTES);
    }
    GL30.glBindVertexArray(quadVAO);
    GL11.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, 4);
    GL30.glBindVertexArray(0);
}

From source file:fr.guillaume.prive.viper.core.graphic.GraphicMotor.java

private static void setupObjModels() {
    ObjModel spaceShipModel = ObjLoader.loadModel(new File("resources/model/fighter.obj"));
    FloatBuffer verticesBuffer = BufferUtils
            .createFloatBuffer(Vertex.ELEMENT_COUNT * spaceShipModel.getVertices().size());
    spaceShipModel.getVertices().stream().forEach((v) -> verticesBuffer.put(v.getElements()));
    verticesBuffer.flip();/*from  w w  w. j a  v a 2s  .c  om*/

    instance.shipVaoId = GL30.glGenVertexArrays();
    GL30.glBindVertexArray(instance.shipVaoId);
    instance.shipVboId = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, instance.shipVboId);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesBuffer, GL15.GL_STATIC_DRAW);
    GL20.glVertexAttribPointer(0, Vertex.POSITION_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE, 0);
    GL20.glVertexAttribPointer(1, Vertex.NORMAL_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE,
            Vertex.NORMAL_BYTE_OFFSET);
    GL20.glVertexAttribPointer(2, Vertex.COLOR_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE,
            Vertex.COLOR_BYTE_OFFSET);
    GL20.glVertexAttribPointer(3, Vertex.TEXTURE_ELEMENT_COUNT, GL11.GL_FLOAT, false, Vertex.STRIDE,
            Vertex.TEXTURE_BYTE_OFFSET);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    GL30.glBindVertexArray(0);

    instance.shipVboiLenght = spaceShipModel.getIndices().length;
    IntBuffer indicesBuffer = BufferUtils.createIntBuffer(instance.shipVboiLenght);
    indicesBuffer.put(spaceShipModel.getIndices());
    indicesBuffer.flip();
    instance.shipVboiId = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, instance.shipVboiId);
    GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
}