LWJGL: Получаване на текстура от .png с множество текстури

Правя игра с LWJGL и като използвам openGL, смятам, че най-добрият ми вариант е да използвам текстури и да ги рендерирам с четириъгълници. Изглежда обаче мога да намеря информация само за зареждане на текстура от изображение, където цялото изображение е само ЕДНА текстура. Това, което бих искал да направя, е да прочета цял спрайт лист и да мога да го разделя на различни текстури. Има ли малко прост начин да направите това?


person CosmicKidd    schedule 21.04.2015    source източник


Отговори (1)


Можете да заредите изображението от напр. .png файл към BufferedImage с

public static BufferedImage loadImage(String location)
{
    try {
        BufferedImage image = ImageIO.read(new File(location));
        return image;
    } catch (IOException e) {
        System.out.println("Could not load texture: " + location);
    }
    return null;
}

Сега можете да извикате getSubimage(int x, int y, int w, int h) на полученото BufferedImage, което ви дава отделената част. Сега просто трябва да създадете текстура на BufferedImage. Този код трябва да свърши работата:

public static int loadTexture(BufferedImage image){
    if (image == null) {
        return 0;
    }

    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();
    glBindTexture(GL_TEXTURE_2D, textureID);

    //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); //GL_RGBA8 was GL_RGB8A

    return textureID;
}

Вече можете да обвържете върнатия textureID с glBindTexture(GL_TEXTURE_2D, textureID);, ако имате нужда от текстурата. По този начин трябва само да разделите BufferedImage на желаните части.

Препоръчвам да прочетете това: LWJGL текстури и низове

person Tanix    schedule 06.05.2015