【问题标题】:C++ OpenGL/SDL 2.0 faild to load a textureC++ OpenGL/SDL 2.0 无法加载纹理
【发布时间】:2014-08-22 21:49:57
【问题描述】:

如果我尝试加载纹理,我会收到此错误:

Access violation reading location.
Unhandled exception at 0x651B5A17 (nvcuda.dll) in nsighttest.exe: 0xC0000005: Access  violation reading location 0x00000010.

图片: http://img5.fotos-hochladen.net/uploads/error1wlbsuf0iv.png

调试图像: http://img5.fotos-hochladen.net/uploads/debugatc1xerob5.png

unsigned int loadTexture(const char*  filename, texProperties properties)
{
    GLint numberofcolors = 0;
    GLenum format;
    SDL_Surface * img = IMG_Load(filename);

    cout << "Image height: " << img->h << endl;
    cout << "Image width: " << img->w << endl;
    cout << "Images Pixels: " << img->pixels << endl;
    cout << "Images BitsPerPixel: " << img->format->BitsPerPixel << endl;
    cout << "Images Rmask: " << img->format->Rmask << endl;
    cout << "Images Surface: " << img << endl;


    if(!(&img)) { std::cout << "Fehler beim laden des bildes: " << filename; std::cout << std::endl; }
    if(img->format->BitsPerPixel == 4) {
        if(img->format->Rmask == 0x000000ff) { format = GL_RGBA; }
        else { format = GL_BGRA; }
        numberofcolors = 4;
    } else {
        if(img->format->Rmask == 0x000000ff) { format = GL_RGB; } 
        else { format = GL_BGR; }
        numberofcolors = 3;
    }

    unsigned int id;
    glGenTextures(1, &id);
    glBindTexture(GL_TEXTURE_2D, id);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, properties.getMagFilter());
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, properties.getMinFilter());

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, properties.getTextureWrap());
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, properties.getTextureWrap());

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, properties.getAnisotropy());

    glTexImage2D(GL_TEXTURE_2D, 0 , numberofcolors=4?GL_RGBA:GL_RGB, img->w, img->w, 0, format, GL_UNSIGNED_BYTE, img->pixels);
    SDL_FreeSurface(img);
    return id;
}

错误来自“glTexImage2D(...)”。

【问题讨论】:

  • 你为什么不检查SDL_LoadBMP()的返回值?或者验证您返回的SDL_Surface 实际上是GL_UNSIGNED_SHORT_5_6_5
  • 我已经检查了退货,但我得到了一些号码,我也尝试使用“GL_UNSIGNED_SHORT_5_6_5”但结果相同
  • 我会冒险猜测,与 99% 的图像问题一样,它没有加载,因为它位于错误的位置或其他地方。您需要检查 SDL_Surface* img 是否是有效的内存位置,即在您执行 SDL_LoadBMP 之后不为 NULL
  • 使用 24 位 BMP 和三个通道。

标签: c++ opengl textures loading sdl-2


【解决方案1】:

查看 glTexImage2D() 函数,看起来您正在使用 img-&gt;w 两次,而不是在第二次使用 img-&gt;h

尝试调用

glTexImage2D(GL_TEXTURE_2D, 0 , numberofcolors=4?GL_RGBA:GL_RGB, img->w, /*Use height as second parameter, rather than width!*/img->h, 0, format, GL_UNSIGNED_BYTE, img->pixels);

相反。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    相关资源
    最近更新 更多