I would like to render game UI, and for that I need rectangles. However, since a button could be textured just as well as solid color or (multistop)gradient a problem arises. I would also like all types of rectangles(buttons) somehow at least partially uniform at handling.
I have been puzzled by this issue for quite a time and idea came across my mind - what if I stored colors to a 1D texture array and treat all rectangles as textured. Could you give me some example how to do this? Or event better a reason not-to solve it this way and point to better solution?

  • \$\begingroup\$ Why not just use a vertex attribute? \$\endgroup\$
    – API-Beast
    Aug 10 '14 at 11:47
  • \$\begingroup\$ Vertex attribute would require unnecessary data(each vertex would have to get both color and UV) and either additional math or condition jump. Remember - rectangle can have either texture or color. \$\endgroup\$
    – wondra
    Aug 10 '14 at 11:56
  • 1
    \$\begingroup\$ You don't need condition jump, a invalid texture (texture index 0) is always 100% white. This seems to be a very premature optimization to me. \$\endgroup\$
    – API-Beast
    Aug 10 '14 at 12:14
  • \$\begingroup\$ +1, I didnt know that about 0 texture. However, this is not much about optimization but the goal was to have a rectangle that can be processed exactly same way regardless background type as background is irrelevant for other (GUI) logic. \$\endgroup\$
    – wondra
    Aug 10 '14 at 12:28
  • \$\begingroup\$ @API-Beast i'm afraid that's not always true: on my gpu/implementation/whatever, a 0 texture acts as if it's filled with zeroes (which would be black and 100% transparent as RGBA). this also makes for interesting effects if used with compositing window managers (I can actually render holes into the window) \$\endgroup\$
    – nonchip
    Sep 13 '14 at 5:55

You could do this:

  • Create a texture holding all the colors you want.

  • For the actual button geometry, set the texture coordinates to the appropriate region of the color texture (they can all be the same value, in fact).

  • \$\begingroup\$ And what if I dont know in advance how many colors(and which) do I need? Is better to use 1d array, rectangle texture or just classic 2d texture? \$\endgroup\$
    – wondra
    Aug 10 '14 at 11:18
  • \$\begingroup\$ Doesn't matter, really; in all cases you'd need to add the new texels to the texture. \$\endgroup\$
    – user1430
    Aug 10 '14 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.