android.opengl.GLES20 Java Examples

The following examples show how to use android.opengl.GLES20. 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: BitmapUtils.java    From In77Camera with MIT License 7 votes vote down vote up
public static void sendImage(int width, int height, Context context, FileUtils.FileSavedCallback fileSavedCallback) {
    final IntBuffer pixelBuffer = IntBuffer.allocate(width * height);

    //about 20-50ms
    long start = System.nanoTime();
    GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE,
            pixelBuffer);
    long end = System.nanoTime();

    Log.d(TAG, "glReadPixels time: " + (end - start)/1000000+" ms");

    //about 700-4000ms(png) 200-1000ms(jpeg)
    //use jpeg instead of png to save time
    //it will consume large memory and may take a long time, depends on the phone
    new SaveBitmapTask(pixelBuffer,width,height,context,fileSavedCallback).execute();
}
 
Example #2
Source File: Spheres.java    From ShapesInOpenGLES2.0 with MIT License 6 votes vote down vote up
/**
 * Instantiate Sphere objects
 * @param activity
 * @param glTrue
 * @param steps
 * @param positions
 * @param colors
 * @param radii
 */
public Spheres(Context activity, int glTrue, int steps, float[] positions, float[] colors, float[] radii){

    /** initialize the sphere program */
    final String sphereVS = RawResourceReader.readTextFileFromRawResource(activity, R.raw.sphere_vertex_shader);
    final String sphereFS = RawResourceReader.readTextFileFromRawResource(activity, R.raw.sphere_fragment_shader);

    final int sphereVSHandle = ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, sphereVS);
    final int sphereFSHandle = ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, sphereFS);

    aSphereProgramHandle = ShaderHelper.createAndLinkProgram(sphereVSHandle, sphereFSHandle,
            new String[]{"a_Position", "a_Color"});

    // Second, copy these buffers into OpenGL's memory. After, we don't need to keep the client-side buffers around.
    glSphereBuffer = new int[4];
    GLES20.glGenBuffers(glSphereBuffer.length, glSphereBuffer, 0);

    BLENDING = (glTrue == GLES20.GL_TRUE) ? true : false;

    createBuffers(positions, colors, radii, steps);
}
 
Example #3
Source File: VideoCapture.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@CalledByNative
public void deallocate() {
    if (mCamera == null)
        return;

    stopCapture();
    try {
        mCamera.setPreviewTexture(null);
        if (mGlTextures != null)
            GLES20.glDeleteTextures(1, mGlTextures, 0);
        mCurrentCapability = null;
        mCamera.release();
        mCamera = null;
    } catch (IOException ex) {
        Log.e(TAG, "deallocate: failed to deallocate camera, " + ex);
        return;
    }
}
 
Example #4
Source File: GameGLRenderer.java    From Eye-blink-detector with MIT License 6 votes vote down vote up
public void onDrawFrame(GL10 unused) {
    // Redraw background color
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    // Set the camera position (View matrix)
    Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    // Calculate the projection and view transformation
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
    // Calculate the projection and view transformation
    long time = SystemClock.uptimeMillis() % 4000L;
    float angle = 0.090f * ((int) time);
    Matrix.setRotateM(mRotationMatrix, 0, angle, 0, 0, -1.0f);

    // Combine the rotation matrix with the projection and camera view
    // Note that the mMVPMatrix factor *must be first* in order
    // for the matrix multiplication product to be correct.
    Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0);

    // Draw triangle
    mTriangle.draw(scratch);
}
 
Example #5
Source File: GlTextureFrameBuffer.java    From sealrtc-android with MIT License 6 votes vote down vote up
/**
 * Generate texture and framebuffer resources. An EGLContext must be bound on the current thread
 * when calling this function. The framebuffer is not complete until setSize() is called.
 */
public GlTextureFrameBuffer(int pixelFormat) {
    switch (pixelFormat) {
        case GLES20.GL_LUMINANCE:
        case GLES20.GL_RGB:
        case GLES20.GL_RGBA:
            this.pixelFormat = pixelFormat;
            break;
        default:
            throw new IllegalArgumentException("Invalid pixel format: " + pixelFormat);
    }

    // Create texture.
    textureId = GlUtil.generateTexture(GLES20.GL_TEXTURE_2D);
    this.width = 0;
    this.height = 0;

    // Create framebuffer object.
    final int frameBuffers[] = new int[1];
    GLES20.glGenFramebuffers(1, frameBuffers, 0);
    frameBufferId = frameBuffers[0];
}
 
