0

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?

Community
  • 1
  • 1
SparkyNZ
  • 6,266
  • 7
  • 39
  • 80
  • @genpfault: Why did you edit my post? Are manners not allowed on this site? – SparkyNZ Mar 04 '15 at 02:03
  • 1
    [Helps to boost the SNR a bit](http://meta.stackexchange.com/a/2960/150045). – genpfault Mar 04 '15 at 02:11
  • It's not that "manners aren't allowed", it's just that the manners on this site are to succinctly describe things. (It's considered poor manners to write "hello" and "thanks" on your question, for example, which is why salutations are automatically stripped out.) – Dietrich Epp Mar 04 '15 at 02:12
  • You should still have some sense of manners when asking questions, but questions should be straight questions. It was probably edited because you had written was unnecessary for the meaning of question. Don't worry about it, its not a big deal really. – Znapi Mar 04 '15 at 02:12
  • I don't have a very clear picture of what you want... do you want a blurred rounded rectangle, or do you want one with a radial gradient inside? – Dietrich Epp Mar 04 '15 at 02:17
  • @DietrichEpp: Good question. I just want the alpha value to drop to zero from the edge of the rectangle. Assume that we have a normal rectangle (no rounded corners for example) and a shadow of 10 pixels in width - I would be wanting the alpha value to go from 1.0 to 0.0 in steps of 0.1. – SparkyNZ Mar 04 '15 at 02:20

1 Answers1

1

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.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • Do you mean interatively drawing more multiple rounded rectangles on top of each other as they get closer to the solid rectangle? – SparkyNZ Mar 04 '15 at 02:39
  • No, one piece of geometry. I added a picture. – Dietrich Epp Mar 04 '15 at 02:44
  • OK. I see - I didn't realise vertices could have alpha values as well. Thank you. – SparkyNZ Mar 04 '15 at 02:48
  • You can add scalar and vector data to vertexes with whatever meaning you want, assuming you're not still dealing with the fixed pipeline. If your vertexes already have color attributes, just stuff the alpha in as part of the color. – Dietrich Epp Mar 04 '15 at 02:52
  • Note that this won't look as "smooth" as drop shadows you'll typically see. If you want to make it smoother, you can use a 1D texture to shape the shadow falloff to something other than linear. – Dietrich Epp Mar 04 '15 at 02:54