亲爱的Stackoverflowers:
最近,我在着色器的世界中迈出了我的第一步,在C#和OpenTK中使用GLSL和openGL,我遇到了一些似乎无法解决的问题。
我从一个着色器程序和一些快速的实验着色器开始,它们工作得很好,给了我以下结果(一个绿色和红色的光环绕一个三角形):

然后我想添加一些光晕,我听说你必须通过将碎片着色器应用于纹理来实现这一点。因此,我设置并渲染我的场景到帧缓冲区。然后,我绑定了framebuffer纹理,并绘制了一个四边形。这向我展示了之前的相同场景,这意味着它可以工作。然后,为了使用新的碎片着色器,我在写入缓冲区之后和编写四边形之前,用我的新程序替换了我的第一个着色器程序。现在的问题是,无论我做什么,屏幕都只提供一种纯色,而不是像我希望的那样进行任何操作。
这是我的绘图代码:
protected override void OnRenderFrame(FrameEventArgs e)
{
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, fboID);
GL.UseProgram(shaderProgramHandle);
GL.PushAttrib(AttribMask.ViewportBit);
{
GL.Viewport(0, 0, 600, 600);
// clear the screen in green, to make it very obvious what the clear affected. only the FBO, not the real framebuffer
GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
GL.PushAttrib(AttribMask.ColorBufferBit);
// make sure no lingering textures are bound to draw vertices clearly.
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.EnableVertexAttribArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);
GL.DrawArrays(BeginMode.Triangles, 0, 3);
GL.DisableVertexAttribArray(0);
GL.Begin(BeginMode.Points);
GL.Vertex3(lmp);
GL.Vertex3(lmp2);
GL.End();
GL.PopAttrib();
}
GL.PopAttrib();
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);
angle += 0.01f;
angle2 -= 0.017f;
lmp = new Vector3((float)(2f * Math.Cos(angle)), (float)(Math.Sin(angle) * 2f), 0f);
lmp2 = new Vector3((float)(2f * Math.Cos(angle2)), (float)(Math.Sin(angle2) * 2f), 0f);
GL.Uniform3(uniformLmp, lmp);
GL.Uniform3(uniformLmp2, lmp2);
GL.UseProgram(secondProgram);
GL.Enable(EnableCap.Texture2D);
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, fboTex);
GL.Uniform1(GL.GetUniformLocation(secondProgram, "tex"), fboTex);
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.Begin(BeginMode.TriangleStrip);
GL.TexCoord2(.0f, .0f);
GL.Vertex3(-1f, -1f, 0f);
GL.TexCoord2(1.0f, .0f);
GL.Vertex3(1f, -1f, 0f);
GL.TexCoord2(.0f, 1.0f);
GL.Vertex3(-1f, 1f, 0f);
GL.TexCoord2(1.0f, 1.0f);
GL.Vertex3(1f, 1f, 0f);
GL.End();
GL.Disable(EnableCap.Texture2D);
GL.UseProgram(0);
SwapBuffers();
}下面是我的着色器:
//vertex shader
#version 330
layout (location = 0) in vec3 Position;
varying vec2 texCoord;
void main()
{
gl_Position = vec4(Position.x, Position.y, Position.z, 1.0);
texCoord = gl_TexCoord[0].st;
}
//fragment shader
#version 330
uniform sampler2D tex;
out vec4 FragColor;
varying vec2 texCoord;
void main()
{
vec4 color = texture2D(tex, texCoord);
color.r += 0.5f;
FragColor = color;
}这会产生纯红色,而不是为现有图像提供红色色调。
有人能发现这里的问题吗?
发布于 2013-06-02 23:44:01
原来我忘了
GL.EnableClientState(ArrayCap.TextureCoordArray);这意味着我的UV坐标没有传递给着色器,因此使它从(0,0)中采样,解释了这种奇怪的行为。
https://stackoverflow.com/questions/16749254
复制相似问题