用于片段着色器的OpenGL GLSL绑定采样器 [英] OpenGL GLSL Binding Sampler for Fragment Shader
问题描述
我希望在2D OpenGL应用程序上实现着色器。我的计划是渲染一个场景到一个framebuffer对象,然后使用着色器渲染该framebuffer对象到屏幕。
这是我画的场景一个framebuffer对象,然后从那里到屏幕。使用箭头键使月亮四处移动(我感到非常自豪!)
但是,当我尝试使用我的着色器程序渲染framebuffer对象到屏幕时, p>
这很伤心。此片段着色器是我从教程
这是Fragment着色器:
#version 330
在vec2 texCoord;
out vec4 outputColor;
uniform sampler2D gSampler;
void main()
{
outputColor = texture2D(gSampler,texCoord);
}
这是我如何设置统一变量:
gSampler = glGetUniformLocation(mProgramID,gSampler);
glUniform1i(gSampler,GL_TEXTURE0);
这是我如何使用我的着色器程序(mProgramID)渲染Framebuffer(fbo_texture)
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);
glUseProgram(mProgramID);
glColor3f(1.0,1.0,1.0);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D,fbo_texture);
glBegin(GL_QUADS); x1 = 0.0; x2 = 400.0; y1 = 0.0; y2 = 400.0;
glTexCoord2f(0.0f,1.0f); glVertex2f(x1,y1);
glTexCoord2f(0.0f,0.0f); glVertex2f(x1,y2);
glTexCoord2f(1.0f,0.0f); glVertex2f(x2,y2);
glTexCoord2f(1.0f,1.0f); glVertex2f(x2,y1);
glEnd(); glDisable(GL_TEXTURE_2D);
SDL_GL_SwapBuffers();
所以我的问题是 - 纹理和采样器之间的联系是什么?如果着色器已经对纹理进行采样,我需要渲染纹理的四边形吗?为什么不是只渲染一个空白的四边形,如果着色器是纹理化它?
编辑:这是顶点着色器:
void main()
{
gl_TexCoord [0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
您为采样器设置统一值:
gSampler = glGetUniformLocation(mProgramID,gSampler);
glUniform1i(gSampler,GL_TEXTURE0);
您需要为制服设置的值是纹理单元的索引,而不是相应的枚举值。由于在这种情况下使用纹理单元0,您需要替换 glUniform1i()
调用:
glUniform1i(gSampler,0);
I am hoping to implement a shader on a 2D OpenGL application. My plan is to render a scene to a framebuffer object, and then render that framebuffer object to the screen using a shader.
Here is the scene, which I have drawn to a framebuffer object, and then to the screen from there. Using the arrow keys makes the moon move around (I am quite proud of it!)
However, when I try to render the framebuffer object to the screen using my shader program, I get this:
It is very sad. This fragment shader is one which I got from a tutorial, and I'm sure the problem has to be with the Uniform Variables.
Here is the Fragment Shader:
#version 330
in vec2 texCoord;
out vec4 outputColor;
uniform sampler2D gSampler;
void main()
{
outputColor = texture2D(gSampler, texCoord);
}
Here is how I set up the Uniform Variables:
gSampler = glGetUniformLocation(mProgramID, "gSampler");
glUniform1i(gSampler, GL_TEXTURE0);
Here is how I render the Framebuffer (fbo_texture) to the screen, using my shader program (mProgramID)
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0 );
glUseProgram(mProgramID);
glColor3f(1.0, 1.0, 1.0);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, fbo_texture);
glBegin(GL_QUADS);x1= 0.0;x2= 400.0;y1= 0.0;y2 = 400.0;
glTexCoord2f(0.0f, 1.0f); glVertex2f(x1, y1);
glTexCoord2f(0.0f, 0.0f); glVertex2f(x1, y2);
glTexCoord2f(1.0f, 0.0f); glVertex2f(x2, y2);
glTexCoord2f(1.0f, 1.0f); glVertex2f(x2, y1);
glEnd();glDisable(GL_TEXTURE_2D);
SDL_GL_SwapBuffers();
So my question is- What is the link between a texture and a sampler? Do I need to render a textured quad, if the shader is already sampling the texture? Why not just render a blank quad if the shader is texturizing it?
EDIT: Here is the vertex shader:
void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
There's indeed a problem with how you set the uniform value for the sampler:
gSampler = glGetUniformLocation(mProgramID, "gSampler");
glUniform1i(gSampler, GL_TEXTURE0);
The value you need to set for the uniform is the index of the texture unit, not the corresponding enum value. Since you are using texture unit 0 in this case, you need to replace the glUniform1i()
call by:
glUniform1i(gSampler, 0);
这篇关于用于片段着色器的OpenGL GLSL绑定采样器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!