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:loon.core.graphics.opengl.LWjglGL11.java

License:Apache License

public void glBufferData(int target, int size, Buffer data, int usage) {
    if (data instanceof ByteBuffer)
        GL15.glBufferData(target, (ByteBuffer) data, usage);
    else if (data instanceof IntBuffer)
        GL15.glBufferData(target, (IntBuffer) data, usage);
    else if (data instanceof FloatBuffer)
        GL15.glBufferData(target, (FloatBuffer) data, usage);
    else if (data instanceof DoubleBuffer)
        GL15.glBufferData(target, (DoubleBuffer) data, usage);
    else if (data instanceof ShortBuffer)
        GL15.glBufferData(target, (ShortBuffer) data, usage);
    else if (data == null)
        GL15.glBufferData(target, size, usage);
}

From source file:main.java.com.YeAJG.game.entity.Entity.java

License:Open Source License

private void SetupEntity(Vector3f[] vertex, Vector3f[] color, Vector2f[] uv) {

    // We'll define our quad using 4 vertices of the custom 'TexturedVertex' class
    vertices = new VertexData[vertex.length];

    for (int i = 0; i < vertex.length; i++) {
        vertices[i] = new VertexData();
        vertices[i].setXYZ(vertex[i].x, vertex[i].y, vertex[i].z);
        vertices[i].setRGBA(color[i].x, color[i].y, color[i].z, 1.0f);
        vertices[i].setST(uv[i].x, uv[i].y);
    }//from  w w w. j  av a2s  . c om

    // Put each 'Vertex' in one FloatBuffer
    verticesByteBuffer = BufferUtils.createByteBuffer(vertices.length * VertexData.stride);
    FloatBuffer verticesFloatBuffer = verticesByteBuffer.asFloatBuffer();
    for (int i = 0; i < vertices.length; i++) {
        // Add position, color and texture floats to the buffer
        verticesFloatBuffer.put(vertices[i].getElements());
    }
    verticesFloatBuffer.flip();

    // OpenGL expects to draw vertices in counter clockwise order by default
    // TODO: Move this to Setup.
    byte[] indices = { 0, 1, 2, 2, 3, 0 };

    indicesCount = indices.length;
    ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indicesCount);
    indicesBuffer.put(indices);
    indicesBuffer.flip();

    // Create a new Vertex Array Object in memory and select it (bind)
    vaoId = GL30.glGenVertexArrays();
    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, VertexData.positionElementCount, GL11.GL_FLOAT, false, VertexData.stride,
            VertexData.positionByteOffset);
    // Put the color components in attribute list 1
    GL20.glVertexAttribPointer(1, VertexData.colorElementCount, GL11.GL_FLOAT, false, VertexData.stride,
            VertexData.colorByteOffset);
    // Put the texture coordinates in attribute list 2
    GL20.glVertexAttribPointer(2, VertexData.textureElementCount, GL11.GL_FLOAT, false, VertexData.stride,
            VertexData.textureByteOffset);

    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);

    Game.cameraPos = new Vector3f(0, 0, -1);

    Game.exitOnGLError("setupQuad");
}

From source file:main.Loader.java

private void storeDataInAttributeList(int attributeNumber, int coordinateSize, float[] data) {
    int vboID = GL15.glGenBuffers();
    vbos.add(vboID);//from  ww  w. ja  v  a  2s .  c om
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
    FloatBuffer buffer = storeDataInFloatBuffer(data);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
    GL20.glVertexAttribPointer(attributeNumber, coordinateSize, GL11.GL_FLOAT, false, 0, 0);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}

From source file:main.Loader.java

private void bindIndicesBuffer(int[] indices) {
    int vboID = GL15.glGenBuffers();
    vbos.add(vboID);//  w ww.  j  a v  a 2s  .  c  o  m
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboID);
    IntBuffer buffer = storeDataInIntBuffer(indices);
    GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
}

From source file:me.sunchiro.game.engine.gl.Graphic.java

License:Open Source License

public void resizeBuffer() {
    vertByteBuff = BufferUtils.createByteBuffer(vertexCount * Vertex.stride);
    ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indicesCount);
    FloatBuffer vertFloatBuff = vertByteBuff.asFloatBuffer();
    for (Drawable object : objects) {
        object.store(vertFloatBuff);/*from   ww  w .  j  a  va  2 s . c om*/
        indicesBuffer.put(object.getIndices());
    }
    for (Drawable object : orthoObjects) {
        object.store(vertFloatBuff);
        indicesBuffer.put(object.getIndices());
    }
    indicesBuffer.flip();
    vertFloatBuff.flip();
    GL30.glBindVertexArray(vaoId);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertFloatBuff, GL15.GL_STREAM_DRAW);
    GL20.glVertexAttribPointer(0, 4, GL11.GL_FLOAT, false, Vertex.stride, 0);
    GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, Vertex.stride, 16);
    GL20.glVertexAttribPointer(2, 2, GL11.GL_FLOAT, false, Vertex.stride, 32);

    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    GL30.glBindVertexArray(0);

    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:me.thehutch.fusion.engine.render.opengl.gl30.OpenGL30VertexArray.java

