LWJGL纹理和字符串 [英] LWJGL Textures and Strings

查看:85
本文介绍了LWJGL纹理和字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以加载PNG纹理并在 LWJGL 中绘制字符串而不使用Slick Framework?

Is it possible to load PNG Textures and draw Strings in LWJGL WITHOUT using the Slick Framework?

每次我google 如何在lwjgl中加载png图像我得到这样的答案 - > 嘿只需使用textureloader从光滑的框架

相同的如何在lwjgl中绘制字符串 - > 只需使用光滑框架中的TTFFont类

Everytime I google "how to load png images in lwjgl" I get answers like this -> "hey just use the textureloader from the slick framework".
Same for "how to draw strings in lwjgl" -> "just use the TTFFont Class from the slick framework"

但我不想使用这种中途交叉框架设计。因为我不认为这是最好的方式。

But I don't want to use this half-way-crossframework design. Because I don't think that this is the best way.

是否有 LWJGL 的任何库或扩展名仅用于纹理或字符串?

Are there any Libraries or Extensions for LWJGL that are only made for Textures or Strings?

推荐答案

基本上,你拿一个 BufferedImage ,使用 getRBG()获取每个像素的RGB,获取该数据并将其放入 ByteBuffer (用于输入图像的数据类型)数据到OpenGL),设置一些纹理数据,并创建 GL_TEXTURE_2D

Basically, you take a BufferedImage, use getRBG() to get the RGB of each pixel, take that data and put it into a ByteBuffer (the data type used to input image data to OpenGL), set some texture data, and create the GL_TEXTURE_2D.

这段代码由 Krythic 完成:

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.ByteBuffer;

import javax.imageio.ImageIO;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL12;

import static org.lwjgl.opengl.GL11.*;

public class TextureLoader {
    private static final int BYTES_PER_PIXEL = 4;//3 for RGB, 4 for RGBA
       public static int loadTexture(BufferedImage image){

          int[] pixels = new int[image.getWidth() * image.getHeight()];
            image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());

            ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * BYTES_PER_PIXEL); //4 for RGBA, 3 for RGB

            for(int y = 0; y < image.getHeight(); y++){
                for(int x = 0; x < image.getWidth(); x++){
                    int pixel = pixels[y * image.getWidth() + x];
                    buffer.put((byte) ((pixel >> 16) & 0xFF));     // Red component
                    buffer.put((byte) ((pixel >> 8) & 0xFF));      // Green component
                    buffer.put((byte) (pixel & 0xFF));               // Blue component
                    buffer.put((byte) ((pixel >> 24) & 0xFF));    // Alpha component. Only for RGBA
                }
            }

            buffer.flip(); //FOR THE LOVE OF GOD DO NOT FORGET THIS

            // You now have a ByteBuffer filled with the color data of each pixel.
            // Now just create a texture ID and bind it. Then you can load it using 
            // whatever OpenGL method you want, for example:

          int textureID = glGenTextures(); //Generate texture ID
            glBindTexture(GL_TEXTURE_2D, textureID); //Bind texture ID

            //Setup wrap mode
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

            //Setup texture scaling filtering
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

            //Send texel data to OpenGL
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

            //Return the texture ID so we can bind it later again
          return textureID;
       }

       public static BufferedImage loadImage(String loc)
       {
            try {
               return ImageIO.read(MainClass.class.getResource(loc));
            } catch (IOException e) {
                //Error Handling Here
            }
           return null;
       }
}

要使用此代码,请执行以下操作:

To use this code, do something like this:

BufferedImage image = TextureLoader.loadImage("/res/test.png");//The path is inside the jar file
int textureID = TextureLoader.loadTexture(image);

您可以将textureID保存为 final 变量(如果纹理永远不会改变),或者在每次渲染后使用 GL11.glDeleteTextures(textureID)卸载纹理;

You can either save the textureID as a final variable(if the texture never changes), or unload the texture after each render using GL11.glDeleteTextures(textureID);

要执行文本,只需手动创建 BufferedImage ,然后使用 createGraphics()获取 graphics2D()实例。然后,使用 drawString()绘制到 BufferedImage ,将其加载到 TextureLoader ,在屏幕上渲染它,并使用上面的方法卸载纹理。

To do text, just create a BufferedImage manually, and use createGraphics() to get a graphics2D() instance for the image. Then, use drawString() to draw onto the BufferedImage, load it into the TextureLoader, render it onscreen, and unload the texture using the method above.

这篇关于LWJGL纹理和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