Java Code Examples for android.opengl.GLES20#glBufferData()

The following examples show how to use android.opengl.GLES20#glBufferData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CameraRender.java    From AudioVideoCodec with Apache License 2.0 6 votes vote down vote up
/**
 * 创建vbo
 */
private void createVBO() {
    //1. 创建VBO
    int[] vbos = new int[1];
    GLES20.glGenBuffers(vbos.length, vbos, 0);
    vboId = vbos[0];
    //2. 绑定VBO
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboId);
    //3. 分配VBO需要的缓存大小
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertexData.length * 4 + textureData.length * 4, null, GLES20.GL_STATIC_DRAW);
    //4. 为VBO设置顶点数据的值
    GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, 0, vertexData.length * 4, vertexBuffer);
    GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, vertexData.length * 4, textureData.length * 4, textureBuffer);
    //5. 解绑VBO
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
}
 
Example 2
Source File: VideoEncodeRender.java    From AudioVideoCodec with Apache License 2.0 6 votes vote down vote up
/**
 * 创建vbo
 */
private void createVBO() {
    //1. 创建VBO
    int[] vbos = new int[1];
    GLES20.glGenBuffers(vbos.length, vbos, 0);
    vboId = vbos[0];
    //2. 绑定VBO
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboId);
    //3. 分配VBO需要的缓存大小
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertexData.length * 4 + textureData.length * 4, null, GLES20.GL_STATIC_DRAW);
    //4. 为VBO设置顶点数据的值
    GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, 0, vertexData.length * 4, vertexBuffer);
    GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, vertexData.length * 4, textureData.length * 4, textureBuffer);
    //5. 解绑VBO
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
}
 
Example 3
Source File: CubeRenderer.java    From HoloKilo with GNU General Public License v3.0 6 votes vote down vote up
private void createVertexBuffers() {
    cubeBuffer.position(0);
    texBuffer.position(0);
    indexBuffer.position(0);

    GLES20.glGenBuffers(1, texturePointer, 0);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, texturePointer[0]);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, tex.length * 4, texBuffer, GLES20.GL_STATIC_DRAW);

    GLES20.glGenBuffers(1, cubePointer, 0);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, cubePointer[0]);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, cube.length * 4, cubeBuffer, GLES20.GL_STATIC_DRAW);

    GLES20.glGenBuffers(1, indexPointer, 0);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexPointer[0]);
    GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, indeces.length * 4, indexBuffer, GLES20.GL_STATIC_DRAW);

    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
}
 
Example 4
Source File: PointCloudRenderer.java    From poly-sample-android with Apache License 2.0 5 votes vote down vote up
/**
 * Allocates and initializes OpenGL resources needed by the plane renderer. Must be called on the
 * OpenGL thread, typically in {@link GLSurfaceView.Renderer#onSurfaceCreated(GL10, EGLConfig)}.
 *
 * @param context Needed to access shader source.
 */
public void createOnGlThread(Context context) throws IOException {
  ShaderUtil.checkGLError(TAG, "before create");

  int[] buffers = new int[1];
  GLES20.glGenBuffers(1, buffers, 0);
  vbo = buffers[0];
  GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbo);

  vboSize = INITIAL_BUFFER_POINTS * BYTES_PER_POINT;
  GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vboSize, null, GLES20.GL_DYNAMIC_DRAW);
  GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

  ShaderUtil.checkGLError(TAG, "buffer alloc");

  int vertexShader =
      ShaderUtil.loadGLShader(TAG, context, GLES20.GL_VERTEX_SHADER, VERTEX_SHADER_NAME);
  int passthroughShader =
      ShaderUtil.loadGLShader(TAG, context, GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER_NAME);

  programName = GLES20.glCreateProgram();
  GLES20.glAttachShader(programName, vertexShader);
  GLES20.glAttachShader(programName, passthroughShader);
  GLES20.glLinkProgram(programName);
  GLES20.glUseProgram(programName);

  ShaderUtil.checkGLError(TAG, "program");

  positionAttribute = GLES20.glGetAttribLocation(programName, "a_Position");
  colorUniform = GLES20.glGetUniformLocation(programName, "u_Color");
  modelViewProjectionUniform = GLES20.glGetUniformLocation(programName, "u_ModelViewProjection");
  pointSizeUniform = GLES20.glGetUniformLocation(programName, "u_PointSize");

  ShaderUtil.checkGLError(TAG, "program  params");
}
 
