List of usage examples for org.lwjgl.opengl ARBVertexBufferObject glBufferDataARB
public static void glBufferDataARB(@NativeType("GLenum") int target, @NativeType("void const *") double[] data, @NativeType("GLenum") int usage)
From source file:com.a2client.corex.MeshBuffer.java
License:Open Source License
public void load(Const.BUFFER_TYPE buffer_type, MyInputStream in) { try {/*ww w .ja v a 2s .c o m*/ 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.mitchellkember.mycraft.GameRenderer.java
License:Open Source License
/** * Updates the VBO when the a {@code chunk} in the GameState has changed. * //from w ww . j a v a 2s . c om * @param chunk the chunk that has changed */ @Override public void gameStateChunkChanged(Chunk chunk) { byte[][][] data = chunk.getData(); IntBuffer vertexData = BufferUtils.createIntBuffer(70000); try { for (int x = 0; x < 16; x++) { for (int y = 0; y < 16; y++) { for (int z = 0; z > -16; z--) { if (data[x][y][-z] != 0) vertexData.put(cubeData(x, y, z)); } } } } catch (BufferOverflowException boe1) { // Try again with more memory try { vertexData = BufferUtils.createIntBuffer(150000); for (int x = 0; x < 16; x++) { for (int y = 0; y < 16; y++) { for (int z = 0; z > -16; z--) { if (data[x][y][-z] != 0) vertexData.put(cubeData(x, y, z)); } } } } catch (BufferOverflowException boe2) { // Bail out System.out.println("Oops! Mycraft has crashed!"); Mycraft.LOGGER.log(Level.SEVERE, boe2.toString(), boe2); System.exit(1); } } numVerts = vertexData.position() / 5; vertexData.flip(); // Upload data ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vertexData, ARBVertexBufferObject.GL_DYNAMIC_DRAW_ARB); }
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."); }//from w w w. j a v a 2 s . co m // 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
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); }/*from w w w. j av a2 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 w w.ja v a 2 s . c o m*/ }
From source file:zildo.fwk.opengl.Utils.java
License:Open Source License
public static void bufferData(int id, Buffer buffer) { ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, id); if (buffer instanceof FloatBuffer) { ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, (FloatBuffer) buffer, ARBVertexBufferObject.GL_STREAM_DRAW_ARB); } else if (buffer instanceof IntBuffer) { ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, (IntBuffer) buffer, ARBVertexBufferObject.GL_STREAM_DRAW_ARB); }//w ww . j av a 2s .co m }
From source file:zildo.platform.opengl.GLUtils.java
License:Open Source License
public static void bufferData(int id, Buffer buffer, boolean statically) { int mode = ARBVertexBufferObject.GL_DYNAMIC_DRAW_ARB; if (statically) { mode = ARBVertexBufferObject.GL_STATIC_DRAW_ARB; }//from w w w . jav a 2 s. c o m ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, id); if (buffer instanceof FloatBuffer) { ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, (FloatBuffer) buffer, mode); } else if (buffer instanceof IntBuffer) { ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, (IntBuffer) buffer, mode); } else if (buffer instanceof ShortBuffer) { ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, (ShortBuffer) buffer, mode); } }