Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


[glsl] problem with sending uniform


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 BraXi   Members   -  Reputation: 187

Like
0Likes
Like

Posted 20 June 2012 - 07:33 AM

Okey so i've used some shaders in my little game which work perfectly and without any troubles but i've hit one problem with light attenuation shader, for some reason i cant send any uniform to it which is very painful for me and i've tried to send uniforms to other shaders which worked without any problems. The problem is that i cannot send my int uniform "lightNum" to attenuation shader, i dont know why its happening because i have that uniform in both vertex and fragment shaders. Shaders compiled without any errors and warnings.
Did somebody had the same trouble before? I can't continue working on light manager without this.

Here's my c++ code to bind and use shader:
shader[1].loadShader( "data/shaders/attenuation.fs", "data/shaders/attenuation.vs" );
	 [....]
    shader[1].begin();
    shader[1].setUniform1i( "lightNum", 1 ); //this throws me an error
	 [.. rendering done here ..]
    shader[1].end();


And here are the shaders:
vertex
varying vec3 normal, lightDir, eyeVec;
varying float att;

uniform int lightNum;

void main()
{
//lightNum = 0;
normal = gl_NormalMatrix * gl_Normal;
vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
lightDir = vec3(gl_LightSource[lightNum].position.xyz - vVertex);
eyeVec = -vVertex;

float d = length(lightDir);

att = 1.0 / ( gl_LightSource[lightNum].constantAttenuation + (gl_LightSource[lightNum].linearAttenuation*d) + (gl_LightSource[lightNum].quadraticAttenuation*d*d) );

gl_Position = ftransform(); 
}


fragment
varying vec3 normal, lightDir, eyeVec;
varying float att;

uniform int lightNum;

void main (void)
{
//lightNum = 0;
vec4 final_color = (gl_FrontLightModelProduct.sceneColor * gl_FrontMaterial.ambient) + (gl_LightSource[lightNum].ambient * gl_FrontMaterial.ambient)*att;
	  
vec3 N = normalize(normal);
vec3 L = normalize(lightDir);

float lambertTerm = dot(N,L);

if(lambertTerm > 0.0)
{
  final_color += gl_LightSource[lightNum].diffuse * gl_FrontMaterial.diffuse * lambertTerm * att;
 
  vec3 E = normalize(eyeVec);
  vec3 R = reflect(-L, N);
 
  float specular = pow( max(dot(R, E), 0.0), gl_FrontMaterial.shininess );
 
  final_color += gl_LightSource[lightNum].specular * gl_FrontMaterial.specular * specular * att;
}
gl_FragColor = final_color;  
}


Sponsor:

#2 BraXi   Members   -  Reputation: 187

Like
0Likes
Like

Posted 20 June 2012 - 12:39 PM

Here's error log:

************ ERROR ************
can't find uniform variable: lightNum

#3 mhagain   Crossbones+   -  Reputation: 9858

Like
0Likes
Like

Posted 20 June 2012 - 01:20 PM

The error is telling you what's wrong - lightNum doesn't exist. The question is: why?

Be sure that you're using the correct shaders here, be sure that they compile and link fine, be sure that you check the info logs from the compile and link stages, be sure that your hardware and driver actually supports ints in GLSL shaders.

You're going through quite an abstracted layer to load and use your shaders. Without seeing the code behind that it's impossible to say any more.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#4 Kraecker   Members   -  Reputation: 563

Like
0Likes
Like

Posted 21 June 2012 - 01:11 AM

You are not using the uniform in your code.

When optimizing GLSL code (on compilation) unused variables are removed.
Use it in your code and you will be abled to set the uniform.

Edit:

gnah, first read, then write ... sry

Edited by Kraecker, 21 June 2012 - 01:14 AM.


#5 BraXi   Members   -  Reputation: 187

Like
0Likes
Like

Posted 23 June 2012 - 03:43 AM

Okey i have changed the shader a bit so it doesnt use gl_LightSource anymore, but now im running into other problem. I've noticed that light source is moving along with camera, im still learning opengl and i would like to solve the problem but i have no idea how to translate local to world coords, it seems hard for me. This is the last question, i promise Posted Image

I think the problem is in vertex shader so im posting it here:
varying vec3 normal, lightDir, eyeVec;
varying float att;

uniform vec4 light_pos;
uniform vec3 light_attenuation;

void main()
{

//problem must be here
normal = gl_NormalMatrix * gl_Normal;
vec3 vVertex = vec3( gl_ModelViewMatrix * gl_Vertex );
lightDir = vec3( light_pos.xyz - vVertex );
eyeVec = -vVertex;
//--------------------------

float d = length(lightDir);

att = 1.0 / ( light_attenuation.x + (light_attenuation.y*d) + (light_attenuation.z*d*d) );

gl_Position = ftransform();
}

Edited by BraXi, 23 June 2012 - 03:45 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS