/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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 #include #include #include #include #include #include #include using namespace osg; using namespace osgText; //#define TREES_CODE_FOR_MAKING_SPACES_EDITABLE Text::Text(): _enableDepthWrites(true), _backdropType(NONE), _backdropImplementation(DELAYED_DEPTH_WRITES), _backdropHorizontalOffset(0.07f), _backdropVerticalOffset(0.07f), _backdropColor(0.0f, 0.0f, 0.0f, 1.0f), _colorGradientMode(SOLID), _colorGradientTopLeft(1.0f, 0.0f, 0.0f, 1.0f), _colorGradientBottomLeft(0.0f, 1.0f, 0.0f, 1.0f), _colorGradientBottomRight(0.0f, 0.0f, 1.0f, 1.0f), _colorGradientTopRight(1.0f, 1.0f, 1.0f, 1.0f) { _supportsVertexBufferObjects = true; } Text::Text(const Text& text,const osg::CopyOp& copyop): osgText::TextBase(text,copyop), _enableDepthWrites(text._enableDepthWrites), _backdropType(text._backdropType), _backdropImplementation(text._backdropImplementation), _backdropHorizontalOffset(text._backdropHorizontalOffset), _backdropVerticalOffset(text._backdropVerticalOffset), _backdropColor(text._backdropColor), _colorGradientMode(text._colorGradientMode), _colorGradientTopLeft(text._colorGradientTopLeft), _colorGradientBottomLeft(text._colorGradientBottomLeft), _colorGradientBottomRight(text._colorGradientBottomRight), _colorGradientTopRight(text._colorGradientTopRight) { computeGlyphRepresentation(); } Text::~Text() { } void Text::setFont(osg::ref_ptr font) { if (_font==font) return; osg::StateSet* previousFontStateSet = _font.valid() ? _font->getStateSet() : Font::getDefaultFont()->getStateSet(); osg::StateSet* newFontStateSet = font.valid() ? font->getStateSet() : Font::getDefaultFont()->getStateSet(); if (getStateSet() == previousFontStateSet) { setStateSet( newFontStateSet ); } TextBase::setFont(font); } Font* Text::getActiveFont() { return _font.valid() ? _font.get() : Font::getDefaultFont().get(); } const Font* Text::getActiveFont() const { return _font.valid() ? _font.get() : Font::getDefaultFont().get(); } String::iterator Text::computeLastCharacterOnLine(osg::Vec2& cursor, String::iterator first,String::iterator last) { Font* activefont = getActiveFont(); if (!activefont) return last; float hr = _characterHeight; float wr = hr/getCharacterAspectRatio(); bool kerning = true; unsigned int previous_charcode = 0; String::iterator lastChar = first; for(bool outOfSpace=false;lastChar!=last;++lastChar) { unsigned int charcode = *lastChar; if (charcode=='\n') { return lastChar; } Glyph* glyph = activefont->getGlyph(_fontSize, charcode); if (glyph) { float width = (float)(glyph->getWidth()) * wr; if (_layout==RIGHT_TO_LEFT) { cursor.x() -= glyph->getHorizontalAdvance() * wr; } // adjust cursor position w.r.t any kerning. if (kerning && previous_charcode) { switch(_layout) { case LEFT_TO_RIGHT: { osg::Vec2 delta(activefont->getKerning(_fontSize, previous_charcode, charcode, _kerningType)); cursor.x() += delta.x() * wr; cursor.y() += delta.y() * hr; break; } case RIGHT_TO_LEFT: { osg::Vec2 delta(activefont->getKerning(_fontSize, charcode, previous_charcode, _kerningType)); cursor.x() -= delta.x() * wr; cursor.y() -= delta.y() * hr; break; } case VERTICAL: break; // no kerning when vertical. } // check to see if we are still within line if not move to next line. } switch(_layout) { case LEFT_TO_RIGHT: { if (_maximumWidth>0.0f && cursor.x()+width>_maximumWidth) outOfSpace=true; if(_maximumHeight>0.0f && cursor.y()<-_maximumHeight) outOfSpace=true; break; } case RIGHT_TO_LEFT: { if (_maximumWidth>0.0f && cursor.x()<-_maximumWidth) outOfSpace=true; if(_maximumHeight>0.0f && cursor.y()<-_maximumHeight) outOfSpace=true; break; } case VERTICAL: if (_maximumHeight>0.0f && cursor.y()<-_maximumHeight) outOfSpace=true; break; } // => word boundary detection & wrapping if (outOfSpace) break; // move the cursor onto the next character. switch(_layout) { case LEFT_TO_RIGHT: cursor.x() += glyph->getHorizontalAdvance() * wr; break; case VERTICAL: cursor.y() -= glyph->getVerticalAdvance() *hr; break; case RIGHT_TO_LEFT: break; // nop. } previous_charcode = charcode; } } // word boundary detection & wrapping if (lastChar!=last) { String::iterator lastValidChar = lastChar; String::iterator prevChar; while (lastValidChar != first){ prevChar = lastValidChar - 1; // last char is after a hyphen if(*lastValidChar == '-') return lastValidChar + 1; // last char is start of whitespace if((*lastValidChar == ' ' || *lastValidChar == '\n') && (*prevChar != ' ' && *prevChar != '\n')) return lastValidChar; // Subtract off glyphs from the cursor position (to correctly center text) if(*prevChar != '-') { Glyph* glyph = activefont->getGlyph(_fontSize, *prevChar); if (glyph) { switch(_layout) { case LEFT_TO_RIGHT: cursor.x() -= glyph->getHorizontalAdvance() * wr; break; case VERTICAL: cursor.y() += glyph->getVerticalAdvance() * hr; break; case RIGHT_TO_LEFT: break; // nop. } } } lastValidChar = prevChar; } } return lastChar; } void Text::computeGlyphRepresentation() { Font* activefont = getActiveFont(); if (!activefont) return; _textureGlyphQuadMap.clear(); _lineCount = 0; if (_text.empty()) { _textBB.set(0,0,0,0,0,0);//no size text TextBase::computePositions(); //to reset the origin return; } //OpenThreads::ScopedLock lock(*(activefont->getSerializeFontCallsMutex())); // initialize bounding box, it will be expanded during glyph position calculation _textBB.init(); osg::Vec2 startOfLine_coords(0.0f,0.0f); osg::Vec2 cursor(startOfLine_coords); osg::Vec2 local(0.0f,0.0f); unsigned int previous_charcode = 0; unsigned int linelength = 0; bool horizontal = _layout!=VERTICAL; bool kerning = true; unsigned int lineNumber = 0; float hr = _characterHeight; float wr = hr/getCharacterAspectRatio(); for(String::iterator itr=_text.begin(); itr!=_text.end(); ) { // record the start of the current line String::iterator startOfLine_itr = itr; // find the end of the current line. osg::Vec2 endOfLine_coords(cursor); String::iterator endOfLine_itr = computeLastCharacterOnLine(endOfLine_coords, itr,_text.end()); linelength = endOfLine_itr - startOfLine_itr; // Set line position to correct alignment. switch(_layout) { case LEFT_TO_RIGHT: { switch(_alignment) { // nothing to be done for these //case LEFT_TOP: //case LEFT_CENTER: //case LEFT_BOTTOM: //case LEFT_BASE_LINE: //case LEFT_BOTTOM_BASE_LINE: // break; case CENTER_TOP: case CENTER_CENTER: case CENTER_BOTTOM: case CENTER_BASE_LINE: case CENTER_BOTTOM_BASE_LINE: cursor.x() = (cursor.x() - endOfLine_coords.x()) * 0.5f; break; case RIGHT_TOP: case RIGHT_CENTER: case RIGHT_BOTTOM: case RIGHT_BASE_LINE: case RIGHT_BOTTOM_BASE_LINE: cursor.x() = cursor.x() - endOfLine_coords.x(); break; default: break; } break; } case RIGHT_TO_LEFT: { switch(_alignment) { case LEFT_TOP: case LEFT_CENTER: case LEFT_BOTTOM: case LEFT_BASE_LINE: case LEFT_BOTTOM_BASE_LINE: cursor.x() = 2*cursor.x() - endOfLine_coords.x(); break; case CENTER_TOP: case CENTER_CENTER: case CENTER_BOTTOM: case CENTER_BASE_LINE: case CENTER_BOTTOM_BASE_LINE: cursor.x() = cursor.x() + (cursor.x() - endOfLine_coords.x()) * 0.5f; break; // nothing to be done for these //case RIGHT_TOP: //case RIGHT_CENTER: //case RIGHT_BOTTOM: //case RIGHT_BASE_LINE: //case RIGHT_BOTTOM_BASE_LINE: // break; default: break; } break; } case VERTICAL: { switch(_alignment) { // TODO: current behaviour top baselines lined up in both cases - need to implement // top of characters alignment - Question is this necessary? // ... otherwise, nothing to be done for these 6 cases //case LEFT_TOP: //case CENTER_TOP: //case RIGHT_TOP: // break; //case LEFT_BASE_LINE: //case CENTER_BASE_LINE: //case RIGHT_BASE_LINE: // break; case LEFT_CENTER: case CENTER_CENTER: case RIGHT_CENTER: cursor.y() = cursor.y() + (cursor.y() - endOfLine_coords.y()) * 0.5f; break; case LEFT_BOTTOM_BASE_LINE: case CENTER_BOTTOM_BASE_LINE: case RIGHT_BOTTOM_BASE_LINE: cursor.y() = cursor.y() - (linelength * _characterHeight); break; case LEFT_BOTTOM: case CENTER_BOTTOM: case RIGHT_BOTTOM: cursor.y() = 2*cursor.y() - endOfLine_coords.y(); break; default: break; } break; } } if (itr!=endOfLine_itr) { for(;itr!=endOfLine_itr;++itr) { unsigned int charcode = *itr; Glyph* glyph = activefont->getGlyph(_fontSize, charcode); if (glyph) { float width = (float)(glyph->getWidth()) * wr; float height = (float)(glyph->getHeight()) * hr; if (_layout==RIGHT_TO_LEFT) { cursor.x() -= glyph->getHorizontalAdvance() * wr; } // adjust cursor position w.r.t any kerning. if (kerning && previous_charcode) { switch(_layout) { case LEFT_TO_RIGHT: { osg::Vec2 delta(activefont->getKerning(_fontSize, previous_charcode, charcode, _kerningType)); cursor.x() += delta.x() * wr; cursor.y() += delta.y() * hr; break; } case RIGHT_TO_LEFT: { osg::Vec2 delta(activefont->getKerning(_fontSize, charcode, previous_charcode, _kerningType)); cursor.x() -= delta.x() * wr; cursor.y() -= delta.y() * hr; break; } case VERTICAL: break; // no kerning when vertical. } } local = cursor; osg::Vec2 bearing(horizontal?glyph->getHorizontalBearing():glyph->getVerticalBearing()); local.x() += bearing.x() * wr; local.y() += bearing.y() * hr; GlyphQuads& glyphquad = _textureGlyphQuadMap[glyph->getTexture()]; glyphquad._glyphs.push_back(glyph); glyphquad._lineNumbers.push_back(lineNumber); // Adjust coordinates and texture coordinates to avoid // clipping the edges of antialiased characters. osg::Vec2 mintc = glyph->getMinTexCoord(); osg::Vec2 maxtc = glyph->getMaxTexCoord(); osg::Vec2 vDiff = maxtc - mintc; float fHorizTCMargin = 1.0f / glyph->getTexture()->getTextureWidth(); float fVertTCMargin = 1.0f / glyph->getTexture()->getTextureHeight(); float fHorizQuadMargin = vDiff.x() == 0.0f ? 0.0f : width * fHorizTCMargin / vDiff.x(); float fVertQuadMargin = vDiff.y() == 0.0f ? 0.0f : height * fVertTCMargin / vDiff.y(); mintc.x() -= fHorizTCMargin; mintc.y() -= fVertTCMargin; maxtc.x() += fHorizTCMargin; maxtc.y() += fVertTCMargin; // set up the coords of the quad osg::Vec2 upLeft = local+osg::Vec2(0.0f-fHorizQuadMargin,height+fVertQuadMargin); osg::Vec2 lowLeft = local+osg::Vec2(0.0f-fHorizQuadMargin,0.0f-fVertQuadMargin); osg::Vec2 lowRight = local+osg::Vec2(width+fHorizQuadMargin,0.0f-fVertQuadMargin); osg::Vec2 upRight = local+osg::Vec2(width+fHorizQuadMargin,height+fVertQuadMargin); glyphquad._coords->push_back(upLeft); glyphquad._coords->push_back(lowLeft); glyphquad._coords->push_back(lowRight); glyphquad._coords->push_back(upRight); // set up the tex coords of the quad glyphquad._texcoords->push_back(osg::Vec2(mintc.x(), maxtc.y())); glyphquad._texcoords->push_back(osg::Vec2(mintc.x(), mintc.y())); glyphquad._texcoords->push_back(osg::Vec2(maxtc.x(), mintc.y())); glyphquad._texcoords->push_back(osg::Vec2(maxtc.x(), maxtc.y())); // move the cursor onto the next character. // also expand bounding box switch(_layout) { case LEFT_TO_RIGHT: cursor.x() += glyph->getHorizontalAdvance() * wr; _textBB.expandBy(osg::Vec3(lowLeft.x(), lowLeft.y(), 0.0f)); //lower left corner _textBB.expandBy(osg::Vec3(upRight.x(), upRight.y(), 0.0f)); //upper right corner break; case VERTICAL: cursor.y() -= glyph->getVerticalAdvance() * hr; _textBB.expandBy(osg::Vec3(upLeft.x(),upLeft.y(),0.0f)); //upper left corner _textBB.expandBy(osg::Vec3(lowRight.x(),lowRight.y(),0.0f)); //lower right corner break; case RIGHT_TO_LEFT: _textBB.expandBy(osg::Vec3(lowRight.x(),lowRight.y(),0.0f)); //lower right corner _textBB.expandBy(osg::Vec3(upLeft.x(),upLeft.y(),0.0f)); //upper left corner break; } previous_charcode = charcode; } } // skip over spaces and return. while (itr != _text.end() && *itr==' ') ++itr; if (itr != _text.end() && *itr=='\n') ++itr; } else { ++itr; } // move to new line. switch(_layout) { case LEFT_TO_RIGHT: { startOfLine_coords.y() -= _characterHeight * (1.0 + _lineSpacing); cursor = startOfLine_coords; previous_charcode = 0; _lineCount++; break; } case RIGHT_TO_LEFT: { startOfLine_coords.y() -= _characterHeight * (1.0 + _lineSpacing); cursor = startOfLine_coords; previous_charcode = 0; _lineCount++; break; } case VERTICAL: { startOfLine_coords.x() += _characterHeight/getCharacterAspectRatio() * (1.0 + _lineSpacing); cursor = startOfLine_coords; previous_charcode = 0; // because _lineCount is the max vertical no. of characters.... _lineCount = (_lineCount >linelength)?_lineCount:linelength; } break; } ++lineNumber; } for(TextureGlyphQuadMap::iterator titr=_textureGlyphQuadMap.begin(); titr!=_textureGlyphQuadMap.end(); ++titr) { titr->second.updateQuadIndices(); if (_useVertexBufferObjects) { titr->second.initGPUBufferObjects(); } } TextBase::computePositions(); computeBackdropBoundingBox(); computeBoundingBoxMargin(); computeColorGradients(); // set up the vertices for any boundinbox or alignment decoration setupDecoration(); } // Returns false if there are no glyphs and the width/height values are invalid. // Also sets avg_width and avg_height to 0.0f if the value is invalid. // This method is used several times in a loop for the same object which will produce the same values. // Further optimization may try saving these values instead of recomputing them. bool Text::computeAverageGlyphWidthAndHeight(float& avg_width, float& avg_height) const { float width = 0.0f; float height = 0.0f; float running_width = 0.0f; float running_height = 0.0f; avg_width = 0.0f; avg_height = 0.0f; int counter = 0; unsigned int i; bool is_valid_size = true; // This section is going to try to compute the average width and height // for a character among the text. The reason I shift by an // average amount per-character instead of shifting each character // by its per-instance amount is because it may look strange to see // the individual backdrop text letters not space themselves the same // way the foreground text does. Using one value gives uniformity. // Note: This loop is repeated for each context. I think it may produce // the same values regardless of context. This code be optimized by moving // this loop outside the loop. for(TextureGlyphQuadMap::const_iterator const_titr=_textureGlyphQuadMap.begin(); const_titr!=_textureGlyphQuadMap.end(); ++const_titr) { const GlyphQuads& glyphquad = const_titr->second; const GlyphQuads::Coords2& coords2 = glyphquad._coords; for (i = 0; i < coords2->size(); i += 4) { width = (*coords2)[i + 2].x() - (*coords2)[i].x(); height = (*coords2)[i].y() - (*coords2)[i + 1].y(); running_width += width; running_height += height; counter++; } } if(0 == counter) { is_valid_size = false; } else { avg_width = running_width/counter; avg_height = running_height/counter; } return is_valid_size; } void Text::computePositions(unsigned int contextID) const { switch(_alignment) { case LEFT_TOP: _offset.set(_textBB.xMin(),_textBB.yMax(),_textBB.zMin()); break; case LEFT_CENTER: _offset.set(_textBB.xMin(),(_textBB.yMax()+_textBB.yMin())*0.5f,_textBB.zMin()); break; case LEFT_BOTTOM: _offset.set(_textBB.xMin(),_textBB.yMin(),_textBB.zMin()); break; case CENTER_TOP: _offset.set((_textBB.xMax()+_textBB.xMin())*0.5f,_textBB.yMax(),_textBB.zMin()); break; case CENTER_CENTER: _offset.set((_textBB.xMax()+_textBB.xMin())*0.5f,(_textBB.yMax()+_textBB.yMin())*0.5f,_textBB.zMin()); break; case CENTER_BOTTOM: _offset.set((_textBB.xMax()+_textBB.xMin())*0.5f,_textBB.yMin(),_textBB.zMin()); break; case RIGHT_TOP: _offset.set(_textBB.xMax(),_textBB.yMax(),_textBB.zMin()); break; case RIGHT_CENTER: _offset.set(_textBB.xMax(),(_textBB.yMax()+_textBB.yMin())*0.5f,_textBB.zMin()); break; case RIGHT_BOTTOM: _offset.set(_textBB.xMax(),_textBB.yMin(),_textBB.zMin()); break; case LEFT_BASE_LINE: _offset.set(0.0f,0.0f,0.0f); break; case CENTER_BASE_LINE: _offset.set((_textBB.xMax()+_textBB.xMin())*0.5f,0.0f,0.0f); break; case RIGHT_BASE_LINE: _offset.set(_textBB.xMax(),0.0f,0.0f); break; case LEFT_BOTTOM_BASE_LINE: _offset.set(0.0f,-_characterHeight*(1.0 + _lineSpacing)*(_lineCount-1),0.0f); break; case CENTER_BOTTOM_BASE_LINE: _offset.set((_textBB.xMax()+_textBB.xMin())*0.5f,-_characterHeight*(1.0 + _lineSpacing)*(_lineCount-1),0.0f); break; case RIGHT_BOTTOM_BASE_LINE: _offset.set(_textBB.xMax(),-_characterHeight*(1.0 + _lineSpacing)*(_lineCount-1),0.0f); break; } AutoTransformCache& atc = _autoTransformCache[contextID]; osg::Matrix& matrix = atc._matrix; if (_characterSizeMode!=OBJECT_COORDS || _autoRotateToScreen) { matrix.makeTranslate(-_offset); osg::Matrix rotate_matrix; if (_autoRotateToScreen) { osg::Vec3d trans(atc._modelview.getTrans()); atc._modelview.setTrans(0.0f,0.0f,0.0f); rotate_matrix.invert(atc._modelview); atc._modelview.setTrans(trans); } matrix.postMultRotate(_rotation); if (_characterSizeMode!=OBJECT_COORDS) { osg::Matrix M(rotate_matrix); M.postMultTranslate(_position); M.postMult(atc._modelview); osg::Matrix& P = atc._projection; // compute the pixel size vector. // pre adjust P00,P20,P23,P33 by multiplying them by the viewport window matrix. // here we do it in short hand with the knowledge of how the window matrix is formed // note P23,P33 are multiplied by an implicit 1 which would come from the window matrix. // Robert Osfield, June 2002. // scaling for horizontal pixels float P00 = P(0,0)*atc._width*0.5f; float P20_00 = P(2,0)*atc._width*0.5f + P(2,3)*atc._width*0.5f; osg::Vec3 scale_00(M(0,0)*P00 + M(0,2)*P20_00, M(1,0)*P00 + M(1,2)*P20_00, M(2,0)*P00 + M(2,2)*P20_00); // scaling for vertical pixels float P10 = P(1,1)*atc._height*0.5f; float P20_10 = P(2,1)*atc._height*0.5f + P(2,3)*atc._height*0.5f; osg::Vec3 scale_10(M(0,1)*P10 + M(0,2)*P20_10, M(1,1)*P10 + M(1,2)*P20_10, M(2,1)*P10 + M(2,2)*P20_10); float P23 = P(2,3); float P33 = P(3,3); float pixelSizeVector_w = M(3,2)*P23 + M(3,3)*P33; float pixelSizeVert=(_characterHeight*sqrtf(scale_10.length2()))/(pixelSizeVector_w*0.701f); float pixelSizeHori=(_characterHeight/getCharacterAspectRatio()*sqrtf(scale_00.length2()))/(pixelSizeVector_w*0.701f); // avoid nasty math by preventing a divide by zero if (pixelSizeVert == 0.0f) pixelSizeVert= 1.0f; if (pixelSizeHori == 0.0f) pixelSizeHori= 1.0f; if (_characterSizeMode==SCREEN_COORDS) { float scale_font_vert=_characterHeight/pixelSizeVert; float scale_font_hori=_characterHeight/getCharacterAspectRatio()/pixelSizeHori; if (P10<0) scale_font_vert=-scale_font_vert; matrix.postMultScale(osg::Vec3f(scale_font_hori, scale_font_vert,1.0f)); } else if (pixelSizeVert>getFontHeight()) { float scale_font = getFontHeight()/pixelSizeVert; matrix.postMultScale(osg::Vec3f(scale_font, scale_font,1.0f)); } } if (_autoRotateToScreen) { matrix.postMult(rotate_matrix); } matrix.postMultTranslate(_position); } else if (!_rotation.zeroRotation()) { matrix.makeRotate(_rotation); matrix.preMultTranslate(-_offset); matrix.postMultTranslate(_position); } else { matrix.makeTranslate(_position-_offset); } // now apply matrix to the glyphs. for(TextureGlyphQuadMap::iterator titr=_textureGlyphQuadMap.begin(); titr!=_textureGlyphQuadMap.end(); ++titr) { GlyphQuads& glyphquad = titr->second; //OSG_NOTICE<<"Text::computePositions("<empty()) { state.setVertexPointer(transformedCoords.get()); state.setTexCoordPointer(0, glyphquad._texcoords.get()); if(_colorGradientMode == SOLID) { state.disableColorPointer(); state.Color(colorMultiplier.r()*_color.r(),colorMultiplier.g()*_color.g(),colorMultiplier.b()*_color.b(),colorMultiplier.a()*_color.a()); } else { state.setColorPointer(glyphquad._colorCoords.get()); } glyphquad._quadIndices->draw(state, _useVertexBufferObjects); } } void Text::renderOnlyForegroundText(osg::State& state, const osg::Vec4& colorMultiplier) const { for(TextureGlyphQuadMap::iterator titr=_textureGlyphQuadMap.begin(); titr!=_textureGlyphQuadMap.end(); ++titr) { // need to set the texture here... state.applyTextureAttribute(0,titr->first.get()); const GlyphQuads& glyphquad = titr->second; drawForegroundText(state, glyphquad, colorMultiplier); } } void Text::renderWithDelayedDepthWrites(osg::State& state, const osg::Vec4& colorMultiplier) const { // If depth testing is disabled, then just render text as normal if( !state.getLastAppliedMode(GL_DEPTH_TEST) ) { drawTextWithBackdrop(state,colorMultiplier); return; } //glPushAttrib( _enableDepthWrites ? (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) : GL_DEPTH_BUFFER_BIT); // Render to color buffer without writing to depth buffer. glDepthMask(GL_FALSE); drawTextWithBackdrop(state,colorMultiplier); // Render to depth buffer if depth writes requested. if( _enableDepthWrites ) { glDepthMask(GL_TRUE); glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); drawTextWithBackdrop(state,colorMultiplier); } state.haveAppliedAttribute(osg::StateAttribute::DEPTH); state.haveAppliedAttribute(osg::StateAttribute::COLORMASK); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); //glPopAttrib(); } void Text::drawTextWithBackdrop(osg::State& state, const osg::Vec4& colorMultiplier) const { unsigned int contextID = state.getContextID(); for(TextureGlyphQuadMap::iterator titr=_textureGlyphQuadMap.begin(); titr!=_textureGlyphQuadMap.end(); ++titr) { // need to set the texture here... state.applyTextureAttribute(0,titr->first.get()); const GlyphQuads& glyphquad = titr->second; if(_backdropType != NONE) { unsigned int backdrop_index; unsigned int max_backdrop_index; if(_backdropType == OUTLINE) { backdrop_index = 0; max_backdrop_index = 8; } else { backdrop_index = _backdropType; max_backdrop_index = _backdropType+1; } state.setTexCoordPointer(0, glyphquad._texcoords.get()); state.disableColorPointer(); state.Color(_backdropColor.r(),_backdropColor.g(),_backdropColor.b(),_backdropColor.a()); for( ; backdrop_index < max_backdrop_index; backdrop_index++) { const GlyphQuads::Coords3& transformedBackdropCoords = glyphquad._transformedBackdropCoords[backdrop_index][contextID]; if (transformedBackdropCoords.valid() && !transformedBackdropCoords->empty()) { state.setVertexPointer(transformedBackdropCoords.get()); glyphquad._quadIndices->draw(state, _useVertexBufferObjects); } } } drawForegroundText(state, glyphquad, colorMultiplier); } } void Text::renderWithPolygonOffset(osg::State& state, const osg::Vec4& colorMultiplier) const { #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) && !defined(OSG_GL3_AVAILABLE) unsigned int contextID = state.getContextID(); if (!osg::PolygonOffset::areFactorAndUnitsMultipliersSet()) { osg::PolygonOffset::setFactorAndUnitsMultipliersUsingBestGuessForDriver(); } // Do I really need to do this for glPolygonOffset? glPushAttrib(GL_POLYGON_OFFSET_FILL); glEnable(GL_POLYGON_OFFSET_FILL); for(TextureGlyphQuadMap::iterator titr=_textureGlyphQuadMap.begin(); titr!=_textureGlyphQuadMap.end(); ++titr) { // need to set the texture here... state.applyTextureAttribute(0,titr->first.get()); const GlyphQuads& glyphquad = titr->second; unsigned int backdrop_index; unsigned int max_backdrop_index; if(_backdropType == OUTLINE) { backdrop_index = 0; max_backdrop_index = 8; } else { backdrop_index = _backdropType; max_backdrop_index = _backdropType+1; } state.setTexCoordPointer( 0, glyphquad._texcoords.get()); state.disableColorPointer(); state.Color(_backdropColor.r(),_backdropColor.g(),_backdropColor.b(),_backdropColor.a()); for( ; backdrop_index < max_backdrop_index; backdrop_index++) { const GlyphQuads::Coords3& transformedBackdropCoords = glyphquad._transformedBackdropCoords[backdrop_index][contextID]; if (transformedBackdropCoords.valid() && !transformedBackdropCoords->empty()) { state.setVertexPointer( transformedBackdropCoords.get()); glPolygonOffset(0.1f * osg::PolygonOffset::getFactorMultiplier(), osg::PolygonOffset::getUnitsMultiplier() * (max_backdrop_index-backdrop_index) ); state.drawQuads(0,transformedBackdropCoords->size()); } } // Reset the polygon offset so the foreground text is on top glPolygonOffset(0.0f,0.0f); drawForegroundText(state, glyphquad, colorMultiplier); } glPopAttrib(); #else OSG_NOTICE<<"Warning: Text::renderWithPolygonOffset(..) not implemented."<first.get()); const GlyphQuads& glyphquad = titr->second; unsigned int backdrop_index; unsigned int max_backdrop_index; if(_backdropType == OUTLINE) { backdrop_index = 0; max_backdrop_index = 8; } else { backdrop_index = _backdropType; max_backdrop_index = _backdropType+1; } state.setTexCoordPointer( 0, glyphquad._texcoords.get()); state.disableColorPointer(); state.Color(_backdropColor.r(),_backdropColor.g(),_backdropColor.b(),_backdropColor.a()); for( ; backdrop_index < max_backdrop_index; backdrop_index++) { const GlyphQuads::Coords3& transformedBackdropCoords = glyphquad._transformedBackdropCoords[backdrop_index][contextID]; if (transformedBackdropCoords.valid() && !transformedBackdropCoords->empty()) { state.setVertexPointer( transformedBackdropCoords.get()); state.drawQuads(0,transformedBackdropCoords->size()); } } drawForegroundText(state, glyphquad, colorMultiplier); } glPopAttrib(); #else OSG_NOTICE<<"Warning: Text::renderWithNoDepthBuffer(..) not implemented."<first.get()); const GlyphQuads& glyphquad = titr->second; unsigned int backdrop_index; unsigned int max_backdrop_index; if(_backdropType == OUTLINE) { backdrop_index = 0; max_backdrop_index = 8; } else { backdrop_index = _backdropType; max_backdrop_index = _backdropType+1; } state.setTexCoordPointer( 0, glyphquad._texcoords.get()); state.disableColorPointer(); state.Color(_backdropColor.r(),_backdropColor.g(),_backdropColor.b(),_backdropColor.a()); for( ; backdrop_index < max_backdrop_index; backdrop_index++) { const GlyphQuads::Coords3& transformedBackdropCoords = glyphquad._transformedBackdropCoords[backdrop_index][contextID]; if (transformedBackdropCoords.valid() && !transformedBackdropCoords->empty()) { state.setVertexPointer( transformedBackdropCoords.get()); double offset = double(max_backdrop_index-backdrop_index)*0.0001; glDepthRange( offset, 1.0+offset); state.drawQuads(0,transformedBackdropCoords->size()); } } glDepthRange(0.0, 1.0); drawForegroundText(state, glyphquad, colorMultiplier); } glPopAttrib(); #else OSG_NOTICE<<"Warning: Text::renderWithDepthRange(..) not implemented."<first.get()); const GlyphQuads& glyphquad = titr->second; unsigned int backdrop_index; unsigned int max_backdrop_index; if(_backdropType == OUTLINE) { backdrop_index = 0; max_backdrop_index = 8; } else { backdrop_index = _backdropType; max_backdrop_index = _backdropType+1; } state.setTexCoordPointer( 0, glyphquad._texcoords.get()); state.disableColorPointer(); for( ; backdrop_index < max_backdrop_index; backdrop_index++) { const GlyphQuads::Coords3& transformedBackdropCoords = glyphquad._transformedBackdropCoords[backdrop_index][contextID]; if (transformedBackdropCoords.valid() && !transformedBackdropCoords->empty()) { state.setVertexPointer( transformedBackdropCoords.get()); state.drawQuads(0,transformedBackdropCoords->size()); } } // Draw the foreground text const GlyphQuads::Coords3& transformedCoords = glyphquad._transformedCoords[contextID]; if (transformedCoords.valid() && !transformedCoords->empty()) { state.setVertexPointer( transformedCoords.get()); state.setTexCoordPointer( 0, glyphquad._texcoords.get()); state.drawQuads(0, transformedCoords->size()); } } // disable the depth buffer // glDisable(GL_DEPTH_TEST); // glDepthMask(GL_FALSE); // glDepthMask(GL_TRUE); // glDepthFunc(GL_ALWAYS); // Set the stencil function to pass when the stencil is 1 // Bug: This call seems to have no effect. Try changing to NOTEQUAL // and see the exact same results. glStencilFunc(GL_EQUAL, 1, 1); // disable writing to the stencil buffer glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); glStencilMask(GL_FALSE); // Re-enable writing to the color buffer so we can see the results glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); // Draw all the text again for(titr=_textureGlyphQuadMap.begin(); titr!=_textureGlyphQuadMap.end(); ++titr) { // need to set the texture here... state.applyTextureAttribute(0,titr->first.get()); const GlyphQuads& glyphquad = titr->second; unsigned int backdrop_index; unsigned int max_backdrop_index; if(_backdropType == OUTLINE) { backdrop_index = 0; max_backdrop_index = 8; } else { backdrop_index = _backdropType; max_backdrop_index = _backdropType+1; } state.setTexCoordPointer( 0, glyphquad._texcoords.get()); state.disableColorPointer(); state.Color(_backdropColor.r(),_backdropColor.g(),_backdropColor.b(),_backdropColor.a()); for( ; backdrop_index < max_backdrop_index; backdrop_index++) { const GlyphQuads::Coords3& transformedBackdropCoords = glyphquad._transformedBackdropCoords[backdrop_index][contextID]; if (transformedBackdropCoords.valid() && !transformedBackdropCoords->empty()) { state.setVertexPointer( transformedBackdropCoords.get()); state.drawQuads(0,transformedBackdropCoords->size()); } } drawForegroundText(state, glyphquad, colorMultiplier); } glPopAttrib(); #else OSG_NOTICE<<"Warning: Text::renderWithStencilBuffer(..) not implemented."<clear(); if (_coords->size() % 4 != 0) { OSG_WARN << "size of _coords is not divisible by 4."; } for (unsigned short i = 0; i < (unsigned short)_coords->size(); i += 4) { _quadIndices->push_back(i); _quadIndices->push_back(i + 1); _quadIndices->push_back(i + 3); _quadIndices->push_back(i + 1); _quadIndices->push_back(i + 2); _quadIndices->push_back(i + 3); } } void Text::GlyphQuads::initGPUBufferObjects() { osg::VertexBufferObject* vbo = new osg::VertexBufferObject(); _coords->setBinding(osg::Array::BIND_PER_VERTEX); _coords->setVertexBufferObject(vbo); _texcoords->setBinding(osg::Array::BIND_PER_VERTEX); _texcoords->setVertexBufferObject(vbo); _colorCoords->setBinding(osg::Array::BIND_PER_VERTEX); _colorCoords->setVertexBufferObject(vbo); for (size_t i = 0; i < _transformedCoords.size(); i++) { if (_transformedCoords[i].valid()) { _transformedCoords[i]->setBinding(osg::Array::BIND_PER_VERTEX); _transformedCoords[i]->setVertexBufferObject(vbo); } } for (int j = 0; j < 8; j++) { for (size_t i = 0; i < _transformedBackdropCoords[j].size(); i++) { if (_transformedBackdropCoords[j][i].valid()) { _transformedBackdropCoords[j][i]->setBinding(osg::Array::BIND_PER_VERTEX); _transformedBackdropCoords[j][i]->setVertexBufferObject(vbo); } } } _quadIndices->setElementBufferObject(new osg::ElementBufferObject()); } void Text::GlyphQuads::resizeGLObjectBuffers(unsigned int maxSize) { _transformedCoords.resize(maxSize); for (int j = 0; j < 8; j++) { for (size_t i = 0; i < _transformedBackdropCoords[j].size(); i++) { if (_transformedBackdropCoords[j][i].valid()) { _transformedBackdropCoords[j][i]->resizeGLObjectBuffers(maxSize); } } } _quadIndices->resizeGLObjectBuffers(maxSize); initGPUBufferObjects(); } void Text::GlyphQuads::releaseGLObjects(osg::State* state) const { for (int j = 0; j < 8; j++) { for (size_t i = 0; i < _transformedBackdropCoords[j].size(); i++) { if (_transformedBackdropCoords[j][i].valid()) { _transformedBackdropCoords[j][i]->releaseGLObjects(state); } } } _quadIndices->releaseGLObjects(state); }