渲染到屏幕的帧缓冲纹理在某些点被拉伸

时间:2016-03-01 20:33:41

标签: c++ opengl

我目前正在尝试将渲染测试到帧缓冲区以用于各种用途,但每当我有一个特定y值的对象(比如一个正方形)时,它就会出现"拉伸"然后超过某个y值或某个x值似乎"稀疏"并消失。我已经确定它消失的x和y值,但坐标似乎没有任何押韵或理由。

当我移除帧缓冲区绑定并直接渲染到屏幕时,无论x还是y值,它都会完美地绘制正方形。

使用宽x值绘制基本方块(使用立即模式删除可能的错误)如下所示:

Stretched Framebuffer image

代码在这里:

Window window("Framebuffer Testing", 1600, 900); //1600x900 right now

int fbowidth = 800, fboheight = 600;

mat4 ortho = mat4::orthographic(0, width, 0, height, -1.0f, 1.0f);

//trimmed out some code from shader creation that is bugless and unneccessary to include
Shader shader("basic"); shader.setUniform("pr_matrix", ortho); 

Shader drawFromFBO("fbotest"); shader.setUniform("pr_matrix", ortho); 

GLfloat screenVertices[] = {
    0, 0, 0,            0, height, 0,       
    width, height, 0,   width, 0, 0};

GLushort indices[] = {
    0, 1, 2,
    2, 3, 0 };

GLfloat texcoords[] = { //texcoords sent to the drawFromFBO shader
    0, 0,   0, 1,   1, 1,
    1, 1,   1, 0,   0, 0 };

IndexBuffer ibo(indices, 6);
VertexArray vao;
vao.addBuffer(new Buffer(screenVertices, 4 * 3, 3), 0);
vao.addBuffer(new Buffer(texcoords, 2 * 6, 2), 1);

GLuint fbo;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);

GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fbowidth, fboheight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);

glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0);

if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    std::cout << "false" << std::endl;

glEnable(GL_TEXTURE_2D);

//the x-values mess up at ~783 thru 800 and the y-values at 0 thru ~313
while(!window.closed()) {
    glClearColor(0.2f, 0.2f, 0.2f, 1.0f); //grey
    window.clear(); //calls glclear for depth and color buffer

    //bind framebuffer and shader
    shader.enable(); //literally just calls glUseProgram(id) with the compiled shader id
    glViewport(0, 0, fbowidth, fboheight);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo); //bind the fbo
    glClearColor(1.0f, 0.0f, 1.0f, 1.0f); //set clear color to pink
    glClear(GL_COLOR_BUFFER_BIT);

    //render a red square to the framebuffer texture
    glBegin(GL_QUADS); {
        glColor3f(1.0f, 0.0f, 0.0f); //set the color to red
        glVertex3f(700, 400, 0);
        glVertex3f(700, 450, 0);
        glVertex3f(750, 450, 0);
        glVertex3f(750, 400, 0);
    } glEnd();

    shader.disable();
    glBindFramebuffer(GL_FRAMEBUFFER, 0); //set framebuffer to the default

    //render from framebuffer to screen
    glViewport(0, 0, width, height);
    drawFromFBO.enable();

    glActiveTexture(GL_TEXTURE0);
    drawFromFBO.setUniform1i("texfbo0", 0);
    glBindTexture(GL_TEXTURE_2D, texture);

    vao.bind();
    ibo.bind();
    glDrawElements(GL_TRIANGLES, ibo.getCount(), GL_UNSIGNED_SHORT, NULL);
    ibo.unbind();
    vao.unbind();

    drawFromFBO.disable();

    window.update();
}

如果你想看到任何额外的东西,该文件位于我的Github:here

0 个答案:

没有答案