License:Open Source License

@Override
public void setIndices(TIntList indices) {
    ensureCreated("VertexArray must be created to set the indices.");

    // Put the indices into an IntBuffer
    final IntBuffer buffer = BufferUtils.createIntBuffer(indices.size());
    indices.forEach((int i) -> {
        buffer.put(i);/*from   w  w  w .j a  va 2  s. c o  m*/
        return true;
    });
    buffer.flip();

    // Bind the index buffer
    GL15.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    // Set the indices data to the vao from the vertex data
    GL15.glBufferData(GL_ELEMENT_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
    // Unbind the index buffer
    GL15.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    // Set the draw count
    this.drawCount = indices.size();

    // Check for errors
    RenderUtil.checkGLError();
}

From source file:me.thehutch.fusion.engine.render.opengl.gl30.OpenGL30VertexArray.java

License:Open Source License

@Override
public void addAttribute(int index, int size, TFloatList data) {
    ensureCreated("VertexArray must be created to add an attribute.");

    if (index > GL11.glGetInteger(GL_MAX_VERTEX_ATTRIBS)) {
        throw new IllegalArgumentException("Vertex attribute index exceeds maximum vertex attribute index.");
    }/*from www.  j a v  a2s  .  com*/
    // Put the indices into an FloatBuffer
    final FloatBuffer buffer = BufferUtils.createFloatBuffer(data.size());
    data.forEach((float f) -> {
        buffer.put(f);
        return true;
    });
    buffer.flip();

    // Bind the VAO
    GL30.glBindVertexArray(vao);
    // Generate and bind the attribute buffer
    final int id = GL15.glGenBuffers();
    GL15.glBindBuffer(GL_ARRAY_BUFFER, id);
    // Set the attribute data
    GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
    // Enable the vertex attribute
    GL20.glEnableVertexAttribArray(index);
    // Set the vertex attribute settings
    GL20.glVertexAttribPointer(index, size, DataType.FLOAT.getGLConstant(), false, 0, 0L);
    // Disable the vertex attribute
    GL20.glDisableVertexAttribArray(index);
    // Unbind the attribute buffer
    GL15.glBindBuffer(GL_ARRAY_BUFFER, 0);
    // Unbind the VAO
    GL30.glBindVertexArray(vao);

    // Add the buffer id to the attributes list
    this.attributes.insert(index, id);

    // Check for errors
    RenderUtil.checkGLError();
}

From source file:model.ModelMD2.java

public void compileVBO() {
    frame_ids = new int[header.num_frames];
    ArrayList<float[]> data = new ArrayList<float[]>();
    for (int currentFrame = 0; currentFrame < header.num_frames; currentFrame++) {
        MD2_Frame frame = frames[currentFrame];// Current frame
        for (int currentTriangle = 0; currentTriangle < header.num_tris; currentTriangle++) {
            for (int j = 0; j < 3; j++) {
                float[] temp = new float[8];
                //Extract position from vertex array using triangles vertex index
                MD2_Vertex vertex = frame.vertices[triangles[currentTriangle].vertexIndex[j]];
                //For MD2 format/exporter Z is up, swap Z and Y for OGL
                temp[0] = vertex.v[1];//from   w ww  .ja v  a  2s  .  co  m
                temp[1] = vertex.v[2];
                temp[2] = vertex.v[0];

                //Extract texture coords from st array using triangle texture index
                float s = (float) (textureCoords[triangles[currentTriangle].textureIndex[j]].s / 256f);
                float t = (float) (textureCoords[triangles[currentTriangle].textureIndex[j]].t / 256f);
                temp[3] = s;
                temp[4] = t;

                //Normals, why hardcoded?
                int index = frame.vertices[triangles[currentTriangle].vertexIndex[j]].lightNormalIndex;
                //Some models seems to use a different normal array and doesn't work with Quake II normals
                if (index < normals.length) {
                    float[] n = normals[index];
                    temp[5] = n[0];
                    temp[6] = n[2];
                    temp[7] = n[1];
                }

                data.add(temp);//Add vertex data
            }
        }
        //Put data into floatbuffer
        ByteBuffer verticesByteBuffer = BufferUtils.createByteBuffer(data.size() * 32);
        FloatBuffer verticesFloatBuffer = verticesByteBuffer.asFloatBuffer();
        for (int i = 0; i < data.size(); i++) {
            // Add position, normal and texture floats to the buffer
            verticesFloatBuffer.put(data.get(i));
        }
        verticesFloatBuffer.flip();

        //Put data into its vbo
        int id = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, id);
        {
            GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesFloatBuffer, GL15.GL_STATIC_DRAW);
        }
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

        //Clear data for next frame
        data.clear();

        //Save frame id
        frame_ids[currentFrame] = id;
    }
    GLUtil.cerror(getClass().getName() + " compileVBO");
}

