Shader crashing

Hi,

I have a default vertex shader where I’m trying to pass in an attribute of type float: (nightCol)


attribute float nightCol;
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans;
varying float vNightCol;
varying vec4 vColor;
varying vec2 vTexCoord;

void main() {
    vNightCol = nightCol;
	vColor = a_color;
	vTexCoord = a_texCoord0;
	gl_Position = u_projTrans * a_position;		
}

And a fragment shader:


#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif

uniform sampler2D u_texture;

varying vec4 vColor;
varying vec2 vTexCoord;
varying float vNightCol;

const vec3 grayScaleMultiplier = vec3(0.299, 0.587, 0.114);

void main() {
  vec4 texColor = texture2D(u_texture, vTexCoord);
  vec3 gray = vec3(dot(texColor.rgb, grayScaleMultiplier));
  gl_FragColor = vec4(gray.r, gray.g, gray.b, texColor.a * vNightCol);
}

I set the attribute up as follows:


		nightShader.begin();
		nightShader.setAttributef("nightCol", nightCol,0,0,0);
		nightShader.end();

Exception I get: java.lang.IllegalArgumentException: no uniform with name ‘u_projTrans’ in shader

It is as though it isn’t finding nightCol which I thought would be passed through from the vertex shader to the pixel shader?

Logging the error after checking compilation fails on the shader I get:

Shader: The fragment shader uses varying vNightCol, but previous shader does not write to it.

Any help is appreciated.

Thanks

To fix the problem I ended up using a uniform in the fragment instead, don’t know why the vertex shader wasn’t writing the value…

What does setAttributef?

Here:

public void setAttributef(java.lang.String name,
float value1,
float value2,
float value3,
float value4)

Sets the given attribute
Parameters:
name - the name of the attribute
value1 - the first value
value2 - the second value
value3 - the third value
value4 - the fourth value

Please carefully read about shaders and what exactly a UNIFORM does/is, as well as what a (VERTEX) ATTRIBUTE (or vertex shader ‘in’ variable) in a vertex shader is for, and what a VARYING (vertex shader ‘out’ and fragment shader ‘in’) variable is.
You’ll understand afterwards.

Also your explanation of @elect’s question what ‘setAttributef’ does made me laugh. :wink:

Question: “What does this ‘doWhatIMean()’ method do?”
Answer: “It does what you mean.”

:wink:

Lol :slight_smile:

Is there a way of passing a float to my vertex shader which then gets sent to my fragment shader?! I thought the first post I did would have done it, but doesn’t work, hence the reason for me passing it directly to the fragment shader using uniform.

Where exactly should this ‘float’ come from?
Should it be constant/uniform over all vertices, in which case a ‘uniform’ is exactly what you need and you don’t need to pass anything from vertex to fragment shader, but just declare and use the uniform in the fragment shader.
Or should it be able to change between the vertices of your mesh, in which case it will get interpolated between the triangles of your mesh? Then you must use a vertex attribute, so essentially what your ‘a_position’, ‘a_color’ and ‘a_texCoord0’ attributes are.
And you must therefore specify that single float as a vertex attribute via glVertexAttribPointer(index, 1, GL_FLOAT, …) or whichever vertex specification method you are using.

I actually wanted to know what the function was doing underneath…

Well, I’ve ended up using a uniform because it is constant across the vertices, this is to allow for night/day cycle.

I did use attribute like you said and varying, don’t know what libgdx uses for glVertexAttribPointer hence I used the setAttributef method.

Thanks