Example usage for org.lwjgl.opengl GL15 glBindBuffer

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

Introduction

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

Prototype

public static void glBindBuffer(@NativeType("GLenum") int target, @NativeType("GLuint") int buffer) 

Source Link

Document

Binds a named buffer object.

Usage

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

License:Open Source License

private synchronized void render() {
    GL11.glClearColor(bgColor.x, bgColor.y, bgColor.z, 0);
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    GL20.glUseProgram(shader.getPID());/*from   ww  w  . j a  va 2 s.  c o  m*/
    //

    GL13.glActiveTexture(GL13.GL_TEXTURE0);
    GL30.glBindVertexArray(vaoId);

    GL20.glEnableVertexAttribArray(0);
    GL20.glEnableVertexAttribArray(1);
    GL20.glEnableVertexAttribArray(2);

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texManager.getTexture(tid));
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId);
    long offset = 0;
    int shift = 0;
    GL11.glAlphaFunc(GL11.GL_GREATER, 0.4f);
    GL11.glDisable(GL11.GL_CULL_FACE);
    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

    for (Drawable object : objects) {
        Matrix4f finalMVP = new Matrix4f(mvpMat);
        Matrix4f modelMat = new Matrix4f();
        Matrix4f scaler = new Matrix4f();
        scaler.scale(object.scale * allScale);
        modelMat.translate(object.translation);
        modelMat.rotateXYZ(object.rotation.x, object.rotation.y, object.rotation.z);
        finalMVP.mul(modelMat);
        finalMVP.mul(scaler);
        finalMVP.get(0, matBuff);
        if (object.isChar) {
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, texManager.getTexture(tid_charmap));
        }
        GL20.glUniformMatrix4fv(shader.getMVPLocation(), false, matBuff);
        GL20.glUniform1i(shader.getInverseLocation(), object.inverseAlpha ? 1 : 0);
        if (object.isVisible())
            GL32.glDrawElementsBaseVertex(GL11.GL_TRIANGLES, object.getIndices().length, GL11.GL_UNSIGNED_BYTE,
                    offset, shift);

        offset += object.getIndices().length;
        shift += object.getVertex().length;
        if (object.isChar) {
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, texManager.getTexture(tid));
        }
    }
    render2d(offset, shift);
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
    GL20.glDisableVertexAttribArray(0);
    GL20.glDisableVertexAttribArray(1);
    GL20.glDisableVertexAttribArray(2);
    GL30.glBindVertexArray(0);
    //
    GL20.glUseProgram(0);
    glfwSwapBuffers(window);
    glfwPollEvents();

}

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  av  a 2  s  . c  om
        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  w w  w  .  j av  a  2s  .c o  m
    // 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:me.thehutch.fusion.engine.render.opengl.gl30.OpenGL30VertexArray.java

License:Open Source License

@Override
public void draw() {
    ensureCreated("VertexArray must be created to draw.");

    //Bind the vertex array object
    GL30.glBindVertexArray(vao);/*  ww  w  . j  ava  2  s .c om*/

    // Enable the vertex attributes
    for (int i = 0; i < attributes.size(); ++i) {
        GL20.glEnableVertexAttribArray(i);
    }

    // Bind the index buffer
    GL15.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    // Draw the vertex elements
    GL11.glDrawElements(GL_TRIANGLES, drawCount, DataType.UNSIGNED_INT.getGLConstant(), 0L);
    // Unbind the index buffer
    GL15.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    // Disable the vertex attributes
    for (int i = 0; i < attributes.size(); ++i) {
        GL20.glDisableVertexAttribArray(i);
    }

    // Unbind the vertex array object
    GL30.glBindVertexArray(0);
}

From source file:model.ModelMD2.java

@Override
public void destroy() {
    //Delete VAO//from w ww. java 2  s.co  m
    GL30.glBindVertexArray(vao_id);
    {
        for (int i = 0; i < 6; i++) {
            GL20.glDisableVertexAttribArray(i);
        }

        //Delete VBOs
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        for (int i = 0; i < frame_ids.length; i++) {
            GL15.glDeleteBuffers(frame_ids[i]);
        }
    }
    GL30.glBindVertexArray(0);
    GL30.glDeleteVertexArrays(vao_id);
    GL11.glDeleteTextures(tex_id);
}

From source file:model.ModelMD2.java

@Override
public void draw(float frame, float[] vpMatrix, float[] matrix, RenderEngine engine) {
    if (frame < 0 || frame > header.num_frames - 1)
        return;/*  w w  w. j  a  va 2s  . co m*/

    //Select current frame and next
    int frame_0 = frame_ids[(int) Math.floor(frame)];

    int frame_1 = frame_ids[(int) Math.min(Math.ceil(frame), header.num_frames - 1)];
    //Upload frame interpolation

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, tex_id);

    ShaderUtils.useProgram(shader_id);
    {
        //Upload uniform values
        ShaderUtils.setUniformMatrix4(shader_id, "viewprojMatrix", vpMatrix);
        ShaderUtils.setUniformMatrix4(shader_id, "modelMatrix", matrix);
        ShaderUtils.setUniformVar(shader_id, "frame_interpolated", (float) (frame - Math.floor(frame)));
        //ShaderUtils.setUniformVar(shader_id, "cameraDir", engine.camera.yaw, engine.camera.pitch, engine.camera.roll);
        //Bind frames to VAO
        GL30.glBindVertexArray(vao_id);
        {

            GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, frame_0);//Bind frame 0
            {
                GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 32, 0);
                GL20.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, 32, 12);
                GL20.glVertexAttribPointer(2, 3, GL11.GL_FLOAT, false, 32, 20);
            }
            GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

            GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, frame_1);//Bind frame 1
            {
                GL20.glVertexAttribPointer(3, 3, GL11.GL_FLOAT, false, 32, 0);
                GL20.glVertexAttribPointer(4, 2, GL11.GL_FLOAT, false, 32, 12);
                GL20.glVertexAttribPointer(5, 3, GL11.GL_FLOAT, false, 32, 20);
            }

            GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

            //Enable attribs and render
            for (int i = 0; i < 6; i++) {
                GL20.glEnableVertexAttribArray(i);
            }
            {
                GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, header.num_tris * 3);
            }
            for (int i = 0; i < 6; i++) {
                GL20.glDisableVertexAttribArray(i);
            }

        }
        GL30.glBindVertexArray(0);
    }
    ShaderUtils.useProgram(0);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
}

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];/*  w  ww.  j av a2  s. c  o  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) {/*from  w  w w.  j a  v  a2s. 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  ww.  j a  v  a2 s .  c om*/

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

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

License:Apache License

/** Disposes this IndexBufferObject and all its associated OpenGL resources. */
public void dispose() {
    tmpHandle.clear();/*from   ww w  .ja  va2s.c o  m*/
    tmpHandle.put(bufferHandle);
    tmpHandle.flip();
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
    GL15.glDeleteBuffers(tmpHandle);
    bufferHandle = 0;

}