在渲染纹理上绘制时出现问题。 (OpengL 3.3)

Posted

技术标签:

【中文标题】在渲染纹理上绘制时出现问题。 (OpengL 3.3)【英文标题】:Issue when drawing on a render texture. (OpengGL 3.3) 【发布时间】:2014-11-30 19:52:56 【问题描述】:

我正在处理一个小的 opengl 问题,我想知道是否有人可以帮助我。 基本上我试图让 fbo 启动并运行,我设法在纹理上绘制了一些东西,但在我看来,整个纹理上只渲染了一个像素。

这里是没有帧缓冲的正常渲染:http://puu.sh/dbMJK/332f60d5dc.png

还有:http://puu.sh/dbMRK/e77134c646.png

帧缓冲区的顶点着色器:

in vec3 in_Vertex;
in vec2 in_TexCoord0;

out vec2 coordTexture;

void main() 
   coordTexture = in_TexCoord0;
   gl_Position = vec4(in_Vertex, 1.0);

片段着色器:

in vec2 coordTexture;

uniform sampler2D tex;

out vec4 out_Color;

void main() 
   out_Color = texture(tex, coordTexture);

以下是我目前负责 FBO 的职能:

GLuint Game::createTexture(int w, int h, bool isDepth)
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

int i = glGetError();
if (i != 0)

    printf("DUCK YOU create texture error: %s\n", glewGetErrorString(i));

glBindTexture(GL_TEXTURE_2D, 0);
return textureID;


void Game::createFBO()
renderTexture = createTexture(800, 600, false);
glGenFramebuffers(1, &FBO);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);

GLuint depthrenderbuffer;
glGenRenderbuffers(1, &depthrenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 800, 600);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer);


// GL_COLOR_ATTACHMENT0
// GL_DEPTH_ATTACHMENT
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderTexture, 0);


// Set the list of draw buffers.
GLenum DrawBuffers[1] =  GL_COLOR_ATTACHMENT0 ;
glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers


int i = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (i != GL_FRAMEBUFFER_COMPLETE)

    printf("DUCK YOU FRAMEBUFFER BUFFED YOU: %i\n", i);

glBindFramebuffer(GL_FRAMEBUFFER, 0);


void Game::windowLoop(sf::Window *window)

static const GLfloat g_quad_vertex_buffer_data[] = 
    -1.0f, -1.0f, 0.0f,
    1.0f, -1.0f, 0.0f,
    -1.0f, 1.0f, 0.0f,
    -1.0f, 1.0f, 0.0f,
    1.0f, -1.0f, 0.0f,
    1.0f, 1.0f, 0.0f,
;

GLuint quad_vertexbuffer;
glGenBuffers(1, &quad_vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_quad_vertex_buffer_data), g_quad_vertex_buffer_data, GL_STATIC_DRAW);


bool running = true;

while (running)
    // handle events
    sf::Event event;
    while (window->pollEvent(event))
        if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
            // end the program
            running = false;
        
        else if (event.type == sf::Event::Resized)
            // adjust the viewport when the window is resized
            glViewport(0, 0, event.size.width, event.size.height);
        
    
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBindFramebuffer(GL_FRAMEBUFFER, FBO);
    glEnable(GL_DEPTH_TEST);
    glClearColor(0.3f, 0.2f, 0.4f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    gameLoop(window);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glUseProgram(screenShader.getProgramID());
    glDisable(GL_DEPTH_TEST);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer);
    glVertexAttribPointer(
        0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
        3,                  // size
        GL_FLOAT,           // type
        GL_FALSE,           // normalized?
        0,                  // stride
        (void*)0            // array buffer offset
        );
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, renderTexture);

    glDrawArrays(GL_TRIANGLES, 0, 6);

    glDisableVertexAttribArray(0);
    glBindTexture(GL_TEXTURE_2D, 0);
    glUseProgram(0);

    window->display();


【问题讨论】:

您不小心将顶点着色器也粘贴为片段着色器。 啊谢谢我修好了。 【参考方案1】:

您不为四边形渲染提供纹理坐标。此外,您的代码 cmets 建议您通过 GL 代码中的 layout 限定符显式设置属性位置,但您不这样做,因此属性变量和位置之间的映射未定义或至少从代码部分中不清楚你粘贴了。

【讨论】:

哦,你似乎是对的,我会解决的。是的,实际上我在过去 2 天里对代码进行了很多次修改,我错过了。 我可以确认现在一切正常。谢谢!

以上是关于在渲染纹理上绘制时出现问题。 (OpengL 3.3)的主要内容,如果未能解决你的问题,请参考以下文章

我的OpenGL学习进阶之旅解决OpenGL绘制带透明通道的png纹理时出现黑边问题,并彻底了解其原理

OpenGL - 在 NVIDIA 卡上渲染到纹理时出现 FBO 黑屏

在 OpenGL 中将深度渲染到纹理时出现奇怪的结果

渲染到纹理时出现黑屏

在 OpenGL 中将纹理映射到球体时出现接缝问题

OpenGL ES 渲染到纹理,然后绘制纹理