Reputation: 1889
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?
Upvotes: 2
Views: 289
Reputation: 4641
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.
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:
Upvotes: 1