Example usage for org.lwjgl.opengl ARBVertexBufferObject glBindBufferARB

List of usage examples for org.lwjgl.opengl ARBVertexBufferObject glBindBufferARB

Introduction

In this page you can find the example usage for org.lwjgl.opengl ARBVertexBufferObject glBindBufferARB.

Prototype

public static native void glBindBufferARB(@NativeType("GLenum") int target, @NativeType("GLuint") int buffer);

Source Link

Document

Binds a named buffer object.

Usage

From source file:com.a2client.corex.MeshBuffer.java

License:Open Source License

public void load(Const.BUFFER_TYPE buffer_type, MyInputStream in) {
    try {/*  w w w . j  a  v a  2 s .  c om*/
        Count = in.readInt();
        Stride = in.readInt();
        int total = Count * Stride;
        byte[] bytes = new byte[total];
        int readed = in.read(bytes);
        if (readed != total) {
            Log.error("MeshBuffer load wrong bytes, total=" + total + " readed=" + readed);
            return;
        }
        Data = ByteBuffer.allocateDirect(total);
        Data.put(bytes);
        Data.flip();
        DataPtr = MemoryUtil.getAddress(Data);

        if (buffer_type == Const.BUFFER_TYPE.btIndex) {
            DType = ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB;
            RType = Const.RES_TYPE.rtMeshIdex;

            switch (Stride) {
            case 1:
                IndexType = GL11.GL_UNSIGNED_BYTE;
                break;
            case 2:
                IndexType = GL11.GL_UNSIGNED_SHORT;
                break;
            case 3:
                IndexType = GL11.GL_FALSE;
                break;
            case 4:
                IndexType = GL11.GL_UNSIGNED_INT;
                break;
            default:
                IndexType = GL11.GL_FALSE;
            }
        } else {
            DType = ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB;
            RType = Const.RES_TYPE.rtMeshVertex;
        }

        //  ? ?  VBO
        ID = ARBVertexBufferObject.glGenBuffersARB();
        ARBVertexBufferObject.glBindBufferARB(DType, ID);
        ARBVertexBufferObject.glBufferDataARB(DType, Data, ARBBufferObject.GL_STATIC_DRAW_ARB);
        ResManager.Active.put(RType, this);
        //            Data = null;
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.a2client.corex.MeshBuffer.java

License:Open Source License

public void bind() {
    if (ResManager.Active.get(RType) != this) {
        ARBVertexBufferObject.glBindBufferARB(DType, ID);
        ResManager.Active.put(RType, this);
    }/*  w  w w. ja  v a2s.  com*/
}

From source file:com.a2client.corex.Render.java

License:Open Source License

static public void ResetBind() {
    ResManager.Active.clear();/*from w ww . j  av a 2s.com*/

    if (!multi_texture) {
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        // GL11.glBindTexture(, 0); // cube map
    } else {
        for (int i = 0; i < 16; i++) {
            ARBMultitexture.glActiveTextureARB(ARBMultitexture.GL_TEXTURE0_ARB + i);
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
            // GL11.glBindTexture(, 0); // cube map
        }
        ARBMultitexture.glActiveTextureARB(ARBMultitexture.GL_TEXTURE0_ARB);
    }

    ARBShaderObjects.glUseProgramObjectARB(0);
    ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
    ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, 0);

    // slick unbind textures
    //        TextureImpl.unbind();
}

From source file:com.badlogic.gdx.backends.lwjgl.LwjglGL11.java

License:Apache License

public void glBindBuffer(int target, int buffer) {
    ARBVertexBufferObject.glBindBufferARB(target, buffer);
}

From source file:com.mitchellkember.mycraft.GameRenderer.java

License:Open Source License

/**
 * Creates the VBO that this GameRenderer will use.
 * // ww  w  .j a v a2  s .  co m
 * @throws LWJGLException if VBOs are not supported
 */
private void initializeData() throws LWJGLException {
    if (!GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
        Mycraft.LOGGER.log(Level.SEVERE, "GL_ARB_vertex_buffer_object not supported.");
        throw new LWJGLException("GL_ARB_vertex_buffer_object not supported");
    }

    // Create it
    bufferObjectID = ARBVertexBufferObject.glGenBuffersARB();

    // Vertex Data interleaved format: XYZST
    final int position = 3;
    final int texcoords = 2;
    final int sizeOfInt = 4; // 4 bytes in an int
    final int vertexDataSize = (position + texcoords) * sizeOfInt;

    // Bind it
    ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, bufferObjectID);

    // Vertex and texture pointers
    glVertexPointer(3, GL_INT, vertexDataSize, 0);
    glTexCoordPointer(2, GL_INT, vertexDataSize, position * sizeOfInt);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}

