2012-06-07 61 views
0

只是試圖讓一個三角形繪製到屏幕上,遵循C++教程。試圖運行該程序,並在所有Opengl調用中收到NullPointerException。另外,我正在關注opengl 3的教程,儘管我的大部分調用都是針對早期版本的,但是這只是如何設置lwjgl,並且函數駐留在源於它們的版本中?NullPointerException在所有的OpenGL調用LWJGL

package examples; 

import org.lwjgl.LWJGLException; 
import org.lwjgl.opengl.*; 

import java.nio.*; 

public class Triangle 
{ 
// An array of 3 vectors which represents 3 vertices 
static final float vertexData[] = { 
    -1.0f, -1.0f, 0.0f, 
    1.0f, -1.0f, 0.0f, 
    0.0f, 1.0f, 0.0f, 
}; 

// This will identify our vertex buffer 
int vertexBufferID; 

public static void main(String[] args) 
{ 
    new Triangle(); 
} 

public Triangle() 
{ 
    // Allocate floatBuffer to hold vertex data 
    FloatBuffer vertexBuffer = FloatBuffer.allocate(9); 
    // Put float data into buffer and position ready to read 
    vertexBuffer.put(vertexData).position(0); 

    // Generate 1 buffer, put the resulting identifier in vertexbuffer 
    IntBuffer buffers = IntBuffer.allocate(1); // allocate 
    GL15.glGenBuffers(buffers); 
    vertexBufferID = buffers.get(0); 

    // Binds a buffer to the ARRAY_BUFFER(target) (1 at a time) (breaks other bonds) 
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBufferID); 

    // Give our vertices to OpenGL. (creates store for data bound to target(above)) 
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexBuffer,GL15.GL_STATIC_DRAW); 

    try { 
     Display.setDisplayMode(new DisplayMode(800,600)); 
     Display.create(); 
    } catch (LWJGLException e) { 
     e.printStackTrace(); 
     System.exit(0); 
    } 

    while(!Display.isCloseRequested()) 
    { 
     // Render 
     // 1st attribute buffer : vertices 
     GL20.glEnableVertexAttribArray(0); // enable vertex attribute index: 0 
     GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBufferID); 

     // Specify location of vertex data for index 0 
     GL33.glVertexAttribP1ui(0, GL11.GL_FLOAT, false, 0); 

     // Draw the triangle! 
     GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle 

     GL20.glDisableVertexAttribArray(0); 
    } 

} 
} 

回答

1

我認爲問題是你必須在任何openGl調用之前創建Display。 嘗試在三角形構造的頂部移動

try { 
     Display.setDisplayMode(new DisplayMode(800,600)); 
     Display.create(); 
    } catch (LWJGLException e) { 
     e.printStackTrace(); 
     System.exit(0); 
    } 

+0

啊啊謝謝。這擺脫了空例外。 –

+0

@KierenAnderson如果你對opengl 3.3感興趣,我建議你閱讀http://www.arcsynthesis.org/gltut/index.html;這些教程是用C++編寫的,如果您對LWJGL代碼感興趣,請查看我的個人資料以獲得鏈接:) – 2012-06-07 13:10:56