public void init() {
    float[] vertex = genVertex(n, radius);
    GLES20.glGenBuffers(vbo.length, vbo, 0);
    vertexBuffer.position(0);
    vertexBuffer.put(vertex).position(0);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbo[0]);
    GLES20.glBufferData(
        GLES20.GL_ARRAY_BUFFER,
        BYTE_PER_FLOAT * vertex.length,
        vertexBuffer,
        GLES20.GL_STATIC_DRAW);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

    short[][] index = genIndex(n);
    GLES20.glGenBuffers(ibo.length, ibo, 0);
    for (int i = 0; i < 3; i++) {
      indexBuffer.position(0);
      indexBuffer.put(index[i]).position(0);
      GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, ibo[i]);
      GLES20.glBufferData(
          GLES20.GL_ELEMENT_ARRAY_BUFFER,
          BYTE_PER_SHORT * index[i].length,
          indexBuffer,
          GLES20.GL_STATIC_DRAW);
      GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
    }

    GLES20.glUniform4fv(mColorHandle, 1, color, 0);
  }
Example #2
0
  public void GenerateVertexArray(
      float[] arrayBuffer, int bufferSize, int valuesPerElement, int attributeNumber) {
    FloatBuffer.allocate(arrayBuffer.length);
    FloatBuffer bufferData =
        ByteBuffer.allocateDirect(bufferSize).order(ByteOrder.nativeOrder()).asFloatBuffer();
    bufferData.put(arrayBuffer).position(0);

    IntBuffer bufferID = (IntBuffer.allocate(1));
    bufferID.position(0);

    GLES20.glGenBuffers(1, bufferID);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, bufferID.get(0));
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, bufferSize, bufferData, GLES20.GL_STATIC_DRAW);

    GLES20.glVertexAttribPointer(
        attributeNumber, // attribute
        valuesPerElement, // size
        GLES20.GL_FLOAT, // type
        false, // normalized?
        valuesPerElement * FLOAT_SIZE_BYTES, // stride
        0 // array buffer offset
        );

    vertexArrayList.add(new GLVertexArrayObject(bufferID.get(), attributeNumber, valuesPerElement));
  }
Example #3
0
 @Override
 public void updateVertexBuffer(float[] object) {
   object = ExtendedGlSurfaceView.allocFloatBuffer((float[]) object);
   GLES20.glGenBuffers(this.mVertexBuffers.length, this.mVertexBuffers, 0);
   GLES20.glBindBuffer(34962, this.mVertexBuffers[0]);
   GLES20.glBufferData(34962, object.limit() * 4, (Buffer) object, 35048);
   GLES20.glBindBuffer(34962, 0);
 }
Example #4
0
  public void init(float[] alldata) {

    FloatBuffer everythingBuffer =
        ByteBuffer.allocateDirect(alldata.length * BYTES_PER_FLOAT)
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer();
    everythingBuffer.put(alldata).position(0);

    ByteBuffer dlb =
        ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            drawOrder.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    ShortBuffer drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder).position(0);

    // First, generate as many buffers as we need.
    // This will give us the OpenGL handles for these buffers.
    GLES20.glGenBuffers(2, buffers, 0);

    // Bind to the buffer. Future commands will affect this buffer
    // specifically.
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, buffers[1]);

    // Transfer data from client memory to the buffer.
    // We can release the client memory after this call.
    GLES20.glBufferData(
        GLES20.GL_ARRAY_BUFFER,
        everythingBuffer.capacity() * BYTES_PER_FLOAT,
        everythingBuffer,
        GLES20.GL_STATIC_DRAW);

    GLES20.glBufferData(
        GLES20.GL_ELEMENT_ARRAY_BUFFER,
        drawListBuffer.capacity() * 2,
        drawListBuffer,
        GLES20.GL_STATIC_DRAW);

    // IMPORTANT: Unbind from the buffer when we're done with it.
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
  }
