我正在尝试将所有字符从AWT字体复制到OpenGL数组中供LWJGL使用。目前我的代码不起作用(空白纹理)。我尝试的是将每个字符串绘制成BufferedImage
以及透明背景,然后将其逐像素复制到ByteBuffer
以获取OpenGL图像。我的问题是,复制这些字符有什么更好的方法? BufferedImage
似乎不起作用。我可以使用另一种帆布类型吗?
到目前为止,这是布局,但它不起作用,我需要一种新方法来绘制纹理:
for (int i = 0; i <= 255; i++)
{
String letter = Character.toString((char)i);
BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setColor(new Color(255, 255, 255, 0));
g.drawRect(0, 0, size, size);
g.setColor(new Color(255, 255, 255));
g.setFont(font);
g.drawString(letter, 0, 0);
int id = i;
glTextures[id] = glGenTextures();
glBindTexture(GL_TEXTURE_2D, glTextures[id]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
ByteBuffer buf = ByteBuffer.allocateDirect(4 * 4 * size * size);
for (int x = 1; x <= size; x++)
{
for (int y = 1; y <= size; y++)
{
Color color = new Color(image.getRGB(x - 1, y - 1));
buf.putFloat((float)(1 / 255 * color.getRed()));
buf.putFloat((float)(1 / 255 * color.getGreen()));
buf.putFloat((float)(1 / 255 * color.getBlue()));
buf.putFloat((float)(1 / 255 * color.getAlpha()));
}
}
buf.flip();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_FLOAT, buf);
}