Addd support for maximum screen text size into osgText when auto scale to
screen is active. Added osgautotransform demo.
This commit is contained in:
155
src/osg/AutoTransform.cpp
Normal file
155
src/osg/AutoTransform.cpp
Normal file
@@ -0,0 +1,155 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#include <osg/AutoTransform>
|
||||
#include <osg/CullStack>
|
||||
|
||||
using namespace osg;
|
||||
|
||||
AutoTransform::AutoTransform():
|
||||
_scale(1.0f,1.0f,1.0f),
|
||||
_autoUpdateEyeMovementTolerance(0.0f),
|
||||
_autoRotateToScreen(false),
|
||||
_autoScaleToScreen(false),
|
||||
_firstTimeToInitEyePoint(true),
|
||||
_matrixDirty(true)
|
||||
{
|
||||
// setNumChildrenRequiringUpdateTraversal(1);
|
||||
}
|
||||
|
||||
AutoTransform::AutoTransform(const AutoTransform& pat,const CopyOp& copyop):
|
||||
Transform(pat,copyop),
|
||||
_position(pat._position),
|
||||
_pivotPoint(pat._pivotPoint),
|
||||
_rotation(pat._rotation),
|
||||
_scale(pat._scale)
|
||||
{
|
||||
// setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()+1);
|
||||
}
|
||||
|
||||
bool AutoTransform::computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor*) const
|
||||
{
|
||||
if (_matrixDirty) computeMatrix();
|
||||
|
||||
if (_referenceFrame==RELATIVE_TO_PARENTS)
|
||||
{
|
||||
matrix.preMult(_cachedMatrix);
|
||||
}
|
||||
else // absolute
|
||||
{
|
||||
matrix = _cachedMatrix;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool AutoTransform::computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor*) const
|
||||
{
|
||||
if (_referenceFrame==RELATIVE_TO_PARENTS)
|
||||
{
|
||||
matrix.postMult(osg::Matrix::translate(-_position)*
|
||||
osg::Matrix::rotate(_rotation.inverse())*
|
||||
osg::Matrix::scale(1.0f/_scale.x(),1.0f/_scale.y(),1.0f/_scale.z())*
|
||||
osg::Matrix::translate(_pivotPoint));
|
||||
}
|
||||
else // absolute
|
||||
{
|
||||
matrix = osg::Matrix::translate(-_position)*
|
||||
osg::Matrix::rotate(_rotation.inverse())*
|
||||
osg::Matrix::scale(1.0f/_scale.x(),1.0f/_scale.y(),1.0f/_scale.z())*
|
||||
osg::Matrix::translate(_pivotPoint);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void AutoTransform::computeMatrix() const
|
||||
{
|
||||
if (!_matrixDirty) return;
|
||||
|
||||
_cachedMatrix.set(osg::Matrix::translate(-_pivotPoint)*
|
||||
osg::Matrix::scale(_scale)*
|
||||
osg::Matrix::rotate(_rotation)*
|
||||
osg::Matrix::translate(_position));
|
||||
|
||||
_matrixDirty = false;
|
||||
}
|
||||
|
||||
void AutoTransform::accept(NodeVisitor& nv)
|
||||
{
|
||||
// if app traversal update the frame count.
|
||||
if (nv.getVisitorType()==NodeVisitor::UPDATE_VISITOR)
|
||||
{
|
||||
}
|
||||
else
|
||||
if (nv.getVisitorType()==NodeVisitor::CULL_VISITOR)
|
||||
{
|
||||
|
||||
CullStack* cs = dynamic_cast<CullStack*>(&nv);
|
||||
if (cs)
|
||||
{
|
||||
|
||||
int width = _previousWidth;
|
||||
int height = _previousHeight;
|
||||
|
||||
osg::Viewport* viewport = cs->getViewport();
|
||||
if (viewport)
|
||||
{
|
||||
width = viewport->width();
|
||||
height = viewport->height();
|
||||
}
|
||||
|
||||
osg::Vec3 eyePoint = cs->getEyeLocal();
|
||||
|
||||
bool doUpdate = _firstTimeToInitEyePoint;
|
||||
if (!_firstTimeToInitEyePoint)
|
||||
{
|
||||
osg::Vec3 dv = _previousEyePoint-eyePoint;
|
||||
if (dv.length2()>getAutoUpdateEyeMovementTolerance()*(eyePoint-getPosition()).length2())
|
||||
{
|
||||
doUpdate = true;
|
||||
}
|
||||
else if (width!=_previousWidth || height!=_previousHeight)
|
||||
{
|
||||
doUpdate = true;
|
||||
}
|
||||
}
|
||||
_firstTimeToInitEyePoint = false;
|
||||
|
||||
if (doUpdate)
|
||||
{
|
||||
|
||||
if (getAutoScaleToScreen())
|
||||
{
|
||||
float size = 1.0f/cs->pixelSize(getPosition(),1.0f);
|
||||
setScale(size);
|
||||
}
|
||||
|
||||
if (getAutoRotateToScreen())
|
||||
{
|
||||
osg::Quat rotation;
|
||||
rotation.set(cs->getModelViewMatrix());
|
||||
setRotation(rotation.inverse());
|
||||
}
|
||||
|
||||
_previousEyePoint = eyePoint;
|
||||
_previousWidth = width;
|
||||
_previousHeight = height;
|
||||
|
||||
_matrixDirty = true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// now do the proper accept
|
||||
Transform::accept(nv);
|
||||
}
|
||||
@@ -559,3 +559,27 @@ void Font::Glyph::subload() const
|
||||
"\t 0x"<<(unsigned int)data()<<");"<<dec<<std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <osg/GL>
|
||||
#include <osg/Math>
|
||||
#include <osgUtil/CullVisitor>
|
||||
#include <osgDB/ReadFile>
|
||||
#include <osgText/Text>
|
||||
|
||||
#include "DefaultFont.h"
|
||||
@@ -68,8 +69,14 @@ struct TextCullCallback : public osg::Drawable::CullCallback
|
||||
|
||||
if (_text->getAutoScaleToScreen())
|
||||
{
|
||||
float size = 1.0f/cs->pixelSize(_text->getPosition(),1.0f);
|
||||
_text->setScale(size);
|
||||
float numPixels = cs->pixelSize(_text->getPosition(),_text->getCharacterHeight());
|
||||
if (numPixels>_text->getFontHeight())
|
||||
{
|
||||
_text->setScale(_text->getFontHeight()/numPixels);
|
||||
}
|
||||
|
||||
//float size = 1.0f/cs->pixelSize(_text->getPosition(),1.0f);
|
||||
//_text->setScale(size);
|
||||
}
|
||||
|
||||
if (_text->getAutoRotateToScreen())
|
||||
@@ -295,6 +302,25 @@ void Text::setColor(const osg::Vec4& color)
|
||||
_color = color;
|
||||
}
|
||||
|
||||
void Text::setDrawMode(unsigned int mode)
|
||||
{
|
||||
if (_drawMode&3 != mode&3)
|
||||
{
|
||||
_drawMode=mode;
|
||||
if (_drawMode&TEXT_PIXMAP)
|
||||
{
|
||||
setAutoScaleToScreen(true);
|
||||
setAutoRotateToScreen(true);
|
||||
}
|
||||
computeGlyphRepresentation();
|
||||
}
|
||||
else
|
||||
{
|
||||
_drawMode=mode;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool Text::computeBound() const
|
||||
{
|
||||
_bbox.init();
|
||||
@@ -436,7 +462,7 @@ void Text::computeGlyphRepresentation()
|
||||
local.x() += bearing.x() * wr;
|
||||
local.y() += bearing.y() * hr;
|
||||
}
|
||||
}
|
||||
; }
|
||||
break;
|
||||
}
|
||||
case VERTICAL:
|
||||
@@ -458,6 +484,8 @@ void Text::computeGlyphRepresentation()
|
||||
|
||||
GlyphQuads& glyphquad = _textureGlyphQuadMap[glyph->getTexture()->getStateSet()];
|
||||
|
||||
glyphquad._glyphs.push_back(glyph);
|
||||
|
||||
// set up the coords of the quad
|
||||
glyphquad._coords.push_back(local+osg::Vec2(0.0f,height));
|
||||
glyphquad._coords.push_back(local+osg::Vec2(0.0f,0.0f));
|
||||
@@ -575,6 +603,13 @@ void Text::computePositions()
|
||||
dirtyBound();
|
||||
}
|
||||
|
||||
static unsigned char local_data[] = { 255,255,255,255,255,255,255,255,
|
||||
255,255,255,255,255,255,255,255,
|
||||
255,255,255,255,255,255,255,255,
|
||||
255,255,255,255,255,255,255,255 };
|
||||
|
||||
|
||||
static osg::Image* loaded_image = osgDB::readImageFile("lz.rgb");
|
||||
|
||||
void Text::drawImplementation(osg::State& state) const
|
||||
{
|
||||
@@ -583,7 +618,7 @@ void Text::drawImplementation(osg::State& state) const
|
||||
glNormal3fv(_normal.ptr());
|
||||
glColor4fv(_color.ptr());
|
||||
|
||||
if (_drawMode & TEXT)
|
||||
if (_drawMode & TEXT && !(_drawMode & TEXT_PIXMAP))
|
||||
{
|
||||
|
||||
state.disableAllVertexArrays();
|
||||
@@ -605,6 +640,36 @@ void Text::drawImplementation(osg::State& state) const
|
||||
}
|
||||
}
|
||||
|
||||
if (_drawMode & TEXT_PIXMAP)
|
||||
{
|
||||
|
||||
state.applyTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OFF);
|
||||
|
||||
for(TextureGlyphQuadMap::const_iterator titr=_textureGlyphQuadMap.begin();
|
||||
titr!=_textureGlyphQuadMap.end();
|
||||
++titr)
|
||||
{
|
||||
const GlyphQuads& glyphquad = titr->second;
|
||||
|
||||
int ci=1;
|
||||
|
||||
for(GlyphQuads::Glyphs::const_iterator gitr=glyphquad._glyphs.begin();
|
||||
gitr!=glyphquad._glyphs.end();
|
||||
++gitr, ci+=4)
|
||||
{
|
||||
|
||||
Font::Glyph* glyph = *gitr;
|
||||
|
||||
|
||||
glRasterPos3fv(glyphquad._transformedCoords[ci].ptr());
|
||||
|
||||
glyph->draw(state);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (_drawMode & BOUNDINGBOX)
|
||||
{
|
||||
|
||||
|
||||
Reference in New Issue
Block a user