Example #5
0
  private void bindBuffers() {
    int[] vboIDs = {0, 0};
    GLES20.glGenBuffers(2, vboIDs, 0);
    vertexBufferID = vboIDs[0];
    indexBufferID = vboIDs[1];
    //        Log.d("Mesh Buffer IDs", "{" + vertexBufferID + ", " + indexBufferID + "}");
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBufferID);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBufferID);

    verticesLength = vertices.length;
    FloatBuffer vertexBuffer =
        ByteBuffer.allocateDirect(vertices.length * 4)
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);
    GLES20.glBufferData(
        GLES20.GL_ARRAY_BUFFER, vertexBuffer.capacity() * 4, vertexBuffer, GLES20.GL_STATIC_DRAW);

    indicesLength = indices.length;
    ShortBuffer indexBuffer =
        ByteBuffer.allocateDirect(indices.length * 2)
            .order(ByteOrder.nativeOrder())
            .asShortBuffer();
    indexBuffer.put(indices);
    indexBuffer.position(0);
    GLES20.glBufferData(
        GLES20.GL_ELEMENT_ARRAY_BUFFER,
        indexBuffer.capacity() * 2,
        indexBuffer,
        GLES20.GL_STATIC_DRAW);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);

    vertexBuffer.limit(0);
    indexBuffer.limit(0);

    vertices = null;
    indices = null;
  }
Example #6
0
  public void setOGLResources() {
    // mBufferHandles[0] is the handle to the vertex buffer
    // mBufferHandles[1] is the handle to the element buffer
    GLES20.glGenBuffers(2, this.mBufferHandles, 0);

    // Bind the vertex buffer generated earlier, and put the buffered vertex data on the graphics
    // card
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.mBufferHandles[0]);
    GLES20.glBufferData(
        GLES20.GL_ARRAY_BUFFER,
        4 * this.mVertices.capacity(),
        this.mVertices,
        GLES20.GL_STATIC_DRAW);

    // Bind the element buffer generated earlier and put the buffered data on the graphics card
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, this.mBufferHandles[1]);
    GLES20.glBufferData(
        GLES20.GL_ELEMENT_ARRAY_BUFFER,
        4 * this.mElements.capacity(),
        this.mElements,
        GLES20.GL_STATIC_DRAW);
  }
Example #7
0
  // Deleta o antigo VBO e carrega 2 VBO separados. Um para o buffer de vertices e o outro para
  // o buffer de cor.
  // Esse metodo so e chamado caso tenha ocorrido GL_INVALID_OPERATION na chamada do comando
  // glDrawArrays no metodo draw().
  private void loadVBO2() {
    if (problemBufferInterleaved) {
      GLES20.glDeleteBuffers(VBO.length, VBO, 0);
      VBO = new int[2];
      GLES20.glGenBuffers(2, VBO, 0);
      GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, VBO[0]);
      GLES20.glBufferData(
          GLES20.GL_ARRAY_BUFFER,
          vertexBuffer.array().length,
          vertexBuffer.position(0),
          GLES20.GL_STATIC_DRAW);

      GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, VBO[1]);
      GLES20.glBufferData(
          GLES20.GL_ARRAY_BUFFER,
          colorBuffer.array().length,
          colorBuffer.position(0),
          GLES20.GL_STATIC_DRAW);
      GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
      buffer.limit(0);
      buffer = null;
    }
  }
 private int initFloatBuffer(float[] data) {
   int[] buffer = new int[1];
   GLES20.glGenBuffers(1, buffer, 0);
   int pointer = buffer[0];
   if (pointer == -1) {
     System.out.println("Error: Couldn't create buffer");
   } else {
     System.out.println("Succesfully created buffer to " + pointer);
   }
   GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, pointer);
   ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.length * 4); // one float size is 4 bytes
   byteBuffer.order(ByteOrder.nativeOrder()); // byte order must be native
   FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
   floatBuffer.put(data);
   floatBuffer.flip();
   GLES20.glBufferData(
       GLES20.GL_ARRAY_BUFFER, data.length * 4, floatBuffer, GLES20.GL_STATIC_DRAW);
   return pointer;
 }
Example #9
0
  private void loadVBO() {
    GLES20.glGenBuffers(1, VBO, 0);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, VBO[0]);
    // GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER,((totalPoints*3)*4 +
    // (totalPoints*3)*4),null,GLES20.GL_STATIC_DRAW);
    GLES20.glBufferData(
        GLES20.GL_ARRAY_BUFFER, sizeBuffer, buffer.position(0), GLES20.GL_STATIC_DRAW);

    if (GLES20.glGetError() == GLES20.GL_OUT_OF_MEMORY) {
      GLES20.glDeleteBuffers(1, VBO, 0);
      Log.e(TAG, "No memory!!");
      buffer.limit(0);
      buffer = null;
      System.gc();
      // Toast.makeText(Global.getContext(),"No Memory!!",Toast.LENGTH_LONG).show();
      System.exit(1);
    }
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
  }
