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?
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);
}
else
{
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);
}
}