How to draw transparent image on top of Opaque surface - opengl

I am drawing text on top of a rectangle.
Rectangle z position is 0
Text z Position is 1
If we render opaque rectagle first and than draw text the rendering comes fine.
if we reverse the order we first draw text and than the geometry than transparent areas of the text cut through the rectangle which is drawn behind.
How can we draw in this order.

Related

How to understand Sprite coordinates in AddSprite function?

I have a bitmap with a sprite on it. Say the sprite with coordinates (0,0)-(5,4) in the bitmap coordinate space (source rectangle). I put it onto the destination rectangle with the same coordinates (0,0)-(5,4) in the render target coordinate space (ID2D1SpriteBatch::AddSprites function). But it draws the sprite with the cut right and bottom edged by one pixel.
If I use a source rectangle with (0,0)-(6,5) coordinates (that's plus one pixel), it solves the problem and Direct2D draws the sprite as needed. OK, but I do not understand why I have to use "one pixel plus" technic to draw "the uncut" sprite? What's wrong with sprite coordinates?
The D2D_RECT_F structure that you are passing to ID2D1SpriteBatch::AddSprites as the destinationRectangles is documented as:
Represents a rectangle defined by the coordinates of the upper-left corner (left, top) and the coordinates of the lower-right corner (right, bottom).
Note that you are specifying the corners on the destination device context, not the inclusive starting/ending pixel rows/columns. Therefore, if you draw from (0,0) to (0,0), you would be asking to draw a 0-sized rectangle, rather than a 1x1 rectangle.

c++ opengGL: Draw polygon + image processing interior pixels

I am using opengl and c++ doing image processing. The idea is simple, I will load an image, draw a polygon by clicking and then apply an effect (desaturation for instance) only to the pixels in the interior of the polygon shape just created.
Can anyone give me any direction on how to limit the effect to the pixels within the interior of the polygon? Loading the image and drawing the polygon is not a problem
Supposing the following situation :
The picture on which you want to apply the effect takes the whole screen
The picture is rendered using opengl , probably through a simple shader, with the picture passed as a texture
You can do the following approach :
consider the screen as being a big texture
you draw a polygon, which will be rendered on top of the rendered texture
inside the polygon's vertices insert the uv's coresponding to the 2D coordinates on the screen (so from screen space to uv space (0 , 1 ) )
draw the picture normaly
on top of the picture draw your polygon using the same picture as texture, but with a different shader
So instead of trying to desaturate a specific region from your picture, create a polygon on top of that region with the same picture, and desaturate that new polygon.
This would help you in avoiding the stencil buffer.
Another approach would be to create the polygon, but draw it only on the stencil buffer, before the picture is drawn.

"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.

Plotting a graph over a QPixmap using QPainter

I need to plot a real time graph keeping the background an image. Let's say I am plotting the graph with a red color.
But the logic I use for plotting is drawing small lines using painter.drawline, where painter is a pixmap painter. Then I am constantly drawing this pixmap using a painter to parent widget on a timer timeout() event. So it appears the updated Image (which is the graph) gets plotted in the Gui.
The pixmap is black in background and plotting pen color is red, So when it reaches the end of screen width, I have to start drawing the graph from position 0, but from the second cycle onwards I have to fill the previously drawn pixels (not the entire pixmap, just a rect of width few pixels and height, height of pixmap) with black color.
But now I need to keep a background image, and draw a graph over it. So the fillling with black color doesn't do, as I have to keep a background an image. So how can I plot a graph over an image like aforementioned?
Is there any way to blend two images such that the pixels only having a particular color (red) in source image is blended/replaced with corresponding pixels in destination image?
Keep previously drawed Ys in memory than fill exactly the same pixels by corresponding background. It's faster and much more reliable.

libgdx texture's alpha value changed

so I have a problem with the alpha value in my texture. Basically the pixmap is set all black then I drew a filled circle with the alpha value set to 0 in order for the area of the circle it to be transparent (yes I set blending to none), then I created the texture out of the pixmap like this:
Texture myTexture = new Pixmap(TransparentCirclePixmap); // this constructor takes a pixmap
if I draw this texture the circle is not transparent it has a color that is not the background in my game.
so basically what i want is to draw a transparent circle in my pixmap and create a texture from that pixmap and when I draw the texture to the screen the interior of the circle displays whatever was in the background of my game.