2012-07-22 88 views
1

我的代碼在使用glDrawArrays時工作正常,但每當我創建索引緩衝區並將其更改爲使用glDrawElements時,它就會發生段錯誤。使用索引緩衝區對象時發生奇怪的Segfault

(奇怪的是,在此之前或拉伸後...但是當主循環退出!)

這裏是簡單code..where我宣佈4個頂點和3個指標向他們畫一個三角形。

int main() 
{ 
    sf::Window win(sf::VideoMode(400,400,32),"Manasij"); 
    GlewInit(); 
    mm::Program program 
    (
     { 
      mm::Shader(GL_VERTEX_SHADER,"vshader.vert"), 
      mm::Shader(GL_FRAGMENT_SHADER,"fshader.frag") 
     } 
    ); 
    float vdata[]= 
    { 
     0.0f,0.0f, 
     0.5f,0.0f, 
     0.5f,0.5f, 
     0.0f,0.5f 
    }; 
    GLubyte indices[]={0,1,3}; 

    GLuint vao,vbo,ebo; 
    glGenVertexArrays(1,&vao); 
    glBindVertexArray(vao); 

    glGenBuffers(1,&vbo); 
    glBindBuffer(GL_ARRAY_BUFFER,vbo); 
    glBufferData(GL_ARRAY_BUFFER,8*sizeof(float),vdata,GL_STATIC_DRAW); 

    glGenBuffers(1,&ebo); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,3*sizeof(GLubyte),indices,GL_STATIC_DRAW); 

    glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,0); 
    glEnableVertexAttribArray(0); 
    glBindAttribLocation(program.getHandle(),0,"pos"); 
    //glBindVertexArray(0); 

    glUseProgram(program.getHandle()); 
    //glBindVertexArray(vao); 
    glClearColor(1.0f,1.0f,1.0f,1.0f); 

    while(win.isOpen()) 
    { 
     sf::Event eve; 
     while(win.pollEvent(eve)) 
      if(eve.type==sf::Event::Closed) 
       win.close(); 
     glClear(GL_COLOR_BUFFER_BIT); 

//  glDrawArrays(GL_TRIANGLES,0,3); 
     glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_BYTE,(GLvoid*)0); 
     win.display(); 
    } 
    return 0; 
} 

而且着色器,因爲他們得到簡單:

#version 330 
in vec2 pos; 
void main() 
{ 
    gl_Position = vec4(pos,0.0f,1.0f); 
} 

#version 330 
void main() 
{ 
    gl_FragColor = vec4(1.0f,0.3f,0.8f,1.0f); 
} 

和gdb顯示:

Breakpoint 1, main() at test.cpp:57 
57      win.display(); 
(gdb) continue 
Continuing. //<- At this point I try to close the window, thus triggeing the main loop to break 

Program received signal SIGSEGV, Segmentation fault. 
0x00007ffff557d164 in ??() from /lib/libnvidia-glcore.so.302.17 

任何想法,我搞壞up?

P.S:如果有人想測試,這裏有一個檔案..完整的makefile!

https://docs.google.com/file/d/0B0oubbp-MVYOeGFuX0owXzhRTEU/edit

+0

也許你需要釋放一些東西用'glDeleteVertexArrays'和'glDeleteBuffer'? – Aesthete 2012-07-22 08:14:03

+0

程序退出時不會自動發生嗎? (比如..say ..open文件) – manasij7479 2012-07-22 08:15:30

+0

我的第一個猜測是否定的。 – Aesthete 2012-07-22 08:16:27

回答

2

問題是,在循環的最後一次迭代中,當檢測到'close'事件時,窗口被告知關閉,但即使在此之後,我最後一次調用了glDrawElements。 因爲一切都已經消失,導致了段錯誤。

解決方法是在win.close()被調用後放置一個break語句,或者將事件處理代碼放到循環結尾。

(感謝來自的archlinux論壇for catching this到cmtptr,我幾乎相信,這是一個驅動程序問題)

0

嘗試刪除您VertexArraysBuffers在平局循環結束:

while(win.isOpen()) 
{ 
    sf::Event eve; 
    while(win.pollEvent(eve)) 
     if(eve.type==sf::Event::Closed) 
      win.close(); 
    glClear(GL_COLOR_BUFFER_BIT); 

    // glDrawArrays(GL_TRIANGLES,0,3); 
    glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_BYTE,(GLvoid*)0); 
    win.display(); 
} 
glDeleteBuffer(1, &ebo); 
glDeleteVertexArrays(1, &vbo);  
return 0; 

這只是一個猜測,請告訴我,如果它的確與衆不同。

+0

不,它沒有。 (順便說一句.. VBO是一個緩衝區太) 所以我做 \t glDeleteBuffers(1,&vbo); \t glDeleteBuffers(1 &ebo); \t glDeleteVertexArrays(1,&vao); – manasij7479 2012-07-22 08:21:23

+0

啊確定..對不起,我什麼都看不到關閉我的頭頂! – Aesthete 2012-07-22 08:25:24