Example 5
Source File: HighPerformanceVertexBufferObject.java    From tilt-game-android with MIT License 5 votes vote down vote up
@Override
protected void onBufferData() {
	// TODO Check if, and how mow this condition affects performance.
	if (SystemUtils.SDK_VERSION_HONEYCOMB_OR_LATER) {
		// TODO Check if this is similar fast or faster than the non Honeycomb codepath.
		this.mFloatBuffer.position(0);
		this.mFloatBuffer.put(this.mBufferData);

		GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, this.mByteBuffer.capacity(), this.mByteBuffer, this.mUsage);
	} else {
		BufferUtils.put(this.mByteBuffer, this.mBufferData, this.mBufferData.length, 0);
		GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, this.mByteBuffer.limit(), this.mByteBuffer, this.mUsage);
	}
}
 
Example 6
Source File: GLState.java    From tilt-game-android with MIT License 5 votes vote down vote up
public int generateArrayBuffer(final int pSize, final int pUsage) {
	GLES20.glGenBuffers(1, this.mHardwareIDContainer, 0);
	final int hardwareBufferID = this.mHardwareIDContainer[0];

	this.bindArrayBuffer(hardwareBufferID);
	GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, pSize, null, pUsage);
	this.bindArrayBuffer(0);

	return hardwareBufferID;
}
 
Example 7
Source File: PointCloudRenderer.java    From react-native-arcore with MIT License 5 votes vote down vote up
/**
 * Allocates and initializes OpenGL resources needed by the plane renderer.  Must be
 * called on the OpenGL thread, typically in
 * {@link GLSurfaceView.Renderer#onSurfaceCreated(GL10, EGLConfig)}.
 *
 * @param context Needed to access shader source.
 */
public void createOnGlThread(Context context) {
    ShaderUtil.checkGLError(TAG, "before create");

    int[] buffers = new int[1];
    GLES20.glGenBuffers(1, buffers, 0);
    mVbo = buffers[0];
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mVbo);

    mVboSize = INITIAL_BUFFER_POINTS * BYTES_PER_POINT;
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, mVboSize, null, GLES20.GL_DYNAMIC_DRAW);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

    ShaderUtil.checkGLError(TAG, "buffer alloc");

    int vertexShader = ShaderUtil.loadGLShader(TAG, context,
        GLES20.GL_VERTEX_SHADER, R.raw.point_cloud_vertex);
    int passthroughShader = ShaderUtil.loadGLShader(TAG, context,
        GLES20.GL_FRAGMENT_SHADER, R.raw.passthrough_fragment);

    mProgramName = GLES20.glCreateProgram();
    GLES20.glAttachShader(mProgramName, vertexShader);
    GLES20.glAttachShader(mProgramName, passthroughShader);
    GLES20.glLinkProgram(mProgramName);
    GLES20.glUseProgram(mProgramName);

    ShaderUtil.checkGLError(TAG, "program");

    mPositionAttribute = GLES20.glGetAttribLocation(mProgramName, "a_Position");
    mColorUniform = GLES20.glGetUniformLocation(mProgramName, "u_Color");
    mModelViewProjectionUniform = GLES20.glGetUniformLocation(
        mProgramName, "u_ModelViewProjection");
    mPointSizeUniform = GLES20.glGetUniformLocation(mProgramName, "u_PointSize");

    ShaderUtil.checkGLError(TAG, "program  params");
}
 
Example 8
Source File: Triangles.java    From ShapesInOpenGLES2.0 with MIT License 5 votes vote down vote up
/**
 * creates buffers for Triangle shape object
 * @param positions
 * @param colors
 */
