Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: uniform arrays

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2004
    Location
    Oegstgeest, The Netherlands
    Posts
    11

    uniform arrays

    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 . 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

  2. #2
    Junior Member Newbie
    Join Date
    Feb 2004
    Location
    Oegstgeest, The Netherlands
    Posts
    11

    Re: uniform arrays

    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

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2004
    Location
    Oegstgeest, The Netherlands
    Posts
    11

    Re: uniform arrays

    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

  4. #4
    Junior Member Newbie
    Join Date
    Feb 2004
    Location
    Oegstgeest, The Netherlands
    Posts
    11

    Re: uniform arrays

    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).]

  5. #5
    Advanced Member Frequent Contributor
    Join Date
    Jan 2001
    Location
    NVIDIA, Austin, TX
    Posts
    555

    Re: uniform arrays

    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.

  6. #6
    Junior Member Newbie
    Join Date
    Feb 2004
    Location
    Oegstgeest, The Netherlands
    Posts
    11

    Re: uniform arrays

    ok, thanx a lot!

  7. #7
    Junior Member Newbie
    Join Date
    Jan 2004
    Posts
    17

    Re: uniform arrays

    wow that just helped me too jra

    i wish the spec was more clear on that

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •