在 glGenTextures 上获得 EXC_BAD_ACCESS
Getting a EXC_BAD_ACCESS on glGenTextures
,使用 SDL2 和 SDL2_image 尝试一些 C++ 和 OpenGL
一旦到达 glGenTextures 调用,我就会收到主题错误。我的大部分搜索都提到尚未创建 gl 上下文。我有,并在此之前成功使用了多个 gl 调用。这是我的主要内容。所以
int main(int argc, const char * argv[]) {
uniforms = std::vector<GLuint>();
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_Window* window = SDL_CreateWindow("OpenGL", 100, 100, 800, 600, SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(window);
glewExperimental = GL_TRUE;
glewInit();
SDL_Event windowEvent;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint vbo;
glGenBuffers(1, &vbo);
GLuint ebo;
glGenBuffers(1, &ebo);
setupVertices(vbo);
setupElementBuffer(ebo);
GLuint fragmentShader;
GLuint vertexShader;
GLuint shaderProgram = compileShaders(vertexShader, fragmentShader);
bindAttributes(shaderProgram);
GLuint tex;
glGenTextures(GL_TEXTURE_2D, &tex);
SDL_Surface *image = IMG_Load("pic.png");
if (!image) {
assert(false);
}
int mode;
if (image->format->BytesPerPixel == 3) { // RGB 24bit
mode = GL_RGB;
} else if (image->format->BytesPerPixel == 4) { // RGBA 32bit
mode = GL_RGBA;
}
glBindTexture(GL_TEXTURE_2D, tex);
// ... etc
glGenTextures()
将 count 作为第一个参数,表示要创建多少个纹理名称,第二个参数是指向一个足以容纳所有纹理的数组的指针名字。您正在呼叫
GLuint tex;
glGenTextures(GL_TEXTURE_2D, &tex);
因此您使用 GL_TEXTURE_2D
作为计数,这是一个相当大的数字,GL 将覆盖堆栈中 tex
之后的任何内容,导致崩溃...
我用这个修复它:
glGenTextures(1, &textureId);
一旦到达 glGenTextures 调用,我就会收到主题错误。我的大部分搜索都提到尚未创建 gl 上下文。我有,并在此之前成功使用了多个 gl 调用。这是我的主要内容。所以
int main(int argc, const char * argv[]) {
uniforms = std::vector<GLuint>();
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_Window* window = SDL_CreateWindow("OpenGL", 100, 100, 800, 600, SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(window);
glewExperimental = GL_TRUE;
glewInit();
SDL_Event windowEvent;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint vbo;
glGenBuffers(1, &vbo);
GLuint ebo;
glGenBuffers(1, &ebo);
setupVertices(vbo);
setupElementBuffer(ebo);
GLuint fragmentShader;
GLuint vertexShader;
GLuint shaderProgram = compileShaders(vertexShader, fragmentShader);
bindAttributes(shaderProgram);
GLuint tex;
glGenTextures(GL_TEXTURE_2D, &tex);
SDL_Surface *image = IMG_Load("pic.png");
if (!image) {
assert(false);
}
int mode;
if (image->format->BytesPerPixel == 3) { // RGB 24bit
mode = GL_RGB;
} else if (image->format->BytesPerPixel == 4) { // RGBA 32bit
mode = GL_RGBA;
}
glBindTexture(GL_TEXTURE_2D, tex);
// ... etc
glGenTextures()
将 count 作为第一个参数,表示要创建多少个纹理名称,第二个参数是指向一个足以容纳所有纹理的数组的指针名字。您正在呼叫
GLuint tex;
glGenTextures(GL_TEXTURE_2D, &tex);
因此您使用 GL_TEXTURE_2D
作为计数,这是一个相当大的数字,GL 将覆盖堆栈中 tex
之后的任何内容,导致崩溃...
我用这个修复它: glGenTextures(1, &textureId);