Example #6
Source File: GlOverlayFilter.java    From CameraRecorder-android with MIT License 6 votes vote down vote up
@Override
public void onDraw() {
    if (bitmap == null) {
        createBitmap();
    }
    if (bitmap.getWidth() != inputResolution.getWidth() || bitmap.getHeight() != inputResolution.getHeight()) {
        createBitmap();
    }

    bitmap.eraseColor(Color.argb(0, 0, 0, 0));
    Canvas bitmapCanvas = new Canvas(bitmap);
    bitmapCanvas.scale(1, -1, bitmapCanvas.getWidth() / 2, bitmapCanvas.getHeight() / 2);
    drawCanvas(bitmapCanvas);

    int offsetDepthMapTextureUniform = getHandle("oTexture");// 3

    GLES20.glActiveTexture(GLES20.GL_TEXTURE3);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);

    if (bitmap != null && !bitmap.isRecycled()) {
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, bitmap, 0);
    }

    GLES20.glUniform1i(offsetDepthMapTextureUniform, 3);
}
 
Example #7
Source File: ShaderUtil.java    From ARCore-Location with MIT License 6 votes vote down vote up
/**
 * Converts a raw text file, saved as a resource, into an OpenGL ES shader.
 *
 * @param type The type of shader we will be creating.
 * @param resId The resource ID of the raw text file about to be turned into a shader.
 * @return The shader object handler.
 */
public static int loadGLShader(String tag, Context context, int type, int resId) {
    String code = readRawTextFile(context, resId);
    int shader = GLES20.glCreateShader(type);
    GLES20.glShaderSource(shader, code);
    GLES20.glCompileShader(shader);

    // Get the compilation status.
    final int[] compileStatus = new int[1];
    GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

    // If the compilation failed, delete the shader.
    if (compileStatus[0] == 0) {
        Log.e(tag, "Error compiling shader: " + GLES20.glGetShaderInfoLog(shader));
        GLES20.glDeleteShader(shader);
        shader = 0;
    }

    if (shader == 0) {
        throw new RuntimeException("Error creating shader.");
    }

    return shader;
}
 
Example #8
Source File: GlUtil.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a GL_TEXTURE_EXTERNAL_OES with default configuration of GL_LINEAR filtering and
 * GL_CLAMP_TO_EDGE wrapping.
 */
public static int createExternalTexture() {
  int[] texId = new int[1];
  GLES20.glGenTextures(1, IntBuffer.wrap(texId));
  GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texId[0]);
  GLES20.glTexParameteri(
      GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
  GLES20.glTexParameteri(
      GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
  GLES20.glTexParameteri(
      GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
  GLES20.glTexParameteri(
      GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
  checkGlError();
  return texId[0];
}
 
Example #9
Source File: EncodeAndMuxTest.java    From Android-MediaCodec-Examples with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a frame of data using GL commands.  We have an 8-frame animation
 * sequence that wraps around.  It looks like this:
 * <pre>
 *   0 1 2 3
 *   7 6 5 4
 * </pre>
 * We draw one of the eight rectangles and leave the rest set to the clear color.
 */
private void generateSurfaceFrame(int frameIndex) {
    frameIndex %= 8;

    int startX, startY;
    if (frameIndex < 4) {
        // (0,0) is bottom-left in GL
        startX = frameIndex * (mWidth / 4);
        startY = mHeight / 2;
    } else {
        startX = (7 - frameIndex) * (mWidth / 4);
        startY = 0;
    }

    GLES20.glClearColor(TEST_R0 / 255.0f, TEST_G0 / 255.0f, TEST_B0 / 255.0f, 1.0f);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    GLES20.glEnable(GLES20.GL_SCISSOR_TEST);
    GLES20.glScissor(startX, startY, mWidth / 4, mHeight / 2);
    GLES20.glClearColor(TEST_R1 / 255.0f, TEST_G1 / 255.0f, TEST_B1 / 255.0f, 1.0f);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    GLES20.glDisable(GLES20.GL_SCISSOR_TEST);
}
 
Example #10
Source File: MD360VideoTexture.java    From Beginner-Level-Android-Studio-Apps with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected int createTextureId() {
    int[] textures = new int[1];

    // Generate the texture to where android view will be rendered
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glGenTextures(1, textures, 0);
    glCheck("Texture generate");

    GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textures[0]);
    glCheck("Texture bind");

    GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);

    return textures[0];
}
 
Example #11
Source File: PointCloudRenderer.java    From react-native-arcore with MIT License 6 votes vote down vote up
/**
 * Renders the point cloud. ArCore point cloud is given in world space.
 *
 * @param cameraView the camera view matrix for this frame, typically from {@link
 *     com.google.ar.core.Camera#getViewMatrix(float[], int)}.
 * @param cameraPerspective the camera projection matrix for this frame, typically from {@link
 *     com.google.ar.core.Camera#getProjectionMatrix(float[], int, float, float)}.
 */
public void draw(float[] cameraView, float[] cameraPerspective) {
      float[] modelViewProjection = new float[16];
      Matrix.multiplyMM(modelViewProjection, 0, cameraPerspective, 0, cameraView, 0);

      ShaderUtil.checkGLError(TAG, "Before draw");

      GLES20.glUseProgram(mProgramName);
      GLES20.glEnableVertexAttribArray(mPositionAttribute);
      GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mVbo);
      GLES20.glVertexAttribPointer(
          mPositionAttribute, 4, GLES20.GL_FLOAT, false, BYTES_PER_POINT, 0);
      GLES20.glUniform4f(mColorUniform, 31.0f / 255.0f, 188.0f / 255.0f, 210.0f / 255.0f, 1.0f);
      GLES20.glUniformMatrix4fv(mModelViewProjectionUniform, 1, false, modelViewProjection, 0);
      GLES20.glUniform1f(mPointSizeUniform, 5.0f);

      GLES20.glDrawArrays(GLES20.GL_POINTS, 0, mNumPoints);
      GLES20.glDisableVertexAttribArray(mPositionAttribute);
      GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

      ShaderUtil.checkGLError(TAG, "Draw");
  }
 
