gpt4 book ai didi

c++ - 图片显示错误

转载 作者:搜寻专家 更新时间:2023-10-30 23:58:41 26 4
gpt4 key购买 nike

我在使用 C++ 中的 opengl 和 sdl 显示图像时看到一些奇怪的错误。这就是我所看到的:

There is a grey line at the top and around the contours of the structure.

顶部和结构轮廓周围有一条灰线。这不应该在那里。这是我要显示的图像:

The image is a png, the black background is added through opengl

图像是png,黑色背景是通过OpenGL添加的。

我把所有相关代码贴在下面,也许有人能告诉我为什么会出现这些错误?

这个函数用来显示图片:

renderTexture("modules/solarpanel/base.png", 10, 10, 1.0);

这是渲染纹理函数:

struct AbstTexture {
GLuint texture;
int w;
int h;
};

void renderTexture(std::string textureFile, int x, int y, float zoom) {
AbstTexture texture = loadTexture(textureFile);

glUseProgram(PROGRAM_SIMPLE_TEXTURE);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture.texture);

glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);


GLfloat box[] = {
(float) x, (float) y, 0, 0,
(float) x + (float) texture.w * zoom, (float) y, 1, 0,
(float) x, (float) y + (float) texture.h * zoom, 0, 1,
(float) x + (float) texture.w * zoom, (float) y + (float) texture.h * zoom, 1, 1,
};

glBufferData(GL_ARRAY_BUFFER, sizeof(box), box, GL_DYNAMIC_DRAW);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);


glBindBuffer(GL_ARRAY_BUFFER, 0);

glUseProgram(0);
};

这是 loadTexture 函数:

std::unordered_map<std::string, AbstTexture> loadedTextures;

AbstTexture loadTexture(std::string textureFile) {
AbstTexture texture;

std::unordered_map<std::string, AbstTexture>::const_iterator got = loadedTextures.find(textureFile);
if(got == loadedTextures.end()) {
std::string textureFilePath = "res/textures/" + textureFile;

SDL_Surface * loadSurface = IMG_Load(textureFilePath.c_str());

if(!loadSurface) {
std::cout << "SDL_Image load error: " << IMG_GetError() << std::endl;
}
if(loadSurface->format->BytesPerPixel < 2) {
std::cout << "Bad image - not true color!" << std::endl;
}

texture.w = loadSurface->w;
texture.h = loadSurface->h;

void * raw = (void *) malloc(texture.w * texture.h * 4);
Uint8 * dstPixel = (Uint8 *) raw;

SDL_LockSurface(loadSurface);

int bpp = loadSurface->format->BytesPerPixel;

Uint8 * srcPixel;
Uint32 truePixel;
for(int y = texture.h - 1; y >= 0; y--) {
for(int x = 0; x < texture.w; x++) {
srcPixel = (Uint8 *) loadSurface->pixels + y * loadSurface->pitch + x * bpp;
switch(bpp) {
case 1:
truePixel = *srcPixel;
break;
case 2:
truePixel = *(Uint16 *) srcPixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
truePixel = srcPixel[0] << 16 | srcPixel[1] << 8 | srcPixel[2];
} else {
truePixel = srcPixel[0] | srcPixel[1] << 8 | srcPixel[2] << 16;
}
break;
case 4:
truePixel = *(Uint32 *) srcPixel;
break;
default:
std::cout << "Image bpp of " << bpp << " unusable" << std::endl;
break;
}

SDL_GetRGBA(truePixel, loadSurface->format, &(dstPixel[0]), &(dstPixel[1]), &(dstPixel[2]), &(dstPixel[3]));

dstPixel += 4;
}
}

SDL_UnlockSurface(loadSurface);
SDL_FreeSurface(loadSurface);

while(glGetError()) {}

glGenTextures(1, &(texture.texture));
glBindTexture(GL_TEXTURE_2D, texture.texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

GLenum errorCode;
errorCode = glGetError();
if(errorCode != 0) {
if(errorCode == GL_OUT_OF_MEMORY) {
std::cout << "Out of texture memory!" << std::endl;
}
}

gluBuild2DMipmaps(GL_TEXTURE_2D, 4, texture.w, texture.h, GL_RGBA, GL_UNSIGNED_BYTE, (Uint8 *) raw);

errorCode = glGetError();
if(errorCode != 0) {
if(errorCode == GL_OUT_OF_MEMORY) {
std::cout << "Out of texture memory!" << std::endl;
}
}

loadedTextures.emplace(textureFile, texture);
} else {
texture = got->second;
}

return texture;
};

这是使用的 OpenGL 程序的配置:

GLuint PROGRAM_SIMPLE_TEXTURE = glCreateProgram();
GLuint vertexBufferObject;

glUseProgram(PROGRAM_SIMPLE_TEXTURE);
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(0);

最后是使用中的 OpenGL 着色器:

vertex shader: #version 330\n\nlayout(location = 0) in vec4 position;\nuniform vec2 screensize;\nvarying vec2 texcoord;\n\nvoid main() {\n  gl_Position = vec4(position.x / screensize.x * 2 - 1, (1 - position.y / screensize.y) * 2 - 1, 0, 1);\n texcoord = vec2(position.z, position.w * -1);\n}

fragment shader: "#version 330\n\nuniform vec4 color;\nuniform sampler2D tex;\nvarying vec2 texcoord;\n\nvoid main() {\n gl_FragColor = texture2D(tex, texcoord);\n}"

最佳答案

将 png 的透明部分设为黑色(但仍然透明)。 Opengl 将像素与其相邻像素混合,那些相邻像素虽然是透明的,但却是白色的,因此结果是灰色轮廓。

关于c++ - 图片显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19428480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com