From source file:com.owens.oobjloader.lwjgl.VBOFactory.java

License:BSD License

public static VBO build(int textureID, ArrayList<Face> triangles) {
    //   log.log(INFO, "building a vbo!");

    if (triangles.size() <= 0) {
        throw new RuntimeException("Can not build a VBO if we have no triangles with which to build it.");
    }//  w  w w .  j a v a2 s . c om

    // Now sort out the triangle/vertex indices, so we can use a
    // VertexArray in our VBO.  Note the following is NOT the most efficient way
    // to do this, but hopefully it is clear.  

    // First build a map of the unique FaceVertex objects, since Faces may share FaceVertex objects.
    // And while we're at it, assign each unique FaceVertex object an index as we run across them, storing
    // this index in the map, for use later when we build the "index" buffer that refers to the vertice buffer.
    // And lastly, keep a list of the unique vertice objects, in the order that we find them in.  
    HashMap<FaceVertex, Integer> indexMap = new HashMap<FaceVertex, Integer>();
    int nextVertexIndex = 0;
    ArrayList<FaceVertex> faceVertexList = new ArrayList<FaceVertex>();
    for (Face face : triangles) {
        for (FaceVertex vertex : face.vertices) {
            if (!indexMap.containsKey(vertex)) {
                indexMap.put(vertex, nextVertexIndex++);
                faceVertexList.add(vertex);
            }
        }
    }

    // Now build the buffers for the VBO/IBO
    int verticeAttributesCount = nextVertexIndex;
    int indicesCount = triangles.size() * 3;

    int numMIssingNormals = 0;
    int numMissingUV = 0;
    FloatBuffer verticeAttributes;
    log.log(INFO,
            "Creating buffer of size " + verticeAttributesCount + " vertices at " + VBO.ATTR_SZ_FLOATS
                    + " floats per vertice for a total of " + (verticeAttributesCount * VBO.ATTR_SZ_FLOATS)
                    + " floats.");
    verticeAttributes = BufferUtils.createFloatBuffer(verticeAttributesCount * VBO.ATTR_SZ_FLOATS);
    if (null == verticeAttributes) {
        log.log(SEVERE, "Unable to allocate verticeAttributes buffer of size "
                + (verticeAttributesCount * VBO.ATTR_SZ_FLOATS) + " floats.");
    }
    for (FaceVertex vertex : faceVertexList) {
        verticeAttributes.put(vertex.v.x);
        verticeAttributes.put(vertex.v.y);
        verticeAttributes.put(vertex.v.z);
        if (vertex.n == null) {
            // @TODO: What's a reasonable default normal?  Maybe add code later to calculate normals if not present in .obj file.
            verticeAttributes.put(1.0f);
            verticeAttributes.put(1.0f);
            verticeAttributes.put(1.0f);
            numMIssingNormals++;
        } else {
            verticeAttributes.put(vertex.n.x);
            verticeAttributes.put(vertex.n.y);
            verticeAttributes.put(vertex.n.z);
        }
        // @TODO: What's a reasonable default texture coord?  
        if (vertex.t == null) {
            //                verticeAttributes.put(0.5f);
            //                verticeAttributes.put(0.5f);
            verticeAttributes.put((float) Math.random());
            verticeAttributes.put((float) Math.random());
            numMissingUV++;
        } else {
            verticeAttributes.put(vertex.t.u);
            verticeAttributes.put(vertex.t.v);
        }
    }
    verticeAttributes.flip();

    log.log(INFO, "Had " + numMIssingNormals + " missing normals and " + numMissingUV + " missing UV coords");

    IntBuffer indices; // indices into the vertices, to specify triangles.
    indices = BufferUtils.createIntBuffer(indicesCount);
    for (Face face : triangles) {
        for (FaceVertex vertex : face.vertices) {
            int index = indexMap.get(vertex);
            indices.put(index);
        }
    }
    indices.flip();

    // Allrighty!  Now give them to OpenGL!
    IntBuffer verticeAttributesIDBuf = BufferUtils.createIntBuffer(1);

    ARBVertexBufferObject.glGenBuffersARB(verticeAttributesIDBuf);
    ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB,
            verticeAttributesIDBuf.get(0));
    ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, verticeAttributes,
            ARBVertexBufferObject.GL_STATIC_DRAW_ARB);

    IntBuffer indicesIDBuf = BufferUtils.createIntBuffer(1);
    ARBVertexBufferObject.glGenBuffersARB(indicesIDBuf);
    ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB,
            indicesIDBuf.get(0));
    ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, indices,
            ARBVertexBufferObject.GL_STATIC_DRAW_ARB);

    // our copy of the data is no longer necessary, it is safe in OpenGL.  
    // We don't need to null this out but it makes the point.
    verticeAttributes = null;
    indices = null;

    return new VBO(textureID, verticeAttributesIDBuf.get(0), indicesIDBuf.get(0), indicesCount);
}

