Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: really simple rgb shader

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2008

    really simple rgb shader

    I hope this is an easy question...

    I'm just trying to create a shader that reads an RGB image frame (width x height) formatted as (R1,G1,B1,R2,G2,B2,...,Rn,Gn,Bn) into a texture.

    The code below actually works, but I'm curious as to why I need the 'test_offset' variable set to 0.5. I arrived at that value from trial and error, and if I don't use it (i.e. set it to zero), the color ends up being wrong.

    Could someone explain what I'm doing wrong? And/or why 0.5 works?

    Code :
    void main() {
      // calculate what image pixel we're on within texture [width*3 x height]
      vec2 pixel_pos = vec2(floor(mImageWidth * 3.0 * gl_TexCoord[0].x),
                            floor(mImageHeight * gl_TexCoord[0].y));
      // only process pixels within width
      if (pixel_pos.x > mImageWidth)
        gl_FragColor = vec4(0, 0, 0, 0.0);
        float test_offset = 0.5; // only works if test_offset == 0.5; why??
        float r_indx = (pixel_pos.x*3.0)+0.0+test_offset;
        float g_indx = (pixel_pos.x*3.0)+1.0+test_offset;
        float b_indx = (pixel_pos.x*3.0)+2.0+test_offset;
        float r_tex = texture2D(mTextureSampler, vec2(r_indx/(mImageWidth*3.0), gl_TexCoord[0].y)).x;
        float g_tex = texture2D(mTextureSampler, vec2(g_indx/(mImageWidth*3.0), gl_TexCoord[0].y)).x;
        float b_tex = texture2D(mTextureSampler, vec2(b_indx/(mImageWidth*3.0), gl_TexCoord[0].y)).x;
        vec3 rgb = vec3(r_tex, g_tex, b_tex);
        gl_FragColor = vec4(rgb, 1.0);

  2. #2
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Grenoble - France
    Do you use GL_NEAREST for your texture sampling ?
    Otherwise sampled values will be linearly interpolated between texels.

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2008
    I tried that, and for an image that should be all red, I get black streaks (shown below):

    It seems like it's interpolating in between the R, G, and B values by treating them each as texels, whereas it should be interpolating by comparing R1 to R2, G1 to G2, B1 to B2, etc. Maybe?

    Click image for larger version. 

Name:	Screenshot-4.jpg 
Views:	205 
Size:	16.7 KB 
ID:	711
    Last edited by outburst; 05-09-2012 at 05:43 PM.

Posting Permissions

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