glReadPixels不使用GL_FLOAT

时间:2018-05-25 10:58:57

标签: opengl glsl shader framebuffer

我正在使用OpenGL着色器并尝试打印颜色值。

所以,我使用了glReadPixels函数。它与GL_UNSIGNED_BYTE合作,但不与GL_FLOAT合作。

我还有什么其他的吗?

这是我的代码。

void settingFrameBuffers()
{
    glGenFramebuffers(NUM_FBO, fbo);
    glGenTextures(NUM_FBO, tex);

    for (int i = 0; i < NUM_FBO; i++)
    {
        glBindFramebuffer(GL_FRAMEBUFFER, fbo[i]);
        glBindTexture(GL_TEXTURE_2D, tex[i]);

        if (i == 0) glActiveTexture(GL_TEXTURE0);
        else if (i == 1) glActiveTexture(GL_TEXTURE1);
        else if (i == 2) glActiveTexture(GL_TEXTURE2);
        else if (i == 3) glActiveTexture(GL_TEXTURE3);
        else if (i == 4) glActiveTexture(GL_TEXTURE4);
        else if (i == 5) glActiveTexture(GL_TEXTURE5);

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, NULL);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex[i], 0);

        GLuint dbo;
        glGenRenderbuffers(1, &dbo);
        glBindRenderbuffer(GL_RENDERBUFFER, dbo);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);

        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, dbo);

        glDrawBuffer(GL_COLOR_ATTACHMENT0);
    }
}

void printWholeMassToConsole()
{
    GLfloat* data =new GLfloat[4 * width*height];

    glBindFramebuffer(GL_FRAMEBUFFER, fbo[otFb]);
    //glReadPixels(0, 0, width, height, GL_RGBA, GL_FLOAT, data);
    glReadnPixels(0, 0, width, height, GL_RGBA, GL_FLOAT, sizeof(data), data);

    float sumR = 0;
    float sumG = 0;
    float sumB = 0;

    for (int i = 0; i < 4 * width*height; i += 4)
    {
        if (i > 0) break;
        sumR += data[i];
        sumG += data[i + 1];
        sumB += data[i + 2];
    }

    float sum = sumR + sumG + sumB;

    float possibleDiff = 3.0*width*height*0.5;
    //if (abs(wholeMass - sum) <= possibleDiff) cout << "O - ";
    //else cout << "X - ";
    printf("mass : %d\t %d\t %d\t %d\n", sum, sumR, sumG, sumB);

    free(data);
}

这是第一个着色器。

// vertex shader
#version 450

in vec4 position;
in vec2 texCoord;

out vec2 fragTexCoord;

void main()
{
    gl_Position = position;
    fragTexCoord = texCoord;
}

// fragment shader
#version 450

in vec2 fragTexCoord;

out vec4 FragColor;

void main()
{
    FragColor = vec4(0.5, 0.0, 0.0, 1.0);
}

这是第二个着色器。

// vertex shader
#version 450

in vec4 position;
in vec2 texCoord;

out vec2 fragTexCoord;

void main()
{
    gl_Position = position;
    fragTexCoord = texCoord;
}

// fragment shader
#version 450

in vec2 fragTexCoord;

uniform sampler2D tex;

out vec4 FragColor;

void main()
{
    FragColor = texture(tex, fragTexCoord);
}

它通过第一个着色器otFb framebuffer 上绘制内容,然后otFb纹理映射到主帧缓冲区

glReadPixelsGL_FLOAT一起使用时,程序会自动终止,当glReadnPixels带有GL_FLOAT时,程序会打印垃圾值。

我用了很多次搜索,但找不到解决方案。

或者我有什么不同的尝试方法吗?

1 个答案:

答案 0 :(得分:-1)

尝试更改framebuffer绑定函数调用的第一个参数( printWholeMassToConsole 函数中的第二行代码),即:

glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo[otFb]);

希望它有效。