public void createBuffers(float[] positions, float[] colors) {
    FloatBuffer aTriangleVerticesBuffer;
    FloatBuffer aTriangleColorBuffer;

    vertexCount = positions.length/POSITION_DATA_SIZE;

    // Initialize the buffers.
    aTriangleVerticesBuffer = ByteBuffer.allocateDirect(positions.length * BYTES_PER_FLOAT)
            .order(ByteOrder.nativeOrder()).asFloatBuffer();

    aTriangleColorBuffer = ByteBuffer.allocateDirect(colors.length * BYTES_PER_FLOAT)
            .order(ByteOrder.nativeOrder()).asFloatBuffer();

    aTriangleVerticesBuffer.put(positions).position(0);
    aTriangleColorBuffer.put(colors).position(0);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, glTriangleBuffer[0]);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, aTriangleVerticesBuffer.capacity() * BYTES_PER_FLOAT, aTriangleVerticesBuffer, GLES20.GL_STATIC_DRAW);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, glTriangleBuffer[1]);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, aTriangleColorBuffer.capacity() * BYTES_PER_FLOAT, aTriangleColorBuffer, GLES20.GL_STATIC_DRAW);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

    aTrianglePositionsBufferIdx = glTriangleBuffer[0];
    aTriangleColorsBufferIdx = glTriangleBuffer[1];

    aTriangleVerticesBuffer.limit(0);
    aTriangleVerticesBuffer = null;
    aTriangleColorBuffer.limit(0);
    aTriangleColorBuffer = null;
}
 
Example 9
Source File: CubicBezier.java    From Muzesto with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a cubic bezier renderable
 * @param renderer
 * @param vboBuffer
 * @param p0x Start point's x
 * @param p0y Start point's y
 * @param p3x End point's x
 * @param p3y End point's y
 * @param p1x First control point's x
 * @param p1y First control point's y
 * @param p2x Second control point's x
 * @param p2y Second control point's y
 * @param color Fill color
 */
public CubicBezier(BezierRenderer renderer, FloatBuffer vboBuffer,
                   float p0x, float p0y,
                   float p3x, float p3y,
                   float p1x, float p1y,
                   float p2x, float p2y,
                   float[] color) {
    this.mRenderer = renderer;
    this.starEndPoints = new float[] { p0x, p0y, p3x, p3y };
    this.controlPoints = new float[] { p1x, p1y, p2x, p2y };
    this.color = color;

    // copy the buffer into OpenGL's memory

    final int buffers[] = new int[1];
    GLES20.glGenBuffers(1, buffers, 0);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vboBuffer.capacity() * Const.BYTES_PER_FLOAT,
            vboBuffer, GLES20.GL_STATIC_DRAW);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

    mBufferId = buffers[0];

    //vboBuffer.limit(0);
    //noinspection UnusedAssignment
    vboBuffer = null;
}
 
Example 10
Source File: GLES20Canvas.java    From LB-Launcher with Apache License 2.0 5 votes vote down vote up
private int uploadBuffer(Buffer buffer, int elementSize) {
    mGLId.glGenBuffers(1, mTempIntArray, 0);
    checkError();
    int bufferId = mTempIntArray[0];
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, bufferId);
    checkError();
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, buffer.capacity() * elementSize, buffer,
            GLES20.GL_STATIC_DRAW);
    checkError();
    return bufferId;
}
 
Example 11
Source File: GLState.java    From 30-android-libraries-in-30-days with Apache License 2.0 5 votes vote down vote up
public int generateIndexBuffer(final int pSize, final int pUsage) {
	GLES20.glGenBuffers(1, this.mHardwareIDContainer, 0);
	final int hardwareBufferID = this.mHardwareIDContainer[0];
	
	this.bindIndexBuffer(hardwareBufferID);
	GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, pSize, null, pUsage);
	this.bindIndexBuffer(0);
	
	return hardwareBufferID;
}
 
Example 12
Source File: CameraViewModel.java    From VIA-AI with MIT License 5 votes vote down vote up
private void checkDataUpdate()
{
    if(mNeedUpdate) {
        // VBO update vertex coordinate
        ByteBuffer bb = ByteBuffer.allocateDirect(mPosition.length * 4);
        bb.order(ByteOrder.nativeOrder());
        FloatBuffer vertexBuffer = bb.asFloatBuffer();
        vertexBuffer.put(mPosition);
        vertexBuffer.position(0);

        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mPositionVBO[0]);
        GLUtility.checkGlError("glBindBuffer");

        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, mPosition.length * 4, vertexBuffer, GLES20.GL_STATIC_DRAW);
        GLUtility.checkGlError("glBufferData");


        // VBO update texture coordinate
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mTextureCoordinateVBO[0]);
        GLUtility.checkGlError("glBindBuffer");

        bb = ByteBuffer.allocateDirect(mTextureCoordinate.length * 4);
        bb.order(ByteOrder.nativeOrder());
        FloatBuffer textureCoordinateBuffer = bb.asFloatBuffer();
        textureCoordinateBuffer.put(mTextureCoordinate);
        textureCoordinateBuffer.position(0);

        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, mTextureCoordinate.length * 4, textureCoordinateBuffer, GLES20.GL_STATIC_DRAW);
        GLUtility.checkGlError("glBufferData");

        mNeedUpdate = false;
    }
}
 
