Opengl es 在OpenGLES着色器中使用LUT进行假着色

Opengl es 在OpenGLES着色器中使用LUT进行假着色,opengl-es,glsl,fragment-shader,glsles,Opengl Es,Glsl,Fragment Shader,Glsles,我试图使用片段着色器将灰度图像转换为彩色输出。这个想法相当直截了当;制作三个长度为256的数组,并将灰度值[0255]映射到RGB(3*[0255])。下面是我编写的代码: #extension GL_OES_EGL_image_external : require precision mediump float; varying vec2 vTextureCoord; uniform samplerExternalOES sTexture; const int red[256] = int[

我试图使用片段着色器将灰度图像转换为彩色输出。这个想法相当直截了当;制作三个长度为256的数组,并将灰度值[0255]映射到RGB(3*[0255])。下面是我编写的代码:

#extension GL_OES_EGL_image_external : require
precision mediump float;
varying vec2 vTextureCoord;
uniform samplerExternalOES sTexture;

const int red[256] = int[256](255, 128, 240, ... , 143); // declaring as const/non-const changes the output
const int green[256] = int[256](230, 120, 34, ... , 100);
const int blue[256] = int[256](90, 73, 99, ... , 42);

void main (void) {
    float r, g, b, y;
    y = texture2D(sTexture, vTextureCoord).r; // using only the r value from YUV received
    y = y * 255.0; // convert y from [0,1] to [0,255]

    int index = int(y); // use y as an index to map value to the lookup arrays

    r = float(red[index]) / 255.0;
    g = float(green[index]) / 255.0;
    b = float(blue[index]) / 255.0;

    // Set the RGB color of our pixel
    gl_FragColor = vec4(r, g, b, 1.0); 
    // here, if I were to instead assign gl_FragColor = vec4(y,y,y, 1.0); I'd see the expected greyscale output
}
我对OpenGL相当陌生,但我已经能够使用简单的公式并获得假彩色输出,其代码如下所示:

y = y * 255;
r = y * 0.47446 + 0.65658;
g = y * 0.16213 + 0.58145;
b = y * 0.047524+ 0.75203;
gl_FragColor = vec4(r / 255.0, g / 255.0, b / 255.0, 1.0); 
然而,使用LUT的输出相当奇怪。将int数组声明为
const
只给出第0个索引和第255个索引的输出;中间没有颜色。没有
const
时,代码会与
SIGSEGV
一起崩溃


有没有其他人尝试过这样做/知道我在LUT代码中可能做错了什么?

我不知道为什么这样做不起作用,但更传统的方法是将LUT存储为256x1纹理,然后只进行纹理查找

void main (void) {
    float luminance = texture2D(inputTexture, vTextureCoord).r;
    gl_FragColor = texture2D(lutTexture, vec2(luminance, 0.5));
}

。。。可能会更快、更灵活。

我对格尔斯还是个新手。我可以制作一个256x1纹理并将它从CPU传递到GL,但是
vec2(lum,0.5)
行做什么呢?你能给出一个更可靠的答案吗?你需要一个二维纹理坐标来进行纹理查找,因此
vec2(lum,0.5)
使用原始数据中的亮度值作为x坐标,并固定0.5作为y坐标。纹理坐标介于0.0和1.0之间,因此您不需要担心输入的分辨率-这一切都“正常工作”。