Android OpenGL - Shape Transparency (No in Texture) - android

This is my first question...
I've a square (triangle strip) with a texture (.png).
This .png have smooth corners like a play card.
The four extremities (corners) are transparent.
When draw the square, in the corners, i see the white color of the shape.
(Like a background behind the texture)
My question is:
How can i draw a transparent color for the shape, but mantain the color of the texture with a full apha?
(If i set transparent colors.... then also the texture become transparent)
How can i separate the two contexts?
Thanks in advance.. and sorry for my bad english.

First of all, you must use a texture format with alpha channel.
Then if you are using fixed function pipeline, you must enable blending.
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
If you are not using fixed function pipeline, please share the fragment shader code; and a little more details on how you are using it.

Related

Blink a shape in OpenGL ES

I'd like to blink out a texture in an android live wallpaper. I can easily just not draw that shape for a second, but I'd like it to look more realistic.
How can I blink (like an eye blink) a shape. I'd like it to progressively close. One idea was to have another duplicate shape with a fragment shader that only covered a percentage of the shape on each iteration of draw, but I'm not sure how to determine if the pixel should be transparent or not from within the shader. Any ideas?
Or is there a better way to accomplish this?

Sprite Opacity using OpenGL ES 2.0 on Android

I'm trying to get my point sprites to display with the correct opacity.
Originally, I was getting my sprite texture on a black square.
So, I added the following to my fragment shader:
"if(color.a < 0.5) "+
"discard;"+
Now, this does seem to work, in that my sprite displays without the black background, however, my texture itself is 'partially transparent' - and it isn't showing this partial transparency, it is appearing solid. It's a bit difficult to explain, but I hope you understand what I mean. If I draw the same texture using canvas/surfaceview, it displays correctly.
Basically I'm trying to get my textures to display in their original format (ie as they do in the software in which they were created - ie, the Gimp / photoshop etc).
Would appreciate any help - thanks
First make sure your textures are loaded from transparent pngs through a Bitmap with either RGBA_8888 or RGBA_4444 configuration, so you don't lose the alpha channel.
Second you need to enable GL_BLEND with the glEnable() command. On Android you will write it like this: GLES20.glEnable(GLES20.GL_BLEND);. This allows you to blend the already drawn color with the new color, achieving a transparent look.
The blending function should be set to GL_ONE, GL_ONE_MINUS_ALPHA for regular transparency: glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
GL_SRC_ALPHA, GL_ONE_MINUS_ALPHA for regular transparency: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Finally, you do not need to use discard, just set the gl_FragColor to a 4-component vector with the alpha in the fourth channel (which is what you get when reading a texture from a sampler), e.g. you could just do gl_FragColor = texture2D(sampler, texCoord); if you wanted to.
You will most likely have to turn off depth-testing with glDisable(GL_DEPTH_TEST) to avoid problems with unsorted triangles.
You can read a little bit more about transparency here.

OpenGL -- Drawing a circle by mapping a texture onto a square

I am currently using VBOs and triangle fans to draw circles. Someone told me that it was more efficient to map a texture of a circle onto a quad, and then apply transparency. My circle needs to gradually change color over time (hundreds of possible colors).
Is texturing a quad really more efficient? If so, could someone please provide me with a relevant link or some code/pseudocode (specifically how to change the colors for just the circular region, and the appropriate blending filter) as to how to make this dream a reality?
If your circle always has the same color over its whole region (colors don't change on different regions idependently), you can just change the color of your quad and multiply it by a white circle texture either using GL_MODULATE texture environment (if using fixed function) or by just writing the constant color instead of the texture color (if using shaders).
Along with mapping a white texture with texture coordinates and vertex coordinates, giving a valid color pointer with required color values in it worked for me. I did not use any GL_MODULATE in 1.x code.

Android: image doesn't keep it's original color with use of canvas.drawBitmap

I'm developing an android app and I'm facing a weird issue.
I'm doing some image processing on a SurfaceView. I'm drawing the processed image using a canvas and the following method:
canvas.drawBitmap(image, x, y, paint)
My SurfaceView has a colored background (#3500ffff, kind of very dark green) and once the image is drawn, I can notice that its original colors are not conserved. It has a very slight dark green tint, like if the bitmap alpha was changed.
Did anyone already encounter this issue? Would you have an idea on how to fix this?
This would happen with a 16 bits destination. 16 bits buffers encode pixels in 565 format, which gives you a higher precision in the green channel, which sometimes result in greenish tints. A 32 bits destination/bitmap would solve this issue.
Presuming that your image is not transparent how did you define paint, it should not be a transparent colour or use some special effect. Try using null for paint.
The other thing is what are you drawing first the image or the background? Just wondering if your drawing order is correct.
If you set your surface to be non-transparent will the image change colour then?
Another thing I noticed which I think is connected with events sychronisation is that sometimes drawing on surface creates a semi-transparent sprite when moving the finger very fast over the screen which initialises drawing.

openGL fading background

I'm trying to create a particular effect where I have a bunch of particles on the screen which leave trails that slowly fade. I was hoping that I could simply use glClear with a small alpha value to do this but that doesn't seem to be working.
My second idea was to draw a black face on the front of the screen but this doesn't seem to be giving me the right effect, the particles are faded but the background doesn't really fade. My next idea is to render to a texture and fade that texture but that's a lot of extra work and I'm not sure if it will solve my problem. Can anyone think of a way to do this? Am I missing something?
Edit Also I'm having trouble finding information about rendering to a texture on android. If anyone has some links to articles that would be great.
Assuming that your 'particles' ar just a bunch of textured sprites, you can simply add color data for each vertex of the sprite using glVertexPointer(). The color you set for the vertices will then be blended with the texture of the sprite. You can easily update these values to achieve a 'fading' effect.
E.g. if you set RGBA = (1,1,1,1) for each vertex, the sprite will appear as before (no translucency), set RGBA = (1,0,0,1) the sprite will appear red (no translucency), set RGBA = (0.5,0.5,0.5,0.5) the sprite will appear half translucent, etc. You will have to set the correct glBlendFunc() beforehand to get the desired behaviour!
Cheers, Aert.

Categories

Resources