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

C++ 渲染到屏幕的帧缓冲区纹理在某些点拉伸,c++,opengl,C++,Opengl,我目前正试图测试渲染到帧缓冲区的各种用途,但每当我有一个对象(比如一个正方形)在某个y值时,它就会出现“拉伸”,然后超过某个y值或某个x值,它就会“变薄”并消失。我已经确定了它消失的x和y值,但坐标似乎没有任何押韵或理由 当我移除帧缓冲区绑定并直接渲染到屏幕时,无论x或y值如何,它都能完美地绘制正方形 绘制具有宽x值的基本正方形(使用即时模式删除可能的错误)如下所示: 代码如下: Window window("Framebuffer Testing", 1600, 900); //1600x9

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

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

绘制具有宽x值的基本正方形(使用即时模式删除可能的错误)如下所示:

代码如下:

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();
}
窗口窗口(“帧缓冲区测试”,1600900)//现在是1600x900
int fbowidth=800,fboheight=600;
mat4正交=mat4::正交(0,宽度,0,高度,-1.0f,1.0f);
//从着色器创建中删除了一些无错误且不需要包含的代码
着色器(“基本”);setUniform(“pr_矩阵”,正交);
着色器drawFromFBO(“fbotest”);setUniform(“pr_矩阵”,正交);
GLfloat屏幕顶点[]={
0,0,0,0,高度,0,
宽度,高度,0,宽度,0,0};
短期指数[]={
0, 1, 2,
2, 3, 0 };
GLfloat texcoords[]={//texcoords发送到drawFromFBO着色器
0, 0,   0, 1,   1, 1,
1, 1,   1, 0,   0, 0 };
ibo指数(指数6);
顶点阵列vao;
添加缓冲区(新缓冲区(屏幕顶点,4*3,3),0);
vao.addBuffer(新的缓冲区(texcoords,2*6,2),1);
GLuint fbo;
GLGEN帧缓冲区(1,&fbo);
glBindFramebuffer(GL_FRAMEBUFFER,fbo);
胶合结构;
glGenTextures(1,&纹理);
glBindTexture(GL_TEXTURE_2D,纹理);
GLTEXAGE2D(GL_纹理_2D,0,GL_RGB,fbowidth,fboheight,0,GL_RGB,GL_无符号_字节,NULL);
glTexParameteri(GL_纹理2D,GL_纹理MAG_过滤器,GL_最近);
glTexParameteri(GL\u纹理\u 2D,GL\u纹理\u最小\u过滤器,GL\u最近);
glBindTexture(GL_TEXTURE_2D,0);
glFramebufferTexture(GL_帧缓冲区,GL_颜色_附件0,纹理,0);
如果(glCheckFramebufferStatus(GL\U FRAMEBUFFER)!=GL\U FRAMEBUFFER\U COMPLETE)

std::cout您的
texcoords
与顶点不匹配。在查看这些值之前就很明显了,因为你有6个顶点的纹理坐标,但只有4个顶点的位置。是的,就是这样。这是一个非常简单的错误,但我根本不知道索引应用于位置/顶点旁边的纹理坐标,因为我仍在学习过程中。非常感谢@Reto。