[英]Fragmentshader and framebuffer solid colour
Dear Stackoverflowers, 亲爱的Stackoverflowers,
Lately I have been making my first baby steps in the world of shaders, using GLSL and openGL in C# with OpenTK, and I've stumbled into some problems which I seem to be unable to solve. 最近,我在着色器领域迈出了第一步,在带有OpenTK的C#中使用GLSL和openGL,但我偶然发现了一些我似乎无法解决的问题。
I started off with one shader program and some fast experimental shaders which worked fine, giving me the following result (just a green and red light circling a triangle): 我从一个着色器程序和一些运行良好的快速实验着色器开始,这给了我以下结果(只是绿色和红色的光围绕一个三角形):
Then I wanted to add some sort of glow to this and I heard you had to do this through applying a fragment shader to a texture. 然后,我想为此添加某种发光效果,听说您必须通过将片段着色器应用于纹理来完成此操作。 So I set out and rendered my scene to a framebuffer.
因此,我开始将场景渲染到帧缓冲区中。 I then bound the framebuffer texture and drew a quad.
然后,我绑定帧缓冲区纹理并绘制一个四边形。 This showed me the same scene I had before, which meant it worked.
这向我展示了以前的场景,这意味着它可以正常工作。 Then to use the new fragmentshader I replace my first shader program with my new program after I wrote to the buffer and before writing the quad.
然后,要使用新的fragmenthader,在写入缓冲区之后并在写入四边形之前,请用新程序替换第一个着色器程序。 Now the problem is that whatever I do the screen only gives one solid color, not doing any manipulations like I would like.
现在的问题是,无论我做什么,屏幕只能给出一种纯色,而没有像我想要的那样进行任何操作。
This is my drawing code: 这是我的绘图代码:
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();
}
And these are my shaders: 这些是我的着色器:
//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;
}
This results in a solid red color, instead of giving the existing image a red hue. 这样会产生纯红色,而不是使现有图像具有红色调。
Can anybody spot the problem here? 有人可以在这里发现问题吗?
It turns out that I forgot 原来我忘了
GL.EnableClientState(ArrayCap.TextureCoordArray);
This meant that my UV coords were not passed to the shader, thus making it sample from just (0,0), explaining this wierd behaviour. 这意味着我的UV坐标未传递到着色器,因此仅从(0,0)进行采样,从而解释了这种奇怪的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.