2016-08-02 75 views
-1

我試圖用一個頂點數組對象繪製在OpenGL 4.1的一些頂點和索引:的OpenGL 4.1:頂點數組對象繪製什麼

float vertices[] = {-0.8, -0.8, 0, 0.8, -0.8, 0, 0, 0.8, 0}; 
unsigned int indices[] = { 0, 1, 2 }; 

建立VAO和使用glDrawElements繪製它()之後,我得到一個空的屏幕。我也嘗試使用一對簡單的着色器,只是返回原始頂點位置和硬編碼顏色,但也沒有運氣。

下面是其中規定了VBO和維羅代碼:

unsigned int vertexArray; 
glGenVertexArrays(1, &vertexArray); 
glBindVertexArray(vertexArray); 

unsigned int vertexBufferID; 
unsigned int indexBufferID; 

glGenBuffers(1, &vertexBufferID); 
glGenBuffers(1, &indexBufferID); 

glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID); 
glBufferData(GL_ARRAY_BUFFER, 4 * vertexCount, vertices, GL_STATIC_DRAW); 
glEnableVertexAttribArray(0); 
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 4 * vertexCount, 0); 

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID); 
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * indexCount, indices, GL_STATIC_DRAW); 

我用畫VAO:

glBindVertexArray(vertexArray); 
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (void*)0); 

這裏的所有的OpenGL一絲電話:

// Setup code 

glViewport(0,0,1024,768) 
glGetString(GL_RENDERER)="GeForce GT 540M/PCIe/SSE2" 
glGetString(GL_VENDOR)="NVIDIA Corporation" 
glGetString(GL_VERSION)="4.1.0 NVIDIA 359.46" 
glGetString(GL_SHADING_LANGUAGE_VERSION)="4.10 NVIDIA via Cg compil..." 
glEnable(GL_DEPTH_TEST) 
glDepthFunc(GL_LESS) 
glGenVertexArrays(1,000000000015F9B4) 
glBindVertexArray(1) 
glGenBuffers(1,000000000015F9D4) 
glGenBuffers(1,000000000015F9F4) 
glBindBuffer(GL_ARRAY_BUFFER,1) 
glBufferData(GL_ARRAY_BUFFER,36,000000000015FA48,GL_STATIC_DRAW) 
glEnableVertexAttribArray(0) 
glVertexAttribPointer(0,3,GL_FLOAT,false,36,0000000000000000) 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,2) 
glBufferData(GL_ELEMENT_ARRAY_BUFFER,12,000000000015FA88,GL_STATIC_DRAW) 
glClearColor(0.300000,0.300000,0.400000,1.000000) 

// End of setup, here's the code rendering two frames (they just repeat): 

glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT) 
glBindVertexArray(1) 
glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_INT,0000000000000000) 
glGetError()=GL_NO_ERROR 
wglSwapBuffers(0000000069011A82)=true 

glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT) 
glBindVertexArray(1) 
glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_INT,0000000000000000) 
glGetError()=GL_NO_ERROR 
wglSwapBuffers(0000000069011A82)=true 

從我可以告訴我做什麼幾乎每個教程說我應該做的,但由於某種原因沒有任何應用程序耳朵在我的屏幕上。我錯過了什麼?

回答

1

您的數據緊緊地放在數組中,因此glVertexAttribPointer中的stride參數應該爲0.順便說一句,如果聲明靜態數組,則應該使用sizeof來傳遞數組的大小而不是實現相關的東西4 * vertexCount

glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID); 
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 
glEnableVertexAttribArray(0); 
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID); 
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); 
+0

你大部分是正確的。如果步幅爲0,則opengl會假設該數組是緊密排列的,並會根據其他參數計算正確的步幅值。不管沒有錯誤,傳遞正確的步幅都是正確的。但是,我把數組的長度作爲跨度值,這當然不起作用>< – Bartvbl

+0

這就是我所指的:) – wasthishelpful