我看到一些奇怪的错误在 c++ 中显示带有 opengl 和 sdl 的图像。这就是我所看到的:
顶部和结构轮廓周围有一条灰线。这不应该在那里。这是我要显示的图像:
图片是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}"