Search code examples
openglshadowrectanglesalphablendingimmediate-mode

How draw rounded rectangle with alpha blending in OpenGL immediate mode


I have found some code that will allow me to draw a rounded rectangle in OpenGL immediate mode (here).

What I would like to do it decrease the alpha, the further away from the centre of the rectangle - as would find under a Windows/Mac window or dialog for example where a shadow is drawn.

Can somebody point me to an example on how to do this?


Solution

  • This is relatively easy to do with geometry.

    Create the vertices for two rounded rectangles: an inner one and an outer one. Assign an alpha of 1 to the inner rectangle, and an alpha of 0 to the outer rectangle. Triangulate both the inner rectangle and the space between the two rectangles. Unless you specifically ask otherwise, the alpha will be interpolated smoothly between the inner rectangle and the outer rectangle.

    Something like this:

    geometry for rounded rectangles

    You may have better results using a texture and slicing the rectangle into 9 parts—this may give you better output with simpler geometry and simpler code, depending on your application.