• Advertisement
Sign in to follow this  

GLSL Newbie issues

This topic is 2912 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to write a GLSL fragment shader that works on a 2D texture that represents some data in the red channel of an RGBA texture. My data has 256 possible values (hence working in a single color channel), and I bilinear smooth that data (after the smoothing, my values still remain in the red channel). I then try to do a lookup into a 1D texture (that has 256 RGB color values) that maps my smoothed data to my visual color representation of that data. Here's my sample shader:
uniform sampler2D data_map;
uniform sampler1D color_table;
vec4 texture2D_bilinear(sampler1D lkup, sampler2D data, vec2 uv)
{
   ivec2 dim = ivec2(1024, 1024);
   float textureWidth = dim.x;
   float textureHeight = dim.y;
   float texel_size_x  = 1.0 / textureWidth;
   float texel_size_y = 1.0 / textureHeight;
   vec2 f;
   f.x   = fract( uv.x * textureWidth );
   f.y   = fract( uv.y * textureHeight );
   vec4 t00 = texture2D( data, uv + vec2( 0.0, 0.0 ));
   vec4 t10 = texture2D(data , uv + vec2( texel_size_x, 0.0 ));
   vec4 tA = mix( t00, t10, f.x);
   vec4 t01 = texture2D( data, uv + vec2( 0.0, texel_size_y ) );
   vec4 t11 = texture2D( data, uv + vec2( texel_size_x, texel_size_y ) );
   vec4 tB = mix( t01, t11, f.x );
   vec4 bilinear = mix( tA, tB, f.y );

   
   // Use the smoothed red value of bilinear
   // to find the appropriate RGB color stored
   // in my 1D texture.  This is where everything goes
   // wrong.
   float idx = float(bilinear.r);
   return texture1D(lkup, idx);
}
void main (void)
{
   gl_FragColor = texture2D_bilinear( color_table, data_map, gl_TexCoord[0].st);
};
I know the bilinear smoothing works, b/c if I do a "return bilinear", instead of "return texture1D(lkup, idx)", I truly get a smoothed red texture as I expect. I think I'm just not referencing my 1D texture correctly, or something I'm completely not understanding altogether. Here's what my c++ code looks like:
         m_qfrag_program.bind();
         glEnable(GL_TEXTURE_2D);
         glEnable(GL_TEXTURE_1D);
         glBindTexture(GL_TEXTURE_1D, m_color_lookup);
         glBindTexture(GL_TEXTURE_2D, m_data_texture);


         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
            GL_NEAREST);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
            GL_NEAREST);

         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER,
            GL_NEAREST);
         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER,
            GL_NEAREST);



         glBegin(GL_QUADS);

         glTexCoord2f(0.0, 0.0);
         glVertex3f(-1.0, -1.0,  0.1f);

         glTexCoord2f(1.0, 0.0);
         glVertex3f(1.0, -1.0,  0.1f);

         glTexCoord2f(1.0, 1.0);
         glVertex3f(1.0, 1.0,  0.1f);

         glTexCoord2f(0.0, 1.0);
         glVertex3f(-1.0, 1.0,  0.1f);
         glEnd();

         glDisable(GL_TEXTURE_2D);
         glDisable(GL_TEXTURE_1D);
         m_qfrag_program.release();

Share this post


Link to post
Share on other sites
Advertisement
you are returning texture1D in a vec4. And gl_FragColor is also vec4. That can cause the problem.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kasya
you are returning texture1D in a vec4. And gl_FragColor is also vec4. That can cause the problem.



texture1D is supposed to return a vec4, which is a 4 element vector for RGBA? I guess I don't understand what the problem is with that... ?

Share this post


Link to post
Share on other sites
Can you actually bind two different textures to the same texture unit - even though they are bound to different targets - and access them independently in the shader program as you do?

Share this post


Link to post
Share on other sites
Quote:
Original post by fcoelho
Can you actually bind two different textures to the same texture unit - even though they are bound to different targets - and access them independently in the shader program as you do?


Since I'm a newbie at glsl, I'm not sure. Let me ask you this, how should one access two textures in a shader program and work on their colors values independently? I've ordered the orange book, and have been looking online for multi-texture examples, but for whatever reason, I'm not finding what I need.

If you do know how to access two textures (in my case 2D and 1D), can someone maybe show a code snippet on how that should look in c++/c and in glsl?

Thanks....

Share this post


Link to post
Share on other sites
Quote:
Let me ask you this, how should one access two textures in a shader program and work on their colors values independently?
I've asked because I never tried to do it that way, I didn't knew if it would work. For a usual multi texture setup, you just have to bind different textures to different texture units with glActiveTexture, i.e., something like
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, myTex2D);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_1D, myTex1D);
and set the samplers to the correct texture units, 0 and 1. The GLSL code won't change.

Just to point something out: enabling/disabling GL_TEXTURE_xD will have no effect on the shader output. When you use a fragment program, using textures or not is dependent on the shader itself, if you don't want to use texturing, write a shader that doesn't use it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement