2015-01-09 26 views
0

我最近決定重新使用基於LWJGL wiki上的教程編寫的使用索引VBO編寫的一些LWJGL渲染代碼。在我的臺式電腦上,一切都按預期工作,但是當我切換到我的學校筆記本電腦時,它拒絕呈現任何東西。LWJGL - VBO在一個系統上渲染但不是其他

我有以下呈現的代碼:

GL20.glUseProgram(PID); System.out.println(「NO Error?」+(GL11.glGetError()== GL11.GL_NO_ERROR));

// Bind to the VAO that has all the information about the vertices and colors 
    GL30.glBindVertexArray(vaoId); 
    GL20.glEnableVertexAttribArray(0); 
    GL20.glEnableVertexAttribArray(1); 
    System.out.println("NO Error? " + (GL11.glGetError() == GL11.GL_NO_ERROR)); 

    // Bind to the index VBO that has all the information about the order of the vertices 
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId); 

    // Draw the vertices 
    GL11.glDrawElements(GL11.GL_TRIANGLES, indicesCount, GL11.GL_UNSIGNED_INT, 0); 

    System.out.println("NO Error? " + (GL11.glGetError() == GL11.GL_NO_ERROR)); 
    // Put everything back to default (deselect) 
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); 
    GL20.glDisableVertexAttribArray(0); 
    GL20.glDisableVertexAttribArray(1); 
    GL30.glBindVertexArray(0); 
    GL20.glUseProgram(0); 
    System.out.println("NO Error? " + (GL11.glGetError() == GL11.GL_NO_ERROR)); 

這裏是我的頂點/片段着色

#version 130 
uniform mat4 MVP; 
in vec4 in_Position; 
in vec4 in_Color; 
out vec4 pass_Color; 
void main(void) { 
gl_Position = MVP * in_Position; 
pass_Color = in_Color; 
} 
#version 130 
in vec4 pass_Color; 
out vec4 out_Color; 
void main(void) { 
out_Color = pass_Color; 
} 

,這是我的VBO初始化函數

public void initialize(float[] vertices, float[] colors, int[] indices) { 
    shaderSetup(); 
    //create the buffers to hold vertex color and index data 
    FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertices.length); 
    verticesBuffer.put(vertices); 
    verticesBuffer.flip(); 
    System.out.println("NO Error? " + (GL11.glGetError() == GL11.GL_NO_ERROR)); 

    FloatBuffer colorsBuffer = BufferUtils.createFloatBuffer(colors.length); 
    colorsBuffer.put(colors); 
    colorsBuffer.flip(); 
    // OpenGL expects vertices in counter clockwise order by default 
    indicesCount = indices.length; 
    IntBuffer indicesBuffer = BufferUtils.createIntBuffer(indicesCount); 
    indicesBuffer.put(indices); 
    indicesBuffer.flip(); 
    System.out.println("NO Error? " + (GL11.glGetError() == GL11.GL_NO_ERROR)); 

    // Create a new Vertex Array Object in memory and select it (bind) 
    vaoId = GL30.glGenVertexArrays(); 
    GL30.glBindVertexArray(vaoId); 
    System.out.println("NO Error? " + (GL11.glGetError() == GL11.GL_NO_ERROR)); 

    // Create a new Vertex Buffer Object in memory and select it (bind) - VERTICES 
    vboId = GL15.glGenBuffers(); 
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId); 
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesBuffer, GL15.GL_STATIC_DRAW); 
    GL20.glVertexAttribPointer(0, 4, GL11.GL_FLOAT, false, 0, 0); 
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); 
    System.out.println("NO Error? " + (GL11.glGetError() == GL11.GL_NO_ERROR)); 

    // Create a new Vertex Buffer Object in memory and select it (bind) - COLORS 
    vbocId = GL15.glGenBuffers(); 
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbocId); 
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, colorsBuffer, GL15.GL_STATIC_DRAW); 
    GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, 0, 0); 
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); 
    System.out.println("NO Error? " + (GL11.glGetError() == GL11.GL_NO_ERROR)); 

    // Deselect (bind to 0) the VAO 
    GL30.glBindVertexArray(0); 
    // Create a new VBO for the indices and select it (bind) - INDICES 
    System.out.println("NO Error? " + (GL11.glGetError() == GL11.GL_NO_ERROR)); 
    vboiId = GL15.glGenBuffers(); 
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId); 
    GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW); 
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); 
    System.out.println("NO Error? " + (GL11.glGetError() == GL11.GL_NO_ERROR)); 
} 

