2015-05-27 30 views
0

有誰知道爲什麼被拋出這個錯誤?陣列vertex_buffer_object必須綁定調用此方法

我以爲我使用glEnableVertexAttribArray時,我綁定到VBO

com.jogamp.opengl.GLException: array vertex_buffer_object must be bound to call this method 
    at jogamp.opengl.gl4.GL4bcImpl.checkBufferObject(GL4bcImpl.java:39146) 
    at jogamp.opengl.gl4.GL4bcImpl.checkArrayVBOBound(GL4bcImpl.java:39178) 
    at jogamp.opengl.gl4.GL4bcImpl.glVertexAttribPointer(GL4bcImpl.java:37371) 

這是我的代碼繪製..

public void draw(final GL2ES2 gl, Matrix4f projectionMatrix, Matrix4f viewMatrix, int shaderProgram, final Vec3 position, final float angle) { 

    // enable glsl 
    gl.glUseProgram(shaderProgram); 

    // enable alpha 
    gl.glEnable(GL2ES2.GL_BLEND); 
    gl.glBlendFunc(GL2ES2.GL_SRC_ALPHA, GL2ES2.GL_ONE_MINUS_SRC_ALPHA); 

    // get handle to glsl variables 
    mPositionHandle = gl.glGetAttribLocation(shaderProgram, "vPosition"); 
    setmColorHandle(gl.glGetUniformLocation(shaderProgram, "vColor")); 
    mProj = gl.glGetUniformLocation(shaderProgram, "mProj"); 
    mView = gl.glGetUniformLocation(shaderProgram, "mView"); 
    mModel = gl.glGetUniformLocation(shaderProgram, "mModel"); 

    // perform translations 
    getModelMatrix().loadIdentity(); 
    getModelMatrix().translate(new Vec3(position.x * 60.0f, position.y * 60.0f, position.z * 60.0f)); 
    getModelMatrix().rotate(angle, 0, 0, 1); 

    // set glsl variables 
    gl.glUniform4fv(getmColorHandle(), 1, getColorArray(), 0); 
    gl.glUniformMatrix4fv(mProj, 1, true, projectionMatrix.getValues(), 0); 
    gl.glUniformMatrix4fv(mView, 1, true, viewMatrix.getValues(), 0); 
    gl.glUniformMatrix4fv(mModel, 1, true, getModelMatrix().getValues(), 0); 

    // Enable a handle to the triangle vertices 
    gl.glEnableVertexAttribArray(mPositionHandle); 


    // Prepare the triangle coordinate data 
    gl.glVertexAttribPointer(
     getmPositionHandle(), 
     COORDS_PER_VERTEX, 
     GL2ES2.GL_FLOAT, 
     false, 
     vertexStride, 0L); // This is the line that throws error 

    // Draw the square 
    gl.glDrawElements(
     GL2ES2.GL_TRIANGLES, 
     drawOrder.length, 
     GL2ES2.GL_UNSIGNED_SHORT, 
     0L); 



    // Disable vertex array 
    gl.glDisableVertexAttribArray(mPositionHandle); 

    gl.glDisable(GL2ES2.GL_BLEND); 
    gl.glUseProgram(0); 
} 

回答

0

(我從來沒有用過的OpenGL與Java,所以我將使用C/C++代碼,但我希望它會遇到好)

不創建或綁定一個頂點緩衝區對象。

首先,使用glGenBuffers創建一個緩衝區,像這樣:

GLuint bufferID; 
glGenBuffers(1, &bufferID); 

這在分配一個bufferID手柄和存儲。

然後,綁定緩衝:

glBindBuffers(GL_ARRAY_BUFFER, bufferID); 

這使得 「當前」 緩衝區使用。

接着,用數據填充緩衝器。假設vertices是存儲你的頂點座標的數組,在平坦的格式,與每個頂點3個浮筒:

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW); 

這實際上使數據在GPU存儲器。

然後使屬性陣列和設置指針:

glEnableVertexAttribArray(mPositionHandle); 
glVertexAttribPointer(mPositionHandle, 3, GL_FLOAT, 0, 0, 0); 

這將使在vertices可用的mPositionHandle頂點屬性位置下着色器程序中的數據。

倒數第二個參數glVertexAttribPointerstride。在這個例子中,它是0,因爲緩衝區只包含頂點位置數據。如果你想在同一個緩衝收拾這兩個頂點位置數據和顏色數據,像這樣:

v1.positionX v1.positionY v1.positionZ v1.colorR v1.colorG v1.colorB 
v2.positionX ... 

,你將需要使用一個非零stridestride指定一個屬性與下一個相同類型之間的偏移量;步伐爲0,他們被認爲是緊密包裝。在這種情況下,您需要設置一個步幅sizeof(GLfloat) * 6,以便在讀取一個頂點的位置後,它將跳過顏色數據以到達下一個頂點,類似於顏色。

// (create, bind and fill vertex buffer here) 
glEnableVertexAttribArray(location_handle_of_position_data); 
glVertexAttribPointer(location_handle_of_position_data, 3, GL_FLOAT, 0, sizeof(GLfloat) * 6, 0); 

glEnableVertexAttribArray(location_handle_of_color_data); 
glVertexAttribPointer(location_handle_of_color_data, 3, GL_FLOAT, 0, sizeof(GLfloat) * 6, sizeof(GLfloat) * 3); 

最後一個參數是第一個屬性的偏移量 - 第一個顏色屬性在第三個浮動值後開始。

其他注意事項:

  • 你應該考慮使用頂點數組對象。沒有它們,它可能會也可能不會運行,但是按照標準它們是必需的,並且它們在任何情況下簡化代碼。
  • 爲了簡單起見,本示例代碼將顏色數據存儲在浮點數中,但對於實際使用字節是更可取的。
+0

我以前'glEnableVertexAttribArray'已經盡力'glGenBuffers','glBindBuffer'和'glBufferData',但我得到這個錯誤,這是非常相似的原..'元素vertex_buffer_object必須綁定調用此method' – bobbyrne01

+0

@ bobbyrne01試着綁定一個[頂點數組對象](https://www.opengl.org/wiki/Vertex_Specification#Vertex_Array_Object)。有些實現不會讓你做任何事情,沒有一個。 – orost

0

glVertexAttribPointer()指定爲屬性數據應該從當前綁定頂點緩衝器被拉動,使用指定的參數。所以,你需要撥打:你叫glVertexAttribPointer()

gl.glBindBuffer(GL_VERTEX_ARRAY, ...); 

之前。

glEnableVertexAttribArray()指定數組應該用於頂點屬性。否則,使用如glVertexAttrib4f()這樣的調用指定的常量值。但它沒有指定數組在緩衝區中。更重要的是,除非綁定特定的緩衝區,否則glVertexAttribPointer()將不知道哪個緩衝區用於該屬性。

相關問題