C++ SDL2(C+;+;)并非所有.png图像都能正确渲染

C++ SDL2(C+;+;)并非所有.png图像都能正确渲染,c++,opengl,sdl,sdl-2,C++,Opengl,Sdl,Sdl 2,注意:我使用的是传统的OpenGL 因此,我遇到了一个问题,即只有少数.png图像能够正确渲染 下面是渲染游戏的示例屏幕截图(左:enemyRed.png,右:(ship.png)): 下面是我用来加载图像的代码: SpriteImage* SDLOpenGL::loadImage(std::string path) { SDL_Surface * surface = IMG_Load(path.c_str()); SpriteImage * image = new Sprit

注意:我使用的是传统的OpenGL

因此,我遇到了一个问题,即只有少数.png图像能够正确渲染

下面是渲染游戏的示例屏幕截图(左:enemyRed.png,右:(ship.png)):

下面是我用来加载图像的代码:

SpriteImage* SDLOpenGL::loadImage(std::string path) {
    SDL_Surface * surface = IMG_Load(path.c_str());
    SpriteImage * image = new SpriteImage(&*surface);

    return image;
}
SpriteImage::SpriteImage(SDL_Surface *surface) {
    this->surface = surface;
    this->TextureID = 0;
}

void SpriteImage::bind() {
    glTexImage2D(GL_TEXTURE_2D,
            0,
            this->mode,
            this->surface->w,
            this->surface->h,
            0,
            this->mode,
            GL_UNSIGNED_BYTE,
            this->surface->pixels
            );
    glBindTexture(GL_TEXTURE_2D, this->TextureID);
}

int SpriteImage::getWidth() {
    return this->surface->w; 
}

int SpriteImage::getHeight() {
    return this->surface->h;
}
这是精灵图像类:

SpriteImage* SDLOpenGL::loadImage(std::string path) {
    SDL_Surface * surface = IMG_Load(path.c_str());
    SpriteImage * image = new SpriteImage(&*surface);

    return image;
}
SpriteImage::SpriteImage(SDL_Surface *surface) {
    this->surface = surface;
    this->TextureID = 0;
}

void SpriteImage::bind() {
    glTexImage2D(GL_TEXTURE_2D,
            0,
            this->mode,
            this->surface->w,
            this->surface->h,
            0,
            this->mode,
            GL_UNSIGNED_BYTE,
            this->surface->pixels
            );
    glBindTexture(GL_TEXTURE_2D, this->TextureID);
}

int SpriteImage::getWidth() {
    return this->surface->w; 
}

int SpriteImage::getHeight() {
    return this->surface->h;
}
这里是我渲染图像的地方:

SpriteImage* SDLOpenGL::loadImage(std::string path) {
    SDL_Surface * surface = IMG_Load(path.c_str());
    SpriteImage * image = new SpriteImage(&*surface);

    return image;
}
SpriteImage::SpriteImage(SDL_Surface *surface) {
    this->surface = surface;
    this->TextureID = 0;
}

void SpriteImage::bind() {
    glTexImage2D(GL_TEXTURE_2D,
            0,
            this->mode,
            this->surface->w,
            this->surface->h,
            0,
            this->mode,
            GL_UNSIGNED_BYTE,
            this->surface->pixels
            );
    glBindTexture(GL_TEXTURE_2D, this->TextureID);
}

int SpriteImage::getWidth() {
    return this->surface->w; 
}

int SpriteImage::getHeight() {
    return this->surface->h;
}
(注意,
this->getCurrentImage()
返回一个“SpriteImage”)

  • 未渲染的图像的宽度不可分为四。您应该在
    glTexImage2D
    之前将
    glPixelStorei
    GL\u UNPACK\u对齐
    一起使用,以指定内存中可能未对齐的行的对齐方式(OpenGL默认假定它们是四字节对齐的)

  • 您应该首先
    glBindTexture
    ,然后使用
    glTexImage2D
    上传数据

  • 你有没有打电话给
    glGenTextures
    ?您应该使用
    glGenTextures
    初始化
    TextureID

  • 不应每次绑定纹理时都上载纹理。相反,将其上传到构造函数中,然后要切换纹理,只需
    glBindTexture
    您的
    TextureID

以下是图片: