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:game.level.map.TileMapGenerator.java

License:Open Source License

public static int[] CreateTemplateMesh(int startX, int startY, float startZ, int WIDTH, int HEIGHT,
        MapTile[][] tiles) {/*from   w  w  w.  j a v a 2 s . c  o  m*/
    long startTime;
    long endTime;
    startTime = System.currentTimeMillis();
    Map = DungeonGenerator.Generate(Game.MapLength, Game.MapLength);

    endTime = System.currentTimeMillis();
    System.out.println("Took " + Long.toString((endTime - startTime) / 1000) + " seconds to generate a map.");
    int count[] = CountWalls_Floors();
    int[] Handles = new int[3];
    int VBOVertexHandle = GL15.glGenBuffers();
    int VBOColorHandle = GL15.glGenBuffers();
    int VBONormalHandle = GL15.glGenBuffers();
    Random rGen = new Random();
    startTime = System.currentTimeMillis();
    FloatBuffer VertexPositionData = BufferUtils
            .createFloatBuffer((int) ((count[0] + count[1] * 6 * DIVISIONS * DIVISIONS * DIVISIONS) * 4 * 3));
    FloatBuffer VertexColorData = BufferUtils
            .createFloatBuffer((int) ((count[0] + count[1] * 6 * DIVISIONS * DIVISIONS * DIVISIONS) * 4 * 3));
    FloatBuffer VertexNormalData = BufferUtils
            .createFloatBuffer((int) ((count[0] + count[1] * 6 * DIVISIONS * DIVISIONS * DIVISIONS) * 4 * 3));
    for (float y = 0; y < Map.length; y++) {
        for (float x = 0; x < Map[0].length; x++) {
            if (Map[(int) y][(int) x] > 0) {
                tiles[(int) y][(int) x] = new WallTile();
                for (float x1 = 0; x1 < DIVISIONS; x1++) {
                    for (float y1 = 0; y1 < DIVISIONS; y1++) {
                        for (float z1 = 0; z1 < DIVISIONS; z1++) {
                            if (y1 == 0 || x1 == 0 || z1 == 0 || y1 == DIVISIONS - 1 || x1 == DIVISIONS - 1
                                    || z1 == DIVISIONS - 1) {
                                VertexPositionData
                                        .put(CreateCube(x * CUBE_LENGTH + CUBE_LENGTH / DIVISIONS * x1,
                                                y * CUBE_LENGTH + CUBE_LENGTH / DIVISIONS * y1,
                                                startZ - z1 * WALL_SCALAR, CUBE_LENGTH / DIVISIONS));
                                GenerateDefaultCubeData(VertexColorData, VertexNormalData,
                                        Map[(int) y][(int) x], rGen);
                            }
                        }
                    }
                }
            } else if (Map[(int) y][(int) x] == 0) {
                tiles[(int) y][(int) x] = new BlankTile();
            } else {
                tiles[(int) y][(int) x] = new FloorTile();
                VertexPositionData.put(CreateFloor(x * CUBE_LENGTH + CUBE_LENGTH / DIVISIONS,
                        y * CUBE_LENGTH + CUBE_LENGTH / DIVISIONS, startZ));
                GenerateDefaultFloorData(VertexColorData, VertexNormalData, Map[(int) y][(int) x], rGen);
            }

        }
    }

    // GEN CUBES HERE
    VertexPositionData.flip();
    VertexColorData.flip();
    VertexNormalData.flip();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOVertexHandle);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexPositionData, GL15.GL_STATIC_DRAW);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOColorHandle);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexColorData, GL15.GL_STATIC_DRAW);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBONormalHandle);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexNormalData, GL15.GL_STATIC_DRAW);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    Handles[0] = VBOVertexHandle;
    Handles[1] = VBOColorHandle;
    Handles[2] = VBONormalHandle;
    endTime = System.currentTimeMillis();
    System.out.println("Took " + Long.toString((endTime - startTime) / 1000) + " seconds to build a map.");
    return Handles;

}

From source file:game.level.map.TileMapGenerator.java

License:Open Source License