Example #10
0
  Map() {
    // Initialize Map
    mMap = new ETile[MAP_SIZE_X][MAP_SIZE_Y][MAP_SIZE_Z]; // [X][Y][Z]
    // Construct Buffers
    mPlaneVertBuffer =
        ByteBuffer.allocateDirect(indexCount * mPositionDataSize * 4)
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer();
    mTexVertBuffer =
        ByteBuffer.allocateDirect(indexCount * mPositionDataSize * 4)
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer();

    // Create Vertices
    mTexVertBuffer.position(0);
    mPlaneVertBuffer.position(0);
    for (int y = MAP_SIZE_Y - 1; y >= 0; y--) {
      for (int x = 0; x < MAP_SIZE_X; x++) {
        final float[] vertData = {
          // X,Y
          x + 1,
          y + 1, // UR
          x,
          y + 1, // UL
          x,
          y, // LL
          x,
          y, // LL
          x + 1,
          y, // LR
          x + 1,
          y + 1 // UR		
        };

        // Build Texture Mapping
        float tx = 1 * Tile.TILE_SIZE;
        float ty = 0 * Tile.TILE_SIZE;
        final float[] texMapData = {
          // X,Y
          tx + Tile.TILE_SIZE,
          ty, // UR
          tx,
          ty, // UL
          tx,
          ty + Tile.TILE_SIZE, // LL
          tx,
          ty + Tile.TILE_SIZE, // LL
          tx + Tile.TILE_SIZE,
          ty + Tile.TILE_SIZE, // LR
          tx + Tile.TILE_SIZE,
          ty // UR
        };

        mPlaneVertBuffer.put(vertData);
        mTexVertBuffer.put(texMapData);
      }
    }

    // Generate handles for the buffers
    GLES20.glGenBuffers(2, mBufferHandles, 0);

    // Bind and Load buffers
    // Texture VBO
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, getTexVBO());
    mTexVertBuffer.position(0);
    GLES20.glBufferData(
        GLES20.GL_ARRAY_BUFFER,
        mTexVertBuffer.capacity() * 4,
        mTexVertBuffer,
        GLES20.GL_STATIC_DRAW);
    // Plane VBO
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, getPlaneVBO());
    mPlaneVertBuffer.position(0);
    GLES20.glBufferData(
        GLES20.GL_ARRAY_BUFFER,
        mPlaneVertBuffer.capacity() * 4,
        mPlaneVertBuffer,
        GLES20.GL_STATIC_DRAW);
    // Unbind Buffers
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
  }
