LWJGL纹理和字符串

Mic*_*ura 15 java image lwjgl

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

每次我google "如何在lwjgl中加载png图像"我得到这样的答案 - > "嘿,只需使用光滑框架中的textureloader ".
同为"如何绘制在LWJGL串" - > "只使用TTFFont类从光滑的框架"

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

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

小智 23

基本上,你拿a BufferedImage,使用getRGB()来获取每个像素的RGB,获取该数据并将其放入ByteBuffer(用于将图像数据输入到OpenGL的数据类型),设置一些纹理数据,并创建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;
       }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

要做文本,只需BufferedImage手动创建,然后使用createGraphics()获取图像的graphics2D()实例.然后,使用drawString()绘制,将其BufferedImage加载到TextureLoader屏幕上,然后使用上面的方法卸载纹理.

  • 上面的代码是我的,我不久前写的.想象一下,我很惊讶在Stack Overflow上随机找到它!让我感到温暖和模糊,知道它一直在互联网上走动!= P (5认同)
  • 有趣的是,我没有添加"为了上帝的爱,不要忘记这个"到原始片段.我想我说的是:"确保你不要忘记这个"或类似的东西.自从我成功以来,它显然经历了几手牌. (2认同)