为什么渲染缓冲区数据由OpenGL中的同一条管线产生的不同?

时间:2019-11-27 09:46:04

标签: c opengl

整个代码:

//two triangles color and position data
Vertex Verts[9] = { 
    { { 255,0,0,255 },{ 0.0f, 0.9f, 0.0f } },
    { { 0,255,0,255 },{ -0.9f, -0.9f, 0.0f } },
    { { 0,0,255,255 },{ 0.9f, -0.9f, 0.0f } } ,
    { { 255,255,255,255 },{ 0.0f, 0.8f, -0.2f } } ,
    { { 255,255,255,255 },{ -0.4f, 0.0f, -0.2f } } ,
    { { 255,255,255,255 },{ 0.4f, 0.0f, -0.2f } } ,
};
GLuint Program = 0;
GLuint vert;
GLuint vbo;
GLuint RendBuf[4], FrameBuf[2];

这是缓冲区设置代码:

void Init()
{
    glGenVertexArrays(1, &vert);
    glBindVertexArray(vert);
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Verts), Verts, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), BUFFER_OFFSET(0));
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(Verts->color)));
    glEnableVertexAttribArray(1);
    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    glUseProgram(Program);

我生成4个渲染缓冲区,一个深度缓冲区,三个颜色缓冲区:

    glGenRenderbuffers(4, RendBuf);
    glBindRenderbuffer(GL_RENDERBUFFER, RendBuf[0]);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, 200, 200);
    glBindRenderbuffer(GL_RENDERBUFFER, RendBuf[1]);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 200, 200);
    glBindRenderbuffer(GL_RENDERBUFFER, RendBuf[2]);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, 200, 200);
    glBindRenderbuffer(GL_RENDERBUFFER, RendBuf[3]);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, 200, 200);
    //all buffer objects are bound to framebuffer object:FrameBuf[0]
    glGenFramebuffers(1, &FrameBuf[0]);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, FrameBuf[0]);
    glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, RendBuf[0]);  
    glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_RENDERBUFFER, RendBuf[2]);
    glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, RendBuf[1]);   
    glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_RENDERBUFFER, RendBuf[3]);
}

这是渲染代码:

void Display()
{
    //draw in framebuffer
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, FrameBuf[0]);
    glViewport(0, 0, 200, 200);
    GLenum bufs[3] = { GL_COLOR_ATTACHMENT0 , GL_COLOR_ATTACHMENT1 , GL_COLOR_ATTACHMENT2};
    glDrawBuffers(3,bufs);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

更改三个三角形的背景颜色

    float Color[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    glClearBufferfv(GL_COLOR, 0, Color);
    float Color1[] = { 1.0f, 1.0f, 0.0f, 1.0f };
    glClearBufferfv(GL_COLOR, 1, Color1);
    float Color2[] = { 0.0f, 1.0f, 1.0f, 1.0f };
    glClearBufferfv(GL_COLOR, 2, Color2);
    glBindVertexArray(vert);
    glEnable(GL_DEPTH_TEST);
    glDrawArrays(GL_TRIANGLES, 0, 6);

将数据从帧缓冲区复制到屏幕缓冲区

    glBindFramebuffer(GL_READ_FRAMEBUFFER, FrameBuf[0]);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    glViewport(0, 0, 400, 400);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glReadBuffer(GL_COLOR_ATTACHMENT0);
    glBlitFramebuffer(0, 0, 200, 200, 0, 0, 200, 200, GL_COLOR_BUFFER_BIT, GL_NEAREST);
    glReadBuffer(GL_COLOR_ATTACHMENT1);
    glBlitFramebuffer(0, 0, 200, 200, 200, 0, 400, 200,GL_COLOR_BUFFER_BIT, GL_NEAREST);
    glReadBuffer(GL_COLOR_ATTACHMENT2);
    glBlitFramebuffer(0, 0, 200, 200, 0, 200, 200, 400,GL_COLOR_BUFFER_BIT, GL_NEAREST);
    glutSwapBuffers();
}

顶点着色器和片段着色器:

layout(location = 0) in vec4 vColor;
layout(location = 1) in vec4 vPos;
void main()
{
    gl_Position = vPos;
    color = vColor;
}

layout(location = 0) out vec4 fColor;
layout(location = 1) out vec4 fColor2;
void main()
{
    fColor = color;
    fColor2 = vec4(0.0, 0.0, 1.0, 1.0);
}

第一个三角形是正确的,第二个是在黄色背景上的蓝色三角形,第三个是浅蓝色背景。

0 个答案:

没有答案