Cocoa OpengGL: draw texture and round corners - cocoa

If I use a standard NSWindow to host a NSOpenGLView which extends for all the window frame, the two bottom corners of the window are automatically rounded.
When swithcing to NSBorderlessWindowMask I have to handle corner rounding myself.
I have already implemented a transparent custom NSWindow and a rounded custom NSView and they both work fine.
After that I have implemented a transparent NSOpenGLContext by setting NSOpenGLCPSurfaceOpacity to 0.
If I set a color background for the OpenGL context, the view is drawn correctly and I obtain the desired rounded corner.
But, since the app is a movie player, I need to draw the texture corresponding to every movie frame.
When I do this (using glTexCoord2f and glVertex2f) the texture is drawn till corners and therefore the image is drawn till outside of the rounded corners and I loose the rounded aspect of my window.
What does the system do when the window is standard and non NSBorderlessWindowMask that I can't seem to be able to reproduce?
What is the best way to round the corner of the texture while drawing it to the frame buffer?

You could apply the texture to a geometry with rounded corners, use an additional alpha mask on the movie texture with rounded corners, or use the stencil test to round off your viewport's corners.

Related

Draw texture to FrameBufferObject using specific color

I have a texture and I want to draw it to a FrameBufferObject. The texture has transparent areas. I would like the following:
For all transparent pixels, let them be drawn transparent in the FrameBufferObject.
For all non-transparent pixels, let them be drawn using a color of my choosing, ie pure red (ignore their actual rgb value)
I've tried Batch.setColor(red) before drawing the texture, but that just tints it - I want all non-transparent pixels to be drawn pure red.
I am also trying to figure out how to achieve this in just opengl directly, looks like there may be a way to do this with blending, which can then be related back to gdx.
Thanks
You need to write a shader and the shader is the OpenGL program that renders the texture. So your shader would render transparency without change and all else would the color of your own choosing.
The following link has libgdx shaders for palette swapping i.e. direct reassignment of color at render, which you can easily adapt for single color and transparency.
https://www.javaer101.com/en/article/12241616.html

How can I have a png image with transparency, visible only from *front*?

I'm using a prefab for a box shape, which has a front and back plane.
My images are PNG and have transparent areas around the edge. I dragged the image onto my front plane, which now has a drop-down box for "Shader".
First I chose Shader: "Standard" but the transparent areas of my PNG image weren't transparent, so in order to fix that I changed it to "Sprites / Diffuse"... now the image looks fine (from the front).
However, when I rotate the shape, the image is also visible from the back. I want a way to not see the image / texture from the back.
How can I make the images only visible from the front side of a plane, whilst also preserving the transparency areas of the image / texture?
If you are using the standard built-in shader, you need to set the rendering mode to transparent in order for the texture's alpha channel to be transparent. The sprite shader, by default, forces the rendering of otherwise invisible back-faces, whereas the standard shader does not.

Custom Progress Indicator - Animate subviews color overlay left to right

I have a container UIView that contains many columns/subviews.
I want to create a color fill animation on the container UIView, left to right as demonstrated on the image below, so it gradually changes the colors of only the subviews.
How to achieve this?
Im guessing i would have to create a mask and move it over this container UIView?
But then how to achieve that the mask only applies to the subviews?
EDIT:
this is a progress indicator for a playing audio file, so it needs to indicate the current time position precisely.
Also i must be able to have the shapes in different sizes as they represent audio waves
I would do it the opposite to the way you describe it. I would draw your audio wave shapes as a mask, thus revealing whatever is behind them. It is then trivial to position / animate a colored rectangle rightwards behind the mask to indicate the progress through the music:
In other words, the color is not an overlay but an underlay.

Blending two sprites in OpenGL ES without affecting background

Basically what I want to achieve is a sprite highlight animation effect as displayed below.
The idea is that the white-translucent gradient sprite moves on top of the other sprite (left-to-right), using a blend mode like Overlay (Photoshop). The difficult part is that the top gradient sprite should only be drawn on the visible pixels of the sprite underneath. The other part of the gradient overlay should be discarded to not affect the background or other sprites underneath (like on the image to the far right).
Is it possible to achieve that effect with a clever combination of OpenGL blend modes and how, or would I have to create a custom shader to combine these sprites?
Background: I'm using libgdx with OpenGL ES 2.0 and the app runs on Desktop, Android and iOS.
There arÄ™ many ways to do it. The simplest one:
You should render button and hilight in a single pass. In fragment shader, after sampling button texture and hilight texture calc the output color as for blending (could be mix(c1,c2,c2.a)) and alpha as button texture alpha only. Of course enable blending in usual way: (srcalpha,1-srcalpha)

Does a given rect intersect a rotated view?

I have an NSView that is rotated using -setFrameRotation. (This is necessary because the view responds to mouse events, and you don't get correct behavior if you merely draw the view with a rotated NSAffineTransform.)
Given a rect in the rotated view's superview coordinates, how can I determine what portion of the rect intersects the rotated view's frame?
Use case: the Cocoa text system proposes to draw text in a line fragment rectangle. I need to determine how much of that rectangle may be filled with text without overlapping the rotated view.
Take the point, use an affine transform to rotate into the coordinate system of the bounds of the rect and then rect-test that.

Resources