Example 13
Source File: BaseRenderer.java    From VidEffects with Apache License 2.0 5 votes vote down vote up
protected void init() {
    GLES20.glGenBuffers(2, bufferHandles, 0);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, bufferHandles[0]);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, Utils.VERTICES.length * FLOAT_SIZE_BYTES, Utils.getVertexBuffer(), GLES20.GL_DYNAMIC_DRAW);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, bufferHandles[1]);
    GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, Utils.INDICES.length * FLOAT_SIZE_BYTES, Utils.getIndicesBuffer(), GLES20.GL_DYNAMIC_DRAW);
    GLES20.glGenTextures(2, textureHandles, 0);
    GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, textureHandles[0]);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glEnable(GLES20.GL_BLEND);
    GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
}
 
Example 14
Source File: LineShaderRenderer.java    From ar-drawing-java with Apache License 2.0 4 votes vote down vote up
/**
 * Allocates and initializes OpenGL resources needed by the Line renderer.  Must be
 * called on the OpenGL thread, typically in
 * {@link GLSurfaceView.Renderer#onSurfaceCreated(GL10, EGLConfig)}.
 *
 * @param context Needed to access shader source.
 */
public void createOnGlThread(Context context) {
    ShaderUtil.checkGLError(TAG, "before create");

    int buffers[] = new int[1];
    GLES20.glGenBuffers(1, buffers, 0);
    mVbo = buffers[0];
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mVbo);
    mVboSize = 0;
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, mVboSize, null, GLES20.GL_DYNAMIC_DRAW);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);


    ShaderUtil.checkGLError(TAG, "buffer alloc");


    /**
     *
     * The LineShaderRenderer uses an ES2 pipeline.  It uses the line_vert.glsl and
     * line_frag.glsl shader to render a volumetric line.  It uses several techniques detailed in
     * the following resources:
     *
     *      Drawing Lines is Hard by Matt DesLauriers
     *          https://mattdesl.svbtle.com/drawing-lines-is-hard
     *
     *      InkSpace an Android Experiment by Zach Lieberman
     *          https://experiments.withgoogle.com/android/ink-space
     *          https://github.com/ofZach/inkSpace
     *
     *      THREEJS.MeshLine by Jaume Sanchez
     *          https://github.com/spite/THREE.MeshLine/blob/master/src/THREE.MeshLine.js
     *
     *
     * The Renderer batches all of the geometry into a single VBO.  This allows us to have a single
     * draw call to render the geometry.  We also optimize the application to only re-upload the
     * geometry data when a new stroke or new points are added to the drawing. The renderer uses
     * a technique detailed in the following link to create degenerate faces between the strokes
     * to disconnect them from one another.
     *      https://developer.apple.com/library/content/documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/TechniquesforWorkingwithVertexData/TechniquesforWorkingwithVertexData.html
     *
     */

    int vertexShader = ShaderUtil.loadGLShader(TAG, context,
            GLES20.GL_VERTEX_SHADER, R.raw.line_vert);
    int fragmentShader = ShaderUtil.loadGLShader(TAG, context,
            GLES20.GL_FRAGMENT_SHADER, R.raw.line_frag);


    mProgramName = GLES20.glCreateProgram();
    GLES20.glAttachShader(mProgramName, vertexShader);
    GLES20.glAttachShader(mProgramName, fragmentShader);
    GLES20.glLinkProgram(mProgramName);
    GLES20.glUseProgram(mProgramName);

    ShaderUtil.checkGLError(TAG, "program");

    mPositionAttribute = GLES20.glGetAttribLocation(mProgramName, "position");
    mPreviousAttribute = GLES20.glGetAttribLocation(mProgramName, "previous");
    mNextAttribute = GLES20.glGetAttribLocation(mProgramName, "next");
    mSideAttribute = GLES20.glGetAttribLocation(mProgramName, "side");
    mWidthAttribte = GLES20.glGetAttribLocation(mProgramName, "width");
    mCountersAttribute = GLES20.glGetAttribLocation(mProgramName, "counters");
    mProjectionUniform = GLES20.glGetUniformLocation(mProgramName, "projectionMatrix");
    mModelViewUniform = GLES20.glGetUniformLocation(mProgramName, "modelViewMatrix");
    mResolutionUniform = GLES20.glGetUniformLocation(mProgramName, "resolution");
    mLineWidthUniform = GLES20.glGetUniformLocation(mProgramName, "lineWidth");
    mColorUniform = GLES20.glGetUniformLocation(mProgramName, "color");
    mOpacityUniform = GLES20.glGetUniformLocation(mProgramName, "opacity");
    mNearUniform = GLES20.glGetUniformLocation(mProgramName, "near");
    mFarUniform = GLES20.glGetUniformLocation(mProgramName, "far");
    mSizeAttenuationUniform = GLES20.glGetUniformLocation(mProgramName, "sizeAttenuation");
    mVisibility = GLES20.glGetUniformLocation(mProgramName, "visibility");
    mAlphaTest = GLES20.glGetUniformLocation(mProgramName, "alphaTest");
    mDrawModeUniform = GLES20.glGetUniformLocation(mProgramName, "drawMode");
    mNearCutoffUniform = GLES20.glGetUniformLocation(mProgramName, "nearCutOff");
    mFarCutoffUniform = GLES20.glGetUniformLocation(mProgramName, "farCutOff");
    mLineDepthScaleUniform = GLES20.glGetUniformLocation(mProgramName, "lineDepthScale");

    ShaderUtil.checkGLError(TAG, "program  params");

    Matrix.setIdentityM(mModelMatrix, 0);

    mColor = new Vector3f(1f, 1f, 1f);
    lineWidth = 0.5f;
}
 
