android - 在 OpenGL ES 2.0 着色器中使用纹理作为调色板

标签 android opengl-es glsl shader

我正在使用 OpenGL ES 2.0 着色器生成分形。它运作良好,直到我决定黑白不够,我需要一个调色板。我将调色板作为 1D 纹理传递给着色器,但我得到的只是黑屏。

着色器基于this one ,传递的纹理是 2D (nx1),因为 OpenGL ES 2.0 不允许 1D 纹理,因此像素颜色由

gl_FragColor = texture2D(palette, vec2((j == int(iterations) ? 0.0 : float(j)) / iterations, 0.5));

(我不确定这里的 0.5)。

相关纹理加载代码:

    Bitmap bitmap = Bitmap.createBitmap(colors, colors.length, 1, Bitmap.Config.ARGB_8888);
    int handle = ShaderUtils.loadTexture(bitmap);
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, handle);
    GLES20.glUniform1i(paletteHandle, handle);

[...]

public static int loadTexture(Bitmap bitmap)
{
    final int[] textureHandle = new int[1];
    GLES20.glGenTextures(1, textureHandle, 0);
    if (textureHandle[0] == 0)
    {
        throw new RuntimeException("Error generating texture name.");
    }
    // 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();

    return textureHandle[0];
}

顶点着色器:

attribute vec4 vPosition;

void main() {
  gl_Position = vPosition;
}

fragment 着色器:

precision mediump float;

uniform sampler2D palette;
uniform float centerX;
uniform float centerY;
uniform float scale;
uniform float iterations;
uniform vec2 resolution;
#define maxiter 1024
void main() {
    vec2 center = vec2(centerX, centerY);
    vec2 coord = vec2(gl_FragCoord.x, gl_FragCoord.y) / resolution;
    vec2 c = (coord - center) / scale;
    int j = 0;
    vec2 z = c;
    for(int i = 0; i<maxiter; i++) {
        if (float(i) >= iterations) break;
        j++;
        float x = (z.x * z.x - z.y * z.y) + c.x;
        float y = (z.y * z.x + z.x * z.y) + c.y;

        if((x * x + y * y) > 4.0) break;
        z.x = x;
        z.y = y;
    }

    gl_FragColor = texture2D(palette, vec2((j == int(iterations) ? 0.0 : float(j)) / iterations, 0.5));
//    vec3 color = vec3(float(j)/float(iterations));
//    gl_FragColor = vec4(color, 1.0);
}

问题是这很难调试。在 IDE 内部,我确保位图包含正确的数据,并且 logcat 中没有 opengl 错误。着色器在没有纹理的情况下工作,所以这可能是这里的问题。可能是什么原因?

最佳答案

你必须为纹理采样器统一设置的值不是纹理对象的“名称”,它必须是纹理单元的索引:

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, handle);

// this is wrong
//GLES20.glUniform1i(paletteHandle, handle);

GLES20.glUniform1i(paletteHandle, 0); // 0, because of GLES20.GL_TEXTURE0


参见 OpenGL 4.6 API Compatibility Profile Specification; 7.10 Samplers; page 154 :

Samplers are special uniforms used in the OpenGL Shading Language to identify the texture object used for each texture lookup. The value of a sampler indicates the texture image unit being accessed. Setting a sampler’s value to i selects texture image unit number i.

关于android - 在 OpenGL ES 2.0 着色器中使用纹理作为调色板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49585086/

相关文章:

Android 在 fragment 上方滑动布局

android - RelativeLayout 中 XML 文件中 View 的 getTop()/getLeft 不起作用

Android无法使用greendao使用sqlcipher加密数据库

android - GL_MAX_TEXTURE_SIZE 较低的 Android 设备上的纹理不佳

matrix - 三.js shader 点光源位置

android - 如何使用 -debug.aar 文件名删除 bintray 上传文件

android - cocos2dx 应用程序在恢复 Activity 时崩溃

android - 将真实世界坐标映射到 OpenGL 坐标系

java - 尽管链接和验证成功,为什么着色器程序无法编译?

c++ - 为什么 OSX Mavericks 不编译我的 GLSL 着色器?