public static int[] RebuildMesh(int[][] map, int startZ) {
    long startTime;
    long endTime;
    Map = map;/*www .j  a v  a  2  s . c om*/

    int count[] = CountWalls_Floors();
    int[] Handles = new int[3];
    int VBOVertexHandle = GL15.glGenBuffers();
    int VBOColorHandle = GL15.glGenBuffers();
    int VBONormalHandle = GL15.glGenBuffers();
    Random rGen = new Random();
    startTime = System.currentTimeMillis();
    FloatBuffer VertexPositionData = BufferUtils
            .createFloatBuffer((int) ((count[0] + count[1] * 6 * DIVISIONS * DIVISIONS * DIVISIONS) * 4 * 3));
    FloatBuffer VertexColorData = BufferUtils
            .createFloatBuffer((int) ((count[0] + count[1] * 6 * DIVISIONS * DIVISIONS * DIVISIONS) * 4 * 3));
    FloatBuffer VertexNormalData = BufferUtils
            .createFloatBuffer((int) ((count[0] + count[1] * 6 * DIVISIONS * DIVISIONS * DIVISIONS) * 4 * 3));
    for (float y = 0; y < Map.length; y++) {
        for (float x = 0; x < Map[0].length; x++) {
            if (Map[(int) y][(int) x] > 0) {

                for (float x1 = 0; x1 < DIVISIONS; x1++) {
                    for (float y1 = 0; y1 < DIVISIONS; y1++) {
                        for (float z1 = 0; z1 < DIVISIONS; z1++) {
                            if (y1 == 0 || x1 == 0 || z1 == 0 || y1 == DIVISIONS - 1 || x1 == DIVISIONS - 1
                                    || z1 == DIVISIONS - 1) {
                                VertexPositionData
                                        .put(CreateCube(x * CUBE_LENGTH + CUBE_LENGTH / DIVISIONS * x1,
                                                y * CUBE_LENGTH + CUBE_LENGTH / DIVISIONS * y1,
                                                startZ - z1 * WALL_SCALAR, CUBE_LENGTH / DIVISIONS));
                                GenerateDefaultCubeData(VertexColorData, VertexNormalData,
                                        Map[(int) y][(int) x], rGen);
                            }
                        }
                    }
                }
            } else if (Map[(int) y][(int) x] == 0) {

            } else {

                VertexPositionData.put(CreateFloor(x * CUBE_LENGTH + CUBE_LENGTH / DIVISIONS,
                        y * CUBE_LENGTH + CUBE_LENGTH / DIVISIONS, startZ));
                GenerateDefaultFloorData(VertexColorData, VertexNormalData, Map[(int) y][(int) x], rGen);
            }

        }
    }

    // GEN CUBES HERE
    VertexPositionData.flip();
    VertexColorData.flip();
    VertexNormalData.flip();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOVertexHandle);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexPositionData, GL15.GL_STATIC_DRAW);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOColorHandle);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexColorData, GL15.GL_STATIC_DRAW);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBONormalHandle);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexNormalData, GL15.GL_STATIC_DRAW);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    Handles[0] = VBOVertexHandle;
    Handles[1] = VBOColorHandle;
    Handles[2] = VBONormalHandle;
    endTime = System.currentTimeMillis();
    System.out.println("Took " + Long.toString((endTime - startTime) / 1000) + " seconds to build a map.");
    return Handles;

}

From source file:go.graphics.swing.opengl.LWJGLDrawContext.java

License:Open Source License

@Override
public GeometryHandle generateGeometry(int bytes) {
    int vertexBufferId;
    if (canUseVBOs) {
        vertexBufferId = allocateVBO();// w  w  w  . ja  va  2s  .c om
        if (vertexBufferId == 0) {
            return null;
        }

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBufferId);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, BufferUtils.createByteBuffer(bytes), GL15.GL_DYNAMIC_DRAW);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    } else {
        ByteBuffer bb = ByteBuffer.allocateDirect(bytes);
        bb.order(ByteOrder.nativeOrder());
        vertexBufferId = geometries.size();
        geometries.add(bb);
    }
    return new LWJGLGeometryHandle(this, vertexBufferId);
}

From source file:gui.lwjgl.GUIElementLoader.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./* ww  w  .ja v  a 2  s .c o  m*/
 * @param data The data that will be stored in the attribute list.
 */
private static void storeDataInAttributeList(int vaoID, int attributeNumber, int coordinateSize, float[] 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.
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);

    // Converts float array to an instance of FloatBuffer, which can
    // be stored in a VBO.
    FloatBuffer buffer = Convert.toReadableFloatBuffer(data);

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

    // Specifies that this is for the Vertex Array.
    GL20.glVertexAttribPointer(attributeNumber, coordinateSize, GL11.GL_FLOAT, false, 0, 0);

    // Unbind the VBO.
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

    // unbind VAO so that another may be bound.
    unbindVAO();
}

From source file:io.root.gfx.glutils.GL.java

License:Apache License

public static void glBufferData(int target, Buffer data, int usage) {
    if (data == null)
        throw new NullPointerException("data provided is null");
    else 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);
}

From source file:itdelatrisu.opsu.render.CurveRenderState.java

License:Open Source License

/**
 * Do the actual drawing of the curve into the currently bound framebuffer.
 * @param color the color of the curve/*from w  w w.j a v a 2s . c  om*/
 * @param borderColor the curve border color
 * @param curve the points along the curve
 */
