Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opengl ES 2.0 : Get texture size and other info

The context of the question is OpenGL ES 2.0 in the Android environment. I have a texture. No problem to display or use it.

Is there a method to know its width and height and other info (like internal format) simply starting from its binding id?

I need to save texture to bitmap without knowing the texture size.

like image 718
xcesco Avatar asked May 09 '15 12:05

xcesco


2 Answers

Not in ES 2.0. It's actually kind of surprising that the functionality is not there. You can get the size of a renderbuffer, but not the size of a texture, which seems inconsistent.

The only thing available are the values you can get with glGetTexParameteriv(), which are the FILTER and WRAP parameters for the texture.

It's still not in ES 3.0 either. Only in ES 3.1, glGetTexLevelParameteriv() was added, which gives you access to all the values you're looking for. For example to get the width and height of the currently bound texture:

int[] texDims = new int[2];
GLES31.glGetTexLevelParameteriv(GLES31.GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, texDims, 0);
GLES31.glGetTexLevelParameteriv(GLES31.GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, texDims, 1);
like image 97
Reto Koradi Avatar answered Nov 17 '22 03:11

Reto Koradi


As @Reto Koradi said there is no way to do it but you can store the width and height of a texture when you are loading it from android context before you bind it in OpenGL.

    AssetManager am = context.getAssets();
    InputStream is = null;
    try {
        is = am.open(name);
    } catch (IOException e) {
        e.printStackTrace();
    }
    final Bitmap bitmap = BitmapFactory.decodeStream(is);
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    // here is you bind your texture in openGL
like image 1
Wallstrider Avatar answered Nov 17 '22 04:11

Wallstrider