Results 1 to 4 of 4
  1.    #1  
    I'm working on leaning OpenGL for an app I'm writing. I think I managed to figure out how to draw what I need to draw, and how to make it look right. However, I'm stumped on the best way to do text.

    In my app, I'm going to have to have some text display for labels and other such things, and I can't know in advanced what it will say. The only options I've seen so far is either pre-rendering all your strings as textures, or trying to roll your own font rendering engine.

    I had figured out how to draw text on an SDL surface, but as far as I can tell, I can't mix 2D SDL with OpenGL. The text I want to render can be flat on the display, so drawing directly on the surface would be ok, if that could work. I'd rather not have to render a font to a texture and pick out exactly which character to display.

    What is the suggested method of font rendering in WebOS in an OpenGL app?
  2. #2  
    I don't know the exact methods (having not done anything in OpenGL), but you can take your 2D SDL text surface and convert it to an OpenGL texture and then draw the OpenGL texture to the screen.

    Again, I don't know the exact method to do so, as I haven't done anything in OpenGL, but I know that it is possible.
    Arthur Thornton

    Former webOS DevRel Engineer at Palm, HP, and LG
    Former webOS app developer (built Voice Memos, Sparrow, and several homebrew apps and patches)
    Former blogger for webOS Nation and webOS Roundup
  3. philbw's Avatar
    Posts
    310 Posts
    Global Posts
    352 Global Posts
    #3  
    Yes that is correct. You can draw text to an SDL surface and simply use it's pixel data in the GL texture. This will work great if you plan to stay just on WebOS or other SDL friendly platforms. However if you wish to go cross platform (iPhone, Android, etc) you'd be better off utilizing "bitmap fonts". I stumbled across a program the other day (cant recall what it's called) that basically converts any old font into a series of TGA images that you can use in OpenGL. Either way you'll end up whipping up a function that converts a string of characters to a series of sprites with textures on them.

    - Phil -
  4. #4  
    Here is the general code I use to do what you are talking about.

    Code:
    SDL_Surface *pSurf = SDL_CreateRGBSurface(
    	SDL_SWSURFACE,
    	LENGTH_OF_SURFACE, 
    	WIDTH_OF_SURFACE,
    	32,
    #if SDL_BYTEORDER == SDL_LIL_ENDIAN // OpenGL RGBA masks
    	0x000000FF, 
    	0x0000FF00, 
    	0x00FF0000, 
    	0xFF000000
    #else
    	0xFF000000,
    	0x00FF0000, 
    	0x0000FF00, 
    	0x000000FF
    #endif
    	);
    //-------------------------
    // Add the text to the SDL surface here
    //-------------------------
    glBindTexture(GL_TEXTURE_2D, [HERE YOU PUT THE ID FOR THE TEXTURE]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D,
    	0,
    	 GL_RGBA,
    	LENGTH_OF_SURFACE, 
    	WIDTH_OF_SURFACE,
    	0,
    	GL_RGBA,
    	GL_UNSIGNED_BYTE,
    	pSurf->pixels);
    
    
    SDL_FreeSurface(pSurf);
    And then to draw you would just need to set the u and v in the texture coord vector to only draw the portion you want.

    Also, I would recommend NFont (NFont - Jonny D's Projects) for the SDL text blting, to save you from having to roll your own.

Posting Permissions