2003-06-24 23:40:09 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
|
2001-11-12 18:04:57 +08:00
|
|
|
*
|
2003-03-03 05:05:05 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2001-11-12 18:04:57 +08:00
|
|
|
|
|
|
|
#include <osgText/Font>
|
2003-03-04 20:34:42 +08:00
|
|
|
#include <osgText/Text>
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
#include <osg/State>
|
2001-11-19 18:40:42 +08:00
|
|
|
#include <osg/Notify>
|
2003-03-03 05:05:05 +08:00
|
|
|
#include <osgDB/ReadFile>
|
2003-03-04 00:42:48 +08:00
|
|
|
#include <osgDB/FileUtils>
|
2003-03-03 05:05:05 +08:00
|
|
|
#include <osg/GLU>
|
2001-11-12 18:04:57 +08:00
|
|
|
|
|
|
|
using namespace osgText;
|
2003-04-14 23:02:13 +08:00
|
|
|
using namespace std;
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2003-03-04 00:42:48 +08:00
|
|
|
std::string findFontFile(const std::string& str)
|
|
|
|
{
|
|
|
|
// try looking in OSGFILEPATH etc first for fonts.
|
|
|
|
std::string filename = osgDB::findDataFile(str);
|
|
|
|
if (!filename.empty()) return std::string(filename);
|
|
|
|
|
|
|
|
|
|
|
|
static osgDB::FilePathList s_FontFilePath;
|
|
|
|
static bool initialized = false;
|
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
initialized = true;
|
|
|
|
#if defined(WIN32)
|
|
|
|
osgDB::Registry::convertStringPathIntoFilePathList(
|
|
|
|
".;C:/winnt/fonts;C:/windows/fonts",
|
|
|
|
s_FontFilePath);
|
|
|
|
|
|
|
|
char *ptr;
|
|
|
|
if ((ptr = getenv( "windir" )))
|
|
|
|
{
|
|
|
|
s_FontFilePath.push_back(ptr);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
osgDB::Registry::convertStringPathIntoFilePathList(
|
|
|
|
".:/usr/share/fonts/ttf:/usr/share/fonts/ttf/western:/usr/share/fonts/ttf/decoratives",
|
|
|
|
s_FontFilePath);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = osgDB::findFileInPath(str,s_FontFilePath);
|
|
|
|
if (!filename.empty()) return filename;
|
|
|
|
|
|
|
|
osg::notify(osg::WARN)<<"Warning: font file \""<<str<<"\" not found."<<std::endl;
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
osgText::Font* osgText::readFontFile(const std::string& filename)
|
2001-11-12 18:04:57 +08:00
|
|
|
{
|
2003-03-04 00:42:48 +08:00
|
|
|
std::string foundFile = findFontFile(filename);
|
|
|
|
if (foundFile.empty()) return 0;
|
|
|
|
|
2003-11-20 19:07:19 +08:00
|
|
|
osg::Object* object = osgDB::readObjectFile(foundFile);
|
2003-03-04 00:42:48 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
// if the object is a font then return it.
|
|
|
|
osgText::Font* font = dynamic_cast<osgText::Font*>(object);
|
|
|
|
if (font) return font;
|
2003-03-04 00:42:48 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
// otherwise if the object has zero references then delete it by doing another unref().
|
|
|
|
if (object && object->referenceCount()==0) object->unref();
|
|
|
|
return 0;
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-07 01:11:24 +08:00
|
|
|
Font::Font(FontImplementation* implementation):
|
2003-03-03 05:05:05 +08:00
|
|
|
_width(16),
|
2003-03-07 01:11:24 +08:00
|
|
|
_height(16),
|
|
|
|
_margin(2),
|
|
|
|
_textureWidthHint(256),
|
|
|
|
_textureHeightHint(256),
|
|
|
|
_minFilterHint(osg::Texture::LINEAR_MIPMAP_LINEAR),
|
|
|
|
_magFilterHint(osg::Texture::LINEAR)
|
2001-11-12 18:04:57 +08:00
|
|
|
{
|
2003-03-07 01:11:24 +08:00
|
|
|
setImplementation(implementation);
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
Font::~Font()
|
2001-11-12 18:04:57 +08:00
|
|
|
{
|
2003-03-07 01:11:24 +08:00
|
|
|
if (_implementation.valid()) _implementation->_facade = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Font::setImplementation(FontImplementation* implementation)
|
|
|
|
{
|
|
|
|
if (_implementation.valid()) _implementation->_facade = 0;
|
|
|
|
_implementation = implementation;
|
|
|
|
if (_implementation.valid()) _implementation->_facade = this;
|
|
|
|
}
|
|
|
|
|
2003-03-10 17:15:59 +08:00
|
|
|
Font::FontImplementation* Font::getImplementation()
|
|
|
|
{
|
|
|
|
return _implementation.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Font::FontImplementation* Font::getImplementation() const
|
|
|
|
{
|
|
|
|
return _implementation.get();
|
|
|
|
}
|
|
|
|
|
2003-03-07 01:11:24 +08:00
|
|
|
std::string Font::getFileName() const
|
|
|
|
{
|
|
|
|
if (_implementation.valid()) return _implementation->getFileName();
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Font::setSize(unsigned int width, unsigned int height)
|
|
|
|
{
|
2003-03-08 05:05:35 +08:00
|
|
|
if (_implementation.valid()) _implementation->setSize(width, height);
|
2003-03-07 01:11:24 +08:00
|
|
|
}
|
|
|
|
|
2003-11-07 17:00:22 +08:00
|
|
|
unsigned int Font::getWidth() const
|
2003-03-10 17:15:59 +08:00
|
|
|
{
|
|
|
|
return _width;
|
|
|
|
}
|
|
|
|
|
2003-11-07 17:00:22 +08:00
|
|
|
unsigned int Font::getHeight() const
|
2003-03-10 17:15:59 +08:00
|
|
|
{
|
|
|
|
return _height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Font::setGlyphImageMargin(unsigned int margin)
|
|
|
|
{
|
|
|
|
_margin = margin;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Font::getGlyphImageMargin() const
|
|
|
|
{
|
|
|
|
return _margin;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Font::setTextureSizeHint(unsigned int width,unsigned int height)
|
|
|
|
{
|
|
|
|
_textureWidthHint = width;
|
|
|
|
_textureHeightHint = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Font::getTextureWidthHint() const
|
|
|
|
{
|
|
|
|
return _textureWidthHint;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Font::getTextureHeightHint() const
|
|
|
|
{
|
|
|
|
return _textureHeightHint;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Font::setMinFilterHint(osg::Texture::FilterMode mode)
|
|
|
|
{
|
|
|
|
_minFilterHint = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Texture::FilterMode Font::getMinFilterHint() const
|
|
|
|
{
|
|
|
|
return _minFilterHint;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set the magnification texture filter to use when creating the texture to store the glyph images when rendering.
|
|
|
|
* Note, this doesn't affect already created Texture Glhph's.*/
|
|
|
|
void Font::setMagFilterHint(osg::Texture::FilterMode mode)
|
|
|
|
{
|
|
|
|
_magFilterHint = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Texture::FilterMode Font::getMagFilterHint() const
|
|
|
|
{
|
|
|
|
return _magFilterHint;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-07 01:11:24 +08:00
|
|
|
Font::Glyph* Font::getGlyph(unsigned int charcode)
|
|
|
|
{
|
|
|
|
SizeGlyphMap::iterator itr = _sizeGlyphMap.find(SizePair(_width,_height));
|
|
|
|
if (itr!=_sizeGlyphMap.end())
|
2003-03-04 20:34:42 +08:00
|
|
|
{
|
2003-03-07 01:11:24 +08:00
|
|
|
GlyphMap& glyphmap = itr->second;
|
|
|
|
GlyphMap::iterator gitr = glyphmap.find(charcode);
|
|
|
|
if (gitr!=glyphmap.end()) return gitr->second.get();
|
2003-03-04 20:34:42 +08:00
|
|
|
}
|
2003-03-07 01:11:24 +08:00
|
|
|
|
|
|
|
if (_implementation.valid()) return _implementation->getGlyph(charcode);
|
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
2003-06-27 00:21:49 +08:00
|
|
|
osg::Vec2 Font::getKerning(unsigned int leftcharcode,unsigned int rightcharcode, KerningType kerningType)
|
2003-03-07 01:11:24 +08:00
|
|
|
{
|
2003-06-27 00:21:49 +08:00
|
|
|
if (_implementation.valid()) return _implementation->getKerning(leftcharcode,rightcharcode,kerningType);
|
2003-03-07 01:11:24 +08:00
|
|
|
else return osg::Vec2(0.0f,0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Font::hasVertical() const
|
|
|
|
{
|
|
|
|
if (_implementation.valid()) return _implementation->hasVertical();
|
|
|
|
else return false;
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
2003-03-07 01:11:24 +08:00
|
|
|
|
|
|
|
|
2003-03-04 20:34:42 +08:00
|
|
|
void Font::addGlyph(unsigned int width, unsigned int height, unsigned int charcode, Glyph* glyph)
|
2002-06-12 02:41:57 +08:00
|
|
|
{
|
2003-03-11 21:30:03 +08:00
|
|
|
|
|
|
|
//cout << "charcode "<<(char)charcode<<" "<<&_glyphTextureList<<endl;
|
|
|
|
|
2003-03-04 20:34:42 +08:00
|
|
|
_sizeGlyphMap[SizePair(width,height)][charcode]=glyph;
|
2003-03-03 05:05:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
int posX=0,posY=0;
|
|
|
|
|
|
|
|
GlyphTexture* glyphTexture = 0;
|
|
|
|
for(GlyphTextureList::iterator itr=_glyphTextureList.begin();
|
|
|
|
itr!=_glyphTextureList.end() && !glyphTexture;
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
if ((*itr)->getSpaceForGlyph(glyph,posX,posY)) glyphTexture = itr->get();
|
|
|
|
}
|
|
|
|
|
2003-03-11 21:30:03 +08:00
|
|
|
if (glyphTexture)
|
|
|
|
{
|
|
|
|
//cout << " found space for texture "<<glyphTexture<<" posX="<<posX<<" posY="<<posY<<endl;
|
|
|
|
}
|
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
if (!glyphTexture)
|
|
|
|
{
|
2002-06-12 02:41:57 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
osg::StateSet* stateset = new osg::StateSet;
|
|
|
|
_stateSetList.push_back(stateset);
|
2002-06-12 02:41:57 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
glyphTexture = new GlyphTexture;
|
2003-03-11 00:40:26 +08:00
|
|
|
|
2003-03-11 21:30:03 +08:00
|
|
|
//std::cout<<" Creating new GlyphTexture "<<glyphTexture<<"& StateSet "<<stateset<<std::endl;
|
|
|
|
|
|
|
|
static int numberOfTexturesAllocated = 0;
|
|
|
|
++numberOfTexturesAllocated;
|
|
|
|
//std::cout << " " << this<< " numberOfTexturesAllocated "<<numberOfTexturesAllocated<<std::endl;
|
2003-03-07 01:11:24 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
// reserve enough space for the glyphs.
|
2003-03-07 01:11:24 +08:00
|
|
|
glyphTexture->setGlyphImageMargin(_margin);
|
|
|
|
glyphTexture->setTextureSize(_textureWidthHint,_textureHeightHint);
|
|
|
|
glyphTexture->setFilter(osg::Texture::MIN_FILTER,_minFilterHint);
|
|
|
|
glyphTexture->setFilter(osg::Texture::MAG_FILTER,_magFilterHint);
|
2003-03-03 05:05:05 +08:00
|
|
|
glyphTexture->setMaxAnisotropy(8);
|
|
|
|
|
|
|
|
_glyphTextureList.push_back(glyphTexture);
|
|
|
|
|
|
|
|
glyphTexture->setStateSet(stateset);
|
|
|
|
stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
|
|
|
|
stateset->setTextureAttributeAndModes(0,glyphTexture,osg::StateAttribute::ON);
|
2003-03-05 20:28:42 +08:00
|
|
|
stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
if (!glyphTexture->getSpaceForGlyph(glyph,posX,posY))
|
|
|
|
{
|
|
|
|
osg::notify(osg::WARN)<<"Warning: unable to allocate texture big enough for glyph"<<std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the glyph into the texture.
|
|
|
|
glyphTexture->addGlyph(glyph,posX,posY);
|
|
|
|
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
|
|
|
|
Font::GlyphTexture::GlyphTexture():
|
|
|
|
_stateset(0),
|
2003-03-07 01:11:24 +08:00
|
|
|
_margin(2),
|
2003-03-03 05:05:05 +08:00
|
|
|
_usedY(0),
|
|
|
|
_partUsedX(0),
|
|
|
|
_partUsedY(0)
|
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
|
|
|
{
|
|
|
|
}
|
2002-01-19 06:25:51 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
Font::GlyphTexture::~GlyphTexture()
|
2001-11-12 18:04:57 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-03-11 21:30:03 +08:00
|
|
|
// return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.
|
2003-03-12 05:42:55 +08:00
|
|
|
int Font::GlyphTexture::compare(const osg::StateAttribute& rhs) const
|
2003-03-11 21:30:03 +08:00
|
|
|
{
|
|
|
|
if (this<&rhs) return -1;
|
|
|
|
else if (this>&rhs) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
bool Font::GlyphTexture::getSpaceForGlyph(Glyph* glyph, int& posX, int& posY)
|
2001-11-12 18:04:57 +08:00
|
|
|
{
|
2003-03-03 05:05:05 +08:00
|
|
|
|
2003-03-07 01:11:24 +08:00
|
|
|
int width = glyph->s()+2*_margin;
|
|
|
|
int height = glyph->t()+2*_margin;
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
// first check box (_partUsedX,_usedY) to (width,height)
|
|
|
|
if (width <= (getTextureWidth()-_partUsedX) &&
|
|
|
|
height <= (getTextureHeight()-_usedY))
|
2002-11-06 23:43:11 +08:00
|
|
|
{
|
2003-03-03 05:05:05 +08:00
|
|
|
// can fit in existing row.
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
// record the position in which the texture will be stored.
|
2003-03-07 01:11:24 +08:00
|
|
|
posX = _partUsedX+_margin;
|
|
|
|
posY = _usedY+_margin;
|
2003-03-03 05:05:05 +08:00
|
|
|
|
|
|
|
// move used markers on.
|
|
|
|
_partUsedX += width;
|
|
|
|
if (_usedY+height>_partUsedY) _partUsedY = _usedY+height;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2001-11-14 22:09:07 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
// start an new row.
|
|
|
|
if (width <= getTextureWidth() &&
|
|
|
|
height <= (getTextureHeight()-_partUsedY))
|
2001-11-14 22:09:07 +08:00
|
|
|
{
|
2003-03-03 05:05:05 +08:00
|
|
|
// can fit next row.
|
|
|
|
_partUsedX = 0;
|
|
|
|
_usedY = _partUsedY;
|
|
|
|
|
2003-03-07 01:11:24 +08:00
|
|
|
posX = _partUsedX+_margin;
|
|
|
|
posY = _usedY+_margin;
|
2003-03-03 05:05:05 +08:00
|
|
|
|
|
|
|
// move used markers on.
|
|
|
|
_partUsedX += width;
|
|
|
|
if (_usedY+height>_partUsedY) _partUsedY = _usedY+height;
|
|
|
|
|
|
|
|
return true;
|
2001-11-14 22:09:07 +08:00
|
|
|
}
|
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
// doesn't fit into glyph.
|
|
|
|
return false;
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
void Font::GlyphTexture::addGlyph(Glyph* glyph, int posX, int posY)
|
2001-11-12 18:04:57 +08:00
|
|
|
{
|
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
_glyphs.push_back(glyph);
|
|
|
|
for(unsigned int i=0;i<_glyphsToSubload.size();++i)
|
|
|
|
{
|
|
|
|
_glyphsToSubload[i].push_back(glyph);
|
|
|
|
}
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
// set up the details of where to place glyph's image in the texture.
|
|
|
|
glyph->setTexture(this);
|
|
|
|
glyph->setTexturePosition(posX,posY);
|
2003-12-03 05:38:52 +08:00
|
|
|
unsigned int sizeAdjustment = 0; // was 1.
|
2003-12-09 18:42:30 +08:00
|
|
|
glyph->setMinTexCoord(osg::Vec2((float)(posX+_margin)/(float)(getTextureWidth()-sizeAdjustment),(float)(posY+_margin)/(float)(getTextureHeight()-sizeAdjustment)));
|
2003-12-03 05:38:52 +08:00
|
|
|
glyph->setMaxTexCoord(osg::Vec2((float)(posX+glyph->s()-_margin)/(float)(getTextureWidth()-sizeAdjustment),(float)(posY+glyph->t()-_margin)/(float)(getTextureHeight()-sizeAdjustment)));
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
void Font::GlyphTexture::apply(osg::State& state) const
|
2001-11-12 18:04:57 +08:00
|
|
|
{
|
2003-03-03 05:05:05 +08:00
|
|
|
// get the contextID (user defined ID of 0 upwards) for the
|
|
|
|
// current OpenGL context.
|
|
|
|
const unsigned int contextID = state.getContextID();
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
if (contextID>=_glyphsToSubload.size())
|
2001-11-14 22:09:07 +08:00
|
|
|
{
|
2003-03-03 05:05:05 +08:00
|
|
|
// graphics context is beyond the number of glyphsToSubloads, so
|
|
|
|
// we must now copy the glyph list across, this is a potential
|
|
|
|
// threading issue though is multiple applies are happening the
|
|
|
|
// same time on this object - to avoid this condition number of
|
|
|
|
// graphics contexts should be set before create text.
|
|
|
|
for(unsigned int i=_glyphsToSubload.size();i<=contextID;++i)
|
|
|
|
{
|
2003-03-04 00:12:00 +08:00
|
|
|
GlyphPtrList& glyphPtrs = _glyphsToSubload[i];
|
|
|
|
for(GlyphRefList::const_iterator itr=_glyphs.begin();
|
|
|
|
itr!=_glyphs.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
glyphPtrs.push_back(itr->get());
|
|
|
|
}
|
2003-03-03 05:05:05 +08:00
|
|
|
}
|
2001-11-14 22:09:07 +08:00
|
|
|
}
|
2001-11-12 18:04:57 +08:00
|
|
|
|
|
|
|
|
2003-03-04 23:47:28 +08:00
|
|
|
const Extensions* extensions = getExtensions(contextID,true);
|
|
|
|
bool generateMipMapSupported = extensions->isGenerateMipMapSupported();
|
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
// get the texture object for the current contextID.
|
|
|
|
TextureObject* textureObject = getTextureObject(contextID);
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
if (textureObject == 0)
|
2001-11-14 22:09:07 +08:00
|
|
|
{
|
2003-07-14 22:42:10 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
// being bound for the first time, need to allocate the texture
|
2003-07-14 22:42:10 +08:00
|
|
|
|
|
|
|
_textureObjectBuffer[contextID] = textureObject = getTextureObjectManager()->reuseOrGenerateTextureObject(
|
|
|
|
contextID,GL_TEXTURE_2D,1,GL_LUMINANCE_ALPHA,getTextureWidth(), getTextureHeight(),1,0);
|
|
|
|
|
|
|
|
textureObject->bind();
|
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
|
|
|
|
applyTexParameters(GL_TEXTURE_2D,state);
|
2003-04-02 04:32:57 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
|
2003-03-04 23:47:28 +08:00
|
|
|
// need to look at generate mip map extension if mip mapping required.
|
|
|
|
switch(_min_filter)
|
|
|
|
{
|
|
|
|
case NEAREST_MIPMAP_NEAREST:
|
|
|
|
case NEAREST_MIPMAP_LINEAR:
|
|
|
|
case LINEAR_MIPMAP_NEAREST:
|
|
|
|
case LINEAR_MIPMAP_LINEAR:
|
2003-04-02 04:32:57 +08:00
|
|
|
if (generateMipMapSupported)
|
|
|
|
{
|
2003-03-04 23:47:28 +08:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE);
|
|
|
|
}
|
|
|
|
else glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, LINEAR);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// not mip mapping so no problems.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
// allocate the texture memory.
|
|
|
|
glTexImage2D( GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA,
|
|
|
|
getTextureWidth(), getTextureHeight(), 0,
|
|
|
|
GL_LUMINANCE_ALPHA,
|
|
|
|
GL_UNSIGNED_BYTE,
|
|
|
|
0 );
|
2003-04-02 04:32:57 +08:00
|
|
|
|
2001-11-14 22:09:07 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// reuse texture by binding.
|
2003-07-14 22:42:10 +08:00
|
|
|
textureObject->bind();
|
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
if (getTextureParameterDirty(contextID))
|
2003-03-04 23:47:28 +08:00
|
|
|
{
|
2003-03-03 05:05:05 +08:00
|
|
|
applyTexParameters(GL_TEXTURE_2D,state);
|
2003-04-02 04:32:57 +08:00
|
|
|
}
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
// // need to look at generate mip map extension if mip mapping required.
|
|
|
|
// switch(_min_filter)
|
|
|
|
// {
|
|
|
|
// case NEAREST_MIPMAP_NEAREST:
|
|
|
|
// case NEAREST_MIPMAP_LINEAR:
|
|
|
|
// case LINEAR_MIPMAP_NEAREST:
|
|
|
|
// case LINEAR_MIPMAP_LINEAR:
|
|
|
|
// if (generateMipMapSupported)
|
|
|
|
// {
|
|
|
|
// glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE);
|
|
|
|
// generateMipMapOn = true;
|
|
|
|
// }
|
|
|
|
// else glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, LINEAR);
|
|
|
|
// break;
|
|
|
|
// default:
|
|
|
|
// // not mip mapping so no problems.
|
|
|
|
// break;
|
|
|
|
// }
|
2003-03-04 23:47:28 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
}
|
2001-11-14 22:09:07 +08:00
|
|
|
|
2003-06-24 23:40:09 +08:00
|
|
|
static const GLubyte* s_renderer = 0;
|
|
|
|
static bool s_subloadAllGlyphsTogether = false;
|
|
|
|
if (!s_renderer)
|
|
|
|
{
|
|
|
|
s_renderer = glGetString(GL_RENDERER);
|
|
|
|
osg::notify(osg::INFO)<<"glGetString(GL_RENDERER)=="<<s_renderer<<std::endl;
|
|
|
|
if (strstr((const char*)s_renderer,"IMPACT")!=0)
|
|
|
|
{
|
|
|
|
// we're running on an Octane, so need to work around its
|
|
|
|
// subloading bugs by loading all at once.
|
|
|
|
s_subloadAllGlyphsTogether = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
// now subload the glyphs that are outstanding for this graphics context.
|
2003-03-04 00:12:00 +08:00
|
|
|
GlyphPtrList& glyphsWereSubloading = _glyphsToSubload[contextID];
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
if (!glyphsWereSubloading.empty())
|
2002-01-16 18:36:20 +08:00
|
|
|
{
|
2003-06-24 23:40:09 +08:00
|
|
|
|
|
|
|
if (!s_subloadAllGlyphsTogether)
|
|
|
|
{
|
|
|
|
// default way of subloading as required.
|
|
|
|
|
|
|
|
//std::cout<<"subloading"<<std::endl;
|
|
|
|
|
|
|
|
for(GlyphPtrList::iterator itr=glyphsWereSubloading.begin();
|
|
|
|
itr!=glyphsWereSubloading.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
(*itr)->subload();
|
|
|
|
}
|
2002-01-16 18:36:20 +08:00
|
|
|
|
2003-06-24 23:40:09 +08:00
|
|
|
// clear the list since we have now subloaded them.
|
|
|
|
glyphsWereSubloading.clear();
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
2003-03-03 05:05:05 +08:00
|
|
|
{
|
2003-06-24 23:40:09 +08:00
|
|
|
//std::cout<<"all loading"<<std::endl;
|
|
|
|
|
|
|
|
// Octane has bugs in OGL driver which mean that subloads smaller
|
|
|
|
// than 32x32 produce errors, and also cannot handle general alignment,
|
|
|
|
// so to get round this copy all glyphs into a temporary image and
|
|
|
|
// then subload the whole lot in one go.
|
|
|
|
|
|
|
|
int tsize = 2 * getTextureHeight() * getTextureWidth();
|
|
|
|
unsigned char *local_data = new unsigned char[tsize];
|
|
|
|
memset( local_data, 0L, tsize);
|
|
|
|
|
|
|
|
for(GlyphRefList::const_iterator itr=_glyphs.begin();
|
|
|
|
itr!=_glyphs.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
//(*itr)->subload();
|
|
|
|
|
|
|
|
// Rather than subloading to graphics, we'll write the values
|
|
|
|
// of the glyphs into some intermediate data and subload the
|
|
|
|
// whole thing at the end
|
|
|
|
for( int t = 0; t < (*itr)->t(); t++ )
|
|
|
|
{
|
|
|
|
for( int s = 0; s < (*itr)->s(); s++ )
|
|
|
|
{
|
|
|
|
int sindex = 2 * (t*(*itr)->s()+s);
|
|
|
|
int dindex = 2 *
|
|
|
|
((((*itr)->getTexturePositionY()+t) * getTextureWidth()) +
|
|
|
|
((*itr)->getTexturePositionX()+s));
|
|
|
|
|
|
|
|
const unsigned char *sptr = &(*itr)->data()[sindex];
|
|
|
|
unsigned char *dptr = &local_data[dindex];
|
|
|
|
|
|
|
|
(*dptr++) = (*sptr++);
|
|
|
|
(*dptr) = (*sptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear the list since we have now subloaded them.
|
|
|
|
glyphsWereSubloading.clear();
|
|
|
|
|
|
|
|
// Subload the image once
|
|
|
|
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0,
|
|
|
|
getTextureWidth(),
|
|
|
|
getTextureHeight(),
|
|
|
|
GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, local_data );
|
|
|
|
|
|
|
|
delete [] local_data;
|
2003-03-03 05:05:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2001-11-14 22:09:07 +08:00
|
|
|
{
|
2003-03-03 05:05:05 +08:00
|
|
|
//std::cout << "no need to subload "<<std::endl;
|
2001-11-14 22:09:07 +08:00
|
|
|
}
|
2003-04-02 04:32:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
// if (generateMipMapTurnedOn)
|
|
|
|
// {
|
|
|
|
// glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_FALSE);
|
|
|
|
// }
|
2003-04-02 04:32:57 +08:00
|
|
|
|
|
|
|
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
2003-03-04 06:18:33 +08:00
|
|
|
// all the methods in Font::Glyph have been made non inline because VisualStudio6.0 is STUPID, STUPID, STUPID PILE OF JUNK.
|
2003-03-03 05:05:05 +08:00
|
|
|
Font::Glyph::Glyph() {}
|
|
|
|
Font::Glyph::~Glyph() {}
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2003-03-04 06:18:33 +08:00
|
|
|
unsigned int Font::Glyph::getGlyphCode() const { return _glyphCode; }
|
|
|
|
|
2003-03-04 20:34:42 +08:00
|
|
|
// void Font::Glyph::setFont(Font* font) { _font = font; }
|
|
|
|
// Font* Font::Glyph::getFont() const { return _font; }
|
2003-03-04 06:18:33 +08:00
|
|
|
|
|
|
|
void Font::Glyph::setHorizontalBearing(const osg::Vec2& bearing) { _horizontalBearing=bearing; }
|
|
|
|
const osg::Vec2& Font::Glyph::getHorizontalBearing() const { return _horizontalBearing; }
|
|
|
|
|
|
|
|
void Font::Glyph::setHorizontalAdvance(float advance) { _horizontalAdvance=advance; }
|
|
|
|
float Font::Glyph::getHorizontalAdvance() const { return _horizontalAdvance; }
|
|
|
|
|
|
|
|
void Font::Glyph::setVerticalBearing(const osg::Vec2& bearing) { _verticalBearing=bearing; }
|
|
|
|
const osg::Vec2& Font::Glyph::getVerticalBearing() const { return _verticalBearing; }
|
|
|
|
|
|
|
|
void Font::Glyph::setVerticalAdvance(float advance) { _verticalAdvance=advance; }
|
|
|
|
float Font::Glyph::getVerticalAdvance() const { return _verticalAdvance; }
|
|
|
|
|
|
|
|
void Font::Glyph::setTexture(GlyphTexture* texture) { _texture = texture; }
|
|
|
|
Font::GlyphTexture* Font::Glyph::getTexture() { return _texture; }
|
|
|
|
const Font::GlyphTexture* Font::Glyph::getTexture() const { return _texture; }
|
|
|
|
|
|
|
|
osg::StateSet* Font::Glyph::getStateSet() { return _texture?_texture->getStateSet():0; }
|
|
|
|
const osg::StateSet* Font::Glyph::getStateSet() const { return _texture?_texture->getStateSet():0; }
|
|
|
|
|
|
|
|
void Font::Glyph::setTexturePosition(int posX,int posY) { _texturePosX = posX; _texturePosY = posY; }
|
|
|
|
int Font::Glyph::getTexturePositionX() const { return _texturePosX; }
|
|
|
|
int Font::Glyph::getTexturePositionY() const { return _texturePosY; }
|
|
|
|
|
|
|
|
void Font::Glyph::setMinTexCoord(const osg::Vec2& coord) { _minTexCoord=coord; }
|
|
|
|
const osg::Vec2& Font::Glyph::getMinTexCoord() const { return _minTexCoord; }
|
|
|
|
|
|
|
|
void Font::Glyph::setMaxTexCoord(const osg::Vec2& coord) { _maxTexCoord=coord; }
|
|
|
|
const osg::Vec2& Font::Glyph::getMaxTexCoord() const { return _maxTexCoord; }
|
|
|
|
|
2003-03-04 00:12:00 +08:00
|
|
|
void Font::Glyph::subload() const
|
2001-11-12 18:04:57 +08:00
|
|
|
{
|
2003-03-03 05:05:05 +08:00
|
|
|
GLenum errorNo = glGetError();
|
|
|
|
if (errorNo!=GL_NO_ERROR)
|
2001-11-14 22:09:07 +08:00
|
|
|
{
|
2003-04-14 23:02:13 +08:00
|
|
|
osg::notify(osg::WARN)<<"before Font::Glyph::subload(): detected OpenGL error '"<<gluErrorString(errorNo)<<std::endl;
|
2001-11-14 22:09:07 +08:00
|
|
|
}
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT,getPacking());
|
|
|
|
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D,0,
|
|
|
|
_texturePosX,_texturePosY,
|
|
|
|
s(),t(),
|
|
|
|
(GLenum)getPixelFormat(),
|
|
|
|
(GLenum)getDataType(),
|
|
|
|
data());
|
|
|
|
|
|
|
|
errorNo = glGetError();
|
|
|
|
if (errorNo!=GL_NO_ERROR)
|
2002-01-19 06:25:51 +08:00
|
|
|
{
|
2003-04-14 23:02:13 +08:00
|
|
|
|
|
|
|
osg::notify(osg::WARN)<<"after Font::Glyph::subload() : detected OpenGL error '"<<gluErrorString(errorNo)<<"'"<<std::endl;
|
2003-04-14 23:03:55 +08:00
|
|
|
std::cout << "\tglTexSubImage2D(0x"<<hex<<GL_TEXTURE_2D<<dec<<" ,"<<0<<"\t"<<std::endl<<
|
2003-04-14 23:02:13 +08:00
|
|
|
"\t "<<_texturePosX<<" ,"<<_texturePosY<<std::endl<<
|
|
|
|
"\t "<<s()<<" ,"<<t()<<std::endl<<hex<<
|
|
|
|
"\t 0x"<<(GLenum)getPixelFormat()<<std::endl<<
|
|
|
|
"\t 0x"<<(GLenum)getDataType()<<std::endl<<
|
2003-06-24 23:40:09 +08:00
|
|
|
"\t 0x"<<(unsigned long)data()<<");"<<dec<<std::endl;
|
2003-03-03 05:05:05 +08:00
|
|
|
}
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
2003-05-02 05:06:18 +08:00
|
|
|
|
|
|
|
void Font::Glyph::draw(osg::State& state) const
|
|
|
|
{
|
|
|
|
GLuint& globj = _globjList[state.getContextID()];
|
|
|
|
|
|
|
|
// call the globj if already set otherwise compile and execute.
|
|
|
|
if( globj != 0 )
|
|
|
|
{
|
|
|
|
glCallList( globj );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
globj = glGenLists( 1 );
|
|
|
|
glNewList( globj, GL_COMPILE_AND_EXECUTE );
|
|
|
|
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT,getPacking());
|
|
|
|
glDrawPixels(s(), t(),
|
|
|
|
(GLenum)getPixelFormat(),
|
|
|
|
(GLenum)getDataType(),
|
|
|
|
data() );
|
|
|
|
|
|
|
|
glEndList();
|
|
|
|
}
|
|
|
|
}
|