0

I will like to load textures during run time ( so I dont need to load all of the texture)

If I try to load a texture during run time with this function, I get the RunTimeException ( I think that is because the context is already created)

Is any way to do this without having 2 OpenlGL context(I read that this may cause error if the drivers are bad implemented)?

public static int loadTexture(final Context context, final int resourceId)
{
    final int[] textureHandle = new int[1];

    GLES20.glGenTextures(1, textureHandle, 0);

    if (textureHandle[0] != 0)
    {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;   // No pre-scaling

        // Read in the resource
        final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);

        // Bind to the texture in OpenGL
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);

        // Set filtering
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

        // Load the bitmap into the bound texture.
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

        // Recycle the bitmap, since its data has been loaded into OpenGL.
        bitmap.recycle();                       
    }

    if (textureHandle[0] == 0)
    {
        throw new RuntimeException("Error loading texture.");
    }

    return textureHandle[0];
}

1 Answer 1

1

I haven't touched OpenGL ES in Java, only C++ but I would have thought this would be perfectly OK - are you trying to create this texture on a different thread to the thread you created the OpenGL context on? Did your OpenGL context creation code succeed?

Sign up to request clarification or add additional context in comments.

3 Comments

So I need to create another OpenGL context in order to charge the texture and then use in the same one?
You should be able to use the same context to create all your textures at any time. You need to perform this all on the same thread as the one you created the context on. glGetError() should provide some useful debugging information, stick that after each GL call and see which one breaks, once you have that information it might be a little clearer as to why its not working.
the comment of @user4627255 worked for me : "You need to perform this all on the same thread as the one you created the context on" -> on OpenGL android this can be achieved with GlSurfaceView.queueEvent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.