Example 15
Source File: AndroidGL.java    From trekarta with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void bufferData(int target, int size, Buffer data, int usage) {
    GLES20.glBufferData(target, size, data, usage);
}
 
Example 16
Source File: LineShaderRenderer.java    From justaline-android with Apache License 2.0 4 votes vote down vote up
/**
     * This takes the float[] and creates FloatBuffers, Binds the VBO, and upload the Attributes to
     * correct locations with the correct offsets so the Vertex and Fragment shader can render the lines
     */
    public void upload() {
        bNeedsUpdate.set(false);

        FloatBuffer current = toFloatBuffer(mPositions);
        FloatBuffer next = toFloatBuffer(mNext);
        FloatBuffer previous = toFloatBuffer(mPrevious);

        FloatBuffer side = toFloatBuffer(mSide);
        FloatBuffer width = toFloatBuffer(mWidth);
        FloatBuffer lengths = toFloatBuffer(mLengths);
        FloatBuffer endCaps = toFloatBuffer(mEndCaps);


//        mNumPoints = mPositions.length;

        mPositionAddress = 0;
        mNextAddress = mPositionAddress + mNumBytes * 3 * BYTES_PER_FLOAT;
        mPreviousAddress = mNextAddress + mNumBytes * 3 * BYTES_PER_FLOAT;
        mSideAddress = mPreviousAddress + mNumBytes * 3 * BYTES_PER_FLOAT;
        mWidthAddress = mSideAddress + mNumBytes * BYTES_PER_FLOAT;
        mLengthAddress = mWidthAddress + mNumBytes * BYTES_PER_FLOAT;
        mEndCapsAddress = mLengthAddress + mNumBytes * BYTES_PER_FLOAT;
        mVboSize = mEndCapsAddress + mNumBytes * BYTES_PER_FLOAT;

        ShaderUtil.checkGLError(TAG, "before update");

        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mVbo);

        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, mVboSize, null, GLES20.GL_DYNAMIC_DRAW);

        GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, mPositionAddress, mNumBytes * 3 * BYTES_PER_FLOAT,
                current);
        GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, mNextAddress, mNumBytes * 3 * BYTES_PER_FLOAT,
                next);
        GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, mPreviousAddress, mNumBytes * 3 * BYTES_PER_FLOAT,
                previous);
        GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, mSideAddress, mNumBytes * BYTES_PER_FLOAT,
                side);
        GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, mWidthAddress, mNumBytes * BYTES_PER_FLOAT,
                width);
        GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, mLengthAddress, mNumBytes * BYTES_PER_FLOAT,
                lengths);
        GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, mEndCapsAddress, mNumBytes * BYTES_PER_FLOAT,
                endCaps);


        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

        ShaderUtil.checkGLError(TAG, "after update");
    }
 
