OpenGL OSG ::纹理乘法只显示一种颜色

问题描述 投票:0回答:1

我的目标是“复制”两个OSG :: Textures,其中一个是光分布(RGBA图片),另一个是我生成的黑白滤镜Image / Texture。目前,两个纹理具有相同的大小,即使它可能很好,即使它们很好。您可以想象,我正在尝试从浅色纹理中删除滤镜纹理上的黑色区域。

目前,我的代码可以使整个屏幕变成一种颜色,无论如何。我认为gl_MultiTexCoord0gl_TexCoord[0]周围的内容不正确。我看过多个在线资源,但没有其他可用的方法。

可悲的是,我必须使用旧版本的glew(2.1.0)[->?由于框架限制,OpenGL #version 120]和OSG(3.0.1)。

因为什么都无法正常工作,所以我现在仅尝试显示filterTexture(因为如果显示lightTexture,则所有内容均为黑色)...在init和update内部,我有多个“ checkError”,以查看发生了什么,但是atm没有错误。这是我的着色器:

// vertex shader
const char* shader_combine_vertex = "\n\
#version 120                                    \n\
                                                \n\
void main()                                     \n\
{                                               \n\
    // homogeneous vertex position              \n\
    gl_Position = ftransform();                 \n\
    //Texture coordinate of texture unit 0 and 1\n\
    gl_TexCoord[0] = gl_MultiTexCoord0;         \n\
    gl_TexCoord[1] = gl_MultiTexCoord1;         \n\
}                                               \n";


// fragment shader
const char* shader_combine_fragment = "                                     \n\
#version 120                                                                \n\
// input                                                                    \n\
uniform sampler2D lightTexture;                                             \n\
uniform sampler2D filterTexture;                                            \n\
//varying vec2 lightTexCoord;                                               \n\
//varying vec2 filterTexCoord;                                              \n\
                                                                            \n\
void main()                                                                 \n\
{                                                                           \n\
    gl_FragColor = texture2D(filterTexture, gl_TexCoord[1].st);             \n\
                                                                            \n\
    // gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); //works and shows red       \n\
                                                                            \n\
    // gl_FragColor = texture2D(filterTexture, filterTexCoord);             \n\
                                                                            \n\
    // vec4 texel = texture2D(lightTexture, gl_TexCoord[0].st);             \n\
    // gl_FragColor = texel * texture2D(filterTexture, gl_TexCoord[1].st);  \n\
}\n";

以及具有有效OpenGL上下文的全局变量以及init和update函数:

    /** textures for each lightSourceID **/
    std::map<int, GLuint> vp_ShaderCombine;
    std::map<int, GLuint> fp_ShaderCombine;
    /** Program linking shaders **/
    std::map<int, GLhandleARB> combineProgram;

    // TODO: maybe generate all texture IDs at once, but works for now
    std::map<int, unsigned int> g_uiSceneTex[2]; // for every sensor - 2 texture IDs (Light | Filter)

---------

bool initShaders(int id, int width, int height, char* texture)
{
    // Shaders compilation
    vp_ShaderCombine.insert(std::pair<int, GLuint>(id, glCreateShaderObjectARB(GL_VERTEX_SHADER)));
    fp_ShaderCombine.insert(std::pair<int, GLuint>(id, glCreateShaderObjectARB(GL_FRAGMENT_SHADER)));

    glShaderSource(vp_ShaderCombine.at(id), 1, &shader_combine_vertex, NULL);
    glShaderSource(fp_ShaderCombine.at(id), 1, &shader_combine_fragment, NULL);

    glCompileShader(vp_ShaderCombine.at(id));
    checkShaderCompileError(vp_ShaderCombine.at(id), "vertex");
    glCompileShader(fp_ShaderCombine.at(id));
    checkShaderCompileError(fp_ShaderCombine.at(id), "fragment");

    combineProgram[id] = glCreateProgram();
    glAttachShader(combineProgram[id], vp_ShaderCombine[id]);
    glAttachShader(combineProgram[id], fp_ShaderCombine[id]);

    glLinkProgram(combineProgram.at(id));
    glValidateProgram(combineProgram.at(id));

    // Check result and display errors if any
    ...
    return ...;
}

void update()
{
    for (... every light source id ...)
    {
        glEnable(GL_TEXTURE_2D);
        glEnable(GL_LIGHTING);
        // ------------  NECESSARY? ----------------
        glDisable(GL_DEPTH_TEST);
        glDisable(GL_CULL_FACE);
        glDisable(GL_BLEND);
        // ----------- LIGHT----------------
        // mainly the same as filter just on g_uiSceneTex[id][0]
        // ----------- FILTER ----------------
        glUseProgramObjectARB(combineProgram[id]); // call before each glActiveTexture (according to some SO post)
        glActiveTexture(GL_TEXTURE0 + 1);
        // TODO: maybe generate all texture IDs at once, but works for now
        glGenTextures(1, &g_uiSceneTex[id][1]); // generate texture names
        glBindTexture(GL_TEXTURE_2D, g_uiSceneTex[id][1]);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, itSHM->myOsgImg->s(), itSHM->myOsgImg->t(), 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, itSHM->myOsgImg);

        GLint filterTexUnitLoc = glGetUniformLocation(combineProgram[id], "filterTexture");
        glUniform1i(filterTexUnitLoc, 0);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);

        // render
        // draw QUAD
        glBegin(GL_QUADS);
        glTexCoord2f(0, 0);
        glVertex3f(0, 1, -1);
        glTexCoord2f(0, ScreenHeight);
        glVertex3f(0, 0, -1);
        glTexCoord2f(ScreenWidth, ScreenHeight);
        glVertex3f(1, 0, -1);
        glTexCoord2f(ScreenWidth, 0);
        glVertex3f(1, 1, -1);
        glEnd();

        // Disable Shaders
        glUseProgramObjectARB(0);

        glDisable(GL_TEXTURE_2D);
        // NECESSARY?
        myShmImages[0].myOsgImg->dirty();
        myPhotometries[0].myOSGTexture->dirtyTextureObject();
    }
}

