简体   繁体   English

Opengl ES 2.0:获取纹理大小和其他信息

[英]Opengl ES 2.0 : Get texture size and other info

The context of the question is OpenGL ES 2.0 in the Android environment. 问题的背景是Android环境中的OpenGL ES 2.0。 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? 是否有一种方法可以从其绑定ID开始知道它的宽度和高度以及其他信息(如内部格式)?

I need to save texture to bitmap without knowing the texture size. 我需要在不知道纹理大小的情况下将纹理保存到位图。

Not in ES 2.0. 不在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. 唯一可用的是glGetTexParameteriv()可以获得的值,它们是纹理的FILTERWRAP参数。

It's still not in ES 3.0 either. 它仍然不在ES 3.0中。 Only in ES 3.1, glGetTexLevelParameteriv() was added, which gives you access to all the values you're looking for. 仅在ES 3.1中添加了glGetTexLevelParameteriv() ,这使您可以访问所需的所有值。 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);

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. 正如@Reto Koradi所说,没有办法做到这一点但你可以在从Android上下文加载它之前存储纹理的宽度和高度,然后再在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

I'll suggest a hack for doing this. 我会建议你这样做。 Use ESSL's textureSize function. 使用ESSL的textureSize函数。 To access its result from the CPU side you're going to have to pass the texture as an uniform to a shader, and output the texture size as the r & g components of your shader output. 要从CPU端访问其结果,您必须将纹理作为制服传递到着色器,并将纹理大小输出为着色器输出的r&g组件。 Apply this shader to an 1x1px primitive drawn to a 1x1px FBO, then readback the drawn value from the GPU with glReadPixels . 将此着色器应用于绘制到1x1px FBO的1x1px基元,然后使用glReadPixels从GPU回读绘制的值。

You'll have to be careful with rounding, clamping and FBO formats. 你必须小心舍入,夹紧和FBO格式。 You may need a 16-bit integer FBO format. 您可能需要16位整数FBO格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM