c++ - 为什么转换 SDL Surface 在 Surface 从文件加载时有效,但在 SDL Surface 从字体文件生成时无效

标签 c++ opengl sdl-2

在我的第二个项目中,我使用 opengl 和 SDL2。我想使用 SDL2 将文本渲染到表面,然后进行转换。

我已经在使用 SDL 从文件中加载纹理,所以我有一个可以转换曲面的函数。当我提供由 TTF_Render 函数生成的表面时,结果仍然是这样的: enter image description here

现在我不知道为什么会这样,所以希望得到一些支持。

我正在使用 SDL_ttf 库加载文件。 我使用 visual studio 2015 作为 ide。

这是我的转换函数:

GLuint JUMA_Texture::loadFromSurface(SDL_Surface *img, GLenum target, GLenum filtering) {
    GLuint TextureID = 0;
    glGenTextures(1, &TextureID);
    glBindTexture(GL_TEXTURE_2D, TextureID);
    std::cout << " got " << img;
    if (target == GL_TEXTURE_2D)
    {
        glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);   // Set texture wrapping to GL_REPEAT
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    }
    int Mode = GL_RGB;
    if (img->format->BytesPerPixel == 4) {
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        transparent = true;
        Mode = GL_RGBA;
    }

    glTexImage2D(GL_TEXTURE_2D, 0, Mode, img->w, img->h, 0, Mode, GL_UNSIGNED_BYTE, img->pixels);
    glGenerateMipmap(target);
    return TextureID;
}

我在发送到函数之前和之后打印了表面指针的地址,它们看起来完全相同。

另外,我正在使用 SDL_SaveBmp 将表面写入文件,看起来也不错。

编辑:

应要求,我现在将发布使用函数的代码以及我的着色器和纹理类的构造函数。

着色器构造函数/加载器

