Question

I have a health bar which is two GL_QUADS rendered at the same z coordinate, one red and one green(the green being the section of health remaining.) However, I just realize now that only 1 GL_QUAD will be visible if both are rendered in the same position. Is there a way to change the color of a section of a single GL_QUAD?

Was it helpful?

Solution

  1. There is a way to change a single GL_QUAD's coloring, although you don't want to do that. It would involve sliding a texture map across it by varying its texture coordinates. Not very smart.

  2. What you want instead are two quads whose dimensions depend on the health. You will want to use linear interpolation (LERP) to find the edge where one quad end and one quad begins.

Here's the math pseudocode, assuming your health bar is horizontal

Quad_1.x start = 0
Quad_1.x end = X%
Quad_2.x start = X%
Quad_2.x end = 1

Quad_1 ends up having X% of the area, and Quad_2 ends up having (1-X)% of the area. They both will always take up 100% of the area, and you can easily scale this to any size. Just make sure you don't have negative health or more than 100% health messing up your drawing.

Note: GL_QUADS is old and deprecated. Use GL_TRIANGLES, or GL_TRIANGLE_STRIP if you're into efficiency (wink wink).

Alternate methods:

  • You can generate a fancy procedural 2D image and use it as a texture map, which you can then use to texture a single quad. You can start by having your texture map be just a simple sliding bar based on the above explanation, but from there you can make it fancy
  • You could, as Xonar and Armin Rigo have pointed out, mess with the Z value. Although if you do this as an ad-hoc fix, it may end up being more trouble down the line.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top