着色器設置代碼:

int errorCheckValue = GL11.glGetError(); 
// Load the vertex shader 
vsId = this.loadShader("Shaders/vertex.glsl", GL20.GL_VERTEX_SHADER); 
// Load the fragment shader 
fsId = this.loadShader("Shaders/fragment.glsl", GL20.GL_FRAGMENT_SHADER); 
// Create a new shader program that links both shaders 
pId = GL20.glCreateProgram(); 
GL20.glAttachShader(pId, vsId); 
GL20.glAttachShader(pId, fsId); 
GL20.glLinkProgram(pId); 
// Position information will be attribute 0 
GL20.glBindAttribLocation(pId, 0, "in_Position"); 
// Color information will be attribute 1 
GL20.glBindAttribLocation(pId, 1, "in_Color"); 
GL20.glValidateProgram(pId); 
errorCheckValue = GL11.glGetError(); 
if (errorCheckValue != GL11.GL_NO_ERROR) { 
System.out.println("ERROR - Could not create the shaders"); 
    System.exit(-1); 
} 

我的臺式機在(它呈現每一個hing如期)運行nVidia GTX 460圖形卡,而我的學校筆記本電腦(不提供任何內容)有一個AMD Firepro m4000圖形卡。我最好的猜測是VBO的某些部分渲染/創建/着色器代碼與我的m4000卡不兼容,但我無法使用glGetError()發現任何錯誤。

+0

在eclipse中對我女朋友的筆記本電腦運行程序也不會渲染任何東西。她的筆記本電腦和我的硬件一樣。 – Loginaut

+0

驅動程序版本如何?這也可以有所作爲。你確定沒有OpenGL錯誤嗎?你在哪裏調用glGetError(),你做了多少次?我建議你用幾個'glGetErrors()'來填充你的代碼,只是爲了確保沒有任何代碼... – Dan

+0

你有沒有顯示的代碼確保'in_Position'被綁定到位置0和'in_Color'到位置1? –

回答

1

調用你在着色器設置代碼如預期將無法正常工作順序:

GL20.glLinkProgram(pId); 
GL20.glBindAttribLocation(pId, 0, "in_Position"); 
GL20.glBindAttribLocation(pId, 1, "in_Color"); 

glBindAttribLocation()需要被稱爲以前glLinkProgram()生效。從man page

只有在調用glLinkProgram之前,屬性綁定纔會生效。程序對象成功鏈接後,通用屬性的索引值保持不變(可以查詢它們的值),直到發生下一個鏈接命令。

因此,調用的順序必須是:

GL20.glBindAttribLocation(pId, 0, "in_Position"); 
GL20.glBindAttribLocation(pId, 1, "in_Color"); 
GL20.glLinkProgram(pId); 

雖然不是一個正確的問題,你還沒有使用VAOs非常有效。屬性啓用/禁用值和GL_ELEMENT_ARRAY_BUFFER綁定是VAO狀態的一部分。因此,您可以在設置期間撥打glEnableVertexAttribArray()GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId)一次,,而VAO綁定。然後在平局中,您只需綁定VAO即可設置所有狀態。

+0

太棒了!謝謝您的幫助!有趣的是,我的桌面顯卡能夠弄清楚發生了什麼。 – Loginaut

+0

如果位置自動分配,許多實現將按發生順序依次分配它們。默認情況下你會得到0和1並不令人吃驚。但其他人可能會按字母順序依次分配它們。或者使用完全不同的東西。 –