Example #12
Source File: UgiRendererView.java    From UltraGpuImage with MIT License 6 votes vote down vote up
private static int loadTexture(Bitmap bitmap, boolean recycle) {
    int textures[] = new int[1];
    GLES20.glGenTextures(1, textures, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
            GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
            GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
            GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
            GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

    if (recycle) {
        bitmap.recycle();
    }

    return textures[0];
}
 
Example #13
Source File: VideoFrameDrawer.java    From SimpleVideoEditor with Apache License 2.0 6 votes vote down vote up
private void createFrameBuffer() {
    GLES20.glGenFramebuffers(1, fboFrame, 0);
    GLES20.glGenTextures(fboTexture.length, fboTexture, 0);
    for (int i = 0; i < fboTexture.length; i++) {
        // bind to fbo texture cause we are going to do setting.
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, fboTexture[i]);
        GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, surfaceWidth, surfaceHeight,
                0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
        // 设置缩小过滤为使用纹理中坐标最接近的一个像素的颜色作为需要绘制的像素颜色
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
        // 设置放大过滤为使用纹理中坐标最接近的若干个颜色,通过加权平均算法得到需要绘制的像素颜色
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
        // 设置环绕方向S,截取纹理坐标到[1/2n,1-1/2n]。将导致永远不会与border融合
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        // 设置环绕方向T,截取纹理坐标到[1/2n,1-1/2n]。将导致永远不会与border融合
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
        // unbind fbo texture.
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
    }
}
 
Example #14
Source File: OGLESShaderRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/*********************************************************************\
|* Shaders                                                           *|
\*********************************************************************/
protected void updateUniformLocation(Shader shader, Uniform uniform) {
    stringBuf.setLength(0);
    stringBuf.append(uniform.getName()).append('\0');
    updateNameBuffer();
    if (verboseLogging) {
        logger.log(Level.INFO, "GLES20.glGetUniformLocation({0}, {1})", new Object[]{shader.getId(), uniform.getName()});
    }
    int loc = GLES20.glGetUniformLocation(shader.getId(), uniform.getName());
    checkGLError();
    if (loc < 0) {
        uniform.setLocation(-1);
        // uniform is not declared in shader
        if (verboseLogging) {
            logger.log(Level.WARNING, "Uniform [{0}] is not declared in shader.", uniform.getName());
        }
    } else {
        uniform.setLocation(loc);
    }
}
 
Example #15
Source File: PhotoFilterView.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private int loadShader(int type, String shaderCode) {
    int shader = GLES20.glCreateShader(type);
    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);
    int[] compileStatus = new int[1];
    GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
    if (compileStatus[0] == 0) {
        if (BuildVars.LOGS_ENABLED) {
            FileLog.e(GLES20.glGetShaderInfoLog(shader));
        }
        GLES20.glDeleteShader(shader);
        shader = 0;
    }
    return shader;
}
 