Example #11
0
 @Override
 public void glBufferData(int target, long size, Buffer data, int usage) {
   GLES20.glBufferData(target, (int) size, data, usage);
 }
 @Override
 protected void onBufferData() {
   GLES20.glBufferData(
       GLES20.GL_ARRAY_BUFFER, this.mByteBuffer.limit(), this.mByteBuffer, this.mUsage);
 }
 public DistortionMesh(
     final Distortion distortionRed,
     final Distortion distortionGreen,
     final Distortion distortionBlue,
     final float screenWidth,
     final float screenHeight,
     final float xEyeOffsetScreen,
     final float yEyeOffsetScreen,
     final float textureWidth,
     final float textureHeight,
     final float xEyeOffsetTexture,
     final float yEyeOffsetTexture,
     final float viewportXTexture,
     final float viewportYTexture,
     final float viewportWidthTexture,
     final float viewportHeightTexture) {
   super();
   this.mArrayBufferId = -1;
   this.mElementBufferId = -1;
   final float[] vertexData = new float[14400];
   short vertexOffset = 0;
   for (int row = 0; row < ROWS; ++row) {
     for (int col = 0; col < COLS; ++col) {
       final float uTextureBlue =
           col / 39.0f * (viewportWidthTexture / textureWidth) + viewportXTexture / textureWidth;
       final float vTextureBlue =
           row / 39.0f * (viewportHeightTexture / textureHeight)
               + viewportYTexture / textureHeight;
       final float xTexture = uTextureBlue * textureWidth - xEyeOffsetTexture;
       final float yTexture = vTextureBlue * textureHeight - yEyeOffsetTexture;
       final float rTexture = (float) Math.sqrt(xTexture * xTexture + yTexture * yTexture);
       final float textureToScreenBlue =
           (rTexture > 0.0f) ? (distortionBlue.distortInverse(rTexture) / rTexture) : 1.0f;
       final float xScreen = xTexture * textureToScreenBlue;
       final float yScreen = yTexture * textureToScreenBlue;
       final float uScreen = (xScreen + xEyeOffsetScreen) / screenWidth;
       final float vScreen = (yScreen + yEyeOffsetScreen) / screenHeight;
       final float rScreen = rTexture * textureToScreenBlue;
       final float screenToTextureGreen =
           (rScreen > 0.0f) ? distortionGreen.distortionFactor(rScreen) : 1.0f;
       final float uTextureGreen =
           (xScreen * screenToTextureGreen + xEyeOffsetTexture) / textureWidth;
       final float vTextureGreen =
           (yScreen * screenToTextureGreen + yEyeOffsetTexture) / textureHeight;
       final float screenToTextureRed =
           (rScreen > 0.0f) ? distortionRed.distortionFactor(rScreen) : 1.0f;
       final float uTextureRed =
           (xScreen * screenToTextureRed + xEyeOffsetTexture) / textureWidth;
       final float vTextureRed =
           (yScreen * screenToTextureRed + yEyeOffsetTexture) / textureHeight;
       final float vignetteSizeTexture = VIGNETTE_SIZE_TAN_ANGLE / textureToScreenBlue;
       final float dxTexture =
           xTexture
               + xEyeOffsetTexture
               - clamp(
                   xTexture + xEyeOffsetTexture,
                   viewportXTexture + vignetteSizeTexture,
                   viewportXTexture + viewportWidthTexture - vignetteSizeTexture);
       final float dyTexture =
           yTexture
               + yEyeOffsetTexture
               - clamp(
                   yTexture + yEyeOffsetTexture,
                   viewportYTexture + vignetteSizeTexture,
                   viewportYTexture + viewportHeightTexture - vignetteSizeTexture);
       final float drTexture = (float) Math.sqrt(dxTexture * dxTexture + dyTexture * dyTexture);
       float vignette;
       if (DistortionRenderer.this.mVignetteEnabled) {
         vignette = 1.0f - clamp(drTexture / vignetteSizeTexture, 0.0f, 1.0f);
       } else {
         vignette = 1.0f;
       }
       vertexData[vertexOffset + 0] = 2.0f * uScreen - 1.0f;
       vertexData[vertexOffset + 1] = 2.0f * vScreen - 1.0f;
       vertexData[vertexOffset + 2] = vignette;
       vertexData[vertexOffset + 3] = uTextureRed;
       vertexData[vertexOffset + 4] = vTextureRed;
       vertexData[vertexOffset + 5] = uTextureGreen;
       vertexData[vertexOffset + 6] = vTextureGreen;
       vertexData[vertexOffset + 7] = uTextureBlue;
       vertexData[vertexOffset + 8] = vTextureBlue;
       vertexOffset += COMPONENTS_PER_VERT;
     }
   }
   this.nIndices = 3158;
   final short[] indexData = new short[this.nIndices];
   short indexOffset = 0;
   vertexOffset = 0;
   for (int row2 = 0; row2 < 39; ++row2) {
     if (row2 > 0) {
       indexData[indexOffset] = indexData[indexOffset - 1];
       ++indexOffset;
     }
     for (int col2 = 0; col2 < 40; ++col2) {
       if (col2 > 0) {
         if (row2 % 2 == 0) {
           ++vertexOffset;
         } else {
           --vertexOffset;
         }
       }
       indexData[indexOffset] = vertexOffset;
       ++indexOffset;
       indexData[indexOffset] = (short) (vertexOffset + 40);
       ++indexOffset;
     }
     vertexOffset += 40;
   }
   final FloatBuffer vertexBuffer =
       ByteBuffer.allocateDirect(vertexData.length * BYTES_PER_FLOAT)
           .order(ByteOrder.nativeOrder())
           .asFloatBuffer();
   vertexBuffer.put(vertexData).position(0);
   final ShortBuffer indexBuffer =
       ByteBuffer.allocateDirect(indexData.length * BYTES_PER_SHORT)
           .order(ByteOrder.nativeOrder())
           .asShortBuffer();
   indexBuffer.put(indexData).position(0);
   final int[] bufferIds = new int[2];
   GLES20.glGenBuffers(2, bufferIds, 0);
   this.mArrayBufferId = bufferIds[0];
   this.mElementBufferId = bufferIds[1];
   GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.mArrayBufferId);
   GLES20.glBufferData(
       GLES20.GL_ARRAY_BUFFER,
       vertexData.length * BYTES_PER_FLOAT,
       (Buffer) vertexBuffer,
       GLES20.GL_STATIC_DRAW);
   GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, this.mElementBufferId);
   GLES20.glBufferData(
       GLES20.GL_ELEMENT_ARRAY_BUFFER,
       indexData.length * BYTES_PER_SHORT,
       (Buffer) indexBuffer,
       GLES20.GL_STATIC_DRAW);
   GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
   GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
 }