0
votes

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?

2
Where is your shader code that sets those sampler uniforms?Nicol Bolas
@Nicol: I'm sorry, what? You don't set uniforms inside shader code...Mason Wheeler
Fair enough: your C++ code that sets those sampler uniforms. Also, you can set samplers in shader code. With the right extensions/GL version.Nicol Bolas
What is your blending function? Do you call glGetError?Luca
@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

2 Answers

0
votes

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 );
0
votes

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.