printf("Loading shader");
    // 1. Retrieve the vertex/fragment source code from filePath
    std::string vertexCode;
    std::string fragmentCode;
    std::ifstream vShaderFile;
    std::ifstream fShaderFile;
    strcpy_s(Fragpath, fragmentPath);
    strcpy_s(Vertexpath, vertexPath);

    printf(".");
    // ensures ifstream objects can throw exceptions:
    vShaderFile.exceptions(std::ifstream::badbit);
    fShaderFile.exceptions(std::ifstream::badbit);
    try
    {
        // Open files
        vShaderFile.open(vertexPath);
        fShaderFile.open(fragmentPath);
        std::stringstream vShaderStream, fShaderStream;
        // Read file's buffer contents into streams
        vShaderStream << vShaderFile.rdbuf();
        fShaderStream << fShaderFile.rdbuf();
        // close file handlers
        vShaderFile.close();
        fShaderFile.close();
        // Convert stream into string
        vertexCode = vShaderStream.str();
        fragmentCode = fShaderStream.str();
        printf(".");
    }
    catch (std::ifstream::failure e)
    {
        std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
    }
    const GLchar* vShaderCode = vertexCode.c_str();
    const GLchar * fShaderCode = fragmentCode.c_str();
    // 2. Compile shaders
    GLuint vertex, fragment;
    GLint success;
    GLchar infoLog[512];
    // Vertex Shader
    vertex = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex, 1, &vShaderCode, NULL);
    glCompileShader(vertex);
    // Print compile errors if any
    glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
    printf(".");
    if (!success)
    {
        glGetShaderInfoLog(vertex, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    // Fragment Shader
    fragment = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment, 1, &fShaderCode, NULL);
    glCompileShader(fragment);
    // Print compile errors if any
    glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(fragment, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    printf(".");
    // Shader Program
    this->Program = glCreateProgram();
    glAttachShader(this->Program, vertex);
    glAttachShader(this->Program, fragment);
    glLinkProgram(this->Program);
    // Print linking errors if any
    glGetProgramiv(this->Program, GL_LINK_STATUS, &success);
    if (!success)
    {
        glGetProgramInfoLog(this->Program, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
    }
    printf(".");
    // Delete the shaders as they're linked into our program now and no longer necessery
    glDeleteShader(vertex);
    glDeleteShader(fragment);
    printf("done\n");

Shader使用函数

void JUMA_Shader::Use()
{
    glUseProgram(this->Program);
}

纹理构造器

printf("loading Texture");
    int pos;
    pos = filePath.find(".");
    if (filePath.substr(pos) == ".png") {

        IMG_Init(IMG_INIT_PNG);
    }
    else if (filePath.substr(pos) == ".jpg")
        IMG_Init(IMG_INIT_JPG);

    SDL_Surface *img = IMG_Load(filePath.c_str());
    id = loadFromSurface(img,target,filtering);

Texture::loadFromSurface(注意:我还没有对 GL_BGR 和 GL_BGRA 进行检查,但是我手动尝试了这两种模式,此时没有任何不同的效果。)

GLuint JUMA_Texture::loadFromSurface(SDL_Surface *img, GLenum target, GLenum filtering) {
    GLuint TextureID = 0;
    glGenTextures(1, &TextureID);
    glBindTexture(GL_TEXTURE_2D, TextureID);
    std::cout << " got " << img;
    if (target == GL_TEXTURE_2D)
    {
        glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);   // Set texture wrapping to GL_REPEAT
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    }
    int Mode = GL_RGB;
    if (img->format->BytesPerPixel == 4) {
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        transparent = true;
        Mode = GL_RGBA;
    }

    glTexImage2D(GL_TEXTURE_2D, 0, Mode, img->w, img->h, 0, Mode, GL_UNSIGNED_BYTE, img->pixels);
    glGenerateMipmap(target);
    return TextureID;
}

纹理::使用

int JUMA_Texture::use(GLenum Channel) {
    glActiveTexture(Channel);   // Activate the texture unit first before binding texture
    glBindTexture(GL_TEXTURE_2D, id);

    if (transparent)
    {
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    }
    return 1;
}

这也是我现在看到的(请注意,我再次检查过,这不是我之前所说的为我的字体设置的颜色。我不知道这种蓝色是从哪里来的。我目前正在尝试弄清楚这一点) enter image description here

最后是我的顶点着色器:

#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 texcoords;
out vec2 texCoords;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
out vec4 pos;
void main()
{
    pos=gl_Position = projection*view*model*vec4(position,1.0f);
    texCoords=vec2(texcoords.xy);
};

还有我的片段着色器:

#version 330 core
out vec4 color;
in vec2 texCoords;
in vec4 pos;
uniform sampler2D ourTexture;
uniform vec4 mixCol;

void main()
{
    vec4 texColor = texture(ourTexture, texCoords);
    if(texColor.a < 0.1f)
         discard;

    color = texColor;
};

我的字体构造器

    JUMA_Font::JUMA_Font(char *path, int size, SDL_Color color, char* text, char *uniform) {
    TTF_Init();
    font = TTF_OpenFont(path, 40);
    sourceSurface = TTF_RenderText_Blended(font, text, color);
    printf("%d %d %d %d", color.r, color.g, color.b, color.a);
};

将字体转换为纹理

JUMA_Texture convertToTexture() {
        JUMA_Texture _this;
        SDL_SaveBMP(sourceSurface, "out.bmp"); //outputs font correctly
        std::cout << "Expected " << sourceSurface;
        _this.id = _this.loadFromSurface(sourceSurface,GL_TEXTURE_2D,GL_NEAREST);

        return _this;
    }

最佳答案

这看起来像是您使用 TTF_Render*_Solid()TTF_Render*_Shaded() 生成的表面。 然而,这些功能return an 8-bit palettized surface , OpenGL 不理解。 这在您的函数中未检测到,您将源格式 (Mode) 设置为 GL_RGB 或 GL_RGBA,在这种情况下两者都是不正确的。

作为解决方案,请确保使用 TTF_Render*_Blended() 函数之一以混合模式呈现文本。 这些将创建一个完整的 BGRA 表面,然后可以与 glTexImage2D() 一起使用。

另请注意 it is recommendedglTexImage2D() 中使用特定的内部格式,因此函数调用应如下所示:

glTexImage2D(GL_TEXTURE_2D, 0,
  GL_RGBA8,
  img->w, img->h, 0,
  GL_BGRA,
  GL_UNSIGNED_BYTE, img->pixels);

关于c++ - 为什么转换 SDL Surface 在 Surface 从文件加载时有效,但在 SDL Surface 从字体文件生成时无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42946546/

相关文章:

c++ - 使用类和模板扩展模板类

c++ - OpenCV imwrite保存完整的黑色jpeg

c++ - Reserve() - 空 vector 上的 data() 技巧 - 这是正确的吗?

c++ - 如何在 C++ 中构建包含主要入口点(如 SDL)的库

c++ - 声明具有两种类型的变量 : "int char"

c++ - OpenGL Redbook loadShaders 不会加载着色器

java - 在最新的 OpenGL 中旋转

c++ - 如何在视网膜显示器上使用 QPainter、QOpenGLPaintDevice 和 QOpenGLWidget 流畅地绘制?

c++ - 为什么我在所有像素的双 for 循环中使用 SDL2 和 SDL_RenderCopy 时性能不佳?

opengl - 可移植 YUV 绘图上下文