Ejemplo n.º 1
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);
  }
Ejemplo n.º 3
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();
 }
Ejemplo n.º 4
0
 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);
 }
Ejemplo n.º 5
0
  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;
  }
Ejemplo n.º 6
0
 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);
 }
Ejemplo n.º 7
0
 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);
 }
Ejemplo n.º 8
0
 public void glGenBuffers(int n, IntBuffer buffers) {
   GL15.glGenBuffers(buffers);
 }
Ejemplo n.º 9
0
 public void setupVBO() {
   setVBO(GL15.glGenBuffers());
 }