English 中文(简体)
在着色器中声明某些变量会导致它停止工作吗?(GLSL)
原标题:Declaring certain variables in shader causes it to stop working? (GLSL)
  • 时间:2011-05-29 20:10:59
  •  标签:
  • glsl

我正在使用GLSL。

我在这里有一个简单的片段着色器:

 "uniform sampler2D backBuffer;",
 "uniform float r;",
 "uniform float g;",
 "uniform float b;",
 "uniform float ratio;",
 "void main() {",
 "  vec4 color;",
 "  float avg, dr, dg, db, multiplier;",
 "  color = texture2D(backBuffer, vec2(gl_TexCoord[0].x * 1,gl_TexCoord[0].y * 1));",
 "  avg = (color.r + color.g + color.b) / 3.0;",
 "  dr = avg * r;",
 "  dg = avg * g;",
 "  db = avg * b;",
 "  color.r =  color.r * (gl_TexCoord[0].x * gl_TexCoord[0].y);",
"   color.g =  color.g * (gl_TexCoord[0].x * gl_TexCoord[0].y);",
"   color.b =  color.b * (gl_TexCoord[0].x * gl_TexCoord[0].y);",
 "  gl_FragColor = color;",
 "}"

现在它工作得很好。

然而,由于一些非常奇怪的原因,添加更多的变量(如vec2或float)会导致它对我的场景没有影响:

 "uniform sampler2D backBuffer;",
 "uniform float r;",
 "uniform float g;",
 "uniform float b;",
 "uniform float ratio;",
 "void main() {",
 "  vec4 color;",
 "  float avg, dr, dg, db, multiplier;",
 "  vec2 divisors;",
 "  color = texture2D(backBuffer, vec2(gl_TexCoord[0].x * 1,gl_TexCoord[0].y * 1));",
 "  avg = (color.r + color.g + color.b) / 3.0;",
 "  dr = avg * r;",
 "  dg = avg * g;",
 "  db = avg * b;",
 "  color.r =  color.r * (gl_TexCoord[0].x * gl_TexCoord[0].y);",
"   color.g =  color.g * (gl_TexCoord[0].x * gl_TexCoord[0].y);",
"   color.b =  color.b * (gl_TexCoord[0].x * gl_TexCoord[0].y);",
 "  gl_FragColor = color;",
 "}"

在这个例子中,我添加了一个称为除数的vec2,这就是我所做的,着色器不再对像素做任何事情。

为什么会这样?关于GLSL中的变量声明,我有什么不理解的地方吗?

谢谢

最佳回答

我注意到每一行都是用逗号分隔的带引号的字符串。在C/C++中,当创建一个大字符串时,通常只会将带引号的字符串并置,所以我想知道你是否在做一些奇怪的事情,比如初始化一个字符串数组,而不考虑添加新行后它的大小发生了变化?

问题回答

暂无回答




相关问题
Why can t I run my OpenGL program outside of Visual Studio?

I have an OpenGL-program using GLSL, that I can run just fine with the Play-button in Visual Studio (2008) -- both in the standard Release and Debug configurations. However, when I try to run the ...

GLSL shader generation of normals

Hi I am writing 3D modeling app and I want to speed up rendering in OpenGL. Currently I use glBegin/glEnd which is really slow and deprecated way. I need to draw very fast flat shaded models. I ...

Reading 32-bit RGBA float values in GLSL

I m trying send some 32 bit float data to a shader, but the results are erratic. If I test with full white (1,1,1,1) the values are all zero. This is my code for creating the texture: gl.glTexImage2D(...

Unable to link compiled shaders (GLSL)

I have a small class that allows me to load shaders and use them in my program. I am able to compile the shaders, but when it s time to link them, they just don t want to. Using glGetProgramInfoLog I ...

How to change a GLSL shader parameter in Processing

I m playing with shaders in openGL using Processing. I m pretty noob at this and a bit lost. I found this thread that has an example on how to use GLSL shaders in Processing. I m just trying to the ...

Shader limitations

I ve been tuning my game s renderer for my laptop, which has a Radeon HD 3850. This chip has a decent amount of processing power, but rather limited memory bandwidth, so I ve been trying to move more ...

热门标签