gpt4 book ai didi

c++ - sdl_ttf 填充不良的表面

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:55 24 4
gpt4 key购买 nike

我正在尝试用 C++ 制作一个 openGL 游戏,并且我正在尝试实现一个文本系统,为此,我正在尝试使用 SDL_ttf。

我已经在另一个项目中使用了 SDL_ttf,但使用了另一个 api,所以我编写了相同的代码,但它恰好没有填充表面的像素数据。

这是我的代码:


void Text2Texture::setText(const char * text, size_t fontIndex){

SDL_Color c = {255, 255, 0, 255};
SDL_Surface * surface;

surface = TTF_RenderUTF8_Blended(loadedFonts_[fontIndex], text, c);
if(surface == nullptr) {
fprintf(stderr, "Error TTF_RenderText\n");
return;
}

GLenum texture_format;
GLint colors = surface->format->BytesPerPixel;

if (colors == 4) { // alpha
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA_EXT;
} else { // no alpha
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR_EXT;
}


glBindTexture(GL_TEXTURE_2D, textureId_);
glTexImage2D(GL_TEXTURE_2D, 0, colors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels);

///This line tell me pixel data is 8 bit witch isn't good ?
std::cout << "pixel size : " << sizeof(surface->pixels) << std::endl;

///This line give me correct result
fprintf(stderr, "texture size : %d %d\n", surface->w, surface->h);

glBindTexture(GL_TEXTURE_2D, 0);

}

正如您在评论中看到的,表面中的指针像素大小为 8 位,这对于纹理来说太低了。我不知道它为什么要那样做。

最后,纹理数据看起来完全被 0 填充(结果是黑色小队使用非常基本的着色器)。

在这个项目中,我使用 glfw 创建一个 openGL 上下文,所以我没有使用 sdl,也没有初始化它。

但是,我确实初始化了 sdl_ttf,这是我在调用 setText 之前所做的一切:


std::vector<TTF_Font *> Text2Texture::loadedFonts_;

void Text2Texture::init(){

if(TTF_Init() == -1) {
fprintf(stderr, "TTF_Init: %s\n", TTF_GetError());
}

}

int Text2Texture::loadFont(std::string const& fontPath){

loadedFonts_.emplace_back();

loadedFonts_.back() = TTF_OpenFont(fontPath.data(), 32);

if( loadedFonts_.back() == nullptr ) {
fprintf(stderr, "TTF_OpenFont: %s \n", TTF_GetError());
loadedFonts_.pop_back();
return -1;
}

return ((int)loadedFonts_.size() - 1);

}

///The constructor initialize the texture :

Text2Texture::Text2Texture(){

glGenTextures(1, &textureId_);
glBindTexture(GL_TEXTURE_2D, textureId_);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_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);

}

我的类(class)有一个静态部分,它是公司:


class Text2Texture {

public:

Text2Texture();

void setText(const char * text, size_t fontIndex = 0);

unsigned int getId() const;

//Partie static
static void init();

static void quit();

static int loadFont(std::string const& fontPath);

private:

unsigned int textureId_;

//Partie static
static std::vector<TTF_Font *> loadedFonts_;

};

我初始化 sdl_ttf 并使用静态方法加载纹理,然后我创建类实例来创建特定纹理。

如果您发现我的错误在哪里,我很乐意阅读您的回答。

(顺便说一下,我不确定使用 sdl_ttf 是不是好方法,如果你有更好的主意我也会接受,但我想先解决这个问题)

最佳答案

glTexImage2D的格式和类型参数指定单个像素的编码方式。
创建纹理字体时,每个像素都被编码为一个字节。这意味着您的纹理由单个颜色 channel 组成,每个像素有 1 个字节。我非常确定 colors = surface->format->BytesPerPixel 是 1。请注意,在一个颜色 channel 中对字形进行编码就足够了,因为字形包含适合单个字节的信息。

默认情况下,OpenGL 假定图像的每一行的开始对齐 4 个字节。这是因为 GL_UNPACK_ALIGNMENT参数默认为 4。由于图像有 1 个(红色)颜色 channel ,并且排列紧密,一行的开头可能未对齐。
更改 GL_UNPACK_ALIGNMENT参数为 1,在指定二维纹理图像 (glTexImage2D) 之前。

由于纹理只有一个(红色)颜色 channel ,因此当查找纹理时,绿色和蓝色将为 0,alpha channel 将为 1。但是您也可以将绿色、蓝色甚至 alpha channel 视为从红色 channel 读取。这可以通过分别设置纹理混合参数 GL_TEXTURE_SWIZZLE_GGL_TEXTURE_SWIZZLE_B GL_TEXTURE_SWIZZLE_A 来实现。参见 glTexParameter .

此外,请注意纹理参数存储在纹理对象中。 glTexParameter 更改当前绑定(bind)到当前纹理单元指定目标的纹理对象。所以在创建纹理图像时设置一次参数就足够了。
相比之下,glPixelStore更改全局状态 ma 必须在指定纹理图像后设置为其默认值(如果以后调用 glTexImage2D 依赖它)。

二维纹理图像的规范和参数设置可能如下所示:

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, surface->w, surface->h, 0,
GL_RED, GL_UNSIGNED_BYTE, surface->pixels);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_RED);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_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);

关于c++ - sdl_ttf 填充不良的表面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57938402/

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