C++ SDL2(C+;+;)并非所有.png图像都能正确渲染
注意:我使用的是传统的OpenGL 因此,我遇到了一个问题,即只有少数.png图像能够正确渲染 下面是渲染游戏的示例屏幕截图(左:enemyRed.png,右:(ship.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
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
一起使用,以指定内存中可能未对齐的行的对齐方式(OpenGL默认假定它们是四字节对齐的)GL\u UNPACK\u对齐
- 您应该首先
,然后使用glBindTexture
上传数据glTexImage2D
- 你有没有打电话给
?您应该使用glGenTextures
初始化glGenTextures
TextureID
- 不应每次绑定纹理时都上载纹理。相反,将其上传到构造函数中,然后要切换纹理,只需
您的glBindTexture
TextureID