整体代码如下,报错:
glGenerateMipmap(GL_TEXTURE_2D);
时0x0000000000000000 处(位于 ConsoleApplication1.exe 中)引发的异常: 0xC0000005: 执行位置 0x0000000000000000 时发生访问冲突。
void textureInit()
{
// Load and bind texture
//glewInit();
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
// Set texture parameters (minification/magnification filters, wrap modes)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Load the image data into the texture
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char* data = stbi_load("D:\\tools\\visualStudio\\VSproject\\ConsoleApplication1\\ConsoleApplication1\\texture.png", &width, &height, &nrChannels, 0);
if (data) {
std::cout << "load pic success..." << std::endl;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
std::cerr << "Failed to load texture" << std::endl;
// Handle texture loading error
}
stbi_image_free(data);
}
排查错误,问题为只进行了glutInit初始化,而没有进行glewInit()初始化,加上glewInit后正常运行