正如标题所示,我的笔记本电脑上出现“访问冲突读取位置0x000000000”错误,但代码来自youtube课程,其他人没有问题。我该怎么做才能解决这个问题?代码实际上是从源代码复制的。
编译到达行时出现问题
if (_vboID == 0) {
glGenBuffers(1, &_vboID);
}
代码:
void MainGame::initSystems() {
//Initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);
//Open an SDL window
_window = SDL_CreateWindow("Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _screenWidth, _screenHeight, SDL_WINDOW_OPENGL);
if (_window == nullptr) {
fatalError("SDL Window could not be created!");
}
//Set up our OpenGL context
SDL_GLContext glContext = SDL_GL_CreateContext(_window);
if (glContext == nullptr) {
fatalError("SDL_GL context could not be created!");
}
glewExperimental = true;
//Set up glew (optional but recommended)
GLenum error = glewInit();
if (error != GLEW_OK) {
fatalError("Could not initialize glew!");
}
if (error == GLEW_OK) { std::cout << "GLEW DID INIT!\n"; }
//Tell SDL that we want a double buffered window so we dont get
//any flickering
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
//Set the background color to blue
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
void Sprite::init(float x, float y, float width, float height) {
//Set up our private vars
_x = x;
_y = y;
_width = width;
_height = height;
//Generate the buffer if it hasn't already been generated
if (_vboID == 0) {
glGenBuffers(1, &_vboID);
}
float vertexData[12];
//First Triangle
vertexData[0] = x + width;
vertexData[1] = y + height;
vertexData[2] = x;
vertexData[3] = y + height;
vertexData[4] = x;
vertexData[5] = y;
//Second Triangle
vertexData[6] = x;
vertexData[7] = y;
vertexData[8] = x + width;
vertexData[9] = y;
vertexData[10] = x + width;
vertexData[11] = y + height;
//Tell opengl to bind our vertex buffer object
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
//Upload the data to the GPU
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
//Unbind the buffer (optional)
//glBindBuffer(GL_ARRAY_BUFFER, 0);
}
答案 0 :(得分:0)
glGenBuffers
是一个在OpenGL-1.2之后引入的函数。目前,在Windows中,ABI合同只需要通过opengl32.dll公开OpenGL-1.1,而在使用LSB-4的Linux中,ABI合同仅指定OpenGL-1.2。 LSB-5几个月前已经发布,但大多数系统还没有关注它。
本质上这意味着对于任何现代OpenGL函数,您必须通过扩展机制在运行时加载OpenGL函数指针。手动(繁琐)或使用一些加载程序库。
在不加载函数指针的情况下,可能定义的符号(取决于您包含OpenGL标头的方式)可能未定义。一些OpenGL实现可能会导出更多符号,但您不能依赖它。
了解OpenGL功能加载和OpenGL扩展机制非常重要。