Java中的OpenGL出错

时间:2017-08-26 20:19:26

标签: java opengl-es lwjgl

我开始使用Java中的lwjgl(2.9.1)进行3D游戏。当我创建Display时,一切都是正确的,但是当我添加了Loader,Renderer和RawModel类时,java会给我这个错误:

Exception in thread "main" java.lang.IllegalStateException: Function is not supported
    at org.lwjgl.BufferChecks.checkFunctionAddress(BufferChecks.java:58)
    at org.lwjgl.opengl.GL30.glGenVertexArrays(GL30.java:1550)
    at com.render.Loader.createVAO(Loader.java:39)
    at com.render.Loader.loadVAO(Loader.java:20)
    at com.Tester.MainLoop.main(MainLoop.java:29)

有人可以帮助我吗?

这是我的Java代码:

渲染器类:

 public class Renderer{

    public void prepare(){
        GL11.glClearColor(1, 0, 0, 1);
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    }

    public void render(RawModel model){
        GL30.glBindVertexArray(model.getVaoID());
        GL20.glEnableVertexAttribArray(0);
        GL11.glDrawArrays(GL11.GL_TRIANGLES,0,model.getVertexCount());
        GL20.glDisableVertexAttribArray(0);
        GL30.glBindVertexArray(0);
    }

}

Loader class:

    public class Loader {

    private List<Integer> vaos = new ArrayList<Integer>();
    private List<Integer> vbos = new ArrayList<Integer>();

    public RawModel loadVAO(float[] pos){

        int vaoID = createVAO();
        storeDataInAttributeList(0,pos);
        unbindVAO();
        return new RawModel(vaoID, pos.length/3);

    }

    public void cleanUp(){
        for(int vao:vaos){
            GL30.glDeleteVertexArrays(vao);
        }

        for(int vbo:vbos){
            GL15.glDeleteBuffers(vbo);
        }
    }

    private int createVAO(){

        int vaoID = GL30.glGenVertexArrays();
        vaos.add(vaoID);
        GL30.glBindVertexArray(vaoID);
        return vaoID;

    }

    private void storeDataInAttributeList(int attrNum, float[] data){
        int vboID = GL15.glGenBuffers();
        vbos.add(vboID);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
        FloatBuffer buffer = storeDataInFloatBuffer(data);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
        GL20.glVertexAttribPointer(attrNum, 3, GL11.GL_FLOAT, false, 0, 0);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    }

    private void unbindVAO(){
        GL30.glBindVertexArray(0);
    }

    private FloatBuffer storeDataInFloatBuffer(float[] data){
        FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
        buffer.put(data);
        buffer.flip();
        return buffer;
    }



}

RawModel类:

    public class RawModel {

    private int vaoID;
    private int vertexCount;

    public RawModel(int id, int num){

        this.vaoID = id;
        this.vertexCount = num;

    }

    public int getVaoID(){
        return this.vaoID;
    }

    public int getVertexCount(){
        return this.vertexCount;
    }

}

DisplayManager类:

    public class DisplayManager{

    private static final int width = 1280;
    private static final int height = 1280;
    private static final int fps = 120;

    public static void createDisplay(){

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

        try{
            System.setProperty("org.lwjgl.opengl.Display.allowSoftwareOpenGL", "true"); 
            Display.setDisplayMode(new DisplayMode(width,height));
            Display.create(new PixelFormat(), attribs);
            Display.setTitle("OpenGL 3D Game");
        }catch(LWJGLException e){
            e.printStackTrace();
        }

        GL11.glViewport(0, 0, width, height);
    }

    public static void updateDisplay(){

        Display.sync(fps);
        Display.update();

    }

    public static void closeDisplay(){

        Display.destroy();

    }

}

MainLoop类:

    public class MainLoop{

    public static void main(String[] args){

        DisplayManager.createDisplay();

        Loader loader = new Loader();
        Renderer renderer = new Renderer();

        float[] vert = {
                -0.5f,0.5f,0f,
                -0.5f,-0.5f,0f,
                0.5f,-0.5f,0f,

                0.5f,-0.5f,0f,
                0.5f,0.5f,0f,
                -0.5f,0.5f,0f
        };

        RawModel model = loader.loadVAO(vert);

        while(!Display.isCloseRequested()){

            renderer.prepare();

            renderer.render(model);

            DisplayManager.updateDisplay();

        }

        loader.cleanUp();
        DisplayManager.closeDisplay();

    }

}

0 个答案:

没有答案