- Home /
Using Rendertexture on Android Native JAR
Hello, I'm trying to render an image using GLES in an Android Java library. When I try to clear the color buffer or attempt to draw to the binded rendertexture, I get a glerror 1286. From what I researched, this error is a result of an invalid framebuffer being used. Are unity rendertextures the same as a framebuffer? How should I use a GL_TEXTURE_EXTERNAL_OES texture in Unity?
Here's what I have so far:
Java:
public void drawFrame(SurfaceTexture st, boolean invert, int renderTexture) {
//bind to unity rendertexture
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, renderTexture);
checkGlError("glBindFramebuffer");
GLES20.glViewport(0,0, 512, 512);
checkGlError("glViewport");
GLES20.glDisable(GLES20.GL_DEPTH_TEST);
checkGlError("onDrawFrame start");
st.getTransformMatrix(mSTMatrix);
if (invert) {
mSTMatrix[5] = -mSTMatrix[5];
mSTMatrix[13] = 1.0f - mSTMatrix[13];
}
GLES20.glUseProgram(mProgram);
checkGlError("glUseProgram");
//binding the gl texture oes from an android surfacetexture
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
mTriangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false,
TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
checkGlError("glVertexAttribPointer maPosition");
GLES20.glEnableVertexAttribArray(maPositionHandle);
checkGlError("glEnableVertexAttribArray maPositionHandle");
mTriangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
GLES20.glVertexAttribPointer(maTextureHandle, 2, GLES20.GL_FLOAT, false,
TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
checkGlError("glVertexAttribPointer maTextureHandle");
GLES20.glEnableVertexAttribArray(maTextureHandle);
checkGlError("glEnableVertexAttribArray maTextureHandle");
Matrix.setIdentityM(mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0);
//draw the surfacetexture to the rendertexture returning glError 1286
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
checkGlError("glDrawArrays");
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
//unbind rendertexture
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
}
Unity C#:
if (!textureTarget)
{
textureTarget = new RenderTexture(512, 512, 16, RenderTextureFormat.ARGB32);
textureTarget.filterMode = FilterMode.Point;
textureTarget.wrapMode = TextureWrapMode.Clamp;
textureTarget.Create();
if (material) material.sharedMaterial.mainTexture = textureTarget;
}
//update surfacetexture
exoPlayer.Call("updateTexture");
//draw to rendertexture
exoPlayer.Call("drawFrame", (int)textureTarget.GetNativeTexturePtr());
did you solve your issue? I'm trying to achieve something similar
When Unity can parse the #extension correctly on 5.4, I wrote the shader in Unity rather than in native. Right now we are using a 3rd party solution to render out the video. If you want to still use the GL_OES_EGL_image_external extension, I have to set the texture pointer in native and draw in Unity.
thanks. will a simple texture without any frame buffer be enough? And by "se the texture pointer in native" you mean creating it native code rather than in unity-side?
Answer by Kajos · Jul 15, 2016 at 03:01 PM
You'll need to adjust the shader to use the GL_TEXTURE_EXTERNAL_OES texture as well, else it won't work. Add to your shader instead of sampler2D:
#extension GL_OES_EGL_image_external : require
uniform samplerExternalOES texture
Used to not work before, but starting on Unity 5.4 (only tested on beta), setting the extensions worked. Not sure if it is still working on the lastest Unity as we're using a 3rd party plugin to render the video.
Your answer

Follow this Question
Related Questions
Complex Depth Shader 0 Answers
Having issue with "unable to convert class to dex format" extremely simple JAR in empty project. 0 Answers
How to load android app when the button clicked in unity scene? 4 Answers
Reading Native Surface Texture into Streaming Unity Texture [Android]? 3 Answers
Rendering a screen aligned quad with Graphics.blit - not working? 0 Answers