1

我想知道是否有一种方法可以处理 OpenGL 纹理缓冲区,以便通过我选择的某些公式将灰度值缓冲区动态转换为 rgb 值。

我已经有一个类似的函数,它运行良好但输出一个颜色为 3 的向量。

rgb convertToColor(float value);

我对 OpenGL 很陌生,想知道我应该使用哪种着色器以及它应该去哪里。我的程序目前这样循环帧:

program1.bind();
program1.setUniformValue("texture", 0);
program1.enableAttributeArray(vertexAttr1);
program1.enableAttributeArray(vertexTexr1);
program1.setAttributeArray(vertexAttr1, vertices.constData());
program1.setAttributeArray(vertexTexr1, texCoords.constData());
glBindTexture(GL_TEXTURE_2D, textures[0]);
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,widthGL,heightGL, GL_LUMINANCE,GL_UNSIGNED_BYTE, &displayBuff[displayStart]);
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
glBindTexture(GL_TEXTURE_2D,0);
//...

着色器

QGLShader *fshader1 = new QGLShader(QGLShader::Fragment, this);
    const char *fsrc1 =
        "uniform sampler2D texture;\n"
        "varying mediump vec4 texc;\n"
        "void main(void)\n"
        "{\n"
        "    gl_FragColor = texture2D(texture, texc.st);\n"
        "}\n";

我正在尝试在 matlab 中重新创建效果,例如 imagesc,如下图所示: 图像c

4

2 回答 2

6

Something like this would work:

    "uniform sampler2D texture;\n"
    "uniform sampler1D mappingTexture;\n"
    "varying mediump vec4 texc;\n"
    "void main(void)\n"
    "{\n"
    "    gl_FragColor = texture1D(mappingTexture, texture2D(texture, texc.st).s);\n"
    "}\n";

where mapping texture is 1D textuer that maps grayscale to color.

Of course, you could also write function that calculates rgb color based on grayscale, but (depending on your hardware) it might be faster just to do texture lookup.

it is not clear how to bind two buffers to an OpenGL program at once

Binding textures to samplers.

于 2013-06-14T03:16:31.953 回答
1

看起来您已经加载并设置了一个程序来读取纹理(代码中的 program1)。假设顶点着色器已经设置为传递像素着色器纹理坐标以查找纹理(在下面的程序中这是“texcoord”),您应该能够将像素着色器更改为如下所示:

uniform texture2d texture;   // this is the greyscale texture
varying vec2 texcoord;       // passed from your vertex shader

void main() 
{  
    float luminance = tex2D(texture, texcoord).r;     // grab luminance texture
    gl_FragColor = convertToColor(luminance);         // run your function
}

这会读取亮度纹理并调用将亮度转换为颜色的函数。如果您的函数仅返回一个 3 分量 rgb 向量,您可以将最后一行更改为:

gl_FragColor = vec4(convertToColor(luminance), 1.0);
于 2013-06-14T02:48:13.073 回答