Example 17
Source File: DebugMeshShaderRenderer.java    From justaline-android with Apache License 2.0 4 votes vote down vote up
/**
 * Allocates and initializes OpenGL resources needed by the Line renderer.  Must be
 * called on the OpenGL thread, typically in
 * {@link GLSurfaceView.Renderer#onSurfaceCreated(GL10, EGLConfig)}.
 *
 * @param context Needed to access shader source.
 */
public void createOnGlThread(Context context) {
    ShaderUtil.checkGLError(TAG, "before create");

    int buffers[] = new int[1];
    GLES20.glGenBuffers(1, buffers, 0);
    mVbo = buffers[0];
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mVbo);
    mVboSize = 0;
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, mVboSize, null, GLES20.GL_DYNAMIC_DRAW);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);


    ShaderUtil.checkGLError(TAG, "buffer alloc");


    /*
     *
     * The LineShaderRenderer uses an ES2 pipeline.  It uses the line_vert.glsl and
     * line_frag.glsl shader to render a volumetric line.  It uses several techniques detailed in
     * the following resources:
     *
     *      Drawing Lines is Hard by Matt DesLauriers
     *          https://mattdesl.svbtle.com/drawing-lines-is-hard
     *
     *      InkSpace an Android Experiment by Zach Lieberman
     *          https://experiments.withgoogle.com/android/ink-space
     *          https://github.com/ofZach/inkSpace
     *
     *      THREEJS.MeshLine by Jaume Sanchez
     *          https://github.com/spite/THREE.MeshLine/blob/master/src/THREE.MeshLine.js
     *
     *
     * The Renderer batches all of the geometry into a single VBO.  This allows us to have a single
     * draw call to render the geometry.  We also optimize the application to only re-upload the
     * geometry data when a new stroke or new points are added to the drawing. The renderer uses
     * a technique detailed in the following link to create degenerate faces between the strokes
     * to disconnect them from one another.
     *      https://developer.apple.com/library/content/documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/TechniquesforWorkingwithVertexData/TechniquesforWorkingwithVertexData.html
     *
     */

    int vertexShader = ShaderUtil.loadGLShader(TAG, context,
            GLES20.GL_VERTEX_SHADER, R.raw.line_vert);
    int fragmentShader = ShaderUtil.loadGLShader(TAG, context,
            GLES20.GL_FRAGMENT_SHADER, R.raw.line_frag);


    mProgramName = GLES20.glCreateProgram();
    GLES20.glAttachShader(mProgramName, vertexShader);
    GLES20.glAttachShader(mProgramName, fragmentShader);
    GLES20.glLinkProgram(mProgramName);
    GLES20.glUseProgram(mProgramName);

    ShaderUtil.checkGLError(TAG, "program");

    mPositionAttribute = GLES20.glGetAttribLocation(mProgramName, "position");
    mPreviousAttribute = GLES20.glGetAttribLocation(mProgramName, "previous");
    mNextAttribute = GLES20.glGetAttribLocation(mProgramName, "next");
    mSideAttribute = GLES20.glGetAttribLocation(mProgramName, "side");
    mWidthAttribte = GLES20.glGetAttribLocation(mProgramName, "width");
    mCountersAttribute = GLES20.glGetAttribLocation(mProgramName, "counters");
    mProjectionUniform = GLES20.glGetUniformLocation(mProgramName, "projectionMatrix");
    mModelViewUniform = GLES20.glGetUniformLocation(mProgramName, "modelViewMatrix");
    mResolutionUniform = GLES20.glGetUniformLocation(mProgramName, "resolution");
    mColorUniform = GLES20.glGetUniformLocation(mProgramName, "color");
    mOpacityUniform = GLES20.glGetUniformLocation(mProgramName, "opacity");
    mNearUniform = GLES20.glGetUniformLocation(mProgramName, "near");
    mFarUniform = GLES20.glGetUniformLocation(mProgramName, "far");
    mSizeAttenuationUniform = GLES20.glGetUniformLocation(mProgramName, "sizeAttenuation");
    mVisibility = GLES20.glGetUniformLocation(mProgramName, "visibility");
    mAlphaTest = GLES20.glGetUniformLocation(mProgramName, "alphaTest");
    mDrawModeUniform = GLES20.glGetUniformLocation(mProgramName, "drawMode");
    mNearCutoffUniform = GLES20.glGetUniformLocation(mProgramName, "nearCutOff");
    mFarCutoffUniform = GLES20.glGetUniformLocation(mProgramName, "farCutOff");
    mLineDepthScaleUniform = GLES20.glGetUniformLocation(mProgramName, "lineDepthScale");

    ShaderUtil.checkGLError(TAG, "program  params");

    Matrix.setIdentityM(mModelMatrix, 0);

    mColor = new Vector3f(1f, 1f, 1f);


}
 