From source file:net.adam_keenan.voxel.world.BlockVBO.java

License:Creative Commons License

public void render(BlockType type, float x, float y, float z) {
    TextureLoader.bind(Textures.SHEET);// ww w .  j av  a  2  s  . c om
    int vertID = this.blockVertexBufferID, texID;
    switch (type) {
    case DIRT:
        texID = dirtTextureID;
        break;
    case STONE:
        texID = stoneTextureID;
        break;
    case GRASS:
        texID = grassTextureID;
        break;
    case AIR:
        texID = 0;
        break;
    case FIRE:
        texID = grassTextureID;
        vertID = this.projectileVBOID;
        break;
    default:
        texID = stoneTextureID;
        break;

    }
    if (texID == 0)
        return;
    GL11.glPushMatrix();
    GL11.glTranslatef(x, y, z);

    GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
    GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);

    // Vertex array
    ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vertID);
    GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);

    // Texture array
    ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, texID);
    GL11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);

    // Index array
    ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, indexBufferID);

    // Draw 'em up
    GL12.glDrawRangeElements(GL11.GL_QUADS, 0, 6 * 4, 6 * 4, GL11.GL_UNSIGNED_INT, 0);

    GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
    GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);

    GL11.glPopMatrix();
    TextureLoader.unbind();
}

From source file:net.adam_keenan.voxel.world.BlockVBO.java

License:Creative Commons License

private void bufferData(int id, FloatBuffer buffer) {
    if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
        ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, id);
        ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, buffer,
                ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
    }/*w w  w  .  java  2  s. c  o  m*/
}

From source file:net.adam_keenan.voxel.world.BlockVBO.java

License:Creative Commons License

private void bufferElementData(int id, IntBuffer buffer) {
    if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
        ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, id);
        ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, buffer,
                ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
    }//from  w  ww .  j av a2  s.c o m
}

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

License:Apache License

public static void renderChunkMesh(Chunk chunk, MeshType meshType) {
    if (chunk.getMesh().getVBO(meshType) <= 0) {
        return;/*from w w w . j  av a  2 s .c  o m*/
    }

    /* Bind the correct texture */
    GL11.glEnable(GL11.GL_TEXTURE_2D);
    TextureStorage.getTexture("blocks").bind();

    if (meshType == MeshType.OPAQUE) {
        GL11.glDisable(GL11.GL_BLEND);
    } else if (meshType == MeshType.TRANSLUCENT) {
        GL11.glDisable(GL11.GL_CULL_FACE);
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glEnable(GL11.GL_ALPHA_TEST);
        GL11.glAlphaFunc(GL11.GL_GREATER, 0.0f);
    }

    ChunkMesh mesh = chunk.getMesh();

    /* Bind the buffer */
    ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, mesh.getVBO(meshType));

    /* Enable the different kinds of data in the buffer */
    GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
    GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
    GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);

    // System.out.println("Chunk Vertices = " + mesh.getVertexCount());

    /* Define the starting positions */
    GL11.glVertexPointer(POSITION_SIZE, GL11.GL_FLOAT, STRIDE * FLOAT_SIZE, POSITION_OFFSET * FLOAT_SIZE);
    GL11.glTexCoordPointer(TEX_COORD_SIZE, GL11.GL_FLOAT, STRIDE * FLOAT_SIZE, TEX_COORD_OFFSET * FLOAT_SIZE);
    GL11.glColorPointer(COLOR_SIZE, GL11.GL_FLOAT, STRIDE * FLOAT_SIZE, COLOR_OFFSET * FLOAT_SIZE);

    /* Draw the buffer */
    GL11.glDrawArrays(GL11.GL_QUADS, 0, mesh.getVertexCount(meshType));

    /* Unbind the buffer */
    ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, 0);

    /* Disable the different kindds of data */
    GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
    GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
    GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);

    if (meshType == MeshType.TRANSLUCENT) {
        GL11.glEnable(GL11.GL_CULL_FACE);
        GL11.glDisable(GL11.GL_ALPHA_TEST);
    }
}