17

LWJGLSlick フレームワークを使用せずに、PNG テクスチャを読み込んで文字列を描画することは可能ですか?

「lwjglでpng画像をロードする方法」をGoogleで検索するたびに、次のような回答が得られます-> 「スリックフレームワークのテクスチャローダーを使用してください」 . 「lwjgl で文字列を描画する方法」についても
同じ-> 「洗練されたフレームワークの TTFFont クラスを使用するだけ」

しかし、私はこの中途半端なクロスフレームワークのデザインを使いたくありません。これが最善の方法だとは思わないからです。

LWJGLテクスチャまたは文字列専用に作成されたライブラリまたは拡張機能はありますか?

4

2 に答える 2

27

基本的に、を取得し、 getRGB()BufferedImageを使用して各ピクセルの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;
       }
}

このコードを使用するには、次のようにします。

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

textureIDを変数として保存するかfinal(テクスチャが変更されない場合)、またはを使用してレンダリングするたびにテクスチャをアンロードできます。GL11.glDeleteTextures(textureID);

テキストを作成するには、BufferedImage手動で作成し、 createGraphics()を使用して画像のgraphics2D()インスタンスを取得します。次に、を使用drawString()して描画し、BufferedImageにロードしTextureLoaderて画面上にレンダリングし、上記の方法を使用してテクスチャをアンロードします。

于 2012-06-03T16:40:08.960 に答える