【发布时间】:2014-11-19 22:59:00
【问题描述】:
编辑 + 总结更清晰:
我的代码是这样做的:给定一个图像,它会修改它(使用着色器)。它将其输出到纹理。然后一个旧的 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);
}
【问题讨论】:
-
等等,又是什么问题?
-
我在上面添加了解释。