I'm a beginner with OpenGL. After reading some tutorials i try to get freetype running for
text rendering. To increase my knowledge I've implemented a simple test, which should
only render a single char on a quad following the NeHe tutorial (43).
After Freetype initialization I create a data buffer with 2 byte image values from freetype
using this:
//Load the Glyph for our character.
if(FT_Load_Glyph(m_face, FT_Get_Char_Index( m_face, 'K' ), FT_LOAD_DEFAULT ))
throw std::runtime_error("FT_Load_Glyph failed");
//Move the face's glyph into a Glyph object.
FT_Glyph glyph;
if(FT_Get_Glyph( m_face->glyph, &glyph ))
throw std::runtime_error("FT_Get_Glyph failed");
//Convert the glyph to a bitmap.
FT_Glyph_To_Bitmap( &glyph, ft_render_mode_normal, 0, 1 );
bitmap_glyph = (FT_BitmapGlyph)glyph;
//This reference will make accessing the bitmap easier
bitmap=bitmap_glyph->bitmap;
//Create power of 2 texture
width = nextP2( bitmap.width );
height = nextP2( bitmap.rows );
//Allocate memory for the texture data.
expanded_data = new GLubyte[ 2 * width * height];
//Here we fill in the data for the expanded bitmap.
//Notice that we are using two channel bitmap (one for
//luminocity and one for alpha), but we assign
//both luminocity and alpha to the value that we
//find in the FreeType bitmap.
//We use the ?: operator so that value which we use
//will be 0 if we are in the padding zone, and whatever
//is the the Freetype bitmap otherwise.
for(int j=0; j <height;j++) {
for(int i=0; i < width; i++){
expanded_data[2*(i+j*width)] = expanded_data[2*(i+j*width)+1] =
(i>=bitmap_glyph->bitmap.width || j>=bitmap_glyph->bitmap.rows) ?
0 :
bitmap_glyph->bitmap.buffer[i + bitmap_glyph->bitmap.width*j];
}
}
And then I create a texture based on this buffer:
// Generate an alpha texture with it. GLuint texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); 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_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // use 2 Channel values. glTexImage2D( GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, width, height, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, data);
Finally try to render the texture on a single quad:
glEnalbe(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texture); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glEnd();
But I only get a black texture with some pixel disortion:

I searched the net and found many examples using this NeHe snipped. So I think it should working ...
Can anyone help?
Thanks,
Snoop