Example #16
Source File: EGLSurfaceTexture.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/** Releases all allocated resources. */
@SuppressWarnings({"nullness:argument.type.incompatible"})
public void release() {
  handler.removeCallbacks(this);
  try {
    if (texture != null) {
      texture.release();
      GLES20.glDeleteTextures(1, textureIdHolder, 0);
    }
  } finally {
    if (display != null && !display.equals(EGL14.EGL_NO_DISPLAY)) {
      EGL14.eglMakeCurrent(
          display, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT);
    }
    if (surface != null && !surface.equals(EGL14.EGL_NO_SURFACE)) {
      EGL14.eglDestroySurface(display, surface);
    }
    if (context != null) {
      EGL14.eglDestroyContext(display, context);
    }
    // EGL14.eglReleaseThread could crash before Android K (see [internal: b/11327779]).
    if (Util.SDK_INT >= 19) {
      EGL14.eglReleaseThread();
    }
    if (display != null && !display.equals(EGL14.EGL_NO_DISPLAY)) {
      // Android is unusual in that it uses a reference-counted EGLDisplay.  So for
      // every eglInitialize() we need an eglTerminate().
      EGL14.eglTerminate(display);
    }
    display = null;
    context = null;
    surface = null;
    texture = null;
  }
}
 
Example #17
Source File: GreedyPVRTexturePixelBufferStrategy.java    From 30-android-libraries-in-30-days with Apache License 2.0 5 votes vote down vote up
@Override
public void loadPVRTextureData(final IPVRTexturePixelBufferStrategyBufferManager pPVRTexturePixelBufferStrategyManager, final int pWidth, final int pHeight, final int pBytesPerPixel, final PixelFormat pPixelFormat, final int pLevel, final int pCurrentPixelDataOffset, final int pCurrentPixelDataSize) throws IOException {
	/* Adjust buffer. */
	final Buffer pixelBuffer = pPVRTexturePixelBufferStrategyManager.getPixelBuffer(PVRTextureHeader.SIZE + pCurrentPixelDataOffset, pCurrentPixelDataSize);

	/* Send to hardware. */
	GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, pLevel, pPixelFormat.getGLInternalFormat(), pWidth, pHeight, 0, pPixelFormat.getGLFormat(), pPixelFormat.getGLType(), pixelBuffer);
}
 
Example #18
Source File: EquirectangularSphereShaderProgram.java    From Spectaculum with Apache License 2.0 5 votes vote down vote up
public void setMode(int mode) {
    if(mode < 0 || mode > 2) {
        throw new RuntimeException("mode must be in range [0, 2]");
    }
    use();
    GLES20.glUniform1i(mModeHandle, mode);
}
 
Example #19
Source File: GLES20Canvas.java    From Trebuchet with GNU General Public License v3.0 5 votes vote down vote up
private void setMatrix(ShaderParameter[] params, float x, float y, float width, float height) {
    Matrix.translateM(mTempMatrix, 0, mMatrices, mCurrentMatrixIndex, x, y, 0f);
    Matrix.scaleM(mTempMatrix, 0, width, height, 1f);
    Matrix.multiplyMM(mTempMatrix, MATRIX_SIZE, mProjectionMatrix, 0, mTempMatrix, 0);
    GLES20.glUniformMatrix4fv(params[INDEX_MATRIX].handle, 1, false, mTempMatrix, MATRIX_SIZE);
    checkError();
}
 
Example #20
Source File: GreedyPVRTexturePixelBufferStrategy.java    From tilt-game-android with MIT License 5 votes vote down vote up
@Override
public void loadPVRTextureData(final IPVRTexturePixelBufferStrategyBufferManager pPVRTexturePixelBufferStrategyManager, final int pWidth, final int pHeight, final int pBytesPerPixel, final PixelFormat pPixelFormat, final int pLevel, final int pCurrentPixelDataOffset, final int pCurrentPixelDataSize) throws IOException {
	/* Adjust buffer. */
	final Buffer pixelBuffer = pPVRTexturePixelBufferStrategyManager.getPixelBuffer(PVRTextureHeader.SIZE + pCurrentPixelDataOffset, pCurrentPixelDataSize);

	/* Send to hardware. */
	GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, pLevel, pPixelFormat.getGLInternalFormat(), pWidth, pHeight, 0, pPixelFormat.getGLFormat(), pPixelFormat.getGLType(), pixelBuffer);
}
 
