List of usage examples for org.lwjgl.opengl GL15 glBindBuffer
public static void glBindBuffer(@NativeType("GLenum") int target, @NativeType("GLuint") int buffer)
From source file:voxicity.ChunkNode.java
License:Open Source License
public void clean() { last_update = Time.get_time_ms(); vert_buf = GL15.glGenBuffers();// w ww .j a va2s .c o m tex_buf = GL15.glGenBuffers(); if (shader_prog == 0) create_shader_prog(); int offset = 0; verts.clear(); tex_coords.clear(); batches.clear(); Map<Integer, IntBuffer> id_ind = new HashMap<Integer, IntBuffer>(); BlockChunkLoc loc = new BlockChunkLoc(0, 0, 0, chunk); // Iterate through all blocks in the chunk for (int i = 0; i < Constants.Chunk.side_length; i++) for (int j = 0; j < Constants.Chunk.side_length; j++) for (int k = 0; k < Constants.Chunk.side_length; k++) { // Get block id int id = chunk.get_block(i, j, k); // If air, do nothing, next block if (id != Constants.Blocks.air) { loc.x = i; loc.y = j; loc.z = k; Block b = Blocks.get(id); // If culled, do nothing, next block if (!cull(loc) && !chunk_edge_cull(loc)) { // Get the vertices for this block and put them in the verts buffer verts.put(Coord.offset_coords(b.vertices(), new Vector3f(pos.x + loc.x, pos.y + loc.y, pos.z + loc.z))); // Get the texture coords for this block and put them in the tex_coords buffer tex_coords.put(b.texture_coords()); // Look up the texture for these vertices int tex_id = TextureManager.get_texture(b.texture_string()); // Look up the index buffer for this texture and create it if needed if (!id_ind.containsKey(tex_id)) id_ind.put(tex_id, BufferUtils.createIntBuffer(24 * Constants.Chunk.block_number)); // Get the index buffer for this texture IntBuffer ind_buf = id_ind.get(tex_id); // Get the indices for this block's vertices and put them in the ind_buf buffer after offsetting them IntBuffer block_indices = b.indices(); while (block_indices.hasRemaining()) ind_buf.put(block_indices.get() + offset); // Increase the offset by the number of indices offset += block_indices.position(); } } } if (verts.position() == 0) { empty = true; dirty = false; return; } verts.limit(verts.position()).rewind(); tex_coords.limit(tex_coords.position()).rewind(); System.out.println(verts.limit() + " " + tex_coords.limit()); AABB box = new AABB(Constants.Chunk.side_length, Constants.Chunk.side_length, Constants.Chunk.side_length); box.center_on(pos.x + box.dimensions().x, pos.y + box.dimensions().y, pos.z + box.dimensions().z); // Pass the buffer to a VBO System.out.println("Binding vertex buffer"); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vert_buf); Util.checkGLError(); System.out.println("Setting buffer data"); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verts, GL15.GL_STATIC_DRAW); Util.checkGLError(); System.out.println("Vertex buffer data set"); System.out.println("Size of vertex buffer is: " + GL15.glGetBufferParameter(GL15.GL_ARRAY_BUFFER, GL15.GL_BUFFER_SIZE)); // Pass the buffer to a VBO System.out.println("Binding tex coord buffer"); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, tex_buf); Util.checkGLError(); System.out.println("Setting buffer data"); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, tex_coords, GL15.GL_STATIC_DRAW); Util.checkGLError(); System.out.println("Size of tex coord buffer is: " + GL15.glGetBufferParameter(GL15.GL_ARRAY_BUFFER, GL15.GL_BUFFER_SIZE)); for (Map.Entry<Integer, IntBuffer> entry : id_ind.entrySet()) { entry.getValue().limit(entry.getValue().position()).rewind(); IntBuffer ibo = BufferUtils.createIntBuffer(1); GL15.glGenBuffers(ibo); if (ibo.get(0) == 0) System.out.println("Could not generate buffer object!"); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo.get(0)); Util.checkGLError(); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, entry.getValue(), GL15.GL_STATIC_DRAW); Util.checkGLError(); System.out.println("Size of index buffer is: " + GL15.glGetBufferParameter(GL15.GL_ELEMENT_ARRAY_BUFFER, GL15.GL_BUFFER_SIZE) + " with " + entry.getValue().limit() + " indices"); System.out.println( "Creating batch: " + entry.getKey() + " " + ibo.get(0) + " " + entry.getValue().limit()); batches.add(new Batch(entry.getKey(), ibo.get(0), entry.getValue().limit(), shader_prog, tex_buf, vert_buf, box)); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); } empty = false; dirty = false; }
From source file:voxicity.ChunkNode.java
License:Open Source License
public void render(Frustum camera) { if (empty)/*from www.jav a 2 s . c o m*/ return; AABB chunk_box = new AABB(Constants.Chunk.side_length, Constants.Chunk.side_length, Constants.Chunk.side_length); chunk_box.center_on(chunk.get_x() + chunk_box.dimensions().x, chunk.get_y() + chunk_box.dimensions().y, chunk.get_z() + chunk_box.dimensions().z); if (!camera.collides(chunk_box)) return; Renderer.draw_calls++; if (shader_prog != 0) GL20.glUseProgram(shader_prog); // Bind VBO to vertex pointer GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vert_buf); GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0); // Bind the texture coord VBO to texture pointer GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, tex_buf); GL11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0); int tex_bak = GL11.glGetInteger(GL11.GL_TEXTURE_BINDING_2D); for (Batch batch : batches) { // 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, tex_bak); // Unbind all buffers GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); // Disable the shader once more if (shader_prog != 0) GL20.glUseProgram(0); }
From source file:voxicity.Renderer.java
License:Open Source License
private void render_batch(ChunkNode.Batch batch) { if (!camera.collides(batch.box)) return;/* w w w . j av a2 s.c o m*/ 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); }
From source file:wrapper.vbo.Vbo.java
License:Open Source License
public void create(Vertex2d[] coordinates) { vertex = BufferUtils.createFloatBuffer(coordinates.length * 2); texture = BufferUtils.createFloatBuffer(coordinates.length * 2); vertcount = coordinates.length;/*from ww w. j a va2 s . co m*/ maxlength = vertcount; for (int i = 0; i < coordinates.length; i++) { vertex.put(coordinates[i].x); vertex.put(coordinates[i].y); texture.put(coordinates[i].u); texture.put(coordinates[i].v); } vertid = GL15.glGenBuffers(); texcoordid = GL15.glGenBuffers(); vertex.rewind(); texture.rewind(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertid); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertex, GL15.GL_DYNAMIC_DRAW); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, texcoordid); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, texture, GL15.GL_DYNAMIC_DRAW); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); }
From source file:wrapper.vbo.Vbo.java
License:Open Source License
public void create(Vertex3d[] coordinates) { vertex = BufferUtils.createFloatBuffer(coordinates.length * 3); texture = BufferUtils.createFloatBuffer(coordinates.length * 2); vertcount = coordinates.length;//from w ww . j a v a2s.c om maxlength = vertcount; for (int i = 0; i < coordinates.length; i++) { vertex.put(coordinates[i].x); vertex.put(coordinates[i].y); vertex.put(coordinates[i].z); texture.put(coordinates[i].u); texture.put(coordinates[i].v); } vertid = GL15.glGenBuffers(); texcoordid = GL15.glGenBuffers(); vertex.rewind(); texture.rewind(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertid); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertex, GL15.GL_DYNAMIC_DRAW); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, texcoordid); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, texture, GL15.GL_DYNAMIC_DRAW); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); }
From source file:wrapper.vbo.Vbo.java
License:Open Source License
public void create(ProgressiveBuffer[] b) {// coordinates are NOT bound by // this//from w ww. ja v a 2s .c om vertex = b[0].get_data(); texture = b[1].get_data(); vertcount = b[0].get_data().limit(); maxlength = vertcount; vertex.rewind(); texture.rewind(); vertid = GL15.glGenBuffers(); texcoordid = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertid); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertex, GL15.GL_DYNAMIC_DRAW); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, texcoordid); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, texture, GL15.GL_DYNAMIC_DRAW); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); }
From source file:wrapper.vbo.Vbo.java
License:Open Source License
public void create(int size) { maxlength = size;/*from w w w . ja va 2 s . co m*/ vertex = BufferUtils.createFloatBuffer(size); texture = BufferUtils.createFloatBuffer(size); vertcount = 0; vertex.rewind(); texture.rewind(); vertid = GL15.glGenBuffers(); texcoordid = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertid); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertex, GL15.GL_DYNAMIC_DRAW); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, texcoordid); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, texture, GL15.GL_DYNAMIC_DRAW); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); }
From source file:wrapper.vbo.Vbo.java
License:Open Source License
public void edit_data(Vertex2d[] coordinates) { vertex = BufferUtils.createFloatBuffer(coordinates.length * 2); texture = BufferUtils.createFloatBuffer(coordinates.length * 2); vertcount = coordinates.length;// w w w .j a v a 2 s .c o m for (int i = 0; i < coordinates.length; i++) { vertex.put(coordinates[i].x); vertex.put(coordinates[i].y); texture.put(coordinates[i].u); texture.put(coordinates[i].v); } vertex.rewind(); texture.rewind(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertid); GL15.glBufferSubData(GL15.GL_ARRAY_BUFFER, 0, vertex); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, texcoordid); GL15.glBufferSubData(GL15.GL_ARRAY_BUFFER, 0, texture); }
From source file:wrapper.vbo.Vbo.java
License:Open Source License
public void edit_data(Vertex3d[] coordinates) { vertex = BufferUtils.createFloatBuffer(coordinates.length * 3); texture = BufferUtils.createFloatBuffer(coordinates.length * 2); vertcount = coordinates.length;// ww w . j a va2 s . co m for (int i = 0; i < coordinates.length; i++) { vertex.put(coordinates[i].x); vertex.put(coordinates[i].y); vertex.put(coordinates[i].z); texture.put(coordinates[i].u); texture.put(coordinates[i].v); } vertex.rewind(); texture.rewind(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertid); GL15.glBufferSubData(GL15.GL_ARRAY_BUFFER, 0, vertex); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, texcoordid); GL15.glBufferSubData(GL15.GL_ARRAY_BUFFER, 0, texture); }
From source file:wrapper.vbo.Vbo.java
License:Open Source License
public void edit_data(ProgressiveBuffer[] b) { if (b[0].get_data() == null || b[1].get_data() == null) { return;// ww w.j a v a2 s . c o m } vertex.clear(); texture.clear(); vertex = BufferUtils.createFloatBuffer(b[0].get_data().capacity()); texture = BufferUtils.createFloatBuffer(b[1].get_data().capacity()); b[0].get_data().rewind(); b[1].get_data().rewind(); vertex = b[0].get_data(); texture = b[1].get_data(); vertcount = b[0].get_data().capacity(); vertex.rewind(); texture.rewind(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertid); GL15.glBufferSubData(GL15.GL_ARRAY_BUFFER, 0, vertex); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, texcoordid); GL15.glBufferSubData(GL15.GL_ARRAY_BUFFER, 0, texture);// doesnt // reassing // meaning its // more // efficient, // only for // removing // objects }