Example 18
Source File: TextureWarmUpVertexBufferObject.java    From 30-android-libraries-in-30-days with Apache License 2.0 4 votes vote down vote up
@Override
protected void onBufferData() {
	GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, this.mByteBuffer.limit(), this.mByteBuffer, this.mUsage);
}
 
Example 19
Source File: LineShaderRenderer.java    From ar-drawing-java with Apache License 2.0 4 votes vote down vote up
/**
     * This takes the float[] and creates FloatBuffers, Binds the VBO, and upload the Attributes to
     * correct locations with the correct offsets so the Vertex and Fragment shader can render the lines
     */
    public void upload() {
        bNeedsUpdate.set(false);

        FloatBuffer current = toFloatBuffer(mPositions);
        FloatBuffer next = toFloatBuffer(mNext);
        FloatBuffer previous = toFloatBuffer(mPrevious);

        FloatBuffer side = toFloatBuffer(mSide);
        FloatBuffer width = toFloatBuffer(mWidth);
        FloatBuffer counter = toFloatBuffer(mCounters);


//        mNumPoints = mPositions.length;

        mPositionAddress = 0;
        mNextAddress = mPositionAddress + mNumBytes * 3 * BYTES_PER_FLOAT;
        mPreviousAddress = mNextAddress + mNumBytes * 3 * BYTES_PER_FLOAT;
        mSideAddress = mPreviousAddress + mNumBytes *3 * BYTES_PER_FLOAT;

        mWidthAddress = mSideAddress + mNumBytes * BYTES_PER_FLOAT;
        mCounterAddress = mWidthAddress + mNumBytes * BYTES_PER_FLOAT;
        mVboSize = mCounterAddress + mNumBytes * BYTES_PER_FLOAT;

        ShaderUtil.checkGLError(TAG, "before update");

        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mVbo);

        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, mVboSize, null, GLES20.GL_DYNAMIC_DRAW);

        GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, mPositionAddress, mNumBytes * 3 * BYTES_PER_FLOAT,
                current);
        GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, mNextAddress, mNumBytes * 3 * BYTES_PER_FLOAT,
                next);
        GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, mPreviousAddress, mNumBytes * 3 * BYTES_PER_FLOAT,
                previous);
        GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, mSideAddress, mNumBytes * BYTES_PER_FLOAT,
                side);
        GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, mWidthAddress, mNumBytes * BYTES_PER_FLOAT,
                width);
        GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, mCounterAddress, mNumBytes * BYTES_PER_FLOAT,
                counter);


        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

        ShaderUtil.checkGLError(TAG, "after update");
    }
 