Example #21
Source File: TextureParameters.java    From ShaderEditor with MIT License 5 votes vote down vote up
private static String wrapToShortcut(int wrap) {
	switch (wrap) {
		case GLES20.GL_CLAMP_TO_EDGE:
			return "c";
		case GLES20.GL_MIRRORED_REPEAT:
			return "m";
		default:
			return "r";
	}
}
 
Example #22
Source File: EngineRenderer.java    From 30-android-libraries-in-30-days with Apache License 2.0 5 votes vote down vote up
@Override
public void onDrawFrame(final GL10 pGL) {
	synchronized(GLState.class) {
		if (this.mMultiSampling && this.mConfigChooser.isCoverageMultiSampling()) {
			final int GL_COVERAGE_BUFFER_BIT_NV = 0x8000;
			GLES20.glClear(GL_COVERAGE_BUFFER_BIT_NV);
		}

		try {
			this.mEngine.onDrawFrame(this.mGLState);
		} catch (final InterruptedException e) {
			Debug.e("GLThread interrupted!", e);
		}
	}
}
 
Example #23
Source File: TextureRenderer.java    From SiliCompressor with Apache License 2.0 5 votes vote down vote up
private int loadShader(int shaderType, String source) {
    int shader = GLES20.glCreateShader(shaderType);
    checkGlError("glCreateShader type=" + shaderType);
    GLES20.glShaderSource(shader, source);
    GLES20.glCompileShader(shader);
    int[] compiled = new int[1];
    GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
    if (compiled[0] == 0) {
        GLES20.glDeleteShader(shader);
        shader = 0;
    }
    return shader;
}
 
Example #24
Source File: ColorFade.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private boolean initGLShaders(Context context) {
    int vshader = loadShader(context, com.android.internal.R.raw.color_fade_vert,
            GLES20.GL_VERTEX_SHADER);
    int fshader = loadShader(context, com.android.internal.R.raw.color_fade_frag,
            GLES20.GL_FRAGMENT_SHADER);
    GLES20.glReleaseShaderCompiler();
    if (vshader == 0 || fshader == 0) return false;

    mProgram = GLES20.glCreateProgram();

    GLES20.glAttachShader(mProgram, vshader);
    GLES20.glAttachShader(mProgram, fshader);
    GLES20.glDeleteShader(vshader);
    GLES20.glDeleteShader(fshader);

    GLES20.glLinkProgram(mProgram);

    mVertexLoc = GLES20.glGetAttribLocation(mProgram, "position");
    mTexCoordLoc = GLES20.glGetAttribLocation(mProgram, "uv");

    mProjMatrixLoc = GLES20.glGetUniformLocation(mProgram, "proj_matrix");
    mTexMatrixLoc = GLES20.glGetUniformLocation(mProgram, "tex_matrix");

    mOpacityLoc = GLES20.glGetUniformLocation(mProgram, "opacity");
    mGammaLoc = GLES20.glGetUniformLocation(mProgram, "gamma");
    mTexUnitLoc = GLES20.glGetUniformLocation(mProgram, "texUnit");

    GLES20.glUseProgram(mProgram);
    GLES20.glUniform1i(mTexUnitLoc, 0);
    GLES20.glUseProgram(0);

    return true;
}
 
Example #25
Source File: GLES20WallpaperRenderer.java    From alynx-live-wallpaper with Apache License 2.0 5 votes vote down vote up
@Override
public void onDrawFrame(GL10 gl10) {
    if (surfaceTexture == null) {
        return;
    }

    if (renderedFrame < updatedFrame) {
        surfaceTexture.updateTexImage();
        ++renderedFrame;
        // Utils.debug(
        //     TAG, "renderedFrame: " + renderedFrame + " updatedFrame: " + updatedFrame
        // );
    }

    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    GLES20.glUseProgram(program);
    GLES20.glUniformMatrix4fv(mvpLocation, 1, false, mvp, 0);
    // No vertex array in OpenGL ES 2.
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]);
    GLES20.glEnableVertexAttribArray(positionLocation);
    GLES20.glVertexAttribPointer(
        positionLocation, 2, GLES20.GL_FLOAT, false, 2 * BYTES_PER_FLOAT, 0
    );
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[1]);
    GLES20.glEnableVertexAttribArray(texCoordLocation);
    GLES20.glVertexAttribPointer(
        texCoordLocation, 2, GLES20.GL_FLOAT, false, 2 * BYTES_PER_FLOAT, 0
    );
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, buffers[2]);
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_INT, 0);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
    GLES20.glDisableVertexAttribArray(texCoordLocation);
    GLES20.glDisableVertexAttribArray(positionLocation);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    GLES20.glUseProgram(0);
}
 