现在在我这边出现多个问题:

  1. 着色器有什么问题,它们仅显示一种颜色?我认为他们只评估一个像素。
  2. 使用这些gl函数与使用OSG本机函数创建着色器有什么区别?有限制吗,或者我的设置可行吗?是“更轻松”吗?关于该主题的任何好读物,我都没有找到?
  3. 更新代码看起来很长且不必要,在每次更新时是否都需要生成/创建纹理,还是只能将其初始化一次?
  4. 当前,纹理显示为全屏,但是我想在OSG树的“光”位置显示它。我具有光分布的视锥,因此,我认为,我只需要使用该视/相机/视锥“映射”新的过滤纹理即可。]
c++ opengl shader fragment-shader openscenegraph
1个回答
0
投票

您根本不提供gl_Multitexcoord1

    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex3f(0, 1, -1);
    [...]
    glEnd();

此代码仅提供gl_MultiTexCoord0的数据。 If每个纹理确实需要不同的texcoord,然后需要通过glMultiTexCoord为每个顶点提供它们:

    glBegin(GL_QUADS);
    glMultiTexCoord2f(GL_TEXTURE0, 0, 0);
    glMultiTexCoord2f(GL_TEXTURE1, ..., ...);
    glVertex3f(0, 1, -1);
    [...]
    glEnd();

但是,如果您确实不需要其他值(您的问题尚不清楚),也可以对这两种纹理使用一组纹理坐标。

请注意,您的代码已经过时,并且在GL中已弃用自2008年以来

。即使限于GL2.x(作为着色器目标),也应never使用内置属性,例如gl_MultiTexCoord*等。使用通用顶点属性,并将数据作为顶点数组提供给顶点缓冲区对象。
© www.soinside.com 2019 - 2024. All rights reserved.