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-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>
|
|
|
|
#include <osg/GLU>
|
2001-11-12 18:04:57 +08:00
|
|
|
|
|
|
|
using namespace osgText;
|
|
|
|
|
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-03 05:05:05 +08:00
|
|
|
osg::Object* object = osgDB::readObjectFile(filename);
|
|
|
|
|
|
|
|
// if the object is a font then return it.
|
|
|
|
osgText::Font* font = dynamic_cast<osgText::Font*>(object);
|
|
|
|
if (font) return font;
|
|
|
|
|
|
|
|
// 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-03 05:05:05 +08:00
|
|
|
Font::Font():
|
|
|
|
_width(16),
|
|
|
|
_height(16)
|
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-03 05:05:05 +08:00
|
|
|
void Font::addGlyph(unsigned int charcode, Glyph* glyph)
|
2002-06-12 02:41:57 +08:00
|
|
|
{
|
2003-03-03 05:05:05 +08:00
|
|
|
_glyphMap[charcode]=glyph;
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!glyphTexture)
|
|
|
|
{
|
|
|
|
//std::cout<<"Creating new GlyphTexture & StateSet"<<std::endl;
|
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;
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
// reserve enough space for the glyphs.
|
|
|
|
glyphTexture->setTextureSize(256,256);
|
2003-03-03 17:37:02 +08:00
|
|
|
glyphTexture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
|
|
|
|
//glyphTexture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR_MIPMAP_LINEAR);
|
2003-03-03 05:05:05 +08:00
|
|
|
//glyphTexture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST);
|
2003-03-03 17:37:02 +08:00
|
|
|
glyphTexture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
|
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);
|
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),
|
|
|
|
_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-03 05:05:05 +08:00
|
|
|
bool Font::GlyphTexture::getSpaceForGlyph(Glyph* glyph, int& posX, int& posY)
|
2001-11-12 18:04:57 +08:00
|
|
|
{
|
2002-01-19 06:25:51 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
int margin = 2;
|
|
|
|
|
|
|
|
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.
|
|
|
|
posX = _partUsedX+margin;
|
|
|
|
posY = _usedY+margin;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
posX = _partUsedX+margin;
|
|
|
|
posY = _usedY+margin;
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
glyph->setMinTexCoord(osg::Vec2((float)posX/((float)getTextureWidth()-1.0f),(float)posY/((float)getTextureHeight()-1.0f)));
|
|
|
|
glyph->setMaxTexCoord(osg::Vec2((float)(posX+glyph->s())/((float)getTextureWidth()-1.0f),(float)(posY+glyph->t())/((float)getTextureHeight()-1.0f)));
|
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-03 05:05:05 +08:00
|
|
|
// get the globj for the current contextID.
|
|
|
|
GLuint& handle = getTextureObject(contextID);
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
if (handle == 0)
|
2001-11-14 22:09:07 +08:00
|
|
|
{
|
2003-03-03 05:05:05 +08:00
|
|
|
// being bound for the first time, need to allocate the texture
|
|
|
|
glGenTextures( 1L, (GLuint *)&handle );
|
|
|
|
glBindTexture( GL_TEXTURE_2D, handle );
|
|
|
|
|
|
|
|
applyTexParameters(GL_TEXTURE_2D,state);
|
|
|
|
|
2003-03-03 17:37:02 +08:00
|
|
|
//glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE);
|
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 );
|
2001-11-14 22:09:07 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// reuse texture by binding.
|
|
|
|
glBindTexture( GL_TEXTURE_2D, handle );
|
|
|
|
if (getTextureParameterDirty(contextID))
|
|
|
|
applyTexParameters(GL_TEXTURE_2D,state);
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2003-03-03 05:05:05 +08:00
|
|
|
}
|
2001-11-14 22:09:07 +08:00
|
|
|
|
2003-03-03 17:37:02 +08:00
|
|
|
//glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_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-03-04 00:12:00 +08:00
|
|
|
for(GlyphPtrList::iterator itr=glyphsWereSubloading.begin();
|
2003-03-03 05:05:05 +08:00
|
|
|
itr!=glyphsWereSubloading.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
(*itr)->subload();
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear the list since we have now subloaded them.
|
|
|
|
glyphsWereSubloading.clear();
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
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 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-03-03 05:05:05 +08:00
|
|
|
osg::notify(osg::WARN)<<"before: 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-03-03 05:05:05 +08:00
|
|
|
std::cout << " "<<GL_TEXTURE_2D<<"\t"<<0<<"\t"<<
|
|
|
|
_texturePosX<<"\t"<<_texturePosY<<"\t"<<
|
|
|
|
s()<<"\t"<<t()<<"\t"<<
|
|
|
|
(GLenum)getPixelFormat()<<"\t"<<
|
|
|
|
(GLenum)getDataType()<<"\t"<<
|
|
|
|
(int)(*data())<<std::endl;
|
|
|
|
|
|
|
|
osg::notify(osg::WARN)<<"after: detected OpenGL error '"<<gluErrorString(errorNo)<<std::endl;
|
|
|
|
}
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|