从SDL_TTF创建的SDL2曲面中提取像素

时间:2018-03-08 17:25:36

标签: c++ opengl sdl sdl-2 sdl-ttf

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

一切正常,除了文本最终看起来像这样(文本应该是“测试”)

text

我的问题:我是否以某种方式弄乱数学并自己做,或者这只是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...

也(重要的部分从我声明起始变量的地方开始)

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);
}

1 个答案:

答案 0 :(得分:1)

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

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

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

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

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

使用浮点数来表示像素数据是非常奇怪的,因为它除了加载速度慢之外没有任何内容。