Xi Yang
05-25-2011, 02:42 AM
My problem is: glGetUniformLocation() always give me -1.
Althogh it fails to set the sampler, the shader program works almost correct, excepts only the first texture is working.
Thanks a lot!
This is the vertex program:
#version 130
uniform int test_var;
uniform sampler2D sp_surface;
uniform sampler2D sp_cloud;
uniform sampler2D sp_bump;
out vec3 normal_v;
out vec3 light_v;
out vec3 eye_v;
void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_FrontColor = gl_Color;
vec4 vertex_p = gl_ModelViewMatrix * gl_Vertex;
vec4 light_p = gl_LightSource[0].position;
normal_v = normalize(gl_NormalMatrix * gl_Normal);
light_v = normalize(gl_LightSource[0].position.xyz - vertex_p.xyz);
eye_v = normalize(-vertex_p.xyz);
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_MultiTexCoord1;
}
And the fragment program:
#version 130
uniform sampler2D sp_surface;
uniform sampler2D sp_cloud;
uniform sampler2D sp_bump;
const vec3 atm_color = vec3(1,1,1);
in vec3 normal_v;
in vec3 light_v;
in vec3 eye_v;
void main() {
float Kd = max( dot(light_v,normal_v), 0.0 );
vec4 diffuse = Kd * gl_FrontLightProduct[0].diffuse;
vec3 half_v = normalize(light_v + eye_v);
float Ks = pow( max(dot(half_v,normal_v),0.0), gl_FrontMaterial.shininess );
float f = 1.0;
if (dot(normal_v,light_v)<0) f = 0;
vec4 specular = f * Ks * gl_FrontLightProduct[0].specular;
vec4 ambient = gl_FrontLightProduct[0].ambient;
// atmosphere effect
float atm_alpha = length(cross(normal_v,eye_v))/length(normal_v)/length(eye_v);
atm_alpha = pow(atm_alpha,100);
// calculate color
vec4 ground_color = texture(sp_surface,gl_TexCoord[0].st);
ground_color.rgb = ground_color.rgb*(1-atm_alpha) + atm_color*atm_alpha;
gl_FragColor = (ambient+diffuse) * ground_color + specular + gl_FrontMaterial.emission;
}
This is the part where uniforms are fetched and set.
// DBG
GLint addr_test = glGetUniformLocation(program,"test_var ");
cout<<"test variable addr: "<<addr_test<<endl;
// set texture sampler
// assume the surface is 0, cloud is 1, bump is 2
GLint addr_sampler_surface = glGetUniformLocation(program,"sp_surface");
GLint addr_sampler_cloud = glGetUniformLocation(program,"sp_cloud");
GLint addr_sampler_bump = glGetUniformLocation(program,"sp_bump");
if ( (addr_sampler_surface==-1)
|| (addr_sampler_cloud==-1)
|| (addr_sampler_bump==-1) )
{
cerr<<"invalid uniform address:\n";
cerr<<"\tsurface addr: "<<addr_sampler_surface<<"\n";
cerr<<"\tcloud addr: "<<addr_sampler_cloud<<"\n";
cerr<<"\tbump addr: "<<addr_sampler_bump<<"\n";
//throw "";
}
glUniform1i(addr_sampler_surface,GL_TEXTURE0);
glUniform1i(addr_sampler_cloud,GL_TEXTURE1);
glUniform1i(addr_sampler_bump,GL_TEXTURE2);
Althogh it fails to set the sampler, the shader program works almost correct, excepts only the first texture is working.
Thanks a lot!
This is the vertex program:
#version 130
uniform int test_var;
uniform sampler2D sp_surface;
uniform sampler2D sp_cloud;
uniform sampler2D sp_bump;
out vec3 normal_v;
out vec3 light_v;
out vec3 eye_v;
void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_FrontColor = gl_Color;
vec4 vertex_p = gl_ModelViewMatrix * gl_Vertex;
vec4 light_p = gl_LightSource[0].position;
normal_v = normalize(gl_NormalMatrix * gl_Normal);
light_v = normalize(gl_LightSource[0].position.xyz - vertex_p.xyz);
eye_v = normalize(-vertex_p.xyz);
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_MultiTexCoord1;
}
And the fragment program:
#version 130
uniform sampler2D sp_surface;
uniform sampler2D sp_cloud;
uniform sampler2D sp_bump;
const vec3 atm_color = vec3(1,1,1);
in vec3 normal_v;
in vec3 light_v;
in vec3 eye_v;
void main() {
float Kd = max( dot(light_v,normal_v), 0.0 );
vec4 diffuse = Kd * gl_FrontLightProduct[0].diffuse;
vec3 half_v = normalize(light_v + eye_v);
float Ks = pow( max(dot(half_v,normal_v),0.0), gl_FrontMaterial.shininess );
float f = 1.0;
if (dot(normal_v,light_v)<0) f = 0;
vec4 specular = f * Ks * gl_FrontLightProduct[0].specular;
vec4 ambient = gl_FrontLightProduct[0].ambient;
// atmosphere effect
float atm_alpha = length(cross(normal_v,eye_v))/length(normal_v)/length(eye_v);
atm_alpha = pow(atm_alpha,100);
// calculate color
vec4 ground_color = texture(sp_surface,gl_TexCoord[0].st);
ground_color.rgb = ground_color.rgb*(1-atm_alpha) + atm_color*atm_alpha;
gl_FragColor = (ambient+diffuse) * ground_color + specular + gl_FrontMaterial.emission;
}
This is the part where uniforms are fetched and set.
// DBG
GLint addr_test = glGetUniformLocation(program,"test_var ");
cout<<"test variable addr: "<<addr_test<<endl;
// set texture sampler
// assume the surface is 0, cloud is 1, bump is 2
GLint addr_sampler_surface = glGetUniformLocation(program,"sp_surface");
GLint addr_sampler_cloud = glGetUniformLocation(program,"sp_cloud");
GLint addr_sampler_bump = glGetUniformLocation(program,"sp_bump");
if ( (addr_sampler_surface==-1)
|| (addr_sampler_cloud==-1)
|| (addr_sampler_bump==-1) )
{
cerr<<"invalid uniform address:\n";
cerr<<"\tsurface addr: "<<addr_sampler_surface<<"\n";
cerr<<"\tcloud addr: "<<addr_sampler_cloud<<"\n";
cerr<<"\tbump addr: "<<addr_sampler_bump<<"\n";
//throw "";
}
glUniform1i(addr_sampler_surface,GL_TEXTURE0);
glUniform1i(addr_sampler_cloud,GL_TEXTURE1);
glUniform1i(addr_sampler_bump,GL_TEXTURE2);