纹理未映射到四边形

时间:2014-06-21 20:02:48

标签: c++ opengl sdl

我正在使用以下内容在OpenGL中绘制文本(使用SDL)

void renderText(const TTF_Font *font, const SDL_Color color,
                const double& x, const double& y, const double& z,  const std::string& text) {
    bool textured = glIsEnabled(GL_TEXTURE_2D);
    glEnable(GL_TEXTURE_2D);
    //test color: black
    glColor3f(0,0,0);
    SDL_Surface *Message = TTF_RenderText_Blended(const_cast<TTF_Font*>(font), text.c_str(), color);
    GLuint Texture = 0;

    //Generate an OpenGL 2D texture from the SDL_Surface
    glGenTextures(1, &Texture);
    glBindTexture(GL_TEXTURE_2D, Texture);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Message->w, Message->h, 0, GL_BGRA,
                 GL_UNSIGNED_BYTE, Message->pixels);

    //Draw this texture on a quad with the given xyz coordinates.
    glBegin(GL_QUADS);
        glTexCoord2d(0, 0); glVertex3d(x, y, z);
        glTexCoord2d(1, 0); glVertex3d(x+Message->w, y, z);
        glTexCoord2d(1, 1); glVertex3d(x+Message->w, y+Message->h, z);
        glTexCoord2d(0, 1); glVertex3d(x, y+Message->h, z);
    glEnd();

    //Clean up
    glDeleteTextures(1, &Texture);
    SDL_FreeSurface(Message);
    if (!textured)
        glDisable(GL_TEXTURE_2D);
}

然而,唯一显示的是(x,y)(在本例中为(10,10))的矩形,背景颜色设为{{ 1}}(在这种情况下为黑色)。从SDL_Surface创建的纹理未显示在四边形上。传递给函数的参数都是有效的:

glColor3f

这里有什么问题?


  • font: previously loaded TTF_Font != NULL color: SDL_Color {255,255,255} (white) x = 10 y = 10 z = 0 text = "test"
  • OpenGL v2.2
  • SDL v1.2.15
  • Kubuntu Raring x64

更新:当使用..._ Solid而不是...... _ Blended时,调用glOrtho(0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, -1.0, 10.0);并传递SDL_Color {255,255,255}会导致一些奇怪的事情:

enter image description here

四边形出现在应该出现的位置,并显示奇怪的内容。怎么了?

1 个答案:

答案 0 :(得分:0)

问题是我在调用上述方法之前没有调用glBlendFunc。因此OpenGL的混合模式设置错误。使用glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);后,它看起来很好。