2014-02-07 160 views
2

我的目標是讓實例化渲染工作,但即使是單一的glDrawElements現在也會失敗。注意:此代碼已在Windows上運行。然而,在OS X上,它失敗GL_INVALID_OPERATIONglDrawElements失敗,出現錯誤GL_INVALID_OPERATION

基本上我把所有的靜態數據加載到緩衝區中,然後最後一個緩衝區包含我在每次繪製之前重新加載的動態數據。然後我調用glDrawElementsInstanced(或者調試glDrawElements),它立即失敗。我知道,因爲在打印之前和打完電話後出現錯誤,並且總是打印出OpenGL錯誤。 (即使與glDrawElements)如果我使用glDrawArrays而不是

如果我使用glDrawArrays,則不會出現此錯誤請參閱代碼中的註釋以獲取其他信息。任何幫助非常感謝。

//Setup code, at this point vertices,textureCoordiantes,normals are all populated 

//Allocate the space for the gpu buffers now 
//and send the static data 
//Rebind the array to bring them into the current context 
glBindVertexArray (vertexArray); 

//Push voxel to gpu 
glBindBuffer (GL_ARRAY_BUFFER, vertexBuffer); 
glBufferData (GL_ARRAY_BUFFER, 36*sizeof(vec3), vertices, GL_STATIC_READ); 
glEnableVertexAttribArray (shader->AttributeVertex()); 
glVertexAttribPointer (shader->AttributeVertex(), 3, GL_FLOAT, GL_FALSE, 0, 0); 

glBindBuffer (GL_ARRAY_BUFFER, textureBuffer); 
glBufferData (GL_ARRAY_BUFFER, 36*sizeof(vec2), textureCoordinates, GL_STATIC_READ); 
glEnableVertexAttribArray (shader->AttributeTexture()); 
glVertexAttribPointer (shader->AttributeTexture(), 2, GL_FLOAT, GL_FALSE, 0, 0); 

glBindBuffer (GL_ARRAY_BUFFER, normalBuffer); 
glBufferData (GL_ARRAY_BUFFER, 36*sizeof(vec3), normals, GL_STATIC_READ); 
glEnableVertexAttribArray (shader->AttributeNormal()); 
glVertexAttribPointer (shader->AttributeNormal(), 3, GL_FLOAT, GL_FALSE, 0, 0); 
//Allocate space for positions 
glBindBuffer (GL_ARRAY_BUFFER, positionBuffer); 
glBufferData (GL_ARRAY_BUFFER, INSTANCE_RENDER_SWEEP*sizeof(vec4), positions, GL_DYNAMIC_READ); 
glEnableVertexAttribArray (shader->AttributePosition()); 
glVertexAttribPointer (shader->AttributePosition(), 4, GL_FLOAT, GL_FALSE, 0, 0); 


    //This code runs a bit later, but runs over and over: 
    //indices is a vector<GLuint> of length 36 and is just 0-35 

glBindVertexArray (vertexArray); 
glBindBuffer (GL_ARRAY_BUFFER, positionBuffer); 
glBufferSubData (GL_ARRAY_BUFFER, 0,INSTANCE_RENDER_SWEEP*sizeof(vec4), positions); 
glEnableVertexAttribArray (shader->AttributePosition()); 
glVertexAttribPointer (shader->AttributePosition(), 4, GL_FLOAT, GL_FALSE, 0, 0); 

//The position is per-instance 
//everything else is per-vertex 
glVertexAttribDivisor(shader->AttributeNormal(),0); 
glVertexAttribDivisor(shader->AttributePosition(),1); 
glVertexAttribDivisor(shader->AttributeTexture(),0); 
glVertexAttribDivisor(shader->AttributeVertex(),0); 

cout << "1Err: " << glGetError() << "\n"; 
    glDrawDelements(GL_TRIANGLES,36,GL_UNSIGNED_BYTE,&indices[0]); 
//glDrawElementsInstanced(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, &indices[0], bufferedVoxels); 
    //This next error prints out 1282 which is GL_INVALID_OPERATION 
    //However if i replace the above with glDrawArrays, it works for one instance (no error) 
cout << "2Err: " << glGetError() << "\n"; 
//All buffered voxels now drawn 
bufferedVoxels = 0; 
+0

你目前在OS X上使用的是哪種OpenGL上下文?核心3.2+?實例化是GL 3.x時代,要在OS X上以未擴展形式使用它,您需要一個Core配置文件。任何僅在Core中暴露的函數,您嘗試從GL 2.1上下文中調用將會在OS X上以靜默方式失敗或產生「GL_INVALID_OPERATION」。這使得它與其他平臺不同,如果該函數不受支持,則您知道這是因爲函數指針將爲NULL。 –

回答

0

我對openGL不太熟悉,但是你使用的是glew嗎?如果是這樣,你是否在glewInit()之前嘗試設置glewExperimental = GL_TRUE

+0

GLEW在OS X上不是必需的。蘋果一直以不同的方式做事,他們的GL做法有點棘手,正如我在對OP問題的評論中解釋的那樣。 –

+0

謝謝你指出 – lightandlight

1

對於GL核心上下文,無法通過indices參數glDrawElementsglDrawElementsInstanced的客戶端陣列。在這兩種情況下,您都需要創建索引緩衝區並將索引存儲在此緩衝區中。繪圖調用的參數indices然後成爲從中讀取索引的綁定索引緩衝區的偏移量(以字節爲單位)。

但是,查看您的索引數組僅爲0 - 35,爲什麼不使用glDrawArraysglDrawArraysInstanced而不是?

相關問題