SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package renderer;
  2.  
  3. import org.lwjgl.system.MemoryUtil;
  4.  
  5. import java.nio.FloatBuffer;
  6.  
  7. import static org.lwjgl.opengl.GL30.*;
  8. import static org.lwjgl.system.MemoryUtil.memFree;
  9.  
  10. public class Buffer {
  11.  
  12.     private final float[] vertices;
  13.     private int vaoId;
  14.     private int vboId;
  15.  
  16.     public Buffer(float[] vertices) {
  17.         this.vertices = vertices;
  18.  
  19.         init();
  20.     }
  21.  
  22.     private void init() {
  23.         FloatBuffer verticesBuffer = MemoryUtil.memAllocFloat(vertices.length);
  24.         verticesBuffer.put(vertices).flip();
  25.  
  26.         vaoId = glGenVertexArrays();
  27.         glBindVertexArray(vaoId);
  28.  
  29.         vboId = glGenBuffers();
  30.         glBindBuffer(GL_ARRAY_BUFFER, vboId);
  31.         glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);
  32.         memFree(verticesBuffer);
  33.  
  34.         glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
  35.  
  36.         glBindBuffer(GL_ARRAY_BUFFER, 0);
  37.         glBindVertexArray(0);
  38.  
  39.         if (verticesBuffer != null) {
  40.             MemoryUtil.memFree(verticesBuffer);
  41.         }
  42.     }
  43.  
  44.     public void destroy() {
  45.         glDisableVertexAttribArray(0);
  46.         glBindBuffer(GL_ARRAY_BUFFER, 0);
  47.         glDeleteBuffers(vboId);
  48.         glBindVertexArray(0);
  49.         glDeleteVertexArrays(vaoId);
  50.     }
  51.  
  52.     public void render(ShaderProgram program) {
  53.         program.bind();
  54.         glBindVertexArray(vaoId);
  55.         glEnableVertexAttribArray(0);
  56.         glDrawArrays(GL_TRIANGLES, 0, 3);
  57.         glDisableVertexAttribArray(0);
  58.         glBindVertexArray(0);
  59.         program.unbind();
  60.     }
  61. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top