将倒置场景渲染为帧缓冲区

时间:2015-11-07 20:02:50

标签: c++ opengl glsl shader mirror

我需要一些帮助才能将倒置场景渲染到帧缓冲区。我想做一个镜子效果。 我得到的结果是: My result 初始化帧缓冲区:

GLuint FramebufferName = 0;
glGenFramebuffers(1, &FramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);

GLuint renderedTexture;
glGenTextures(1, &renderedTexture);
glBindTexture(GL_TEXTURE_2D, renderedTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SCREEN_WIDTH, SCREEN_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderedTexture, 0);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glReadBuffer(GL_NONE);

将场景绘制到帧缓冲区:

    mView = camera.getViewMatrix();
    mProjection = perspective(45.0f, 4.0f / 3.0f, 0.1f, 300.0f) * scale(mat4(1.0f), vec3(1.0f, -1.0f, 1.0f)); 
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, FramebufferName);
    glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glCullFace(GL_FRONT);
    glEnable(GL_CULL_FACE);
    mWorld = rotate(mWorld, -15.0f, vec3(1, 0, 0));
    mWorld = rotate(mWorld, -30.0f, vec3(0, 1, 0));
    cubemap.Draw(mView, mProjection, mWorld);
    glDisable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);
    glUseProgram(program);
    mWorld = translate(mat4(1.0f), vec3(0, -10, 0));
    glUniformMatrix4fv(mvp_object, 1, GL_FALSE, value_ptr(mProjection * mView * mWorld * scale(mat4(1.0f), vec3(10.0f))));
    cannon.Draw(program);

    mWorld = mat4(1.0f);
    mWorld = translate(mat4(1.0f), vec3(50, -10, 0));
    mWorld = rotate(mWorld, 90.0f, vec3(0, 1, 0));
    glUniformMatrix4fv(mvp_object, 1, GL_FALSE, value_ptr(mProjection * mView * mWorld * scale(mat4(1.0f), vec3(10.0f))));
    cannon.Draw(program);

    mWorld = mat4(1.0f);
    mWorld = translate(mat4(1.0f), vec3(0, -10, 50));
    mWorld = rotate(mWorld, 140.0f, vec3(0, 1, 0));
    glUniformMatrix4fv(mvp_object, 1, GL_FALSE, value_ptr(mProjection * mView * mWorld * scale(mat4(1.0f), vec3(10.0f))));
    cannon.Draw(program);

    glDisable(GL_DEPTH_TEST);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

我看到一些使用glScalef(1.0,-1.0,1.0)来反转场景的教程。 glScalef用于旧版本的opengl。如何在新版本的opengl中反转场景? 对于这种效果,我将比例上的投影矩阵(mat4(1.0f),vec3(1.0,-1.0,1.0))相乘。是不是?

1 个答案:

答案 0 :(得分:1)

这是用GLSL编写的顶点着色器。

void main(void)
{
   // Clean up inaccuracies
   vec2 Pos = sign(gl_Vertex.xy);

   gl_Position = vec4(Pos.xy, 0.0, 1.0);
   // Image-space
   gl_TexCoord[0].xy = vec2(-Pos.x, Pos.y) * 0.5 + 0.5; // x texture coord is inverted       
}

Pos指的是用于帧缓冲输出的屏幕对齐四元组。

片段着色器

uniform sampler2D framebufferSampler;

void main(void)
{
   gl_FragColor = texture2D(framebufferSampler, gl_TexCoord[0].xy);
}

可以绘制屏幕对齐的四边形

glBegin(GL_QUADS)
{
    glVertex2f(-1.0f, -1.0f);
    glVertex2f(1.0f, -1.0f);
    glVertex2f(1.0f, 1.0f);
    glVertex2f(-1.0f, 1.0f);
}
glEnd();

或使用顶点数组。

GLfloat vertices[] = {
    -1.0f, -1.0f,
    1.0f, -1.0f,
    1.0f, 1.0f,
    -1.0f, 1.0f
};

glEnableClientState(GL_VERTEX_ARRAY);

glVertexPointer(2, GL_FLOAT, 0, (void*)vertices);

glDrawArrays(GL_QUADS, 0, 4);

glDisableClientState(GL_VERTEX_ARRAY);

用于包裹到四边形的纹理是具有先前渲染内容的帧缓冲信息(多遍着色器技术)。

您也可以使用其他2D纹理坐标或图像代替帧缓冲和屏幕对齐四边形。

原始图片。

enter image description here

倒像。

enter image description here

可以在Learn OpenGL找到更多信息。