【LWJGL2 WIKI】【辅助库篇】Slick-Util库:第一部分-读取图片

原文:http://wiki.lwjgl.org/wiki/Slick-Util_Library_-Part_1-_Loading_Images_for_LWJGL

Slick-Util支持png, jpg, gif, tga格式图片,它可以读取它们用作OpenGL纹理。
用它读图片很简单,图片的细节会被存在Slick-Util的Texture类里。Texture类为你提供一些属性,比如宽和高之类的。用TextureLoader类和getTexuture()方法读取图片到Texture类中,需要先指定图片类型(”PNG”,”TGA”等),然后指定图片路径。下面是个例子:

Texture texture;

public void init() throws IOException {

  texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/image.png"));
}

读完后,用Texture.bind()绑定纹理。
将一个纹理绑定到OpenGL方形的完整代码如下:

import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class TextureExample {

    /** The texture that will hold the image details */
    private Texture texture;


    /**
     * Start the example
     */
    public void start() {
        initGL(800,600);
        init();

        while (true) {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
            render();

            Display.update();
            Display.sync(100);

            if (Display.isCloseRequested()) {
                Display.destroy();
                System.exit(0);
            }
        }
    }

    /**
     * Initialise the GL display
     *
     * @param width The width of the display
     * @param height The height of the display
     */
    private void initGL(int width, int height) {
        try {
            Display.setDisplayMode(new DisplayMode(width,height));
            Display.create();
            Display.setVSyncEnabled(true);
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }

        GL11.glEnable(GL11.GL_TEXTURE_2D);              

        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);         

            // enable alpha blending
            GL11.glEnable(GL11.GL_BLEND);
            GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

            GL11.glViewport(0,0,width,height);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, width, height, 0, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
    }

    /**
     * Initialise resources
     */
    public void init() {

        try {
            // load texture from PNG file
            texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/image.png"));

            System.out.println("Texture loaded: "+texture);
            System.out.println(">> Image width: "+texture.getImageWidth());
            System.out.println(">> Image height: "+texture.getImageHeight());
            System.out.println(">> Texture width: "+texture.getTextureWidth());
            System.out.println(">> Texture height: "+texture.getTextureHeight());
            System.out.println(">> Texture ID: "+texture.getTextureID());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * draw a quad with the image on it
     */
    public void render() {
        Color.white.bind();
        texture.bind(); // or GL11.glBind(texture.getTextureID());

        GL11.glBegin(GL11.GL_QUADS);
            GL11.glTexCoord2f(0,0);
            GL11.glVertex2f(100,100);
            GL11.glTexCoord2f(1,0);
            GL11.glVertex2f(100+texture.getTextureWidth(),100);
            GL11.glTexCoord2f(1,1);
            GL11.glVertex2f(100+texture.getTextureWidth(),100+texture.getTextureHeight());
            GL11.glTexCoord2f(0,1);
            GL11.glVertex2f(100,100+texture.getTextureHeight());
        GL11.glEnd();
    }

    /**
     * Main Class
     */
    public static void main(String[] argv) {
        TextureExample textureExample = new TextureExample();
        textureExample.start();
    }
}

Credit

Tutorial Credit - Ninja Cave

Kevin Glass for writing the Slick Library and initial example code.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值