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

时间:2014-11-30 19:52:57

标签: c++ opengl opengl-3

我正在运行一个小小的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);
}

fargment着色器:

in vec2 coordTexture;

uniform sampler2D tex;

out vec4 out_Color;

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

Heres是我现在负责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 个答案:

答案 0 :(得分:1)

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