private void draw_curve(Color color, Color borderColor, Vec2f[] curve) {
    staticState.initGradient();
    RenderState state = startRender();
    int vtx_buf;
    // the size is: floatsize * (position + texture coordinates) * (number of cones) * (vertices in a cone)
    FloatBuffer buff = BufferUtils
            .createByteBuffer(4 * (4 + 2) * (2 * curve.length - 1) * (NewCurveStyleState.DIVIDES + 2))
            .asFloatBuffer();
    staticState.initShaderProgram();
    vtx_buf = GL15.glGenBuffers();
    for (int i = 0; i < curve.length; ++i) {
        float x = curve[i].x;
        float y = curve[i].y;
        //if (i == 0 || i == curve.length - 1){
        fillCone(buff, x, y, NewCurveStyleState.DIVIDES);
        if (i != 0) {
            float last_x = curve[i - 1].x;
            float last_y = curve[i - 1].y;
            double diff_x = x - last_x;
            double diff_y = y - last_y;
            x = (float) (x - diff_x / 2);
            y = (float) (y - diff_y / 2);
            fillCone(buff, x, y, NewCurveStyleState.DIVIDES);
        }
    }
    buff.flip();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vtx_buf);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buff, GL15.GL_STATIC_DRAW);
    GL20.glUseProgram(staticState.program);
    GL20.glEnableVertexAttribArray(staticState.attribLoc);
    GL20.glEnableVertexAttribArray(staticState.texCoordLoc);
    GL20.glUniform1i(staticState.texLoc, 0);
    GL20.glUniform3f(staticState.colLoc, color.r, color.g, color.b);
    GL20.glUniform4f(staticState.colBorderLoc, borderColor.r, borderColor.g, borderColor.b, borderColor.a);
    //stride is 6*4 for the floats (4 bytes) (u,v)(x,y,z,w)
    //2*4 is for skipping the first 2 floats (u,v)
    GL20.glVertexAttribPointer(staticState.attribLoc, 4, GL11.GL_FLOAT, false, 6 * 4, 2 * 4);
    GL20.glVertexAttribPointer(staticState.texCoordLoc, 2, GL11.GL_FLOAT, false, 6 * 4, 0);
    for (int i = 0; i < curve.length * 2 - 1; ++i)
        GL11.glDrawArrays(GL11.GL_TRIANGLE_FAN, i * (NewCurveStyleState.DIVIDES + 2),
                NewCurveStyleState.DIVIDES + 2);
    GL20.glDisableVertexAttribArray(staticState.texCoordLoc);
    GL20.glDisableVertexAttribArray(staticState.attribLoc);
    GL15.glDeleteBuffers(vtx_buf);
    endRender(state);
}

From source file:ivorius.ivtoolkit.models.data.IndexBufferObject.java

License:Apache License

/**
 * <p>//w  w w.  j a v  a2 s  .c  om
 * Sets the indices of this IndexBufferObject, discarding the old indices. The count must equal the number of indices to be
 * copied to this IndexBufferObject.
 * </p>
 * <p/>
 * <p>
 * This can be called in between calls to {@link #bind()} and {@link #unbind()}. The index data will be updated instantly.
 * </p>
 *
 * @param indices the vertex data
 * @param offset  the offset to start copying the data from
 * @param count   the number of shorts to copy
 */
public void setIndices(short[] indices, int offset, int count) {
    isDirty = true;
    buffer.clear();
    buffer.put(indices, offset, count);
    buffer.flip();
    byteBuffer.position(0);
    byteBuffer.limit(count << 1);

    if (isBound) {
        GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, byteBuffer, usage);
        isDirty = false;
    }
}

From source file:ivorius.ivtoolkit.models.data.IndexBufferObject.java

License:Apache License

/**
 * Binds this IndexBufferObject for rendering with glDrawElements.
 *//*  w ww  .j a va 2  s.  com*/
public void bind() {
    if (bufferHandle == 0)
        throw new RuntimeException("No buffer allocated!");

    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:ivorius.ivtoolkit.models.data.VertexBufferObject.java

License:Apache License

private void bufferChanged() {
    if (isBound) {
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, byteBuffer, usage);
        isDirty = false;
    }
}

From source file:jpcsp.graphics.RE.RenderingEngineLwjgl15.java

License:Open Source License

@Override
public void setBufferData(int target, int size, Buffer buffer, int usage) {
    if (buffer instanceof ByteBuffer) {
        GL15.glBufferData(bufferTargetToGL[target], getDirectBuffer(size, (ByteBuffer) buffer),
                bufferUsageToGL[usage]);
    } else if (buffer instanceof IntBuffer) {
        GL15.glBufferData(bufferTargetToGL[target], getDirectBuffer(size, (IntBuffer) buffer),
                bufferUsageToGL[usage]);
    } else if (buffer instanceof ShortBuffer) {
        GL15.glBufferData(bufferTargetToGL[target], getDirectBuffer(size, (ShortBuffer) buffer),
                bufferUsageToGL[usage]);
    } else if (buffer instanceof FloatBuffer) {
        GL15.glBufferData(bufferTargetToGL[target], getDirectBuffer(size, (FloatBuffer) buffer),
                bufferUsageToGL[usage]);
    } else if (buffer == null) {
        GL15.glBufferData(bufferTargetToGL[target], size, bufferUsageToGL[usage]);
    } else {//from  w w w . j  av a2s  .  c o m
        throw new IllegalArgumentException();
    }
}