googlecreativelab / shadercam

Simple OpenGL Shaders with the camera2 apis in Android 5.0+
http://www.androidexperiments.com
Other
241 stars 63 forks source link

addTexture() and updateTexture() functions do nothing #6

Open mcghorayeb opened 8 years ago

mcghorayeb commented 8 years ago

I want to render a Canvas object into the camera surface so it appears in the recording. After googling it I found that it is done by adding a the bitmap from the canvas as a texture. I found something that does this in your app: the "addTexture" and the "updateTexture" functions, however It's been two working weeks and I'm trying to get this to work, with no result.

I tried extracting the canvas to a file as a bitmap to check if it is drawing successfully, and it does. It seems that there is a problem with those functions found in the "CameraRenderer.java".

Please I need your help, I don't know whether those functions should be used/working or not, nor if this is the way to do it. I also tried to do it using codes copied from from google search without success. Please let me know if it is an error in the function, or if it is not the way it's done, and in both cases, I am kindly asking for your support to do it, because it's been the only way to do my "drawing on camera while recording" app. I tried many ways and found the openGL way the best one.

trippedout commented 8 years ago

Can you please paste some snippets/gist of how you have already tried? addTexture is just a helper function and if you pass incorrect type the shader compilation errors should explain how to fix this

mcghorayeb commented 7 years ago

Yes of course, this is my subclass of CameraRenderer (A modified copy of Example Renderer):

`public class WriteDrawRenderer extends CameraRenderer { private float offsetR = 1f; private float offsetG = 1f; private float offsetB = 1f;

private float touchX = 1000000000;
private float touchY = 1000000000;

private  Bitmap textBitmap;

private int textureId;

private boolean isFirstTime = true;

//creates a new canvas that will draw into a bitmap instead of rendering into the screen
private Canvas bitmapCanvas;

/**
 * By not modifying anything, our default shaders will be used in the assets folder of shadercam.
 *
 * Base all shaders off those, since there are some default uniforms/textures that will
 * be passed every time for the camera coordinates and texture coordinates
 */
public WriteDrawRenderer(Context context, SurfaceTexture previewSurface, int width, int height)
{
    super(context, previewSurface, width, height, "touchcolor.frag.glsl", "touchcolor.vert.glsl");
    //other setup if need be done here

}

/**
 * we override {@link #setUniformsAndAttribs()} and make sure to call the super so we can add
 * our own uniforms to our shaders here. CameraRenderer handles the rest for us automatically
 */
@Override
protected void setUniformsAndAttribs()
{
    super.setUniformsAndAttribs();

    int offsetRLoc = GLES20.glGetUniformLocation(mCameraShaderProgram, "offsetR");
    int offsetGLoc = GLES20.glGetUniformLocation(mCameraShaderProgram, "offsetG");
    int offsetBLoc = GLES20.glGetUniformLocation(mCameraShaderProgram, "offsetB");

    GLES20.glUniform1f(offsetRLoc, offsetR);
    GLES20.glUniform1f(offsetGLoc, offsetG);
    GLES20.glUniform1f(offsetBLoc, offsetB);

    if (touchX < 1000000000 && touchY < 1000000000)
    {
        //creates a Paint object
        Paint yellowPaint = new Paint();
        //makes it yellow
        yellowPaint.setColor(Color.YELLOW);
        //sets the anti-aliasing for texts
        yellowPaint.setAntiAlias(true);
        yellowPaint.setTextSize(70);

        if (isFirstTime)
        {
            textBitmap = Bitmap.createBitmap(mSurfaceWidth, mSurfaceHeight, Bitmap.Config.ARGB_8888);
            bitmapCanvas = new Canvas(textBitmap);
        }

        bitmapCanvas.drawText("Maroun Ghorayeb", touchX, touchY, yellowPaint);

        if (isFirstTime)
        {
            textureId = addTexture(textBitmap, "textBitmap");
            isFirstTime = false;
        }
        else
        {
            updateTexture(textureId, textBitmap);
        }

        touchX = 1000000000;
        touchY = 1000000000;
    }
}

/**
 * take touch points on that textureview and turn them into multipliers for the color channels
 * of our shader, simple, yet effective way to illustrate how easy it is to integrate app
 * interaction into our glsl shaders
 * @param rawX raw x on screen
 * @param rawY raw y on screen
 */
public void setTouchPoint(float rawX, float rawY)
{
    this.touchX = rawX;
    this.touchY = rawY;
}

}`

I modified the camera renderer a little bit by just removing the bitmap.recycle function because I need my bitmap again to keep the canvas saved.

Thanks in advance for your help.