From 4c3f6004df1c4b3aba546f43857122c5dbb1a0d0 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 9 Dec 2003 11:31:33 +0000 Subject: [PATCH] Moved osgText across to create GL_ALPHA textures instead of GL_LUMINANCE_ALPHA --- include/osgText/Font | 3 ++ src/osgPlugins/freetype/FreeTypeFont.cpp | 40 ++++++++++++++- src/osgText/DefaultFont.cpp | 65 +++++++++++++++++------- src/osgText/Font.cpp | 4 ++ 4 files changed, 93 insertions(+), 19 deletions(-) diff --git a/include/osgText/Font b/include/osgText/Font index 9eacd62cd..a6ca7c345 100644 --- a/include/osgText/Font +++ b/include/osgText/Font @@ -19,6 +19,7 @@ #include #include #include +#include #include @@ -137,6 +138,8 @@ protected: osg::Texture::FilterMode _minFilterHint; osg::Texture::FilterMode _magFilterHint; + osg::ref_ptr _texEnv; + osg::ref_ptr _implementation; diff --git a/src/osgPlugins/freetype/FreeTypeFont.cpp b/src/osgPlugins/freetype/FreeTypeFont.cpp index 8fed89968..aa274d69a 100644 --- a/src/osgPlugins/freetype/FreeTypeFont.cpp +++ b/src/osgPlugins/freetype/FreeTypeFont.cpp @@ -79,6 +79,10 @@ osgText::Font::Glyph* FreeTypeFont::getGlyph(unsigned int charcode) osg::ref_ptr glyph = new osgText::Font::Glyph; + + +//#define USE_LUMINANCE_ALPHA +#ifdef USE_LUMINANCE_ALPHA unsigned int dataSize = width*height*2; unsigned char* data = new unsigned char[dataSize]; @@ -92,7 +96,7 @@ osgText::Font::Glyph* FreeTypeFont::getGlyph(unsigned int charcode) data, osg::Image::USE_NEW_DELETE, 1); - + // skip the top margin data += (margin*width)*2; @@ -109,7 +113,41 @@ osgText::Font::Glyph* FreeTypeFont::getGlyph(unsigned int charcode) } data+=2*margin; // skip the right margin. } +#else + unsigned int dataSize = width*height; + unsigned char* data = new unsigned char[dataSize]; + + // clear the image to zeros. + for(unsigned char* p=data;psetImage(width,height,1, + GL_ALPHA, + GL_ALPHA,GL_UNSIGNED_BYTE, + data, + osg::Image::USE_NEW_DELETE, + 1); + + glyph->setInternalTextureFormat(GL_ALPHA); + + // skip the top margin + data += (margin*width); + + // copy image across to osgText::Glyph image. + for(int r=sourceHeight-1;r>=0;--r) + { + data+=margin; // skip the left margin + + unsigned char* ptr = buffer+r*pitch; + for(unsigned int c=0;cmetrics); glyph->setHorizontalBearing(osg::Vec2((float)metrics->horiBearingX/64.0f,(float)(metrics->horiBearingY-metrics->height)/64.0f)); // bottom left. diff --git a/src/osgText/DefaultFont.cpp b/src/osgText/DefaultFont.cpp index 5616be481..9f0fed4fa 100644 --- a/src/osgText/DefaultFont.cpp +++ b/src/osgText/DefaultFont.cpp @@ -195,10 +195,10 @@ void DefaultFont::constructGlyphs() unsigned int sourceWidth = 8; unsigned int sourceHeight = 12; +//#define USE_LUMINANCE_ALPHA +#ifdef USE_LUMINANCE_ALPHA _width = sourceWidth+2*_margin; _height = sourceHeight+2*_margin; - - // populate the glyph mp for(unsigned int i=32;i<127;i++) @@ -229,22 +229,6 @@ void DefaultFont::constructGlyphs() for(unsigned int row=0;row glyph = new Glyph; + unsigned int dataSize = _width*_height; + unsigned char* data = new unsigned char[dataSize]; + + // clear the image to zeros. + for(unsigned char* p=data;psetImage(_width,_height,1, + GL_ALPHA, + GL_ALPHA,GL_UNSIGNED_BYTE, + data, + osg::Image::USE_NEW_DELETE, + 1); + + glyph->setInternalTextureFormat(GL_ALPHA); + + // now populate data arry by converting bitmap into a luminance_alpha map. + unsigned char* ptr = rasters[i-32]; + unsigned char value_on = 255; + unsigned char value_off = 0; + + // skip the top margin + data += (_margin*_width); + + for(unsigned int row=0;rowsetHorizontalBearing(osg::Vec2(0.0f,0.0f)); // bottom left. glyph->setHorizontalAdvance((float)_width); diff --git a/src/osgText/Font.cpp b/src/osgText/Font.cpp index baee04865..edc99f6de 100644 --- a/src/osgText/Font.cpp +++ b/src/osgText/Font.cpp @@ -17,6 +17,8 @@ #include #include +#include + #include #include #include @@ -87,6 +89,7 @@ Font::Font(FontImplementation* implementation): _magFilterHint(osg::Texture::LINEAR) { setImplementation(implementation); + _texEnv = new osg::TexEnv(osg::TexEnv::BLEND); } Font::~Font() @@ -259,6 +262,7 @@ void Font::addGlyph(unsigned int width, unsigned int height, unsigned int charco glyphTexture->setStateSet(stateset); stateset->setMode(GL_BLEND,osg::StateAttribute::ON); stateset->setTextureAttributeAndModes(0,glyphTexture,osg::StateAttribute::ON); + if (_texEnv.valid()) stateset->setTextureAttribute(0,_texEnv.get()); stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); if (!glyphTexture->getSpaceForGlyph(glyph,posX,posY))