@Override public void draw() { checkCreated(); // Bind the vao and enable all attributes GL30.glBindVertexArray(id); for (int i = 0; i < attributeBufferIDs.length; i++) { GL20.glEnableVertexAttribArray(i); } // Bind the indices buffer GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBufferID); // Draw all indices with the provided mode GL11.glDrawElements( drawingMode.getGLConstant(), indicesCount, GL11.GL_UNSIGNED_INT, indicesOffset * DataType.INT.getByteSize()); // Unbind the indices buffer GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); // Disable all attributes and unbind the vao for (int i = 0; i < attributeBufferIDs.length; i++) { GL20.glDisableVertexAttribArray(i); } GL30.glBindVertexArray(0); // Check for errors LWJGLUtil.checkForGLError(); }
private int createTubeVbo(int tube, Color color) { float[] c = new float[] {color.getBlue() / 255f, color.getGreen() / 255f, color.getRed() / 255f}; totalNumVerts[tube] = tubes[tube].indeces.length; IntBuffer buf = BufferUtils.createIntBuffer(1); GL15.glGenBuffers(buf); int vbo = buf.get(); FloatBuffer data = BufferUtils.createFloatBuffer(tubes[tube].indeces.length * 9); for (int i = 0; i < tubes[tube].indeces.length; i++) { data.put(c); Vector3D vertex = tubes[tube].vertices[tubes[tube].indeces[i]]; Vector3D normal = tubes[tube].normals[tubes[tube].indeces[i]]; float[] vertexf = new float[] {vertex.x, vertex.y, vertex.z}; float[] normalf = new float[] {normal.x, normal.y, normal.z}; data.put(vertexf); data.put(normalf); } data.rewind(); int bytesPerFloat = Float.SIZE / Byte.SIZE; int numBytes = data.capacity() * bytesPerFloat; GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, data, GL15.GL_STATIC_DRAW); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); return vbo; }
private void bindIndicesBuffer(int[] indices) { int vboID = GL15.glGenBuffers(); vbos.add(vboID); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboID); IntBuffer buffer = storeDataInIntBuffer(indices); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); }
public void destroyOpenGL() { // Delete the shaders GL20.glUseProgram(0); GL20.glDetachShader(pId, vsId); GL20.glDetachShader(pId, fsId); GL20.glDeleteShader(vsId); GL20.glDeleteShader(fsId); GL20.glDeleteProgram(pId); // Select the VAO GL30.glBindVertexArray(vaoId); // Disable the VBO index from the VAO attributes list GL20.glDisableVertexAttribArray(0); GL20.glDisableVertexAttribArray(1); // Delete the vertex VBO GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); GL15.glDeleteBuffers(vboId); // Delete the index VBO GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); GL15.glDeleteBuffers(vboiId); // Delete the VAO GL30.glBindVertexArray(0); GL30.glDeleteVertexArrays(vaoId); Display.destroy(); }
private void storeDataInAttributeList(int attributeNumber, int attributeSize, float[] data) { int vboID = GL15.glGenBuffers(); vbos.add(vboID); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID); FloatBuffer buffer = storeDataInFloatBuffer(data); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); GL20.glVertexAttribPointer(attributeNumber, attributeSize, GL11.GL_FLOAT, false, 0, 0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); }
public void glBufferData(int target, int size, Buffer data, int usage) { if (data == null) throw new GdxRuntimeException("Using null for the data not possible, blame LWJGL"); 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); }
public void setupVIBO() { ByteBuffer indexBuffer = BufferUtils.createByteBuffer(indexElementCount * getNumObjects()); for (int i = 0; i < getNumObjects(); ++i) { indexBuffer.put(Rectangle.getOrder(4 * i)); } indexBuffer.flip(); setVIBO(GL15.glGenBuffers()); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, getVIBO()); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL15.GL_DYNAMIC_DRAW); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); }
public void render() { if (((ViewerContainer3D) v.getContainer()).getPbuffer() != currentPbuffer) { for (int i = 0; i < vbo.length; i++) { vbo[i] = -1; selectedVbo[i] = -1; } currentPbuffer = ((ViewerContainer3D) v.getContainer()).getPbuffer(); } for (int i = 0; i < tubes.length; i++) { if (hidden[i]) continue; int vbo; if (!selected[i]) { if (this.vbo[i] == -1) { Color c = colors[i]; GL15.glDeleteBuffers(this.vbo[i]); this.vbo[i] = createTubeVbo(i, colors[i]); } vbo = this.vbo[i]; } else { if (this.selectedVbo[i] == -1) { Color c = selectedColors[i]; GL15.glDeleteBuffers(this.selectedVbo[i]); this.selectedVbo[i] = createTubeVbo(i, selectedColors[i]); } vbo = this.selectedVbo[i]; } GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY); GL11.glEnableClientState(GL11.GL_COLOR_ARRAY); GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY); GL11.glColorMaterial(GL11.GL_FRONT_AND_BACK, GL11.GL_AMBIENT_AND_DIFFUSE); GL11.glEnable(GL11.GL_COLOR_MATERIAL); GL11.glColorPointer(3, GL11.GL_FLOAT, vertexStride, colorPointer); GL11.glVertexPointer(3, GL11.GL_FLOAT, vertexStride, vertexPointer); GL11.glNormalPointer(GL11.GL_FLOAT, vertexStride, normalPointer); GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, totalNumVerts[i]); GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY); GL11.glDisableClientState(GL11.GL_COLOR_ARRAY); GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY); GL11.glDisable(GL11.GL_COLOR_MATERIAL); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); } }
public void RebuildMesh(float startX, float startY, float startZ) { VBOColorHandle = GL15.glGenBuffers(); VBOVertexHandle = GL15.glGenBuffers(); // Using 6 * 4 * 3 because 6 faces * 4 pts per face * 3 coords per point FloatBuffer VertexPositionData = BufferUtils.createFloatBuffer((CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE) * 6 * 4 * 3); FloatBuffer VertexColorData = BufferUtils.createFloatBuffer((CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE) * 6 * 4 * 3); for (float x = 0; x < CHUNK_SIZE; x += 1) { for (float y = 0; y < CHUNK_SIZE; y += 1) { for (float z = 0; z < CHUNK_SIZE; z += 1) { VertexPositionData.put( CreateCube( (float) startX + x * CUBE_LENGTH, (float) startY + y * CUBE_LENGTH, (float) startZ + z * CUBE_LENGTH)); VertexColorData.put(CreateCubeVertexCol(GetCubeColor(Blocks[(int) x][(int) y][(int) z]))); } } } VertexColorData.flip(); VertexPositionData.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); }
public void setupQuad() { // We'll define our quad using 4 vertices of the custom 'Vertex' class Vertex v0 = new Vertex(); v0.setXYZ(-0.5f, 0.5f, 0f); v0.setRGB(1, 0, 0); Vertex v1 = new Vertex(); v1.setXYZ(-0.5f, -0.5f, 0f); v1.setRGB(0, 1, 0); Vertex v2 = new Vertex(); v2.setXYZ(0.5f, -0.5f, 0f); v2.setRGB(0, 0, 1); Vertex v3 = new Vertex(); v3.setXYZ(0.5f, 0.5f, 0f); v3.setRGB(1, 1, 1); Vertex[] vertices = new Vertex[] {v0, v1, v2, v3}; // Put each 'Vertex' in one FloatBuffer FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertices.length * Vertex.elementCount); for (int i = 0; i < vertices.length; i++) { verticesBuffer.put(vertices[i].getXYZW()); verticesBuffer.put(vertices[i].getRGBA()); } verticesBuffer.flip(); // OpenGL expects to draw vertices in counter clockwise order by default byte[] indices = { 0, 1, 2, 2, 3, 0 }; indicesCount = indices.length; ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indicesCount); indicesBuffer.put(indices); indicesBuffer.flip(); // Create a new Vertex Array Object in memory and select it (bind) vaoId = GL30.glGenVertexArrays(); GL30.glBindVertexArray(vaoId); // Create a new Vertex Buffer Object in memory and select it (bind) vboId = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesBuffer, GL15.GL_STATIC_DRAW); // Put the positions in attribute list 0 GL20.glVertexAttribPointer(0, 4, GL11.GL_FLOAT, false, Vertex.sizeInBytes, 0); // Put the colors in attribute list 1 GL20.glVertexAttribPointer( 1, 4, GL11.GL_FLOAT, false, Vertex.sizeInBytes, Vertex.elementBytes * 4); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); // Deselect (bind to 0) the VAO GL30.glBindVertexArray(0); // Create a new VBO for the indices and select it (bind) - INDICES vboiId = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); }
@Override public void create() { if (isCreated()) { throw new IllegalStateException("Vertex array has already been created"); } if (vertexData == null) { throw new IllegalStateException("Vertex data has not been set"); } // Generate and bind the vao id = GL30.glGenVertexArrays(); GL30.glBindVertexArray(id); // Generate, bind and fill the indices vbo then unbind indicesBufferID = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBufferID); GL15.glBufferData( GL15.GL_ELEMENT_ARRAY_BUFFER, vertexData.getIndicesBuffer(), GL15.GL_STATIC_DRAW); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); // Save the count of indices to draw indicesCountCache = vertexData.getIndicesCount(); resetIndicesCountAndOffset(); // Create the map for attribute index to buffer ID attributeBufferIDs = new int[vertexData.getAttributeCount()]; // For each attribute, generate, bind and fill the vbo, then setup the attribute in the vao and // save the buffer ID for the index for (int i = 0; i < vertexData.getAttributeCount(); i++) { final VertexAttribute attribute = vertexData.getAttribute(i); final int bufferID = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, bufferID); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, attribute.getData(), GL15.GL_STATIC_DRAW); attributeBufferIDs[i] = bufferID; // Three ways to interpret integer data if (attribute.getType().isInteger() && attribute.getUploadMode() == UploadMode.KEEP_INT) { // Directly as an int GL30.glVertexAttribIPointer( i, attribute.getSize(), attribute.getType().getGLConstant(), 0, 0); } else { // Or as a float, normalized or not GL20.glVertexAttribPointer( i, attribute.getSize(), attribute.getType().getGLConstant(), attribute.getUploadMode().normalize(), 0, 0); } } // Unbind the vbo and vao GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); GL30.glBindVertexArray(0); // Update state super.create(); // Check for errors LWJGLUtil.checkForGLError(); }
private void render_batch(ChunkNode.Batch batch) { if (!camera.collides(batch.box)) return; Renderer.draw_calls++; // Use the shader the batch needs GL20.glUseProgram(batch.shader); // Bind VBO to vertex pointer GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, batch.vert_buf); GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0); // Bind the texture coord VBO to texture pointer GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, batch.tex_buf); GL11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0); // Bind the texture for this batch GL11.glBindTexture(GL11.GL_TEXTURE_2D, batch.tex); // Bind index array GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, batch.indices); // Draw the block GL12.glDrawRangeElements( GL11.GL_QUADS, 0, Constants.Chunk.block_number * 24 - 1, batch.num_elements, GL11.GL_UNSIGNED_INT, 0); Renderer.batch_draw_calls++; Renderer.quads += batch.num_elements; // Unbind the texture GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); // Unbind all buffers GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); GL20.glUseProgram(0); }
private void renderVbo(int id) { if (lock.tryLock()) { try { if (vertexBuffers[id] <= 0 || disposed) { return; } glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, idxBuffers[id]); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBuffers[id]); glVertexPointer(SIZE_VERTEX, GL11.GL_FLOAT, STRIDE, OFFSET_VERTEX); GL13.glClientActiveTexture(GL13.GL_TEXTURE0); glTexCoordPointer(SIZE_TEX0, GL11.GL_FLOAT, STRIDE, OFFSET_TEX_0); GL13.glClientActiveTexture(GL13.GL_TEXTURE1); glTexCoordPointer(SIZE_TEX1, GL11.GL_FLOAT, STRIDE, OFFSET_TEX_1); glColorPointer(SIZE_COLOR * 4, GL11.GL_UNSIGNED_BYTE, STRIDE, OFFSET_COLOR); glNormalPointer(GL11.GL_FLOAT, STRIDE, OFFSET_NORMAL); GL11.glDrawElements(GL11.GL_TRIANGLES, vertexCount[id], GL11.GL_UNSIGNED_INT, 0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); } finally { lock.unlock(); } } }
public void cleanUP() { for (int vao : vaos) { GL30.glDeleteVertexArrays(vao); } for (int vbo : vbos) { GL15.glDeleteBuffers(vbo); } for (int texture : textures) { GL11.glDeleteTextures(texture); } }
@Override public void destroy() { checkCreated(); // Unbind any bound buffer GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); // Unbind and delete the indices buffer GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); GL15.glDeleteBuffers(indicesBufferID); // Bind the vao for deletion GL30.glBindVertexArray(id); // Disable each attribute and delete its buffer for (int i = 0; i < attributeBufferIDs.length; i++) { GL20.glDisableVertexAttribArray(i); GL15.glDeleteBuffers(attributeBufferIDs[i]); } // Unbind the vao and delete it GL30.glBindVertexArray(0); GL30.glDeleteVertexArrays(id); super.destroy(); // Check for errors LWJGLUtil.checkForGLError(); }
public void loopCycle() { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); GL20.glUseProgram(pId); // Bind to the VAO that has all the information about the vertices GL30.glBindVertexArray(vaoId); GL20.glEnableVertexAttribArray(0); GL20.glEnableVertexAttribArray(1); // Bind to the index VBO that has all the information about the order of the vertices GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId); // Draw the vertices GL11.glDrawElements(GL11.GL_TRIANGLES, indicesCount, GL11.GL_UNSIGNED_BYTE, 0); // Put everything back to default (deselect) GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); GL20.glDisableVertexAttribArray(0); GL20.glDisableVertexAttribArray(1); GL30.glBindVertexArray(0); GL20.glUseProgram(0); }
@Override public ByteBuffer map(int off, int len) { if (off < 0 || len <= 0) { throw new IllegalArgumentException(); } final int end = off + len; if (end > bufferSizes[currentBufferIndex]) { bufferSizes[currentBufferIndex] = end; glBufferData(glTarget, bufferSizes[currentBufferIndex], glUsage); } ByteBuffer mapped; if (GLContext.getCapabilities().OpenGL30) { int flags = GL30.GL_MAP_WRITE_BIT | GL30.GL_MAP_UNSYNCHRONIZED_BIT; // | GL30.GL_MAP_INVALIDATE_RANGE_BIT; mapped = GL30.glMapBufferRange(glTarget, off, len, flags, null); } else if (GLContext.getCapabilities().GL_ARB_map_buffer_range) { int flags = ARBMapBufferRange.GL_MAP_WRITE_BIT | ARBMapBufferRange .GL_MAP_UNSYNCHRONIZED_BIT; // | ARBMapBufferRange.GL_MAP_INVALIDATE_RANGE_BIT; mapped = ARBMapBufferRange.glMapBufferRange(glTarget, off, len, flags, null); } else { mapped = GL15.glMapBuffer(glTarget, GL15.GL_WRITE_ONLY, null); } if (mapped == null) { throw new IllegalStateException( "mapped buffer is null: length=" + len + ", requestedSize=" + requestedSize + ", allocatedSize=" + allocatedSize); } return mapped; }
public void setupVBO() { setVBO(GL15.glGenBuffers()); }
public void glBindBuffer(int target, int buffer) { GL15.glBindBuffer(target, buffer); }
public boolean glIsBuffer(int buffer) { return GL15.glIsBuffer(buffer); }
public void glGetBufferParameteriv(int target, int pname, IntBuffer params) { GL15.glGetBufferParameter(target, pname, params); }
public void glGenBuffers(int n, IntBuffer buffers) { GL15.glGenBuffers(buffers); }
public void glDeleteBuffers(int n, IntBuffer buffers) { GL15.glDeleteBuffers(buffers); }
public void unbindVIBO() { GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); }
public void bindVIBO() { GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, getVIBO()); }
public void unbindVBO() { GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); }
public void bindVBO() { GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, getVBO()); }