From source file:net.kubin.rendering.ChunkMeshBuilder.java

License:Apache License

public static void generateChunkMesh(Chunk chunk, MeshType meshType) {
    if (DEBUG) {/*w  ww  .  j a  va 2s. c  o m*/
        System.out.println("Building " + meshType.name() + " Mesh for " + chunk.toString() + "...");
    }

    /* Make sure there are no list edits anymore */
    chunk.performListChanges();

    ChunkMesh mesh = chunk.getMesh();
    mesh.destroy(meshType);

    /* Compute vertex count */
    int vertexCount = chunk.getVertexCount(meshType);

    if (DEBUG) {
        System.out.println("\tVertex Count = " + vertexCount);
    }

    /*
     * If there are no faces visible yet (because of generating busy), don't
     * create a buffer
     */
    if (vertexCount == 0) {
        return;
    }

    mesh.setVertexCount(meshType, vertexCount);

    /* Create a buffer */
    int vbo = BufferManager.getInstance().createBuffer();
    mesh.setVBO(meshType, vbo);

    /* Bind the buffer */
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);

    /* Allocate size for the buffer */
    int size = vertexCount * STRIDE * FLOAT_SIZE;
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, size, GL15.GL_STATIC_DRAW);

    if (DEBUG) {
        System.out.println(
                "\tCreate VBO: " + vbo + " with size = " + size + " (ERROR: " + GL11.glGetError() + ")");
    }

    /* Get the native buffer to write to */
    ByteBuffer byteBuffer = GL15.glMapBuffer(GL15.GL_ARRAY_BUFFER, GL15.GL_WRITE_ONLY, size, null);

    if (byteBuffer == null) {
        System.out.println("\tCouldn't create a native VBO!: GL Error Code = " + GL11.glGetError());

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

        mesh.destroy(meshType);
        Thread.dumpStack();
        mesh.setVBO(meshType, -1);
        mesh.setVertexCount(meshType, 0);

        return;
    }

    FloatBuffer vertexBuffer = byteBuffer.asFloatBuffer();

    /* Store all vertices in the buffer */
    USED_SIZE = 0;

    /* Local temporary variables, used to speed up */
    IntList blockList = chunk.getVisibleBlocks();
    int blockIndex = -1;
    byte BlockDef = 0;
    boolean special = false;
    Vec3i vec = new Vec3i();
    BlockDef type;
    Block block = null;
    LightBuffer lightBuffer = chunk.getLightBuffer();
    byte faceMask = 0;

    /* Iterate over the blocks */
    for (int i = 0; i < blockList.size(); ++i) {
        blockIndex = blockList.get(i);
        BlockDef = chunk.getChunkData().getBlockDef(blockIndex);

        if (BlockDef == 0) {
            continue;
        }

        special = chunk.getChunkData().isSpecial(blockIndex);
        type = _blockManager.getBlockDef(BlockDef);

        if ((meshType == MeshType.OPAQUE && !type.isTranslucent() && type.hasNormalAABB())
                || (meshType == MeshType.TRANSLUCENT && (type.isTranslucent() || !type.hasNormalAABB()))) {

            ChunkData.indexToPosition(blockIndex, vec);

            /* Build the light buffer */
            vec.setX(vec.x() + chunk.getAbsoluteX());
            vec.setZ(vec.z() + chunk.getAbsoluteZ());

            lightBuffer.setReferencePoint(vec.x(), vec.y(), vec.z());

            if (special) {
                block = chunk.getChunkData().getSpecialBlock(blockIndex);

                if (block.isVisible()) {
                    block.storeInVBO(vertexBuffer, lightBuffer);
                }
            } else {
                faceMask = chunk.getChunkData().getFaceMask(blockIndex);
                type.getDefaultBlockBrush().setFaceMask(faceMask);
                type.getBrush().storeInVBO(vertexBuffer, vec.x() + 0.5f, vec.y() + 0.5f, vec.z() + 0.5f,
                        lightBuffer);

            }
        }
    }

    /* Perform a check */
    if (USED_SIZE != STRIDE * FLOAT_SIZE * mesh.getVertexCount(meshType)) {
        System.out.println("\t[WARNING!]: Used size = " + USED_SIZE);
        System.out.println("\t[WARNING!]: Vertex count = " + USED_SIZE / STRIDE / FLOAT_SIZE);
        mesh.setVertexCount(meshType, USED_SIZE / STRIDE / FLOAT_SIZE);
    }

    byteBuffer.flip();

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

}

From source file:net.neilcsmith.praxis.video.opengl.internal.IndexBufferObject.java

License:Apache License

/** Binds this IndexBufferObject for rendering with glDrawElements. */
public void bind() {
    if (bufferHandle == 0) {
        throw new RuntimeException("buuh");
    }/*from w w w .jav a  2s  .  co m*/

    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, bufferHandle);
    if (isDirty) {
        byteBuffer.limit(buffer.limit() * 2);
        GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, byteBuffer, usage);
        isDirty = false;
    }

    isBound = true;
}