Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 渲染到帧缓冲区/纹理显示为空_C++_Opengl_Shader_Framebuffer - Fatal编程技术网

C++ 渲染到帧缓冲区/纹理显示为空

C++ 渲染到帧缓冲区/纹理显示为空,c++,opengl,shader,framebuffer,C++,Opengl,Shader,Framebuffer,我试图渲染到帧缓冲区(与窗口/屏幕大小相同)进行后期处理。所有的东西都是二维的,所以不会有太多麻烦。直接渲染到屏幕效果很好。但我只能得到清晰的颜色和一个空的缓冲区。渲染任何内容的唯一方法是在位置(0,0)处渲染一个具有缓冲区精确大小的矩形,这很奇怪 我尝试在缓冲区中的任意位置渲染,但没有任何效果。在屏幕上显示/呈现缓冲区是可行的 以下是我如何创建缓冲区: glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFE

我试图渲染到帧缓冲区(与窗口/屏幕大小相同)进行后期处理。所有的东西都是二维的,所以不会有太多麻烦。直接渲染到屏幕效果很好。但我只能得到清晰的颜色和一个空的缓冲区。渲染任何内容的唯一方法是在位置(0,0)处渲染一个具有缓冲区精确大小的矩形,这很奇怪

我尝试在缓冲区中的任意位置渲染,但没有任何效果。在屏幕上显示/呈现缓冲区是可行的

以下是我如何创建缓冲区:

    glGenFramebuffers(1, &fbo);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    std::cout << "frame buffer fbo: " << fbo << std::endl;

    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);

    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    {
        std::cout << "error creating screen buffer width: " << width << " height: " << height << std::endl;
    }
    else {
        std::cout << "created screen buffer width: " << width << " height: " << height << std::endl;
    }

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
// binding and clearing the buffer to render into it
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glViewport(0, 0, width, height);
glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

// render the rectangle
renderer->drawRectangle(0, 0, 10, 10, glm::vec4(1.0f, 0.0f, 1.0f, 1.0f));

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, resolutionWidth, resolutionHeight);

// rendering main screen showing the quad
片段着色器:

#version 330 core

uniform vec4 in_color;
out vec4 FragColor ;

void main() {
    FragColor = in_color;
}
用于渲染的投影矩阵:

glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(this->width), static_cast<GLfloat>(this->height), 0.0f, -1.0f, 1.0f);
结果是(缓冲区以一半大小呈现到屏幕):

我在执行此操作时遵循了本教程:

作为我的解决方案,我只需更改屏幕着色器以接受顶点/纹理数据。然后我注意到实际的缓冲区数据非常好

顶点着色器:

#version 330 core
layout (location = 0) in vec4 aPos;

out vec2 TexCoords;

void main()
{
    TexCoords = aPos.zw;
    gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
} 
片段着色器:

#version 330 core
out vec4 FragColor;

in vec2 TexCoords;

uniform sampler2D screenTexture;

void main()
{ 
    FragColor = texture(screenTexture, TexCoords);
}

试着给你的帧缓冲区添加一个深度附件,希望对你有帮助。我试过了,但没什么不同。我还禁用了深度测试:glDisable(GL_depth_TEST);glDisable(GLU消隐面);此外,我也很抱歉虹彩清晰和填充颜色看起来一样(它不是在实际的代码中,只是当我复制粘贴在一起的这篇文章),所以我编辑了这篇文章。问题仍然存在。。。我还想补充一点,类似的代码在opengl es上运行得非常好。投影在初始化时设置,但即使在绘图之前设置好,投影也不会改变,因为窗口/屏幕和帧缓冲区的大小相同。不过,四元组使用了不同的着色器在屏幕上绘制。我想我发现了这个bug,它是在我从教程中获取的屏幕着色器中发现的。看,这不可能是对的。一旦它被修复,我可能会更新这个帖子。
#version 330 core
out vec4 FragColor;

in vec2 TexCoords;

uniform sampler2D screenTexture;

void main()
{ 
    FragColor = texture(screenTexture, TexCoords);
}