不支持LWJGL功能?

时间:2015-04-17 01:20:25

标签: java image lwjgl

BS:对不起我的英文:p

我正在尝试使用lwjgl在屏幕上呈现一个简单的文本,但每次都失败了!

我的显示管理器中有我的上下文属性:

private static ContextAttribs attribs = new ContextAttribs(3, 2).withForwardCompatible(true).withProfileCore(true);

我的代码可以导入和读取图像(非常简单):

public class FontTexture {

private int width;
private int height;
private int id;

public FontTexture(String fileName)
{
    int[] pixels = null;
    try {
        BufferedImage image = ImageIO.read(new FileInputStream("res/" + fileName + ".png"));
        int x = width = image.getWidth();
        int y = height = image.getHeight();
        pixels = new int[x * y];
        image.setRGB(0, 0, x, y, pixels, 0, 0);
    } catch (IOException e) {
        e.printStackTrace();
    }

    int[] data = new int[width * height];

    for(int i = 0; i < data.length; i++){
        int a = (pixels[i] & 0xff000000) >> 24;
        int r = (pixels[i] & 0xff0000) >> 16;
        int g = (pixels[i] & 0xff00) >> 8;
        int b = (pixels[i] & 0xff);

        data[i] = a << 24 | b << 16 | g << 8 | r;
    }

    int id = GL11.glGenTextures();
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);

    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);

    IntBuffer buffer = BufferUtils.createIntBuffer(data.length);
    buffer.put(data);
    buffer.flip();

    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);

    this.id = id;
}

}

加载后,读取图像。我会用这些方法渲染它:

public void renderString(String text, int x, int y)
{
    font.bind();
    GL11.glBegin(GL11.GL_QUADS);
    for(int i = 0; i < text.length(); i++){
        charData(text.charAt(i), x + i * 8, y, 20);
    }
    GL11.glEnd();
    font.unbind();
}

private void charData(char character, int x, int y, int size)
{
    int i = chars.indexOf(character);
    int xo = i % 16;
    int yo = 1 / 16;

    GL11.glTexCoord2f((0 + xo) / 16.0f, (0 + yo) / 4);
    GL11.glVertex2f(x, y);
    GL11.glTexCoord2f((1 + xo) / 16.0f, (0 + yo) / 4);
    GL11.glVertex2f(x + size, y);
    GL11.glTexCoord2f((1 + xo) / 16.0f, (1 + yo) / 4);
    GL11.glVertex2f(x + size, y + size);
    GL11.glTexCoord2f((0 + xo) / 16.0f, (1 + yo) / 4);
    GL11.glVertex2f(x, y + size);
}

我会将我的renderString功能放在我的主游戏循环中,但是当我启动游戏时,它会崩溃并抛出我的错误:

Exception in thread "main" java.lang.IllegalStateException: Function is not supported
at org.lwjgl.BufferChecks.checkFunctionAddress(BufferChecks.java:58)
at org.lwjgl.opengl.GL11.glBegin(GL11.java:682)
at com.zarax.gui.GuiRenderer.renderString(GuiRenderer.java:63)
at com.zarax.main.Main.main(Main.java:106)

我需要使用现代的openGL东西还是什么?我真的需要帮助x)

2 个答案:

答案 0 :(得分:0)

您需要致电Display.create()

Display.setDisplayMode(new DisplayMode(1080, 720));
Display.setTitle("Game");
Display.create(attribs);

答案 1 :(得分:0)

我看到glTexCoord2f和glVertex2f不再是lwjgl的核心,我必须找到另一种方法:x