2015-05-18 63 views
-1

我無法用我的頂點數據填充VAO。我不確定問題是什麼。這是錯誤。OpenGL GL30.glGenVertexArrays()返回null

Exception in thread "main" java.lang.NullPointerException 
at org.lwjgl.opengl.GL30.nglGenVertexArrays(GL30.java:3265) 
at org.lwjgl.opengl.GL30.glGenVertexArrays(GL30.java:3294) 
at javaapplication73.Loader.createVAO(Loader.java:35) 
at javaapplication73.Loader.loadToVao(Loader.java:27) 
at javaapplication73.HelloWorld.initial(HelloWorld.java:139) 
at javaapplication73.HelloWorld.loop(HelloWorld.java:120) 
at javaapplication73.HelloWorld.run(HelloWorld.java:32) 
at javaapplication73.HelloWorld.main(HelloWorld.java:154) 

Java結果:1

這裏是被拋出異常

public class Loader { 

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

public RawModel loadToVao(float[] positions){ 
    int vaoID = this.createVAO(); 
    this.storeDataInAttributeList(0, positions); 
    this.unbindVAO(); 
    return new RawModel(1, positions.length/3); 

} 

private int createVAO(){ 
    int vaoID = GL30.glGenVertexArrays(1, vao); 
    vaos.add(vaoID); 
    GL30.glBindVertexArray(vaoID); 
    return vaoID; 
} 

public void storeDataInAttributeList(int attributeNumber, 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(attributeNumber, 3, GL11.GL_FLOAT, false, 0, 0); 
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); 
} 

public void unbindVAO(){ 
    //unbind the array 
    GL30.glBindVertexArray(0); 
} 


private FloatBuffer storeDataInFloatBuffer(float[] data){ 
    FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length); 
    buffer.put(data); 
    //need to flip between wright and read 
    buffer.flip(); 
    return buffer; 
} 

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

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

這裏裝載機類是主類

package javaapplication73; 
public class HelloWorld { 

// We need to strongly reference callback instances. 
private GLFWErrorCallback errorCallback; 
private GLFWKeyCallback keyCallback; 

// The window handle 
private long window; 
private final int fpsCap = 120; 

Loader loader = new Loader(); 
Renderer renderer = new Renderer(); 
private RawModel model; 

public void run() { 
    System.out.println("Hello LWJGL " + Sys.getVersion() + "!"); 
    try { 
     init(); 
     loop(); 

     // Release window and window callbacks 
     glfwDestroyWindow(window); 
     keyCallback.release(); 
    } finally { 
     // Terminate GLFW and release the GLFWerrorfun 
     glfwTerminate(); 
     errorCallback.release(); 
    } 
} 

private void init() { 
    // Setup an error callback. The default implementation 
    // will print the error message in System.err. 
    glfwSetErrorCallback(errorCallback = errorCallbackPrint(System.err)); 

    // Initialize GLFW. Most GLFW functions will not work before doing this. 
    if (glfwInit() != GL11.GL_TRUE) 
     throw new IllegalStateException("Unable to initialize GLFW"); 

    // Configure our window 
    glfwDefaultWindowHints(); // optional, the current window hints are already the default 
    glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden after creation 
    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // the window will be resizable 

    int WIDTH = 1200; 
    int HEIGHT = 800; 

    // Create the window 
    window = glfwCreateWindow(WIDTH, HEIGHT, "Not so Still Life", NULL, NULL); 
    if (window == NULL) 
     throw new RuntimeException("Failed to create the GLFW window"); 

    // Setup a key callback. It will be called every time a key is pressed, repeated or released. 
    glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() { 
     @Override 
     public void invoke(long window, int key, int scancode, int action, int mods) { 
      if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) 
       glfwSetWindowShouldClose(window, GL_TRUE); // We will detect this in our rendering loop 
      cleanUp(); 
     } 
    }); 

    // Get the resolution of the primary monitor 
    ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); 
    // Center our window 
    glfwSetWindowPos(
     window, 
     (GLFWvidmode.width(vidmode) - WIDTH)/2, 
     (GLFWvidmode.height(vidmode) - HEIGHT)/2 
    ); 

    // Make the OpenGL context current 
    glfwMakeContextCurrent(window); 
    // Enable v-sync 
    glfwSwapInterval(1); 

    // Make the window visible 
    glfwShowWindow(window); 



} 

private void loop() { 
    // This line is critical for LWJGL's interoperation with GLFW's 
    // OpenGL context, or any context that is managed externally. 
    // LWJGL detects the context that is current in the current thread, 
    // creates the ContextCapabilities instance and makes the OpenGL 
    // bindings available for use. 
    GLContext.createFromCurrent(); 

    // Set the clear color 
    glClearColor(.3243f,0,.83251234f,0); 

    // Run the rendering loop until the user has attempted to close 
    // the window or has pressed the ESCAPE key. 
    GL11.glMatrixMode(GL11.GL_PROJECTION); 
GL11.glLoadIdentity(); 
GL11.glOrtho(0, 800, 0, 600, 1, -1); 
GL11.glMatrixMode(GL11.GL_MODELVIEW); 



    while (glfwWindowShouldClose(window) == GL_FALSE) { 
     // Clear the screen and depth buffer 
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); 
     initial(); 
     Render(); 
     glfwSwapBuffers(window); // swap the color buffers 
     glfwPollEvents(); 
    } 
} 


public void initial(){ 
    System.out.println(glGetString(GL_VERSION)); 
    //Pre game stuff 
    float[] verticies = { 
     -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, 
    }; 
    model = loader.loadToVao(verticies); 
} 

public void Render(){ 
    renderer.prepare(); 
    renderer.Render(model); 
} 

public void cleanUp(){ 
    System.out.println("CleanUp"); 
    this.loader.cleanUp(); 
} 

public static void main(String[] args) { 
    System.setProperty("org.lwjgl.librarypath", "/Users/Bayjose/LWJGL/lwjgl/native"); 
    new HelloWorld().run(); 
} 

} 

我運行Mac OSX優勝美地與Java 8,我正在使用LWJGL3作爲這個項目的圖書館。

+0

編輯[MCVE](http://stackoverflow.com/help/mcve)到問題中。現在我們不知道您是否創建了Core上下文。 – genpfault

+0

是否支持GL3.0?你得到什麼'glGetString(GL_VERSION)'? – danielspaniol

+0

不確定,我得到的2.1 INTEL-10.0.86作爲glGetString(GL_VERSION)的輸出。任何方式我可以更新? –

回答

0

刪除參數1和VAO所以

INT vaoID = GL30.glGenVertexArrays();

+0

首先,謝謝,好吧,這給出了完全相同的錯誤。任何意見,我還能做些什麼? –

+0

你可以發佈你的顯示類和你的浮動位置嗎 – 0x2B

+0

好吧,我添加了那個類 –