[英]Stb_image creates weird artifacts
Hello I am making a image and texture class for my openGL project and when I added stb_image some textures just start adding noise at the edge of the image.您好,我正在为我的 openGL 项目制作图像和纹理 class,当我添加 stb_image 时,一些纹理开始在图像边缘添加噪声。
Image class:图片 class:
Image::Image() {
width = 0;
height = 0;
channels = 0;
data = NULL;
}
Image::Image(const Image& image) {
width = image.width;
height = image.height;
channels = image.channels;
int allocation = image.width * image.height * image.channels;
data = new unsigned char[allocation];
std::memcpy(data, image.data, allocation);
}
Image& Image::operator=(const Image& image) {
if (this != &image && image.data != nullptr) {
if(data != NULL)
delete[] data;
data = NULL;
int allocation = image.width * image.height * image.channels;
data = new unsigned char[allocation];
std::memcpy(data, image.data, allocation);
width = image.width;
height = image.height;
channels = image.channels;
filepath = image.filepath;
}
return *this;
}
Image::Image(const std::string& _filepath) {
width = 0;
height = 0;
channels = 0;
data = NULL;
filepath = _filepath;
data = stbi_load(filepath.c_str(), &width, &height, &channels, STBI_rgb_alpha);
if (data == NULL) {
Debug::systemErr("Couldn't load image: " + filepath);
}
}
Image::~Image() {
if (data != NULL) {
delete[] data;
}
data = NULL;
}
bool Image::hasData() const{
return (data != NULL);
}
Texture class:纹理 class:
Texture::Texture() {
textureID = 0;
}
Texture::Texture(const Texture& texture) {
image = Image(texture.image);
textureID = 0;
}
Texture::Texture(const std::string& path) {
image = Image(path);
textureID = 0;
}
Texture& Texture::operator=(const Texture& texture) {
textureID = texture.textureID;
image = texture.image;
return *this;
}
Texture::~Texture() {
destroy();
}
void Texture::destroy() {
glDeleteTextures(1, &textureID);
}
void Texture::create() {
if (!isCreated && image.hasData()) {
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.width, image.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image.data);
glGenerateMipmap(GL_TEXTURE_2D);
isCreated = true;
}
}
Image is 128x128 pixels图像为 128x128 像素
Here is what it looks like in paint.net:这是它在paint.net中的样子:
Here is what it looks like when being rendered:这是渲染时的样子:
I believe the image loading is fine because I set the icon of the window with the image class and that looks fine.我相信图像加载很好,因为我用图像 class 设置了 window 的图标,看起来很好。 I think the problem stems from the texture class.
我认为问题源于纹理 class。
SOLVED to Jérôme Richard for solving this for me.向Jérôme Richard 解决了这个问题。 The reason this didn't work was because even though I put rgba for the channels, the image still came back as rgb.
这不起作用的原因是,即使我将 rgba 用于通道,图像仍然以 rgb 形式返回。 Then when I try to copy the data, instead of doing 4 * width * height for the size.
然后当我尝试复制数据时,而不是做 4 * width * height 的大小。 I would do 3 * width * height.
我会做 3 * 宽度 * 高度。 So after stbi_load(), I just put channels = 4.
所以在 stbi_load() 之后,我只放了 channels = 4。
You should read stbi documentation more carefully.您应该更仔细地阅读 stbi 文档。 It's right there at the start:
它在一开始就在那里:
int x,y,n; unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
... replace '0' with '1'..'4' to force that many components per pixel
... 将 '0' 替换为 '1'..'4' 以强制每个像素有那么多分量
... but 'n' will always be the number that it would have been if you said 0...但是“n”将始终是您说 0 时的数字
Therefore if you pass anything other than 0
as the last argument, that's the number of channels that you will get.因此,如果您将
0
以外的任何值作为最后一个参数传递,这就是您将获得的通道数。 You should ignore n
in such case and go by the number of channels you requested.在这种情况下,您应该忽略
n
并按您请求的通道数忽略 go。 Your code, on the other hand, does it the other way: it requests four channels, but then uses the returned n
(in your code it's called channels
) as the source of truth.另一方面,您的代码以另一种方式执行此操作:它请求四个通道,但随后使用返回的
n
(在您的代码中称为channels
)作为事实来源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.