简体   繁体   English

LWJGL OpenGL使用纹理图集UV坐标绘制文本

[英]LWJGL OpenGL drawing text using texture atlas UV coordinates

I'm trying to draw text in my OpenGL program, using textured quads. 我正在尝试使用纹理四边形在我的OpenGL程序中绘制文本。 My texture's dimension is 256x32 and each character is roughly 6 pixels large. 我的纹理尺寸为256x32,每个字符大约6个像素大。 (I got 42 characters per 'row', and 5 rows.) (每个“行”有42个字符,有5行。)

When generating the UV coordinate for let's say the top-left character, I do this: 当我们为左上角的字符生成UV坐标时,我这样做:

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? 我得到的问题是我的V坐标似乎完全错误,并且V坐标似乎总是在底部,我该如何解决?

also, how do I deal with spaces? 另外,我该如何处理空格?

That looks ok for the u coordinate. 对于u坐标,看起来不错。 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: 但是,由于在v方向上只有5行,因此您需要基于行数计算出的该坐标的不同增量:

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); ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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