Example #26
Source File: YuvConverter.java    From webrtc_android with MIT License 5 votes vote down vote up
@Override
public void onPrepareShader(GlShader shader, float[] texMatrix, int frameWidth, int frameHeight,
    int viewportWidth, int viewportHeight) {
  GLES20.glUniform4fv(coeffsLoc, /* count= */ 1, coeffs, /* offset= */ 0);
  // Matrix * (1;0;0;0) / (width / stepSize). Note that OpenGL uses column major order.
  GLES20.glUniform2f(
      xUnitLoc, stepSize * texMatrix[0] / frameWidth, stepSize * texMatrix[1] / frameWidth);
}
 
Example #27
Source File: GLTwoInputProgram.java    From Pano360 with MIT License 5 votes vote down vote up
@Override
public void create() {
    super.create();
    maTexture2Handle = GLES20.glGetAttribLocation(getProgramId(), "aTextureCoord2");
    checkGlError("glGetAttribLocation aTextureCoord");
    if (maTexture2Handle == -1) {
        throw new RuntimeException("Could not get attrib location for aTextureCoord2");
    }
    uTextureSamplerHandle= GLES20.glGetUniformLocation(getProgramId(),"sTexture");
    checkGlError("glGetUniformLocation uniform samplerExternalOES sTexture");

}
 
Example #28
Source File: EglUtil.java    From SimpleVideoEditor with Apache License 2.0 5 votes vote down vote up
/**
 * Checks for a GL error using {@link GLES20#glGetError()}.
 *
 * @throws RuntimeException if there is a GL error
 */
static void checkGlError() {
    int errorCode;
    if ((errorCode = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
        throw new RuntimeException("gl error: " + Integer.toHexString(errorCode));
    }
}
 
Example #29
Source File: FilterGroup.java    From Pano360 with MIT License 5 votes vote down vote up
public void drawToFBO(int textureId,FBO fbo) {
    runPreDrawTasks();
    if (fboList==null || fbo==null) {
        return;
    }
    int size = filters.size();
    int previousTexture = textureId;
    for (int i = 0; i < size; i++) {
        AbsFilter filter = filters.get(i);
        Log.d(TAG, "onDrawFrame: "+i+" / "+size +" "+filter.getClass().getSimpleName()+" "+
                filter.surfaceWidth+" "+filter.surfaceHeight);
        if (i < size - 1) {
            filter.setViewport();
            fboList[i].bind();
            GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            if(filter instanceof FilterGroup){
                ((FilterGroup) filter).drawToFBO(previousTexture,fboList[i]);
            }else{
                filter.onDrawFrame(previousTexture);
            }
            fboList[i].unbind();
            previousTexture = fboList[i].getFrameBufferTextureId();
        }else{
            fbo.bind();
            filter.setViewport();
            if(filter instanceof FilterGroup){
                ((FilterGroup) filter).drawToFBO(previousTexture,fbo);
            }else{
                filter.onDrawFrame(previousTexture);
            }
            fbo.unbind();
        }
    }
}
 
Example #30
Source File: RotationFilterRender.java    From rtmp-rtsp-stream-client-java with Apache License 2.0 5 votes vote down vote up
@Override
protected void initGlFilter(Context context) {
  String vertexShader = GlUtil.getStringFromRaw(context, R.raw.simple_vertex);
  String fragmentShader = GlUtil.getStringFromRaw(context, R.raw.simple_fragment);

  program = GlUtil.createProgram(vertexShader, fragmentShader);
  aPositionHandle = GLES20.glGetAttribLocation(program, "aPosition");
  aTextureHandle = GLES20.glGetAttribLocation(program, "aTextureCoord");
  uMVPMatrixHandle = GLES20.glGetUniformLocation(program, "uMVPMatrix");
  uSTMatrixHandle = GLES20.glGetUniformLocation(program, "uSTMatrix");
  uSamplerHandle = GLES20.glGetUniformLocation(program, "uSampler");
}