OSX上的OpenGL和SDL - glDrawElements()上的EXC_BAD_ACCESS - 适用于GLUT,而不适用于SDL

时间:2010-11-03 16:01:57

标签: opengl sdl

我使用GLUT编写了一个简单的应用程序,我现在移植到SDL将其变成游戏。

我有一个奇怪的问题,特别是使用glDrawElements和Vertex Buffer Objects,SDL 1.2.14 OSX。如果我不使用VBO,程序运行正常。它只在使用VBO时抛出“EXC_BAD_ACCESS”。使事情更加模糊。该程序在GLUT中运行完全正常。我必须在初始化中丢失一些可能导致这种情况的东西。

这是绘图代码:

if (glewGetExtension("GL_ARB_vertex_buffer_object"))
{
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);        

    //Load vertices
    glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_vertices);
    glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));

    //Load normals
    glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_normals);
    glNormalPointer(GL_FLOAT, 0, BUFFER_OFFSET(0));

    //Load UVs
    glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_uvs);
    glTexCoordPointer(2, GL_FLOAT, 0, BUFFER_OFFSET(0));

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->mesh->vbo_index);
    App dies here -----> glDrawElements(GL_TRIANGLES, 3*this->mesh->numFaces, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);

} else {

    //BTW: If I run this block of code instead of the above, everything renders fine. App doesn't die. 

    //Drawing with vertex arrays
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glVertexPointer(3, GL_FLOAT, 0, this->mesh->vertexArray);
    glNormalPointer(GL_FLOAT, 0, this->mesh->normalsArray);
    glTexCoordPointer(2, GL_FLOAT, 0, this->mesh->uvArray);

    glDrawElements(GL_TRIANGLES, 3*this->mesh->numFaces, GL_UNSIGNED_INT, this->mesh->indexArray);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

这是调试信息:

Program received signal:  “EXC_BAD_ACCESS”.

Thread-1-<com.apple.main-thread>
#0  0x17747a93 in gleRunVertexSubmitImmediate
#1  0x1774772c in gleLLVMArrayFunc
#2  0x177476e4 in gleSetVertexArrayFunc
#3  0x1773073c in gleDrawArraysOrElements_ExecCore
#4  0x176baa7b in glDrawElements_Exec
#5  0x97524050 in glDrawElements

asm gleRunVertexSubmitImmediate

0x17747a93  <+0771>  mov    (%eax,%ecx,4),%eax      <-- the app dies on this.

这是我的SDL初始化代码:

//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
    cout << "Could not initialize SDL" << endl << SDL_GetError();
    exit(2);
}

//Set window
SDL_WM_SetCaption("Hello World!", "Hello World!");

//Set openGL window
if ( SDL_SetVideoMode(width, height, 32, SDL_OPENGL | SDL_RESIZABLE) == NULL ) {
    cout << "Unable to create OpenGL context: %s\n" << endl << SDL_GetError();
    SDL_Quit();
    exit(2);
}

//Set up event handling
SDL_Event event;
bool quit = false;

//Initialize GLEW
GLenum err = glewInit();
if (GLEW_OK != err)
{
    //Problem: glewInit failed, something is seriously wrong. 
    fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
    exit(1);
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));

2 个答案:

答案 0 :(得分:1)

我认为您的代码没有任何问题。

当您获得EXC_BAD_ACCESS时,通常是因为尝试访问未分配的对象或已被解除分配的对象。

通过启用NSZombieEnabled环境变量,您可以获得有关对象的更详细的调试信息。 This是一篇关于如何启用此环境变量的简单博客文章(我不是作者)。

这有助于在调试控制台中获取有关崩溃发生原因的更多信息。

答案 1 :(得分:1)

如果你的模型(obj)文件中没有vn,你应该注释掉以下几行:

//glEnableClientState(GL_NORMAL_ARRAY);
//glDisableClientState(GL_NORMAL_ARRAY);

那个人为我工作。