0

I'm trying to render colored text to the screen. I've got a texture containing a black (RGBA 0, 0, 0, 255) representation of the text to display, and I've got another texture containing the color pattern I want to render the text in. This should be a fairly simple multitexturing exercise, but I can't seem to get the second texture to work. Both textures are Rectangle textures, because the integer coordinate values are easier to work with.

Rendering code:

glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, TextHandle);

glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, ColorsHandle);

glBegin(GL_QUADS);
  glMultiTexCoord2iARB(GL_TEXTURE0_ARB, 0, 0);
  glMultiTexCoord2iARB(GL_TEXTURE1_ARB, colorRect.Left, colorRect.Top);
  glVertex2f(x, y);

  glMultiTexCoord2iARB(GL_TEXTURE0_ARB, 0, textRect.Height);
  glMultiTexCoord2iARB(GL_TEXTURE1_ARB, colorRect.Left, colorRect.Top + colorRect.Height);
  glVertex2f(x, y + textRect.Height);

  glMultiTexCoord2iARB(GL_TEXTURE0_ARB, textRect.Width, textRect.Height);
  glMultiTexCoord2iARB(GL_TEXTURE1_ARB, colorRect.Left + colorRect.Width, colorRect.Top + colorRect.Height);
  glVertex2f(x + textRect.Width, y + textRect.Height);

  glMultiTexCoord2iARB(GL_TEXTURE0_ARB, textRect.Width, 0);
  glMultiTexCoord2iARB(GL_TEXTURE1_ARB, colorRect.Left + colorRect.Width, colorRect.Top);
  glVertex2f(x + textRect.Width, y);
glEnd;

Vertex shader:

void main()
{
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
   gl_TexCoord[0] = gl_MultiTexCoord0;
   gl_TexCoord[1] = gl_MultiTexCoord1;
}

Fragment shader:

uniform sampler2DRect texAlpha;
uniform sampler2DRect texRGB;
void main()
{   
   float alpha = texture2DRect(texAlpha, gl_TexCoord[0].st).a;
   vec3 rgb = texture2DRect(texRGB, gl_TexCoord[1].st).rgb;
   gl_FragColor = vec4(rgb, alpha);
}

This seems really straightforward, but it ends up rendering solid black text instead of colored text. I get the exact same result if the last line of the fragment shader reads gl_FragColor = texture2DRect(texAlpha, gl_TexCoord[0].st);. Changing the last line to gl_FragColor = texture2DRect(texRGB, gl_TexCoord[1].st); causes it to render nothing at all.

Based on this, it appears that calling texture2DRect on texRGB always returns (0, 0, 0, 0). I've made sure that GL_MULTISAMPLE is enabled, and bound the texture on unit 1, but for whatever reason I don't seem to actually get access to it inside my fragment shader. What am I doing wrong?

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
  • Where is your shader code that sets those sampler uniforms? – Nicol Bolas Dec 19 '11 at 02:48
  • @Nicol: I'm sorry, what? You don't set uniforms inside shader code... – Mason Wheeler Dec 19 '11 at 13:34
  • Fair enough: your C++ code that sets those sampler uniforms. Also, you *can* set samplers in shader code. With [the right extensions/GL version.](http://www.opengl.org/wiki/GLSL_Sampler#Verison_4.20_binding) – Nicol Bolas Dec 19 '11 at 17:19
  • What is your blending function? Do you call glGetError? – Luca Dec 19 '11 at 19:40
  • @Luca: Blend function is `glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)`, but does that even matter when I'm rendering with a fragment shader? And no, my code isn't calling glGetError, but I'm running it under gDEBugger and I have it set to break on any GL errors. – Mason Wheeler Dec 19 '11 at 20:07
  • What is the value of **colorRect**? What are the texture parameters? – Luca Dec 20 '11 at 21:28

2 Answers2

0

The overalls look fine. It is possible that your texcoords for unit 1 are messed up, causing sampling outside the colored portion of your texture.

Is your color texture fully filled with color ?

What do you mean by "causes it to render nothing at all." ? This should not happen except if your alpha channel in color texture is set to 0.

Did you try with the following code, to override the alpha channel ?

  gl_FragColor = vec4( texture2DRect(texRGB, gl_TexCoord[1].st).rgb, 1.0 );
rotoglup
  • 5,223
  • 25
  • 37
  • Just tried that. It gives me black boxes. Like I said, the texture2DRect call seems to be returning (0, 0, 0, 0). – Mason Wheeler Dec 18 '11 at 21:51
  • Can you post a link to your image ? and a copy of your texture init code ? I think the problem comes either from your texcoords, or your texture initialisation. – rotoglup Dec 18 '11 at 21:59
  • No, those are both working fine. If I switch the two textures around, I get the color quad as output. I've verified everything I can with gDEBugger, and everything is working as expected, right up until I start drawing. And unfortunately, gDEBugger can't take me inside the shader itself, so that's the point where I get lost. – Mason Wheeler Dec 18 '11 at 22:03
  • Sorry then, I run out of ideas, your shader code looks fine. I suppose that you checked the uniform sampler values assignment to... You could try to fiddle with GLIntercept shader editor, to live change your shader code, this may give a you a lead... good luck ! – rotoglup Dec 18 '11 at 22:12
0

Are you sure the the font outline texture contains a valid alpha values? You said that the texture is black and white, but you are using the alpha value! Instead of using the a component, try to use the r one.

Blending affects fragment shader output: it blends ths fragment color with the corresponding one.

Luca
  • 11,646
  • 11
  • 70
  • 125
  • Yes, I'm sure the font outline contains a valid alpha value. I've verified that in gDEBugger. The blank pixels are (0, 0, 0, 0) and the black pixels are (0, 0, 0, 255). (Yeah, with nothing in between. I'll worry about antialiasing once I get the base case working right.) – Mason Wheeler Dec 20 '11 at 13:49