How to change color in lwjgl texture - java

I have the following texture:
The black pixels are characters, the background is transparent.
How do I change the black pixels of the characters to another color?
I use this texture as a bitmap for tekst rendering in lwjgl. Is there a way to change the black pixels to red?
I would appreciate an example.

As far as I am aware glColor* also applies for textures. If you change the black color of your font to white the given color would simply apply to this.

Related

Background in black and image in white colour

im doing the project convert the image colour to black and white. what i want is to change the background that it will be black and the image will be white in colour..but mostly of the code that found is changing the image in black and background in white. can anyone know how to change? i already have a code for converting that image to Black and white.
Google is your best friend... http://android-er.blogspot.it/2015/02/invert-bitmap-using-colormatrix.html.
In this example you see that white becomes black and vice/versa.
It also works on all other colors as well.
And it's very fast, because it uses a ColorMatrix, not a pixel by pixel color change, which will wuold require width*hight operations.
It's a single pass!
[EDIT]
I thought you already had the black and white image.
If this is not your case, just use a greyscale ColorMatrix as reported in Lalit Poptani's answer here.
[EDIT 2]
To change brightness and contrast after desaturating (converting to greyscale), try the code found here: Here something to help changing brightness and contrast: http://android.okhelp.cz/bitmap-set-contrast-and-brightness-android/

With Java's graphics shape drawing operations, is there an easy way to give the shapes transparency gradients and blurry edges?

What I would like is to give circles a fill color with a gradient that starts from the middle and then as it moves out to the edges becomes progressively more transparent, giving a blur effect. What is the simplest way of doing this?
Try setting an appropiately defined java.awt.RadialGradientPaint (using Colors with alpha), then render your circles using that. You may need to translate the graphics coordinate system to get the gradient centered in the circle. (http://docs.oracle.com/javase/7/docs/api/java/awt/RadialGradientPaint.html)
Or just make an image in a graphics program and simply draw the image.

OpenGL muliply/add texture with glColor

I'm making a game in Java using the LWJGL and Slick.
From what I understand glColor multiplies the RGB values, but what if I wanted to add color to a texture that's had RBG removed? (desaturated)
The purpose of this is to create a grey texture and then use OpenGL to add color to it in code.
Adding is most likely not what you wan't. Adding is more of a brightening effect.
It sounds like the feature that you wan't is multiplying (glColor). If you have a grayscale texture, mulyiplying it with a color like red will create an image where the white parts of the texture are pure red, and the dark sections are darker shades of red.
Read:
http://en.wikipedia.org/wiki/Blend_modes

Why do I need to disable GL_TEXTURE_2D after drawing string in order for shapes to render?

I'm using the Slick2D library in order to render text to the screen but in order to render gl shapes like Rect, I need to first disable GL_TEXTURE_2D. I'm just curious as to why that is needed. Why does GL_TEXTURE_2D disable the rendering of shapes?
The way OpenGL works is basically one large, global state machine. When you bind a texture, every triangle you draw afterwards will use that texture.
The issue here is that the text drawing doesn't unbind it's texture afterwards, so the shapes you draw afterwards will be using that texture instead of no texture. The reason why you think it's "disabling" rendering is because the texture is made up of characters with everything else being transparent. What you're seeing is OpenGL drawing your shape with opacity at 0.
What happens when you disable GL_TEXTURE_2D is that the texture gets unbound and you draw regularly without a texture.
Because the string's texture is applied. As you probably don't set any texture coords it probably uses a section of the texture that is transparent and hence you see nothing.

Using blending to create multiple areas of transparency surrounded by black

I've been playing around in openGL a little bit and I was wondering how I can take an image (say for example a black background) and then render a smaller white gradient over the top but have it make the white gradient transparent (instead of white) and remove from the black background. Leaving a black background with a transparent gradient in the center. I'm using LWJGL if that helps.
Is there any way of doing this with blending?
Edit: Okay, is there any way of instead of subtracting two images, I draw black around an image? So I have my white gradient, change it to be transparent and then draw black all around that?
Fixed function is usually implemented using shaders in modern GL drivers (there are old ATI's presentations with SM2.0+ shaders for all of the fixed functionality).
The close-to-trivial blending fragment shader like this
in vec2 TexCoord;
uniform sampler2D Texture0;
uniform sampler2D Texture1;
out vec4 out_FragColor;
void main()
{
vec4 Color = texture(Texture0, TexCoord);
/// The mask contains (R,G,B,A) - image + transparency
/// for both the mask and the source
vec4 Mask = texture(Texture1, TexCoord);
/// Mask is also significant as a color source, not only the alpha
out_FragColor = Color * ( 1.0 - Mask.a ) + Mask * Mask.a;
/// Thanks to Tim's comment, the last line can be done simpler:
/* out_FragColor = mix(Color, Mask, Mask.a); */
}
should mix the image the way you want to.
This is not overkill for modern hardware and it is 100% compatible even with the GL ES 2.0 hardware.
EDIT:
The image from one of my projects (simple mask used to create the fade-away reflection).
We use this mask
and get the final result in this image: Gallery
It may be possible to achieve what you want in the specific case, but in general once you 'draw a black background', there's no way to make it transparent afterward. You can't remove or erase pixels that have already been drawn.
If you can draw the gradient before the black background, then you can deposit the alpha into the framebuffer, and then sample the destination alpha when you draw the black background afterward. You'll need to have a real alpha texture though, as you can't really convert a white gradient to an alpha gradient at draw time.
Here's my suggested pseudocode:
draw whatever you want in the background
create texture with alpha gradient
lock color channels with glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_TRUE);
draw alpha gradient over background
unmask color channels with glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
draw 'black texture' with following blend options: glBlend(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA)
That should leave you with a black texture with a blended gradient to whatever was underneath it.

Categories

Resources