从使用 SDL_TTF 创建的 SDL2 表面提取像素

Extracting Pixels From SDL2 Surface Created With SDL_TTF

我正在开发一个使用 http://www.fontspace.com/work-ins-studio/variane-script 创建 SDL_Surface 的程序。我将表面的背景设置为透明。然后我从表面提取像素,并将它们放置在 opengl 纹理的一部分中。

一切正常,只是文本最终看起来像这样(文本应显示为 "testing")

我的问题:我是否以某种方式搞砸了数学并自己做了这个,或者这只是 SDL_TTF 的行为?而且,如果它只是 SDL_TTF 的行为,我该如何解决它以获得我可以使用的像素数据?

相关代码如下:

int main(int argc, char* args[]) {
//other sdl and opengl overhead stuff here...
TTF_Init();
//shader setup here...
TTF_Font *font;
font = TTF_OpenFont("VarianeScript.ttf", 50);
SDL_Surface* surface;
SDL_Color color = { 255, 0, 0 };
surface = TTF_RenderText_Solid(font, "testing", color);
SDL_SetSurfaceAlphaMod(surface, 255);
int surfaceWidth = surface->w;
int surfaceHeight = surface->h;
Uint8 red, green, blue, alpha;
float* textImage = new float[(surfaceWidth * surfaceHeight) * 4];
int countText = 0;
SDL_LockSurface(surface);
Uint8* p = (Uint8*)surface->pixels;
for (int y = 0; y < surfaceHeight; ++y) {
    for (int x = 0; x < (surfaceWidth); ++x) {
        Uint8 pixel = p[(y * surface->w) + x];
        SDL_GetRGBA(pixel, surface->format, &red, &green, &blue, &alpha);
        textImage[countText] = ((float)red / 255.0f);
        ++countText;
        textImage[countText] = ((float)green / 255.0f);
        ++countText;
        textImage[countText] = ((float)blue / 255.0f);
        ++countText;
        textImage[countText] = ((float)alpha / 255.0f);
        ++countText;
    }
}
SDL_UnlockSurface(surface);
SDL_FreeSurface(surface);
GLuint texture;
float* image;
int width = 1000, height = 1000;
int textX = width - (int)(width / 1.5);
int textY = height - (int)(height / 1.5);
setupTexture(texture, shader, width, height, image, textImage, textX, textY, surfaceWidth, surfaceHeight);
//etc...

还有(重要的部分从我声明 startpos 变量的地方开始)

void setupTexture(GLuint &texture, Shader &shader, int &width, int &height, float* &image, float* text, int textX, int textY, int textW, int textH) {
glGenTextures(1, &texture);
image = new float[(width * height) * 3];
for (int a = 0; a < (width * height) * 3; ) {
    if (a < ((width * height) * 3) / 2) {
        image[a] = 0.5f;
        ++a;
        image[a] = 1.0f;
        ++a;
        image[a] = 0.3f;
        ++a;
    }
    else {
        image[a] = 0.0f;
        ++a;
        image[a] = 0.5f;
        ++a;
        image[a] = 0.7f;
        ++a;
    }
}
int startpos1, startpos2;
for(int y = 0; y < textH; ++y) {
    for(int x = 0; x < textW; ++x) {
        startpos1 = (((y + textY) * width) * 3) + ((x + textX) * 3);
        startpos2 = ((y * textW) *4) + (x * 4);
        if (text[startpos2 + 3] != 0.0) {
            image[startpos1] = text[startpos2];
            image[startpos1 + 1] = text[startpos2 + 1];
            image[startpos1 + 2] = text[startpos2 + 2];
        }
    }
}
glActiveTexture(GL_TEXTURE0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT, image);
glUniform1i(glGetUniformLocation(shader.shaderProgram, "texSampler"), 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}

您的问题在于从表面提取像素的方式:

Uint8 pixel = p[(y * surface->w) + x];

您假设每个像素占用一个字节(可以通过检查 surface->format->BytesPerPixel 来验证),并且每行的长度为 surface->w*1 字节 - 但事实并非如此。相反,每一行都是 surface->pitch 字节长,因此您的代码应该是

Uint8 pixel = p[y * surface->pitch + x];

(仍然假设它是 1 个字节长,但这不是重点)。

您使用浮点数来表示像素数据很奇怪,因为除了加载速度慢得多之外,它在这里什么也没有给您。