简体   繁体   中英

LWJGL OpenGL drawing text using texture atlas UV coordinates

I'm trying to draw text in my OpenGL program, using textured quads. My texture's dimension is 256x32 and each character is roughly 6 pixels large. (I got 42 characters per 'row', and 5 rows.)

When generating the UV coordinate for let's say the top-left character, I do this:

float textureDelta = 1.0f / 42.0f //0.0238
float u = 0;
float v = 0;

glBegin(GL_QUADS);
glTexCoord2f(u,1-v); glVertex2f(0,0);
glTexCoord2f(u+textureDelta, 1-v); glVertex2f(16,0);
glTexCoord2f(u+textureDela, 1-v + textureDelta); glVertex2f(16,16);
glTexCoord2f(u, 1-v+textureDelta); glVertex2f(0,16);
glEnd();

The problem I get is that my V coordinates seems entierly wrong, and the v coordinate seems to always be at the bottom, how do I fix this?

also, how do I deal with spaces?

That looks ok for the u coordinate. But since you have only 5 rows in the v direction, you need a different delta for that coordinate that is calculated based on the number of rows:

float uDelta = 1.0f / 42.0f;
float vDelta = 1.0f / 5.0f;

glTexCoord2f(u, 1.0f - v); ...
glTexCoord2f(u + uDelta, 1.0f - v); ...
glTexCoord2f(u + uDelta, 1.0f - v + vDelta); ...
glTexCoord2f(u, 1.0f - v + vDelta); ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM