PDA

View Full Version : uniform arrays



Marty666
02-10-2004, 02:56 AM
Hi all,

Can i send an array to a uniform? Like:

in shader:
uniform sampler2D tex[2];

in application:

GLuint textures[2];

shader_uniform_tex = glGetUniformLocationARB(g_programObj, "tex");
if (shader_uniform_tex != -1)
{
glUniform2iARB(shader_uniform_tex, textures);
}

I know the glUniform2iARB doesn't work. Is there an other way?

I'm asking because I had 2 uniforms before and it didn't find one of them all the time http://www.opengl.org/discussion_boards/ubb/frown.gif. I spelled it right, changed the name from 'bump' to 'bumptex' (in case it is reserved) and tried changing the order of the two uniforms. None of this helped.

Can anyone tell me the solution?

Thanx,
Marty

Marty666
02-10-2004, 03:24 AM
very strange... this is my fragment shader:

varying vec3 v;
uniform sampler2D tex;
uniform sampler2D bump;

const vec3 addvec = vec3(0.5, 0.5, 0.5); // normals in map are clamped 0-1

void main (void)
{
vec4 TexCol = texture2D(tex, vec2(gl_TexCoord[0]));
vec4 BumpCol = texture2D(bump, vec2(gl_TexCoord[1]));
vec3 N = normalize(gl_NormalMatrix * (BumpCol.xyz - addvec));
N.y = -N.y;

vec3 L = normalize(gl_LightSource[0].position.xyz - v);
vec3 E = normalize(-v); // we are in Eye Coordinates, so EyePos is (0,0,0)
vec3 R = normalize(-reflect(L,N));

vec4 MyColor = gl_FrontLightProduct[0].ambient * TexCol;
MyColor += gl_FrontLightProduct[0].diffuse * max(dot(N,L), 0.0) * TexCol;
MyColor += gl_FrontLightProduct[0].specular * pow(max(dot(R,E),0.0), 0.3 * gl_FrontMaterial.shininess);
gl_FragColor = MyColor;
}

and this is the code in my application:


if (!(NormalTexture = LoadTexture("texture.bmp")))
{MessageBox(NULL, "Could not load 'texture.bmp'!", "ERROR!", MB_OK|MB_ICONEXCLAMATION);}
if (!(BumpTexture = LoadTexture("bump.bmp")))
{MessageBox(NULL, "Could not load 'bump.bmp'!", "ERROR!", MB_OK|MB_ICONEXCLAMATION);}

InitExtentions();
glUseProgramObjectARB(g_programObj);
shader_uniform_tex = glGetUniformLocationARB(g_programObj, "tex");
if (shader_uniform_tex != -1)
{glUniform1iARB(shader_uniform_tex, NormalTexture);} else
{MessageBox(NULL, "Could not find uniform location for 'tex'!", "ERROR!", MB_OK|MB_ICONEXCLAMATION);}

shader_uniform_bump = glGetUniformLocationARB(g_programObj, "bump");
if (shader_uniform_bump != -1)
{glUniform1iARB(shader_uniform_bump, BumpTexture);} else
{MessageBox(NULL, "Could not find uniform location for 'bump'!", "ERROR!", MB_OK|MB_ICONEXCLAMATION);}
glUseProgramObjectARB(NULL);


With the fixed pipeline it all looks fine multitextured. With this i only get to see one texture and that is 'bump.bmp' which is stored in 'uniform sampler2D tex'!!
Can't be right...

Anyone know what's wrong?

Thanx,
Marty

Marty666
02-10-2004, 03:37 AM
ok, i found out what might be the problem:

I changed my texture loading and sending code to this:

// if (!(NormalTexture = LoadTexture("texture.bmp")))
// {MessageBox(NULL, "Could not load 'texture.bmp'!", "ERROR!", MB_OK|MB_ICONEXCLAMATION);}

if (!(BumpTexture = LoadTexture("bump.bmp")))
{MessageBox(NULL, "Could not load 'bump.bmp'!", "ERROR!", MB_OK|MB_ICONEXCLAMATION);}

InitExtentions();
glUseProgramObjectARB(g_programObj);
// shader_uniform_tex = glGetUniformLocationARB(g_programObj, "tex");
// if (shader_uniform_tex != -1)
// {glUniform1iARB(shader_uniform_tex, NormalTexture);} else
// {MessageBox(NULL, "Could not find uniform location for 'tex'!", "ERROR!", MB_OK|MB_ICONEXCLAMATION);}

shader_uniform_bump = glGetUniformLocationARB(g_programObj, "bump");
if (shader_uniform_bump != -1)
{glUniform1iARB(shader_uniform_bump, BumpTexture);} else
{MessageBox(NULL, "Could not find uniform location for 'bump'!", "ERROR!", MB_OK|MB_ICONEXCLAMATION);}
glUseProgramObjectARB(NULL);

If i use the first two lines I loose my texture and the other one comes instead. I'm kinda new with multitexturing, so I might be doing something wrong with that.

Marty

Marty666
02-10-2004, 04:05 AM
I found out it is the way in which i send the uniforms. When i loadTexture("bump.bmp") first, it's all ok, when i do it with "texture.bmp" it goes wrong. So the uniform must be undefined, but contains a 0 or 1 which is interpreted as texnum 0 or 1 all the time. Then again, the other uniform must undefined aswell, and probably contains the same texnum, which isn't the case, because it seems to be a totally black texture.

Anyone?
Marty

[This message has been edited by Marty666 (edited 02-10-2004).]

jra101
02-10-2004, 09:06 AM
The value you pass into glUniform1iARB when setting a sampler specifies the texture unit for that sampler. It does NOT specify the texture object bound to that sampler.

You need to bind each sampler to a specific texture unit (0, 1, 2, etc...) then using standard OpenGL multitexturing, bind your texture objects to the appropriate texture unit using glActiveTexture and glBindTexture.

Marty666
02-12-2004, 03:11 AM
ok, thanx a lot!

imr1984
03-03-2004, 04:20 AM
wow that just helped me too jra http://www.opengl.org/discussion_boards/ubb/smile.gif

i wish the spec was more clear on that