Example 20
Source File: Spheres.java    From ShapesInOpenGLES2.0 with MIT License 4 votes vote down vote up
/**
     * creates buffers for Sphere shape objects
     * @param spherePositions
     * @param sphereColors
     * @param sphereRadii
     * @param steps
     */
    public void createBuffers(float[] spherePositions,float[] sphereColors, float[] sphereRadii, int steps){
        int length = spherePositions.length;
        int colorLength = sphereColors.length;

        // there are lats*longs number of quads, each requires two triangles with six vertices, each vertex takes 3 floats;
        int lats = steps, longs = steps;
        _vertices = new float[lats*longs* 6 * POSITION_DATA_SIZE * (length/ POSITION_DATA_SIZE)];
        _colors = new float[lats*longs*6* COLOR_DATA_SIZE * (colorLength/ COLOR_DATA_SIZE)];

        try {
            int offset = 0;
            for(int i = 0; i < length/3; i ++) {
                int index = i * POSITION_DATA_SIZE;
                float[] vertices = createSphere((2 * sphereRadii[i]), lats, longs, new float[]{spherePositions[index], spherePositions[index+1], spherePositions[index+2]});
                System.arraycopy(vertices, 0,_vertices, offset, vertices.length);
                offset += vertices.length;
                int idx = i * COLOR_DATA_SIZE;
                final float[] currColor = new float[]{sphereColors[idx],sphereColors[idx+1],sphereColors[idx+2],sphereColors[idx+3]};
                for (int j = 0; j < vertices.length/POSITION_DATA_SIZE; j++){
                    System.arraycopy(currColor, 0, _colors, (((vertices.length* idx)/POSITION_DATA_SIZE) + (j * COLOR_DATA_SIZE)), currColor.length);
                }
            }
            vertexCount = (_vertices.length/COORDS_PER_VERTEX) * (length/POSITION_DATA_SIZE);

//        float[] buff1 = createSphere(radius, lats, longs, new float[]{-1,-1,-1});
//        float[] buff2 = createSphere(radius, lats, longs, new float[]{1,1,1});
//        float[] buff3 = createSphere(radius, lats, longs, new float[]{1,.9f,1});
//        vertexCount = (_vertices.length/COORDS_PER_VERTEX) * 3;
//        System.arraycopy(buff1, 0,_vertices, 0, buff1.length);
//        System.arraycopy(buff2, 0,_vertices, buff1.length, buff2.length);
//        System.arraycopy(buff3, 0,_vertices, buff1.length*2, buff3.length);

        }catch (Exception e){
            Log.d("Tag","error: "+ e);
        }

//        FloatBuffer tempVertexBuffer = createSphere((float) radius,lats,longs);

        // initialize vertex byte buffer for shape coordinates
        ByteBuffer bb = ByteBuffer.allocateDirect(_vertices.length * 4);
        bb.order(ByteOrder.nativeOrder());
        _vertexBuffer = bb.asFloatBuffer();
        _vertexBuffer.put(_vertices);
        _vertexBuffer.position(0);

        ByteBuffer bb2 = ByteBuffer.allocateDirect(_colors.length * 4);
        bb2.order(ByteOrder.nativeOrder());
        _colorBuffer = bb2.asFloatBuffer();
        _colorBuffer.put(_colors);
        _colorBuffer.position(0);

        vertexCount = getVertexCount();
        spheresPositionsLength = spherePositions.length;
        FloatBuffer spheresVerticesBuffer = _vertexBuffer;
        FloatBuffer spheresColorsBuffer = _colorBuffer;
        ShortBuffer indicesBuffer = getIndicesBuffer();
        indexCount = getIndexCount();

        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, glSphereBuffer[0]);
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, spheresVerticesBuffer.capacity() * BYTES_PER_FLOAT, spheresVerticesBuffer, GLES20.GL_STATIC_DRAW);

        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, glSphereBuffer[1]);
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, spheresColorsBuffer.capacity() * BYTES_PER_FLOAT, spheresColorsBuffer, GLES20.GL_STATIC_DRAW);

        if(indicesBuffer != null) {
            GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, glSphereBuffer[2]);
            GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer.capacity() * 2, indicesBuffer,
                    GLES20.GL_STATIC_DRAW);
            GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
        }

        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

        aSpheresVerticesBufferIdx = glSphereBuffer[0];
        aSpheresColorsBufferIdx = glSphereBuffer[1];
        aSphereIndicesBufferIdx = glSphereBuffer[2];

        spheresColorsBuffer.limit(0);
        spheresColorsBuffer = null;
        spheresVerticesBuffer.limit(0);
        spheresVerticesBuffer = null;
        if(indicesBuffer != null) {
            indicesBuffer.limit(0);
        }
    }