opengl纹理加载glGenerateMipmap(GL_TEXTURE_2D);

文章描述了一个在使用OpenGL加载纹理时遇到的异常,错误发生在调用`glGenerateMipmap(GL_TEXTURE_2D)`时,原因是缺少glewInit()初始化。在添加glewInit后,程序可以正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

整体代码如下,报错:

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后正常运行

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值