将glortho与着色器代码结合使用

时间:2014-09-25 08:35:23

标签: opengl shader gpu fbo

编辑+摘要以更清晰:

我的代码执行此操作:给定图像,它会修改它(使用着色器)。它将其输出到纹理。然后一个旧的opengl代码(使用glOrtho)移动并重新调整此图像到应在屏幕上显示的位置。

我的问题:如何更改当前着色器代码以包含此翻译和重新缩放?


我目前的管道是这个(见下面的代码):

图片 - >现代opengl处理 - >渲染到纹理T - >使用旧的opengl将纹理T渲染到屏幕

但我希望它是这样的:

图片 - >现代opengl处理 - >现代opengl直接渲染给定坐标

基本上我的问题是:

给定glOrtho中的屏幕坐标,如何更改下面的着色器代码以渲染到给定的坐标,同时仍然执行我的增强功能?

这是我当前代码的草图:

//render to texture T
RenderEnhancementIntoTexture(T);  

//bind this texture:
glBindTexture(GL_TEXTURE_2D, T);

//clean-up shaders, vertices, return to fixed-pipeline:
glBindFramebuffer(GL_FRAMEBUFFER,0);
glUseProgram(0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);


//position the canvas on screen based on the gui:
glOrtho(l, r, b, t, 0, 10.0);

//use this texture to paint on the screen segment:
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(left, bottom,  -5);    // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( right, bottom,  -5);  // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( right, top,  -5); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(left,  top,  -5);  // Top Left Of The Texture and Quad
glEnd();

着色器代码:

顶点着色器:

#version 330 core

in vec2 VertexPosition;
uniform vec2 u_step;
void main(void) {
  gl_Position = vec4(VertexPosition, 0.0, 1.0);
}

和片段着色器:

//simple example - just output the pixel value:
#version 330 core
uniform sampler2D image;
uniform vec2 u_step;
out vec4 FragmentColor;

void main(void)
{
  vec3 accum = texture( image, vec2(gl_FragCoord)*u_step ).rgb;
  FragmentColor = vec4(accum , 1.0);
}

和初始化顶点的代码:

void initVAO(void)
{
    GLuint positionLocation = 0;

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

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

    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjID[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer((GLuint)positionLocation, 2, GL_FLOAT, GL_FALSE, 0, 0); 
    glEnableVertexAttribArray(positionLocation);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertexBufferObjID[2]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}

1 个答案:

答案 0 :(得分:2)

为什么这段复杂的代码:

//position the canvas on screen based on the gui:
glOrtho(l, r, b, t, 0, 10.0);

//use this texture to paint on the screen segment:
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(left, bottom,  -5);    // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( right, bottom,  -5);  // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( right, top,  -5); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(left,  top,  -5);  // Top Left Of The Texture and Quad
glEnd();

你想做的就是(据我所知)你的纹理四边形覆盖了整个视口。好吧,没什么比这更容易了。而不是正交投影和一些模型视图转换,只需使用标识变换并直接在剪辑空间中指定顶点。视口的边界始终是NDC空间的坐标-1和1。并且通过剪辑空间== ND空间进行身份变换。

所以只需使用顶点位置(-1,-1),(1,-1),(1,1)和(-1,1)并将它们直接传递到顶点中的gl_Position着色器。