Example usage for org.lwjgl.opengl GL15 GL_BUFFER_SIZE

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

Introduction

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

Prototype

int GL_BUFFER_SIZE

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

Click Source Link

Document

Accepted by the pname parameter of GetBufferParameteriv.

Usage

From source file:voxicity.ChunkNode.java

License:Open Source License

public void clean() {
    last_update = Time.get_time_ms();

    vert_buf = GL15.glGenBuffers();/*from w w w.jav  a 2s.c  o m*/
    tex_buf = GL15.glGenBuffers();

    if (shader_prog == 0)
        create_shader_prog();

    int offset = 0;

    verts.clear();
    tex_coords.clear();
    batches.clear();

    Map<Integer, IntBuffer> id_ind = new HashMap<Integer, IntBuffer>();

    BlockChunkLoc loc = new BlockChunkLoc(0, 0, 0, chunk);

    // Iterate through all blocks in the chunk
    for (int i = 0; i < Constants.Chunk.side_length; i++)
        for (int j = 0; j < Constants.Chunk.side_length; j++)
            for (int k = 0; k < Constants.Chunk.side_length; k++) {
                // Get block id
                int id = chunk.get_block(i, j, k);

                // If air, do nothing, next block
                if (id != Constants.Blocks.air) {
                    loc.x = i;
                    loc.y = j;
                    loc.z = k;

                    Block b = Blocks.get(id);

                    // If culled, do nothing, next block
                    if (!cull(loc) && !chunk_edge_cull(loc)) {
                        // Get the vertices for this block and put them in the verts buffer
                        verts.put(Coord.offset_coords(b.vertices(),
                                new Vector3f(pos.x + loc.x, pos.y + loc.y, pos.z + loc.z)));

                        // Get the texture coords for this block and put them in the tex_coords buffer
                        tex_coords.put(b.texture_coords());

                        // Look up the texture for these vertices
                        int tex_id = TextureManager.get_texture(b.texture_string());

                        // Look up the index buffer for this texture and create it if needed
                        if (!id_ind.containsKey(tex_id))
                            id_ind.put(tex_id, BufferUtils.createIntBuffer(24 * Constants.Chunk.block_number));

                        // Get the index buffer for this texture
                        IntBuffer ind_buf = id_ind.get(tex_id);

                        // Get the indices for this block's vertices and put them in the ind_buf buffer after offsetting them
                        IntBuffer block_indices = b.indices();
                        while (block_indices.hasRemaining())
                            ind_buf.put(block_indices.get() + offset);

                        // Increase the offset by the number of indices
                        offset += block_indices.position();
                    }
                }
            }

    if (verts.position() == 0) {
        empty = true;
        dirty = false;
        return;
    }

    verts.limit(verts.position()).rewind();
    tex_coords.limit(tex_coords.position()).rewind();
    System.out.println(verts.limit() + " " + tex_coords.limit());

    AABB box = new AABB(Constants.Chunk.side_length, Constants.Chunk.side_length, Constants.Chunk.side_length);
    box.center_on(pos.x + box.dimensions().x, pos.y + box.dimensions().y, pos.z + box.dimensions().z);

    // Pass the buffer to a VBO
    System.out.println("Binding vertex buffer");
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vert_buf);
    Util.checkGLError();
    System.out.println("Setting buffer data");
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verts, GL15.GL_STATIC_DRAW);
    Util.checkGLError();
    System.out.println("Vertex buffer data set");

    System.out.println("Size of vertex buffer is: "
            + GL15.glGetBufferParameter(GL15.GL_ARRAY_BUFFER, GL15.GL_BUFFER_SIZE));

    // Pass the buffer to a VBO
    System.out.println("Binding tex coord buffer");
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, tex_buf);
    Util.checkGLError();
    System.out.println("Setting buffer data");
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, tex_coords, GL15.GL_STATIC_DRAW);
    Util.checkGLError();

    System.out.println("Size of tex coord buffer is: "
            + GL15.glGetBufferParameter(GL15.GL_ARRAY_BUFFER, GL15.GL_BUFFER_SIZE));

    for (Map.Entry<Integer, IntBuffer> entry : id_ind.entrySet()) {
        entry.getValue().limit(entry.getValue().position()).rewind();

        IntBuffer ibo = BufferUtils.createIntBuffer(1);
        GL15.glGenBuffers(ibo);

        if (ibo.get(0) == 0)
            System.out.println("Could not generate buffer object!");

        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo.get(0));
        Util.checkGLError();
        GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, entry.getValue(), GL15.GL_STATIC_DRAW);
        Util.checkGLError();
        System.out.println("Size of index buffer is: "
                + GL15.glGetBufferParameter(GL15.GL_ELEMENT_ARRAY_BUFFER, GL15.GL_BUFFER_SIZE) + " with "
                + entry.getValue().limit() + " indices");

        System.out.println(
                "Creating batch: " + entry.getKey() + " " + ibo.get(0) + " " + entry.getValue().limit());
        batches.add(new Batch(entry.getKey(), ibo.get(0), entry.getValue().limit(), shader_prog, tex_buf,
                vert_buf, box));

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

    }

    empty = false;
    dirty = false;
}