来自SDL Surface的openGL纹理

时间:2013-09-06 21:56:11

标签: c++ opengl textures sdl

我从一个大项目中取出我的字体系统并将其修复以正常工作。将其插回到我的主项目中,现在字体无法正确呈现。我留下了白色框而不是字形纹理渲染。以下是与此问题相关的代码。

bool KFont::MapGlyph(char a) { 
    const char* T = &a;
    SDL_Surface* ThisGlyph;
    //Font is a struct that holds the font information Font->Font is the TTF pointer.
    if(!(ThisGlyph = TTF_RenderUTF8_Blended(Font->Font, T, GlyphColor))) {
        //std::cout << "!" << std::endl;
        return false;
    }

    KTexture* Temp;
    Temp = new KTexture;

    if(!(Temp->LoadImage(ThisGlyph))) {     
        //std::cout<< "!!" << std::endl;
        return false;
    }

    SDL_FreeSurface(ThisGlyph);
    Glyphs->Glyphs.insert( std::pair<char,KTexture*>(a,Temp) );
    return true;

}

LoadImage函数是:

bool KTexture::LoadImage(SDL_Surface* Surface) {

    glEnable(GL_TEXTURE_2D);
    if(TextureID > 0) {
        glDeleteTextures(1, &TextureID);
        TextureID = 0;
    }

    if(Surface == NULL) return false;

    GLenum Mode = 0;
    switch (Surface->format->BytesPerPixel) {
    case 1: {
        Mode = GL_ALPHA;
            break;
            }
    case 3: {
        Mode = GL_RGB;
        break;
            }
    case 4: {
        Mode = GL_RGBA;
        break;
            }
    default: { break;
             }
    }

    glGenTextures(1, &TextureID);

    if(TextureID & GL_INVALID_VALUE) exit(1);

    glBindTexture(GL_TEXTURE_2D, TextureID);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexImage2D(GL_TEXTURE_2D, 0, Mode, Surface->w, Surface->h, 0, Mode, GL_UNSIGNED_BYTE, Surface->pixels);

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

    _Width = Surface->w;
    _Height = Surface->h;

    glDisable(GL_TEXTURE_2D);
    return true;

}

在渲染过程中,字符是正确的,要渲染的X / Y位置是正确的,Surface-&gt; w / h都是一样的? TextureID始终保持为0,而在工作版本中它会更改。 glBindTexture()似乎有问题,但我已经多次查看了不同的代码,无法弄清楚为什么这不会渲染。

渲染功能,如果有帮助

void KTexture::RenderQuad(int x, int y, int w, int h) {
    glPushMatrix();
    glEnable(GL_TEXTURE_2D);

    if(TextureID & GL_INVALID_VALUE) exit(1);

    glBindTexture(GL_TEXTURE_2D,TextureID);

    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);

    glBegin(GL_QUADS);
        glTexCoord2f(0,0); glVertex3f(x,y,0);
        glTexCoord2f(1,0); glVertex3f(x+w,y,0);
        glTexCoord2f(1,1); glVertex3f(x+w,y+h,0);
        glTexCoord2f(0,1); glVertex3f(x,y+h,0);
    glEnd();

    glDisable(GL_TEXTURE_2D);
    glPopMatrix();
    glFlush();
}

这些是我的意思

if(SDL_Init(SDL_INIT_EVERYTHING) < 0 ) { 
        exit(0x1);
    }

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE,           8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,         8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,          8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,         8);

    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,        16);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,       32);

    SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,     8);
    SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE,   8);
    SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE,    8);
    SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE,   8);

    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);

    if((Surf_Display = SDL_SetVideoMode(winWidth,winHeight,32, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER |SDL_OPENGL | SDL_RESIZABLE )) == NULL) {
        exit(2);
    }

    glClearColor(0, 0, 0, 0);
    glClearDepth(1.0f);

    glViewport(0, 0, winWidth, winHeight);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho(0, winWidth, winHeight, 0, 1, -1);

    glMatrixMode(GL_MODELVIEW);
    glEnable (GL_BLEND); 

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glLoadIdentity();

知道为什么我的字体不会呈现?我已经尝试将glTexEnvf模式更改为decal,modulate和blend中没有任何效果。只是小白方块。

1 个答案:

答案 0 :(得分:2)

确保在KTexture::LoadImage()之后调用SDL_SetVideoMode(),以便它具有当前的GL上下文。