BlendFunc for textured fonts with changing background - opengl

I am attempting to use Textured fonts as so that I can display text in my openGL scene. However I am having trouble finding glBlendFunc values that will work.
The background that the text will be placed on is a grayscale image but will change throughout the execution. Because the background changes the text could possibly be on top of any color from black to white.
The best values I have found are glBlendFunc(Gl.GL_SRC_COLOR, Gl.GL_ONE_MINUS_SRC_ALPHA). This will make the black box surrounding the character disappear but the character itself will fade as the background goes towards white.
Please help!

Do you want the text to invert based on the background color? white text over black background, black text on white? I think you can achieve an invert via blendfunc.
Alternatively you can use a font texture which has a "border" built into it to help set the character apart from the background. Imagine a white font w/ a smooth alpha blended black "glow". The font will look good against almost all colors.

Related

How to change draw order of MFC graph elements

I am trying to modify the look of the graph displayed to have a white background with black axes.
However, simply modifying the RGB values of
m_OScopeCtrl.SetBackgroundColor(RGB(0, 64, 0)) ;
m_OScopeCtrl.SetGridColor(RGB(192, 255, 192)) ;
m_OScopeCtrl.SetPlotColor(RGB(255, 255, 255)) ;
in “TestOScopeDlg.cpp” doesn’t seem to do the trick, the entire graph just turns white and the plots disappear (presumably being hidden by the white background).
Link to source code on codeproject.
http://www.codeproject.com/Articles/241/Oscilloscope-StripChart-Control
How can I make the graph appear with a white background and black axes?
Found the answer. Changing SRCPAINT to SRCAND works.
It seems SRCPAINT works when trying to overlay a light color on top of a dark color and SRCAND works when trying to overlay a dark color on top of a light color.

TTF_RenderTextSolid() returns a Surface with 1 byte per pixel?

This code:
TTF_Font * titania = TTF_OpenFont( "chintzy.ttf",28);
SDL_Color textColor = {255,255,0};
SDL_Surface * textSurface = TTF_RenderText_Solid(titania,"Its Working!",textColor);
std::cout << (int)textSurface->format->BytesPerPixel;
Prints the number one, meaning that the surface returned by TTF_RenderTextSolid has one byte per pixel. If I am correct it should be 4 bytes per pixel. Does anyone know why this is happening?
Looks like it's doing exactly what the documentation says it ought to:
Solid
Create an 8-bit palettized surface and render the given text at fast quality with the given font and color. The pixel value of 0 is the colorkey, giving a transparent background when blitted. Pixel and colormap value 1 is set to the text foreground color. This allows you to change the color without having to render the text again. Palette index 0 is of course not drawn when blitted to another surface, since it is the colorkey, and thus transparent, though its actual color is 255 minus each of the RGB components of the foreground color. This is the fastest rendering speed of all the rendering modes. This results in no box around the text, but the text is not as smooth. The resulting surface should blit faster than the Blended one. Use this mode for FPS and other fast changing updating text displays.
If you want 32bpp you need to use the *_Blended() variants:
Blended
Create a 32-bit ARGB surface and render the given text at high quality, using alpha blending to dither the font with the given color. This results in a surface with alpha transparency, so you don't have a solid colored box around the text. The text is antialiased. This will render slower than Solid, but in about the same time as Shaded mode. The resulting surface will blit slower than if you had used Solid or Shaded. Use this when you want high quality, and the text isn't changing too fast.
Self Answer:
TTF_RenderTextSolid() returns a surface in false color. This means that it is kind of like black and white but white is a color you have defined and black is just the opposite of that color (which is usually completely transparent). It can be changed into a regular surface by using SDL_ConvertSurface.

Displaying images without the black portion

I am trying to display a bitmap using opengl but I don't want the black portion of the image to be displayed. I can do this in DirectX but not in opengl. In other words - I have images of plants with a black background and I want the plants to be drawn so they look realistic (without a black border).
You can do this using alpha-testing:
Add an alpha channel to your image before uploading it to a texture, 0.0 on black pixels and 1.0 everywhere else.
Enable alpha-testing with glEnable( GL_ALPHA_TEST )
glAlphaFunc( GL_LESS, 0.1f )
Render textured quad as usual. OpenGL will skip the zero-alpha texels thanks to the alpha-test.
There are a couple of ways you can do this.
One is to use an image editing program like Photoshop or GIMP to add an alpha channel to your image and then set the black portions of the image to a max alpha value. The upside to this is it allows you to decide which portions of the image you want to be transparent, since a fully programmatic approach can sometimes hide things you want to be seen.
Another method is to loop through every pixel in your bitmap and set the alpha based on some defined threshold (i.e. if you want true black, check to see if each color channel is at 255). The downside to this is it will occasionally cause some of your lines to disappear.
Also, you will need to make sure that you have actually enabled the alpha channel and test, as stated in the answer above. Make sure to double check the order of your calls as well, as this can cause a lot of issues when you're trying to use transparency.
That's about as much as I can suggest since you haven't posted the code itself, but hopefully it should be enough to at least get you on the way to a solution.

OpenGL: blend is disable, but still blending

I try to render an object, but I don't want its edge blend with the background.
I use only 2 colors: red for the object, and white for the background.
When I get the pixels of the frame with glReadPixel() I see that there are other colors except for
red and white, a blending colors. These colors are located on the edges of the object.
Eventually, I need to get a picture of 2 colors only.
Someone can help me to solve this?
(note: I used glDisable(GL_BLEND) to make sure it didn't work)

"Lantern effect" or show only a part of a scene

I'm trying to show only a part of a background image (game scenenario in the future). The basic way to work is for example, first I draw a background image, after that i need to "hide"/cover the image with some dark or darness (no light, don't know what option must be chosen) and use the mouse click to using a circle or a triangle (my options) show only the part of the image background over with the circle/triangle centered on mouse position. I called this "lantern effect".
First Option: Play with the alpha channel, creating a an square covering all the window size and after that trying to substract the circle area over the alpha square over the image.
Second Option: Play again with a black square covering all the image background and trying to substract a circle/triangle. Try with glLogicOp but this method only plays mixing colors. Don't know how to do operation with 2D polygons with OpenGL.
...
Any other idea or easy example to learn how to do something similar.
Image example:
That's quite easy to achieve actually:
Create black texture with your lantern light shape in Alpha channel. (Texture could be animated)
Render background.
Render Lantern texture centered at your in-game mouse cursor.
Render black padding around the lantern texture to hide everything around till screen edges.
There's no need to use any special blending modes, just an overlay.