| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osgText/Text> |
|---|
| 16 | |
|---|
| 17 | #include <osg/Math> |
|---|
| 18 | #include <osg/GL> |
|---|
| 19 | #include <osg/Notify> |
|---|
| 20 | #include <osg/PolygonOffset> |
|---|
| 21 | #include <osg/TexEnv> |
|---|
| 22 | |
|---|
| 23 | #include <osgUtil/CullVisitor> |
|---|
| 24 | |
|---|
| 25 | #include <osgDB/ReadFile> |
|---|
| 26 | |
|---|
| 27 | using namespace osg; |
|---|
| 28 | using namespace osgText; |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | Text::Text(): |
|---|
| 33 | _enableDepthWrites(true), |
|---|
| 34 | _backdropType(NONE), |
|---|
| 35 | _backdropImplementation(DELAYED_DEPTH_WRITES), |
|---|
| 36 | _backdropHorizontalOffset(0.07f), |
|---|
| 37 | _backdropVerticalOffset(0.07f), |
|---|
| 38 | _backdropColor(0.0f, 0.0f, 0.0f, 1.0f), |
|---|
| 39 | _colorGradientMode(SOLID), |
|---|
| 40 | _colorGradientTopLeft(1.0f, 0.0f, 0.0f, 1.0f), |
|---|
| 41 | _colorGradientBottomLeft(0.0f, 1.0f, 0.0f, 1.0f), |
|---|
| 42 | _colorGradientBottomRight(0.0f, 0.0f, 1.0f, 1.0f), |
|---|
| 43 | _colorGradientTopRight(1.0f, 1.0f, 1.0f, 1.0f) |
|---|
| 44 | {} |
|---|
| 45 | |
|---|
| 46 | Text::Text(const Text& text,const osg::CopyOp& copyop): |
|---|
| 47 | osgText::TextBase(text,copyop), |
|---|
| 48 | _enableDepthWrites(text._enableDepthWrites), |
|---|
| 49 | _backdropType(text._backdropType), |
|---|
| 50 | _backdropImplementation(text._backdropImplementation), |
|---|
| 51 | _backdropHorizontalOffset(text._backdropHorizontalOffset), |
|---|
| 52 | _backdropVerticalOffset(text._backdropVerticalOffset), |
|---|
| 53 | _backdropColor(text._backdropColor), |
|---|
| 54 | _colorGradientMode(text._colorGradientMode), |
|---|
| 55 | _colorGradientTopLeft(text._colorGradientTopLeft), |
|---|
| 56 | _colorGradientBottomLeft(text._colorGradientBottomLeft), |
|---|
| 57 | _colorGradientBottomRight(text._colorGradientBottomRight), |
|---|
| 58 | _colorGradientTopRight(text._colorGradientTopRight) |
|---|
| 59 | { |
|---|
| 60 | computeGlyphRepresentation(); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | Text::~Text() |
|---|
| 64 | { |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void Text::setFont(osg::ref_ptr<Font> font) |
|---|
| 68 | { |
|---|
| 69 | if (_font==font) return; |
|---|
| 70 | |
|---|
| 71 | osg::StateSet* previousFontStateSet = _font.valid() ? _font->getStateSet() : Font::getDefaultFont()->getStateSet(); |
|---|
| 72 | osg::StateSet* newFontStateSet = font.valid() ? font->getStateSet() : Font::getDefaultFont()->getStateSet(); |
|---|
| 73 | |
|---|
| 74 | if (getStateSet() == previousFontStateSet) |
|---|
| 75 | { |
|---|
| 76 | setStateSet( newFontStateSet ); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | TextBase::setFont(font); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | Font* Text::getActiveFont() |
|---|
| 84 | { |
|---|
| 85 | return _font.valid() ? _font.get() : Font::getDefaultFont().get(); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | const Font* Text::getActiveFont() const |
|---|
| 89 | { |
|---|
| 90 | return _font.valid() ? _font.get() : Font::getDefaultFont().get(); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | String::iterator Text::computeLastCharacterOnLine(osg::Vec2& cursor, String::iterator first,String::iterator last) |
|---|
| 94 | { |
|---|
| 95 | Font* activefont = getActiveFont(); |
|---|
| 96 | if (!activefont) return last; |
|---|
| 97 | |
|---|
| 98 | float hr = _characterHeight; |
|---|
| 99 | float wr = hr/getCharacterAspectRatio(); |
|---|
| 100 | |
|---|
| 101 | bool kerning = true; |
|---|
| 102 | unsigned int previous_charcode = 0; |
|---|
| 103 | |
|---|
| 104 | String::iterator lastChar = first; |
|---|
| 105 | |
|---|
| 106 | for(bool outOfSpace=false;lastChar!=last;++lastChar) |
|---|
| 107 | { |
|---|
| 108 | unsigned int charcode = *lastChar; |
|---|
| 109 | |
|---|
| 110 | if (charcode=='\n') |
|---|
| 111 | { |
|---|
| 112 | return lastChar; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | Glyph* glyph = activefont->getGlyph(_fontSize, charcode); |
|---|
| 116 | if (glyph) |
|---|
| 117 | { |
|---|
| 118 | |
|---|
| 119 | float width = (float)(glyph->getWidth()) * wr; |
|---|
| 120 | |
|---|
| 121 | if (_layout==RIGHT_TO_LEFT) |
|---|
| 122 | { |
|---|
| 123 | cursor.x() -= glyph->getHorizontalAdvance() * wr; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | if (kerning && previous_charcode) |
|---|
| 128 | { |
|---|
| 129 | switch(_layout) |
|---|
| 130 | { |
|---|
| 131 | case LEFT_TO_RIGHT: |
|---|
| 132 | { |
|---|
| 133 | osg::Vec2 delta(activefont->getKerning(previous_charcode,charcode,_kerningType)); |
|---|
| 134 | cursor.x() += delta.x() * wr; |
|---|
| 135 | cursor.y() += delta.y() * hr; |
|---|
| 136 | break; |
|---|
| 137 | } |
|---|
| 138 | case RIGHT_TO_LEFT: |
|---|
| 139 | { |
|---|
| 140 | osg::Vec2 delta(activefont->getKerning(charcode,previous_charcode,_kerningType)); |
|---|
| 141 | cursor.x() -= delta.x() * wr; |
|---|
| 142 | cursor.y() -= delta.y() * hr; |
|---|
| 143 | break; |
|---|
| 144 | } |
|---|
| 145 | case VERTICAL: |
|---|
| 146 | break; |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | switch(_layout) |
|---|
| 151 | { |
|---|
| 152 | case LEFT_TO_RIGHT: |
|---|
| 153 | { |
|---|
| 154 | if (_maximumWidth>0.0f && cursor.x()+width>_maximumWidth) outOfSpace=true; |
|---|
| 155 | if(_maximumHeight>0.0f && cursor.y()<-_maximumHeight) outOfSpace=true; |
|---|
| 156 | break; |
|---|
| 157 | } |
|---|
| 158 | case RIGHT_TO_LEFT: |
|---|
| 159 | { |
|---|
| 160 | if (_maximumWidth>0.0f && cursor.x()<-_maximumWidth) outOfSpace=true; |
|---|
| 161 | if(_maximumHeight>0.0f && cursor.y()<-_maximumHeight) outOfSpace=true; |
|---|
| 162 | break; |
|---|
| 163 | } |
|---|
| 164 | case VERTICAL: |
|---|
| 165 | if (_maximumHeight>0.0f && cursor.y()<-_maximumHeight) outOfSpace=true; |
|---|
| 166 | break; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | if (outOfSpace) break; |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | switch(_layout) |
|---|
| 174 | { |
|---|
| 175 | case LEFT_TO_RIGHT: cursor.x() += glyph->getHorizontalAdvance() * wr; break; |
|---|
| 176 | case VERTICAL: cursor.y() -= glyph->getVerticalAdvance() *hr; break; |
|---|
| 177 | case RIGHT_TO_LEFT: break; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | previous_charcode = charcode; |
|---|
| 181 | |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | if (lastChar!=last) |
|---|
| 188 | { |
|---|
| 189 | String::iterator lastValidChar = lastChar; |
|---|
| 190 | String::iterator prevChar; |
|---|
| 191 | while (lastValidChar != first){ |
|---|
| 192 | prevChar = lastValidChar - 1; |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | if(*lastValidChar == '-') |
|---|
| 196 | return lastValidChar + 1; |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | if((*lastValidChar == ' ' || *lastValidChar == '\n') && (*prevChar != ' ' && *prevChar != '\n')) |
|---|
| 200 | return lastValidChar; |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | if(*prevChar != '-') |
|---|
| 204 | { |
|---|
| 205 | Glyph* glyph = activefont->getGlyph(_fontSize, *prevChar); |
|---|
| 206 | if (glyph) |
|---|
| 207 | { |
|---|
| 208 | switch(_layout) |
|---|
| 209 | { |
|---|
| 210 | case LEFT_TO_RIGHT: cursor.x() -= glyph->getHorizontalAdvance() * wr; break; |
|---|
| 211 | case VERTICAL: cursor.y() += glyph->getVerticalAdvance() * hr; break; |
|---|
| 212 | case RIGHT_TO_LEFT: break; |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | lastValidChar = prevChar; |
|---|
| 218 | } |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | return lastChar; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | void Text::computeGlyphRepresentation() |
|---|
| 226 | { |
|---|
| 227 | Font* activefont = getActiveFont(); |
|---|
| 228 | if (!activefont) return; |
|---|
| 229 | |
|---|
| 230 | _textureGlyphQuadMap.clear(); |
|---|
| 231 | _lineCount = 0; |
|---|
| 232 | |
|---|
| 233 | if (_text.empty()) |
|---|
| 234 | { |
|---|
| 235 | _textBB.set(0,0,0,0,0,0); |
|---|
| 236 | TextBase::computePositions(); |
|---|
| 237 | return; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | _textBB.init(); |
|---|
| 244 | |
|---|
| 245 | osg::Vec2 startOfLine_coords(0.0f,0.0f); |
|---|
| 246 | osg::Vec2 cursor(startOfLine_coords); |
|---|
| 247 | osg::Vec2 local(0.0f,0.0f); |
|---|
| 248 | |
|---|
| 249 | unsigned int previous_charcode = 0; |
|---|
| 250 | unsigned int linelength = 0; |
|---|
| 251 | bool horizontal = _layout!=VERTICAL; |
|---|
| 252 | bool kerning = true; |
|---|
| 253 | |
|---|
| 254 | unsigned int lineNumber = 0; |
|---|
| 255 | |
|---|
| 256 | float hr = _characterHeight; |
|---|
| 257 | float wr = hr/getCharacterAspectRatio(); |
|---|
| 258 | |
|---|
| 259 | for(String::iterator itr=_text.begin(); |
|---|
| 260 | itr!=_text.end(); |
|---|
| 261 | ) |
|---|
| 262 | { |
|---|
| 263 | |
|---|
| 264 | String::iterator startOfLine_itr = itr; |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | osg::Vec2 endOfLine_coords(cursor); |
|---|
| 268 | String::iterator endOfLine_itr = computeLastCharacterOnLine(endOfLine_coords, itr,_text.end()); |
|---|
| 269 | |
|---|
| 270 | linelength = endOfLine_itr - startOfLine_itr; |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | switch(_layout) |
|---|
| 274 | { |
|---|
| 275 | case LEFT_TO_RIGHT: |
|---|
| 276 | { |
|---|
| 277 | switch(_alignment) |
|---|
| 278 | { |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | case CENTER_TOP: |
|---|
| 287 | case CENTER_CENTER: |
|---|
| 288 | case CENTER_BOTTOM: |
|---|
| 289 | case CENTER_BASE_LINE: |
|---|
| 290 | case CENTER_BOTTOM_BASE_LINE: |
|---|
| 291 | cursor.x() = (cursor.x() - endOfLine_coords.x()) * 0.5f; |
|---|
| 292 | break; |
|---|
| 293 | case RIGHT_TOP: |
|---|
| 294 | case RIGHT_CENTER: |
|---|
| 295 | case RIGHT_BOTTOM: |
|---|
| 296 | case RIGHT_BASE_LINE: |
|---|
| 297 | case RIGHT_BOTTOM_BASE_LINE: |
|---|
| 298 | cursor.x() = cursor.x() - endOfLine_coords.x(); |
|---|
| 299 | break; |
|---|
| 300 | default: |
|---|
| 301 | break; |
|---|
| 302 | } |
|---|
| 303 | break; |
|---|
| 304 | } |
|---|
| 305 | case RIGHT_TO_LEFT: |
|---|
| 306 | { |
|---|
| 307 | switch(_alignment) |
|---|
| 308 | { |
|---|
| 309 | case LEFT_TOP: |
|---|
| 310 | case LEFT_CENTER: |
|---|
| 311 | case LEFT_BOTTOM: |
|---|
| 312 | case LEFT_BASE_LINE: |
|---|
| 313 | case LEFT_BOTTOM_BASE_LINE: |
|---|
| 314 | cursor.x() = 2*cursor.x() - endOfLine_coords.x(); |
|---|
| 315 | break; |
|---|
| 316 | case CENTER_TOP: |
|---|
| 317 | case CENTER_CENTER: |
|---|
| 318 | case CENTER_BOTTOM: |
|---|
| 319 | case CENTER_BASE_LINE: |
|---|
| 320 | case CENTER_BOTTOM_BASE_LINE: |
|---|
| 321 | cursor.x() = cursor.x() + (cursor.x() - endOfLine_coords.x()) * 0.5f; |
|---|
| 322 | break; |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | default: |
|---|
| 331 | break; |
|---|
| 332 | } |
|---|
| 333 | break; |
|---|
| 334 | } |
|---|
| 335 | case VERTICAL: |
|---|
| 336 | { |
|---|
| 337 | switch(_alignment) |
|---|
| 338 | { |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | case LEFT_CENTER: |
|---|
| 351 | case CENTER_CENTER: |
|---|
| 352 | case RIGHT_CENTER: |
|---|
| 353 | cursor.y() = cursor.y() + (cursor.y() - endOfLine_coords.y()) * 0.5f; |
|---|
| 354 | break; |
|---|
| 355 | case LEFT_BOTTOM_BASE_LINE: |
|---|
| 356 | case CENTER_BOTTOM_BASE_LINE: |
|---|
| 357 | case RIGHT_BOTTOM_BASE_LINE: |
|---|
| 358 | cursor.y() = cursor.y() - (linelength * _characterHeight); |
|---|
| 359 | break; |
|---|
| 360 | case LEFT_BOTTOM: |
|---|
| 361 | case CENTER_BOTTOM: |
|---|
| 362 | case RIGHT_BOTTOM: |
|---|
| 363 | cursor.y() = 2*cursor.y() - endOfLine_coords.y(); |
|---|
| 364 | break; |
|---|
| 365 | default: |
|---|
| 366 | break; |
|---|
| 367 | } |
|---|
| 368 | break; |
|---|
| 369 | } |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | if (itr!=endOfLine_itr) |
|---|
| 373 | { |
|---|
| 374 | |
|---|
| 375 | for(;itr!=endOfLine_itr;++itr) |
|---|
| 376 | { |
|---|
| 377 | unsigned int charcode = *itr; |
|---|
| 378 | |
|---|
| 379 | Glyph* glyph = activefont->getGlyph(_fontSize, charcode); |
|---|
| 380 | if (glyph) |
|---|
| 381 | { |
|---|
| 382 | float width = (float)(glyph->getWidth()) * wr; |
|---|
| 383 | float height = (float)(glyph->getHeight()) * hr; |
|---|
| 384 | |
|---|
| 385 | if (_layout==RIGHT_TO_LEFT) |
|---|
| 386 | { |
|---|
| 387 | cursor.x() -= glyph->getHorizontalAdvance() * wr; |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | if (kerning && previous_charcode) |
|---|
| 392 | { |
|---|
| 393 | switch(_layout) |
|---|
| 394 | { |
|---|
| 395 | case LEFT_TO_RIGHT: |
|---|
| 396 | { |
|---|
| 397 | osg::Vec2 delta(activefont->getKerning(previous_charcode,charcode,_kerningType)); |
|---|
| 398 | cursor.x() += delta.x() * wr; |
|---|
| 399 | cursor.y() += delta.y() * hr; |
|---|
| 400 | break; |
|---|
| 401 | } |
|---|
| 402 | case RIGHT_TO_LEFT: |
|---|
| 403 | { |
|---|
| 404 | osg::Vec2 delta(activefont->getKerning(charcode,previous_charcode,_kerningType)); |
|---|
| 405 | cursor.x() -= delta.x() * wr; |
|---|
| 406 | cursor.y() -= delta.y() * hr; |
|---|
| 407 | break; |
|---|
| 408 | } |
|---|
| 409 | case VERTICAL: |
|---|
| 410 | break; |
|---|
| 411 | } |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | local = cursor; |
|---|
| 415 | osg::Vec2 bearing(horizontal?glyph->getHorizontalBearing():glyph->getVerticalBearing()); |
|---|
| 416 | local.x() += bearing.x() * wr; |
|---|
| 417 | local.y() += bearing.y() * hr; |
|---|
| 418 | |
|---|
| 419 | GlyphQuads& glyphquad = _textureGlyphQuadMap[glyph->getTexture()]; |
|---|
| 420 | |
|---|
| 421 | glyphquad._glyphs.push_back(glyph); |
|---|
| 422 | glyphquad._lineNumbers.push_back(lineNumber); |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | osg::Vec2 mintc = glyph->getMinTexCoord(); |
|---|
| 427 | osg::Vec2 maxtc = glyph->getMaxTexCoord(); |
|---|
| 428 | osg::Vec2 vDiff = maxtc - mintc; |
|---|
| 429 | |
|---|
| 430 | float fHorizTCMargin = 1.0f / glyph->getTexture()->getTextureWidth(); |
|---|
| 431 | float fVertTCMargin = 1.0f / glyph->getTexture()->getTextureHeight(); |
|---|
| 432 | float fHorizQuadMargin = vDiff.x() == 0.0f ? 0.0f : width * fHorizTCMargin / vDiff.x(); |
|---|
| 433 | float fVertQuadMargin = vDiff.y() == 0.0f ? 0.0f : height * fVertTCMargin / vDiff.y(); |
|---|
| 434 | |
|---|
| 435 | mintc.x() -= fHorizTCMargin; |
|---|
| 436 | mintc.y() -= fVertTCMargin; |
|---|
| 437 | maxtc.x() += fHorizTCMargin; |
|---|
| 438 | maxtc.y() += fVertTCMargin; |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | osg::Vec2 upLeft = local+osg::Vec2(0.0f-fHorizQuadMargin,height+fVertQuadMargin); |
|---|
| 442 | osg::Vec2 lowLeft = local+osg::Vec2(0.0f-fHorizQuadMargin,0.0f-fVertQuadMargin); |
|---|
| 443 | osg::Vec2 lowRight = local+osg::Vec2(width+fHorizQuadMargin,0.0f-fVertQuadMargin); |
|---|
| 444 | osg::Vec2 upRight = local+osg::Vec2(width+fHorizQuadMargin,height+fVertQuadMargin); |
|---|
| 445 | glyphquad._coords.push_back(upLeft); |
|---|
| 446 | glyphquad._coords.push_back(lowLeft); |
|---|
| 447 | glyphquad._coords.push_back(lowRight); |
|---|
| 448 | glyphquad._coords.push_back(upRight); |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | glyphquad._texcoords.push_back(osg::Vec2(mintc.x(),maxtc.y())); |
|---|
| 452 | glyphquad._texcoords.push_back(osg::Vec2(mintc.x(),mintc.y())); |
|---|
| 453 | glyphquad._texcoords.push_back(osg::Vec2(maxtc.x(),mintc.y())); |
|---|
| 454 | glyphquad._texcoords.push_back(osg::Vec2(maxtc.x(),maxtc.y())); |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | switch(_layout) |
|---|
| 459 | { |
|---|
| 460 | case LEFT_TO_RIGHT: |
|---|
| 461 | cursor.x() += glyph->getHorizontalAdvance() * wr; |
|---|
| 462 | _textBB.expandBy(osg::Vec3(lowLeft.x(), lowLeft.y(), 0.0f)); |
|---|
| 463 | _textBB.expandBy(osg::Vec3(upRight.x(), upRight.y(), 0.0f)); |
|---|
| 464 | break; |
|---|
| 465 | case VERTICAL: |
|---|
| 466 | cursor.y() -= glyph->getVerticalAdvance() * hr; |
|---|
| 467 | _textBB.expandBy(osg::Vec3(upLeft.x(),upLeft.y(),0.0f)); |
|---|
| 468 | _textBB.expandBy(osg::Vec3(lowRight.x(),lowRight.y(),0.0f)); |
|---|
| 469 | break; |
|---|
| 470 | case RIGHT_TO_LEFT: |
|---|
| 471 | _textBB.expandBy(osg::Vec3(lowRight.x(),lowRight.y(),0.0f)); |
|---|
| 472 | _textBB.expandBy(osg::Vec3(upLeft.x(),upLeft.y(),0.0f)); |
|---|
| 473 | break; |
|---|
| 474 | } |
|---|
| 475 | previous_charcode = charcode; |
|---|
| 476 | |
|---|
| 477 | } |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | |
|---|
| 481 | while (itr != _text.end() && *itr==' ') ++itr; |
|---|
| 482 | if (itr != _text.end() && *itr=='\n') ++itr; |
|---|
| 483 | } |
|---|
| 484 | else |
|---|
| 485 | { |
|---|
| 486 | ++itr; |
|---|
| 487 | } |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | |
|---|
| 491 | switch(_layout) |
|---|
| 492 | { |
|---|
| 493 | case LEFT_TO_RIGHT: |
|---|
| 494 | { |
|---|
| 495 | startOfLine_coords.y() -= _characterHeight * (1.0 + _lineSpacing); |
|---|
| 496 | cursor = startOfLine_coords; |
|---|
| 497 | previous_charcode = 0; |
|---|
| 498 | _lineCount++; |
|---|
| 499 | break; |
|---|
| 500 | } |
|---|
| 501 | case RIGHT_TO_LEFT: |
|---|
| 502 | { |
|---|
| 503 | startOfLine_coords.y() -= _characterHeight * (1.0 + _lineSpacing); |
|---|
| 504 | cursor = startOfLine_coords; |
|---|
| 505 | previous_charcode = 0; |
|---|
| 506 | _lineCount++; |
|---|
| 507 | break; |
|---|
| 508 | } |
|---|
| 509 | case VERTICAL: |
|---|
| 510 | { |
|---|
| 511 | startOfLine_coords.x() += _characterHeight/getCharacterAspectRatio() * (1.0 + _lineSpacing); |
|---|
| 512 | cursor = startOfLine_coords; |
|---|
| 513 | previous_charcode = 0; |
|---|
| 514 | |
|---|
| 515 | _lineCount = (_lineCount >linelength)?_lineCount:linelength; |
|---|
| 516 | } |
|---|
| 517 | break; |
|---|
| 518 | } |
|---|
| 519 | |
|---|
| 520 | ++lineNumber; |
|---|
| 521 | |
|---|
| 522 | } |
|---|
| 523 | |
|---|
| 524 | TextBase::computePositions(); |
|---|
| 525 | computeBackdropBoundingBox(); |
|---|
| 526 | computeBoundingBoxMargin(); |
|---|
| 527 | computeColorGradients(); |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | |
|---|
| 532 | |
|---|
| 533 | |
|---|
| 534 | bool Text::computeAverageGlyphWidthAndHeight(float& avg_width, float& avg_height) const |
|---|
| 535 | { |
|---|
| 536 | float width = 0.0f; |
|---|
| 537 | float height = 0.0f; |
|---|
| 538 | float running_width = 0.0f; |
|---|
| 539 | float running_height = 0.0f; |
|---|
| 540 | avg_width = 0.0f; |
|---|
| 541 | avg_height = 0.0f; |
|---|
| 542 | int counter = 0; |
|---|
| 543 | unsigned int i; |
|---|
| 544 | bool is_valid_size = true; |
|---|
| 545 | |
|---|
| 546 | |
|---|
| 547 | |
|---|
| 548 | |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | |
|---|
| 552 | |
|---|
| 553 | |
|---|
| 554 | for(TextureGlyphQuadMap::const_iterator const_titr=_textureGlyphQuadMap.begin(); |
|---|
| 555 | const_titr!=_textureGlyphQuadMap.end(); |
|---|
| 556 | ++const_titr) |
|---|
| 557 | { |
|---|
| 558 | const GlyphQuads& glyphquad = const_titr->second; |
|---|
| 559 | const GlyphQuads::Coords2& coords2 = glyphquad._coords; |
|---|
| 560 | for(i = 0; i < coords2.size(); i+=4) |
|---|
| 561 | { |
|---|
| 562 | width = coords2[i+2].x() - coords2[i].x(); |
|---|
| 563 | height = coords2[i].y() - coords2[i+1].y(); |
|---|
| 564 | |
|---|
| 565 | running_width += width; |
|---|
| 566 | running_height += height; |
|---|
| 567 | counter++; |
|---|
| 568 | } |
|---|
| 569 | } |
|---|
| 570 | if(0 == counter) |
|---|
| 571 | { |
|---|
| 572 | is_valid_size = false; |
|---|
| 573 | } |
|---|
| 574 | else |
|---|
| 575 | { |
|---|
| 576 | avg_width = running_width/counter; |
|---|
| 577 | avg_height = running_height/counter; |
|---|
| 578 | } |
|---|
| 579 | return is_valid_size; |
|---|
| 580 | } |
|---|
| 581 | |
|---|
| 582 | |
|---|
| 583 | void Text::computePositions(unsigned int contextID) const |
|---|
| 584 | { |
|---|
| 585 | switch(_alignment) |
|---|
| 586 | { |
|---|
| 587 | case LEFT_TOP: _offset.set(_textBB.xMin(),_textBB.yMax(),_textBB.zMin()); break; |
|---|
| 588 | case LEFT_CENTER: _offset.set(_textBB.xMin(),(_textBB.yMax()+_textBB.yMin())*0.5f,_textBB.zMin()); break; |
|---|
| 589 | case LEFT_BOTTOM: _offset.set(_textBB.xMin(),_textBB.yMin(),_textBB.zMin()); break; |
|---|
| 590 | |
|---|
| 591 | case CENTER_TOP: _offset.set((_textBB.xMax()+_textBB.xMin())*0.5f,_textBB.yMax(),_textBB.zMin()); break; |
|---|
| 592 | case CENTER_CENTER: _offset.set((_textBB.xMax()+_textBB.xMin())*0.5f,(_textBB.yMax()+_textBB.yMin())*0.5f,_textBB.zMin()); break; |
|---|
| 593 | case CENTER_BOTTOM: _offset.set((_textBB.xMax()+_textBB.xMin())*0.5f,_textBB.yMin(),_textBB.zMin()); break; |
|---|
| 594 | |
|---|
| 595 | case RIGHT_TOP: _offset.set(_textBB.xMax(),_textBB.yMax(),_textBB.zMin()); break; |
|---|
| 596 | case RIGHT_CENTER: _offset.set(_textBB.xMax(),(_textBB.yMax()+_textBB.yMin())*0.5f,_textBB.zMin()); break; |
|---|
| 597 | case RIGHT_BOTTOM: _offset.set(_textBB.xMax(),_textBB.yMin(),_textBB.zMin()); break; |
|---|
| 598 | |
|---|
| 599 | case LEFT_BASE_LINE: _offset.set(0.0f,0.0f,0.0f); break; |
|---|
| 600 | case CENTER_BASE_LINE: _offset.set((_textBB.xMax()+_textBB.xMin())*0.5f,0.0f,0.0f); break; |
|---|
| 601 | case RIGHT_BASE_LINE: _offset.set(_textBB.xMax(),0.0f,0.0f); break; |
|---|
| 602 | |
|---|
| 603 | case LEFT_BOTTOM_BASE_LINE: _offset.set(0.0f,-_characterHeight*(1.0 + _lineSpacing)*(_lineCount-1),0.0f); break; |
|---|
| 604 | case CENTER_BOTTOM_BASE_LINE: _offset.set((_textBB.xMax()+_textBB.xMin())*0.5f,-_characterHeight*(1.0 + _lineSpacing)*(_lineCount-1),0.0f); break; |
|---|
| 605 | case RIGHT_BOTTOM_BASE_LINE: _offset.set(_textBB.xMax(),-_characterHeight*(1.0 + _lineSpacing)*(_lineCount-1),0.0f); break; |
|---|
| 606 | } |
|---|
| 607 | |
|---|
| 608 | AutoTransformCache& atc = _autoTransformCache[contextID]; |
|---|
| 609 | osg::Matrix& matrix = atc._matrix; |
|---|
| 610 | |
|---|
| 611 | if (_characterSizeMode!=OBJECT_COORDS || _autoRotateToScreen) |
|---|
| 612 | { |
|---|
| 613 | |
|---|
| 614 | matrix.makeTranslate(-_offset); |
|---|
| 615 | |
|---|
| 616 | osg::Matrix rotate_matrix; |
|---|
| 617 | if (_autoRotateToScreen) |
|---|
| 618 | { |
|---|
| 619 | osg::Vec3d trans(atc._modelview.getTrans()); |
|---|
| 620 | atc._modelview.setTrans(0.0f,0.0f,0.0f); |
|---|
| 621 | |
|---|
| 622 | rotate_matrix.invert(atc._modelview); |
|---|
| 623 | |
|---|
| 624 | atc._modelview.setTrans(trans); |
|---|
| 625 | } |
|---|
| 626 | |
|---|
| 627 | matrix.postMultRotate(_rotation); |
|---|
| 628 | |
|---|
| 629 | if (_characterSizeMode!=OBJECT_COORDS) |
|---|
| 630 | { |
|---|
| 631 | |
|---|
| 632 | osg::Matrix M(rotate_matrix); |
|---|
| 633 | M.postMultTranslate(_position); |
|---|
| 634 | M.postMult(atc._modelview); |
|---|
| 635 | osg::Matrix& P = atc._projection; |
|---|
| 636 | |
|---|
| 637 | |
|---|
| 638 | |
|---|
| 639 | |
|---|
| 640 | |
|---|
| 641 | |
|---|
| 642 | |
|---|
| 643 | |
|---|
| 644 | |
|---|
| 645 | float P00 = P(0,0)*atc._width*0.5f; |
|---|
| 646 | float P20_00 = P(2,0)*atc._width*0.5f + P(2,3)*atc._width*0.5f; |
|---|
| 647 | osg::Vec3 scale_00(M(0,0)*P00 + M(0,2)*P20_00, |
|---|
| 648 | M(1,0)*P00 + M(1,2)*P20_00, |
|---|
| 649 | M(2,0)*P00 + M(2,2)*P20_00); |
|---|
| 650 | |
|---|
| 651 | |
|---|
| 652 | float P10 = P(1,1)*atc._height*0.5f; |
|---|
| 653 | float P20_10 = P(2,1)*atc._height*0.5f + P(2,3)*atc._height*0.5f; |
|---|
| 654 | osg::Vec3 scale_10(M(0,1)*P10 + M(0,2)*P20_10, |
|---|
| 655 | M(1,1)*P10 + M(1,2)*P20_10, |
|---|
| 656 | M(2,1)*P10 + M(2,2)*P20_10); |
|---|
| 657 | |
|---|
| 658 | float P23 = P(2,3); |
|---|
| 659 | float P33 = P(3,3); |
|---|
| 660 | |
|---|
| 661 | float pixelSizeVector_w = M(3,2)*P23 + M(3,3)*P33; |
|---|
| 662 | |
|---|
| 663 | float pixelSizeVert=(_characterHeight*sqrtf(scale_10.length2()))/(pixelSizeVector_w*0.701f); |
|---|
| 664 | float pixelSizeHori=(_characterHeight/getCharacterAspectRatio()*sqrtf(scale_00.length2()))/(pixelSizeVector_w*0.701f); |
|---|
| 665 | |
|---|
| 666 | |
|---|
| 667 | if (pixelSizeVert == 0.0f) |
|---|
| 668 | pixelSizeVert= 1.0f; |
|---|
| 669 | if (pixelSizeHori == 0.0f) |
|---|
| 670 | pixelSizeHori= 1.0f; |
|---|
| 671 | |
|---|
| 672 | if (_characterSizeMode==SCREEN_COORDS) |
|---|
| 673 | { |
|---|
| 674 | float scale_font_vert=_characterHeight/pixelSizeVert; |
|---|
| 675 | float scale_font_hori=_characterHeight/getCharacterAspectRatio()/pixelSizeHori; |
|---|
| 676 | |
|---|
| 677 | if (P10<0) |
|---|
| 678 | scale_font_vert=-scale_font_vert; |
|---|
| 679 | matrix.postMultScale(osg::Vec3f(scale_font_hori, scale_font_vert,1.0f)); |
|---|
| 680 | } |
|---|
| 681 | else if (pixelSizeVert>getFontHeight()) |
|---|
| 682 | { |
|---|
| 683 | float scale_font = getFontHeight()/pixelSizeVert; |
|---|
| 684 | matrix.postMultScale(osg::Vec3f(scale_font, scale_font,1.0f)); |
|---|
| 685 | } |
|---|
| 686 | |
|---|
| 687 | } |
|---|
| 688 | |
|---|
| 689 | if (_autoRotateToScreen) |
|---|
| 690 | { |
|---|
| 691 | matrix.postMult(rotate_matrix); |
|---|
| 692 | } |
|---|
| 693 | |
|---|
| 694 | matrix.postMultTranslate(_position); |
|---|
| 695 | } |
|---|
| 696 | else if (!_rotation.zeroRotation()) |
|---|
| 697 | { |
|---|
| 698 | matrix.makeRotate(_rotation); |
|---|
| 699 | matrix.preMultTranslate(-_offset); |
|---|
| 700 | matrix.postMultTranslate(_position); |
|---|
| 701 | } |
|---|
| 702 | else |
|---|
| 703 | { |
|---|
| 704 | matrix.makeTranslate(_position-_offset); |
|---|
| 705 | } |
|---|
| 706 | |
|---|
| 707 | |
|---|
| 708 | for(TextureGlyphQuadMap::iterator titr=_textureGlyphQuadMap.begin(); |
|---|
| 709 | titr!=_textureGlyphQuadMap.end(); |
|---|
| 710 | ++titr) |
|---|
| 711 | { |
|---|
| 712 | GlyphQuads& glyphquad = titr->second; |
|---|
| 713 | GlyphQuads::Coords2& coords2 = glyphquad._coords; |
|---|
| 714 | GlyphQuads::Coords3& transformedCoords = glyphquad._transformedCoords[contextID]; |
|---|
| 715 | |
|---|
| 716 | unsigned int numCoords = coords2.size(); |
|---|
| 717 | if (numCoords!=transformedCoords.size()) |
|---|
| 718 | { |
|---|
| 719 | transformedCoords.resize(numCoords); |
|---|
| 720 | } |
|---|
| 721 | |
|---|
| 722 | for(unsigned int i=0;i<numCoords;++i) |
|---|
| 723 | { |
|---|
| 724 | transformedCoords[i] = osg::Vec3(coords2[i].x(),coords2[i].y(),0.0f)*matrix; |
|---|
| 725 | } |
|---|
| 726 | } |
|---|
| 727 | |
|---|
| 728 | computeBackdropPositions(contextID); |
|---|
| 729 | |
|---|
| 730 | _normal = osg::Matrix::transform3x3(osg::Vec3(0.0f,0.0f,1.0f),matrix); |
|---|
| 731 | _normal.normalize(); |
|---|
| 732 | |
|---|
| 733 | const_cast<Text*>(this)->dirtyBound(); |
|---|
| 734 | } |
|---|
| 735 | |
|---|
| 736 | |
|---|
| 737 | void Text::computeBackdropPositions(unsigned int contextID) const |
|---|
| 738 | { |
|---|
| 739 | if(_backdropType == NONE) |
|---|
| 740 | { |
|---|
| 741 | return; |
|---|
| 742 | } |
|---|
| 743 | |
|---|
| 744 | float avg_width = 0.0f; |
|---|
| 745 | float avg_height = 0.0f; |
|---|
| 746 | unsigned int i; |
|---|
| 747 | bool is_valid_size; |
|---|
| 748 | |
|---|
| 749 | AutoTransformCache& atc = _autoTransformCache[contextID]; |
|---|
| 750 | osg::Matrix& matrix = atc._matrix; |
|---|
| 751 | |
|---|
| 752 | |
|---|
| 753 | |
|---|
| 754 | |
|---|
| 755 | is_valid_size = computeAverageGlyphWidthAndHeight(avg_width, avg_height); |
|---|
| 756 | |
|---|
| 757 | if (!is_valid_size) return; |
|---|
| 758 | |
|---|
| 759 | |
|---|
| 760 | for(TextureGlyphQuadMap::iterator titr=_textureGlyphQuadMap.begin(); |
|---|
| 761 | titr!=_textureGlyphQuadMap.end(); |
|---|
| 762 | ++titr) |
|---|
| 763 | { |
|---|
| 764 | GlyphQuads& glyphquad = titr->second; |
|---|
| 765 | GlyphQuads::Coords2& coords2 = glyphquad._coords; |
|---|
| 766 | |
|---|
| 767 | unsigned int backdrop_index; |
|---|
| 768 | unsigned int max_backdrop_index; |
|---|
| 769 | if(_backdropType == OUTLINE) |
|---|
| 770 | { |
|---|
| 771 | |
|---|
| 772 | backdrop_index = 0; |
|---|
| 773 | max_backdrop_index = 8; |
|---|
| 774 | } |
|---|
| 775 | else |
|---|
| 776 | { |
|---|
| 777 | |
|---|
| 778 | |
|---|
| 779 | |
|---|
| 780 | |
|---|
| 781 | |
|---|
| 782 | |
|---|
| 783 | |
|---|
| 784 | |
|---|
| 785 | |
|---|
| 786 | |
|---|
| 787 | backdrop_index = _backdropType; |
|---|
| 788 | max_backdrop_index = _backdropType+1; |
|---|
| 789 | } |
|---|
| 790 | for( ; backdrop_index < max_backdrop_index; backdrop_index++) |
|---|
| 791 | { |
|---|
| 792 | GlyphQuads::Coords3& transformedCoords = glyphquad._transformedBackdropCoords[backdrop_index][contextID]; |
|---|
| 793 | unsigned int numCoords = coords2.size(); |
|---|
| 794 | if (numCoords!=transformedCoords.size()) |
|---|
| 795 | { |
|---|
| 796 | transformedCoords.resize(numCoords); |
|---|
| 797 | } |
|---|
| 798 | |
|---|
| 799 | for(i=0;i<numCoords;++i) |
|---|
| 800 | { |
|---|
| 801 | float horizontal_shift_direction; |
|---|
| 802 | float vertical_shift_direction; |
|---|
| 803 | switch(backdrop_index) |
|---|
| 804 | { |
|---|
| 805 | case DROP_SHADOW_BOTTOM_RIGHT: |
|---|
| 806 | { |
|---|
| 807 | horizontal_shift_direction = 1.0f; |
|---|
| 808 | vertical_shift_direction = -1.0f; |
|---|
| 809 | break; |
|---|
| 810 | } |
|---|
| 811 | case DROP_SHADOW_CENTER_RIGHT: |
|---|
| 812 | { |
|---|
| 813 | horizontal_shift_direction = 1.0f; |
|---|
| 814 | vertical_shift_direction = 0.0f; |
|---|
| 815 | break; |
|---|
| 816 | } |
|---|
| 817 | case DROP_SHADOW_TOP_RIGHT: |
|---|
| 818 | { |
|---|
| 819 | horizontal_shift_direction = 1.0f; |
|---|
| 820 | vertical_shift_direction = 1.0f; |
|---|
| 821 | break; |
|---|
| 822 | } |
|---|
| 823 | case DROP_SHADOW_BOTTOM_CENTER: |
|---|
| 824 | { |
|---|
| 825 | horizontal_shift_direction = 0.0f; |
|---|
| 826 | vertical_shift_direction = -1.0f; |
|---|
| 827 | break; |
|---|
| 828 | } |
|---|
| 829 | case DROP_SHADOW_TOP_CENTER: |
|---|
| 830 | { |
|---|
| 831 | horizontal_shift_direction = 0.0f; |
|---|
| 832 | vertical_shift_direction = 1.0f; |
|---|
| 833 | break; |
|---|
| 834 | } |
|---|
| 835 | case DROP_SHADOW_BOTTOM_LEFT: |
|---|
| 836 | { |
|---|
| 837 | horizontal_shift_direction = -1.0f; |
|---|
| 838 | vertical_shift_direction = -1.0f; |
|---|
| 839 | break; |
|---|
| 840 | } |
|---|
| 841 | case DROP_SHADOW_CENTER_LEFT: |
|---|
| 842 | { |
|---|
| 843 | horizontal_shift_direction = -1.0f; |
|---|
| 844 | vertical_shift_direction = 0.0f; |
|---|
| 845 | break; |
|---|
| 846 | } |
|---|
| 847 | case DROP_SHADOW_TOP_LEFT: |
|---|
| 848 | { |
|---|
| 849 | horizontal_shift_direction = -1.0f; |
|---|
| 850 | vertical_shift_direction = 1.0f; |
|---|
| 851 | break; |
|---|
| 852 | } |
|---|
| 853 | default: |
|---|
| 854 | { |
|---|
| 855 | horizontal_shift_direction = 1.0f; |
|---|
| 856 | vertical_shift_direction = -1.0f; |
|---|
| 857 | } |
|---|
| 858 | } |
|---|
| 859 | transformedCoords[i] = osg::Vec3(horizontal_shift_direction * _backdropHorizontalOffset * avg_width+coords2[i].x(),vertical_shift_direction * _backdropVerticalOffset * avg_height+coords2[i].y(),0.0f)*matrix; |
|---|
| 860 | } |
|---|
| 861 | } |
|---|
| 862 | } |
|---|
| 863 | } |
|---|
| 864 | |
|---|
| 865 | |
|---|
| 866 | |
|---|
| 867 | void Text::computeBackdropBoundingBox() const |
|---|
| 868 | { |
|---|
| 869 | if(_backdropType == NONE) |
|---|
| 870 | { |
|---|
| 871 | return; |
|---|
| 872 | } |
|---|
| 873 | |
|---|
| 874 | float avg_width = 0.0f; |
|---|
| 875 | float avg_height = 0.0f; |
|---|
| 876 | bool is_valid_size; |
|---|
| 877 | |
|---|
| 878 | |
|---|
| 879 | |
|---|
| 880 | is_valid_size = computeAverageGlyphWidthAndHeight(avg_width, avg_height); |
|---|
| 881 | |
|---|
| 882 | |
|---|
| 883 | |
|---|
| 884 | |
|---|
| 885 | if((!_textBB.valid() || !is_valid_size)) |
|---|
| 886 | { |
|---|
| 887 | return; |
|---|
| 888 | } |
|---|
| 889 | |
|---|
| 890 | |
|---|
| 891 | |
|---|
| 892 | |
|---|
| 893 | switch(_backdropType) |
|---|
| 894 | { |
|---|
| 895 | case DROP_SHADOW_BOTTOM_RIGHT: |
|---|
| 896 | { |
|---|
| 897 | _textBB.set( |
|---|
| 898 | _textBB.xMin(), |
|---|
| 899 | _textBB.yMin() - avg_height * _backdropVerticalOffset, |
|---|
| 900 | _textBB.zMin(), |
|---|
| 901 | _textBB.xMax() + avg_width * _backdropHorizontalOffset, |
|---|
| 902 | _textBB.yMax(), |
|---|
| 903 | _textBB.zMax() |
|---|
| 904 | ); |
|---|
| 905 | break; |
|---|
| 906 | } |
|---|
| 907 | case DROP_SHADOW_CENTER_RIGHT: |
|---|
| 908 | { |
|---|
| 909 | _textBB.set( |
|---|
| 910 | _textBB.xMin(), |
|---|
| 911 | _textBB.yMin(), |
|---|
| 912 | _textBB.zMin(), |
|---|
| 913 | _textBB.xMax() + avg_width * _backdropHorizontalOffset, |
|---|
| 914 | _textBB.yMax(), |
|---|
| 915 | _textBB.zMax() |
|---|
| 916 | ); |
|---|
| 917 | break; |
|---|
| 918 | } |
|---|
| 919 | case DROP_SHADOW_TOP_RIGHT: |
|---|
| 920 | { |
|---|
| 921 | _textBB.set( |
|---|
| 922 | _textBB.xMin(), |
|---|
| 923 | _textBB.yMin(), |
|---|
| 924 | _textBB.zMin(), |
|---|
| 925 | _textBB.xMax() + avg_width * _backdropHorizontalOffset, |
|---|
| 926 | _textBB.yMax() + avg_height * _backdropVerticalOffset, |
|---|
| 927 | _textBB.zMax() |
|---|
| 928 | ); |
|---|
| 929 | break; |
|---|
| 930 | } |
|---|
| 931 | case DROP_SHADOW_BOTTOM_CENTER: |
|---|
| 932 | { |
|---|
| 933 | _textBB.set( |
|---|
| 934 | _textBB.xMin(), |
|---|
| 935 | _textBB.yMin() - avg_height * _backdropVerticalOffset, |
|---|
| 936 | _textBB.zMin(), |
|---|
| 937 | _textBB.xMax(), |
|---|
| 938 | _textBB.yMax(), |
|---|
| 939 | _textBB.zMax() |
|---|
| 940 | ); |
|---|
| 941 | break; |
|---|
| 942 | } |
|---|
| 943 | case DROP_SHADOW_TOP_CENTER: |
|---|
| 944 | { |
|---|
| 945 | _textBB.set( |
|---|
| 946 | _textBB.xMin(), |
|---|
| 947 | _textBB.yMin(), |
|---|
| 948 | _textBB.zMin(), |
|---|
| 949 | _textBB.xMax(), |
|---|
| 950 | _textBB.yMax() + avg_height * _backdropVerticalOffset, |
|---|
| 951 | _textBB.zMax() |
|---|
| 952 | ); |
|---|
| 953 | break; |
|---|
| 954 | } |
|---|
| 955 | case DROP_SHADOW_BOTTOM_LEFT: |
|---|
| 956 | { |
|---|
| 957 | _textBB.set( |
|---|
| 958 | _textBB.xMin() - avg_width * _backdropHorizontalOffset, |
|---|
| 959 | _textBB.yMin() - avg_height * _backdropVerticalOffset, |
|---|
| 960 | _textBB.zMin(), |
|---|
| 961 | _textBB.xMax(), |
|---|
| 962 | _textBB.yMax(), |
|---|
| 963 | _textBB.zMax() |
|---|
| 964 | ); |
|---|
| 965 | break; |
|---|
| 966 | } |
|---|
| 967 | case DROP_SHADOW_CENTER_LEFT: |
|---|
| 968 | { |
|---|
| 969 | _textBB.set( |
|---|
| 970 | _textBB.xMin() - avg_width * _backdropHorizontalOffset, |
|---|
| 971 | _textBB.yMin(), |
|---|
| 972 | _textBB.zMin(), |
|---|
| 973 | _textBB.xMax(), |
|---|
| 974 | _textBB.yMax(), |
|---|
| 975 | _textBB.zMax() |
|---|
| 976 | ); break; |
|---|
| 977 | } |
|---|
| 978 | case DROP_SHADOW_TOP_LEFT: |
|---|
| 979 | { |
|---|
| 980 | _textBB.set( |
|---|
| 981 | _textBB.xMin() - avg_width * _backdropHorizontalOffset, |
|---|
| 982 | _textBB.yMin(), |
|---|
| 983 | _textBB.zMin(), |
|---|
| 984 | _textBB.xMax(), |
|---|
| 985 | _textBB.yMax() + avg_height * _backdropVerticalOffset, |
|---|
| 986 | _textBB.zMax() |
|---|
| 987 | ); |
|---|
| 988 | break; |
|---|
| 989 | } |
|---|
| 990 | case OUTLINE: |
|---|
| 991 | { |
|---|
| 992 | _textBB.set( |
|---|
| 993 | _textBB.xMin() - avg_width * _backdropHorizontalOffset, |
|---|
| 994 | _textBB.yMin() - avg_height * _backdropVerticalOffset, |
|---|
| 995 | _textBB.zMin(), |
|---|
| 996 | _textBB.xMax() + avg_width * _backdropHorizontalOffset, |
|---|
| 997 | _textBB.yMax() + avg_height * _backdropVerticalOffset, |
|---|
| 998 | _textBB.zMax() |
|---|
| 999 | ); |
|---|
| 1000 | break; |
|---|
| 1001 | } |
|---|
| 1002 | default: |
|---|
| 1003 | { |
|---|
| 1004 | break; |
|---|
| 1005 | } |
|---|
| 1006 | } |
|---|
| 1007 | } |
|---|
| 1008 | |
|---|
| 1009 | |
|---|
| 1010 | void Text::computeBoundingBoxMargin() const |
|---|
| 1011 | { |
|---|
| 1012 | if(_drawMode & (BOUNDINGBOX | FILLEDBOUNDINGBOX)){ |
|---|
| 1013 | _textBB.set( |
|---|
| 1014 | _textBB.xMin() - _textBBMargin, |
|---|
| 1015 | _textBB.yMin() - _textBBMargin, |
|---|
| 1016 | _textBB.zMin(), |
|---|
| 1017 | _textBB.xMax() + _textBBMargin, |
|---|
| 1018 | _textBB.yMax() + _textBBMargin, |
|---|
| 1019 | _textBB.zMax() |
|---|
| 1020 | ); |
|---|
| 1021 | } |
|---|
| 1022 | } |
|---|
| 1023 | |
|---|
| 1024 | void Text::computeColorGradients() const |
|---|
| 1025 | { |
|---|
| 1026 | switch(_colorGradientMode) |
|---|
| 1027 | { |
|---|
| 1028 | case SOLID: |
|---|
| 1029 | return; |
|---|
| 1030 | break; |
|---|
| 1031 | case PER_CHARACTER: |
|---|
| 1032 | computeColorGradientsPerCharacter(); |
|---|
| 1033 | break; |
|---|
| 1034 | case OVERALL: |
|---|
| 1035 | computeColorGradientsOverall(); |
|---|
| 1036 | break; |
|---|
| 1037 | default: |
|---|
| 1038 | break; |
|---|
| 1039 | } |
|---|
| 1040 | } |
|---|
| 1041 | |
|---|
| 1042 | void Text::computeColorGradientsOverall() const |
|---|
| 1043 | { |
|---|
| 1044 | |
|---|
| 1045 | float min_x = FLT_MAX; |
|---|
| 1046 | float min_y = FLT_MAX; |
|---|
| 1047 | float max_x = FLT_MIN; |
|---|
| 1048 | float max_y = FLT_MIN; |
|---|
| 1049 | |
|---|
| 1050 | unsigned int i; |
|---|
| 1051 | |
|---|
| 1052 | for(TextureGlyphQuadMap::const_iterator const_titr=_textureGlyphQuadMap.begin(); |
|---|
| 1053 | const_titr!=_textureGlyphQuadMap.end(); |
|---|
| 1054 | ++const_titr) |
|---|
| 1055 | { |
|---|
| 1056 | const GlyphQuads& glyphquad = const_titr->second; |
|---|
| 1057 | const GlyphQuads::Coords2& coords2 = glyphquad._coords; |
|---|
| 1058 | |
|---|
| 1059 | for(i=0;i<coords2.size();++i) |
|---|
| 1060 | { |
|---|
| 1061 | |
|---|
| 1062 | if(coords2[i].x() > max_x) |
|---|
| 1063 | { |
|---|
| 1064 | max_x = coords2[i].x(); |
|---|
| 1065 | } |
|---|
| 1066 | if(coords2[i].x() < min_x) |
|---|
| 1067 | { |
|---|
| 1068 | min_x = coords2[i].x(); |
|---|
| 1069 | } |
|---|
| 1070 | if(coords2[i].y() > max_y) |
|---|
| 1071 | { |
|---|
| 1072 | max_y = coords2[i].y(); |
|---|
| 1073 | } |
|---|
| 1074 | if(coords2[i].y() < min_y) |
|---|
| 1075 | { |
|---|
| 1076 | min_y = coords2[i].y(); |
|---|
| 1077 | } |
|---|
| 1078 | |
|---|
| 1079 | } |
|---|
| 1080 | } |
|---|
| 1081 | |
|---|
| 1082 | for(TextureGlyphQuadMap::iterator titr=_textureGlyphQuadMap.begin(); |
|---|
| 1083 | titr!=_textureGlyphQuadMap.end(); |
|---|
| 1084 | ++titr) |
|---|
| 1085 | { |
|---|
| 1086 | GlyphQuads& glyphquad = titr->second; |
|---|
| 1087 | GlyphQuads::Coords2& coords2 = glyphquad._coords; |
|---|
| 1088 | GlyphQuads::ColorCoords& colorCoords = glyphquad._colorCoords; |
|---|
| 1089 | |
|---|
| 1090 | unsigned int numCoords = coords2.size(); |
|---|
| 1091 | if (numCoords!=colorCoords.size()) |
|---|
| 1092 | { |
|---|
| 1093 | colorCoords.resize(numCoords); |
|---|
| 1094 | } |
|---|
| 1095 | |
|---|
| 1096 | for(i=0;i<numCoords;++i) |
|---|
| 1097 | { |
|---|
| 1098 | float red = bilinearInterpolate( |
|---|
| 1099 | min_x, |
|---|
| 1100 | max_x, |
|---|
| 1101 | min_y, |
|---|
| 1102 | max_y, |
|---|
| 1103 | coords2[i].x(), |
|---|
| 1104 | coords2[i].y(), |
|---|
| 1105 | _colorGradientBottomLeft[0], |
|---|
| 1106 | _colorGradientTopLeft[0], |
|---|
| 1107 | _colorGradientBottomRight[0], |
|---|
| 1108 | _colorGradientTopRight[0] |
|---|
| 1109 | ); |
|---|
| 1110 | |
|---|
| 1111 | float green = bilinearInterpolate( |
|---|
| 1112 | min_x, |
|---|
| 1113 | max_x, |
|---|
| 1114 | min_y, |
|---|
| 1115 | max_y, |
|---|
| 1116 | coords2[i].x(), |
|---|
| 1117 | coords2[i].y(), |
|---|
| 1118 | _colorGradientBottomLeft[1], |
|---|
| 1119 | _colorGradientTopLeft[1], |
|---|
| 1120 | _colorGradientBottomRight[1], |
|---|
| 1121 | _colorGradientTopRight[1] |
|---|
| 1122 | ); |
|---|
| 1123 | |
|---|
| 1124 | float blue = bilinearInterpolate( |
|---|
| 1125 | min_x, |
|---|
| 1126 | max_x, |
|---|
| 1127 | min_y, |
|---|
| 1128 | max_y, |
|---|
| 1129 | coords2[i].x(), |
|---|
| 1130 | coords2[i].y(), |
|---|
| 1131 | _colorGradientBottomLeft[2], |
|---|
| 1132 | _colorGradientTopLeft[2], |
|---|
| 1133 | _colorGradientBottomRight[2], |
|---|
| 1134 | _colorGradientTopRight[2] |
|---|
| 1135 | ); |
|---|
| 1136 | |
|---|
| 1137 | float alpha = bilinearInterpolate( |
|---|
| 1138 | min_x, |
|---|
| 1139 | max_x, |
|---|
| 1140 | min_y, |
|---|
| 1141 | max_y, |
|---|
| 1142 | coords2[i].x(), |
|---|
| 1143 | coords2[i].y(), |
|---|
| 1144 | _colorGradientBottomLeft[3], |
|---|
| 1145 | _colorGradientTopLeft[3], |
|---|
| 1146 | _colorGradientBottomRight[3], |
|---|
| 1147 | _colorGradientTopRight[3] |
|---|
| 1148 | ); |
|---|
| 1149 | |
|---|
| 1150 | colorCoords[i] = osg::Vec4(red,green,blue,alpha); |
|---|
| 1151 | } |
|---|
| 1152 | } |
|---|
| 1153 | } |
|---|
| 1154 | |
|---|
| 1155 | void Text::computeColorGradientsPerCharacter() const |
|---|
| 1156 | { |
|---|
| 1157 | for(TextureGlyphQuadMap::iterator titr=_textureGlyphQuadMap.begin(); |
|---|
| 1158 | titr!=_textureGlyphQuadMap.end(); |
|---|
| 1159 | ++titr) |
|---|
| 1160 | { |
|---|
| 1161 | GlyphQuads& glyphquad = titr->second; |
|---|
| 1162 | GlyphQuads::Coords2& coords2 = glyphquad._coords; |
|---|
| 1163 | GlyphQuads::ColorCoords& colorCoords = glyphquad._colorCoords; |
|---|
| 1164 | |
|---|
| 1165 | unsigned int numCoords = coords2.size(); |
|---|
| 1166 | if (numCoords!=colorCoords.size()) |
|---|
| 1167 | { |
|---|
| 1168 | colorCoords.resize(numCoords); |
|---|
| 1169 | } |
|---|
| 1170 | |
|---|
| 1171 | for(unsigned int i=0;i<numCoords;++i) |
|---|
| 1172 | { |
|---|
| 1173 | switch(i%4) |
|---|
| 1174 | { |
|---|
| 1175 | case 0: |
|---|
| 1176 | { |
|---|
| 1177 | colorCoords[i] = _colorGradientTopLeft; |
|---|
| 1178 | break; |
|---|
| 1179 | } |
|---|
| 1180 | case 1: |
|---|
| 1181 | { |
|---|
| 1182 | colorCoords[i] = _colorGradientBottomLeft; |
|---|
| 1183 | break; |
|---|
| 1184 | } |
|---|
| 1185 | case 2: |
|---|
| 1186 | { |
|---|
| 1187 | colorCoords[i] = _colorGradientBottomRight; |
|---|
| 1188 | break; |
|---|
| 1189 | } |
|---|
| 1190 | case 3: |
|---|
| 1191 | { |
|---|
| 1192 | colorCoords[i] = _colorGradientTopRight; |
|---|
| 1193 | break; |
|---|
| 1194 | } |
|---|
| 1195 | default: |
|---|
| 1196 | { |
|---|
| 1197 | colorCoords[i] = osg::Vec4(0.0f,0.0f,0.0f,1.0f); |
|---|
| 1198 | } |
|---|
| 1199 | } |
|---|
| 1200 | } |
|---|
| 1201 | } |
|---|
| 1202 | } |
|---|
| 1203 | |
|---|
| 1204 | void Text::drawImplementation(osg::RenderInfo& renderInfo) const |
|---|
| 1205 | { |
|---|
| 1206 | drawImplementation(*renderInfo.getState(), osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 1207 | } |
|---|
| 1208 | |
|---|
| 1209 | void Text::drawImplementation(osg::State& state, const osg::Vec4& colorMultiplier) const |
|---|
| 1210 | { |
|---|
| 1211 | unsigned int contextID = state.getContextID(); |
|---|
| 1212 | |
|---|
| 1213 | state.applyMode(GL_BLEND,true); |
|---|
| 1214 | #if defined(OSG_GL_FIXED_FUNCTION_AVAILABLE) |
|---|
| 1215 | state.applyTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::ON); |
|---|
| 1216 | state.applyTextureAttribute(0,getActiveFont()->getTexEnv()); |
|---|
| 1217 | #endif |
|---|
| 1218 | if (_characterSizeMode!=OBJECT_COORDS || _autoRotateToScreen) |
|---|
| 1219 | { |
|---|
| 1220 | unsigned int frameNumber = state.getFrameStamp()?state.getFrameStamp()->getFrameNumber():0; |
|---|
| 1221 | AutoTransformCache& atc = _autoTransformCache[contextID]; |
|---|
| 1222 | const osg::Matrix& modelview = state.getModelViewMatrix(); |
|---|
| 1223 | const osg::Matrix& projection = state.getProjectionMatrix(); |
|---|
| 1224 | |
|---|
| 1225 | osg::Vec3 newTransformedPosition = _position*modelview; |
|---|
| 1226 | |
|---|
| 1227 | int width = atc._width; |
|---|
| 1228 | int height = atc._height; |
|---|
| 1229 | |
|---|
| 1230 | const osg::Viewport* viewport = state.getCurrentViewport(); |
|---|
| 1231 | if (viewport) |
|---|
| 1232 | { |
|---|
| 1233 | width = static_cast<int>(viewport->width()); |
|---|
| 1234 | height = static_cast<int>(viewport->height()); |
|---|
| 1235 | } |
|---|
| 1236 | |
|---|
| 1237 | bool doUpdate = atc._traversalNumber==-1; |
|---|
| 1238 | if (atc._traversalNumber>=0) |
|---|
| 1239 | { |
|---|
| 1240 | if (atc._modelview!=modelview) |
|---|
| 1241 | { |
|---|
| 1242 | doUpdate = true; |
|---|
| 1243 | } |
|---|
| 1244 | else if (width!=atc._width || height!=atc._height) |
|---|
| 1245 | { |
|---|
| 1246 | doUpdate = true; |
|---|
| 1247 | } |
|---|
| 1248 | else if (atc._projection!=projection) |
|---|
| 1249 | { |
|---|
| 1250 | doUpdate = true; |
|---|
| 1251 | } |
|---|
| 1252 | } |
|---|
| 1253 | |
|---|
| 1254 | atc._traversalNumber = frameNumber; |
|---|
| 1255 | atc._width = width; |
|---|
| 1256 | atc._height = height; |
|---|
| 1257 | |
|---|
| 1258 | if (doUpdate) |
|---|
| 1259 | { |
|---|
| 1260 | atc._transformedPosition = newTransformedPosition; |
|---|
| 1261 | atc._projection = projection; |
|---|
| 1262 | atc._modelview = modelview; |
|---|
| 1263 | |
|---|
| 1264 | computePositions(contextID); |
|---|
| 1265 | } |
|---|
| 1266 | |
|---|
| 1267 | } |
|---|
| 1268 | |
|---|
| 1269 | |
|---|
| 1270 | |
|---|
| 1271 | |
|---|
| 1272 | |
|---|
| 1273 | if ( !_textureGlyphQuadMap.empty() ) |
|---|
| 1274 | { |
|---|
| 1275 | const GlyphQuads& glyphquad = (_textureGlyphQuadMap.begin())->second; |
|---|
| 1276 | if ( glyphquad._transformedCoords[contextID].empty() ) |
|---|
| 1277 | { |
|---|
| 1278 | computePositions(contextID); |
|---|
| 1279 | } |
|---|
| 1280 | } |
|---|
| 1281 | |
|---|
| 1282 | osg::GLBeginEndAdapter& gl = (state.getGLBeginEndAdapter()); |
|---|
| 1283 | |
|---|
| 1284 | state.Normal(_normal.x(), _normal.y(), _normal.z()); |
|---|
| 1285 | |
|---|
| 1286 | if (_drawMode & FILLEDBOUNDINGBOX) |
|---|
| 1287 | { |
|---|
| 1288 | if (_textBB.valid()) |
|---|
| 1289 | { |
|---|
| 1290 | #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) && !defined(OSG_GL3_AVAILABLE) |
|---|
| 1291 | state.applyTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OFF); |
|---|
| 1292 | |
|---|
| 1293 | const osg::Matrix& matrix = _autoTransformCache[contextID]._matrix; |
|---|
| 1294 | |
|---|
| 1295 | osg::Vec3 c00(osg::Vec3(_textBB.xMin(),_textBB.yMin(),_textBB.zMin())*matrix); |
|---|
| 1296 | osg::Vec3 c10(osg::Vec3(_textBB.xMax(),_textBB.yMin(),_textBB.zMin())*matrix); |
|---|
| 1297 | osg::Vec3 c11(osg::Vec3(_textBB.xMax(),_textBB.yMax(),_textBB.zMin())*matrix); |
|---|
| 1298 | osg::Vec3 c01(osg::Vec3(_textBB.xMin(),_textBB.yMax(),_textBB.zMin())*matrix); |
|---|
| 1299 | |
|---|
| 1300 | switch(_backdropImplementation) |
|---|
| 1301 | { |
|---|
| 1302 | case NO_DEPTH_BUFFER: |
|---|
| 1303 | |
|---|
| 1304 | break; |
|---|
| 1305 | case DEPTH_RANGE: |
|---|
| 1306 | glPushAttrib(GL_DEPTH_BUFFER_BIT); |
|---|
| 1307 | |
|---|
| 1308 | |
|---|
| 1309 | |
|---|
| 1310 | glDepthRange(0.001, 1.001); |
|---|
| 1311 | break; |
|---|
| 1312 | |
|---|
| 1313 | |
|---|
| 1314 | default: |
|---|
| 1315 | glPushAttrib(GL_POLYGON_OFFSET_FILL); |
|---|
| 1316 | glEnable(GL_POLYGON_OFFSET_FILL); |
|---|
| 1317 | glPolygonOffset(0.1f * osg::PolygonOffset::getFactorMultiplier(), 10.0f * osg::PolygonOffset::getUnitsMultiplier() ); |
|---|
| 1318 | } |
|---|
| 1319 | |
|---|
| 1320 | gl.Color4f(colorMultiplier.r()*_textBBColor.r(),colorMultiplier.g()*_textBBColor.g(),colorMultiplier.b()*_textBBColor.b(),colorMultiplier.a()*_textBBColor.a()); |
|---|
| 1321 | gl.Begin(GL_QUADS); |
|---|
| 1322 | gl.Vertex3fv(c00.ptr()); |
|---|
| 1323 | gl.Vertex3fv(c10.ptr()); |
|---|
| 1324 | gl.Vertex3fv(c11.ptr()); |
|---|
| 1325 | gl.Vertex3fv(c01.ptr()); |
|---|
| 1326 | gl.End(); |
|---|
| 1327 | |
|---|
| 1328 | switch(_backdropImplementation) |
|---|
| 1329 | { |
|---|
| 1330 | case NO_DEPTH_BUFFER: |
|---|
| 1331 | |
|---|
| 1332 | break; |
|---|
| 1333 | case DEPTH_RANGE: |
|---|
| 1334 | glDepthRange(0.0, 1.0); |
|---|
| 1335 | glPopAttrib(); |
|---|
| 1336 | break; |
|---|
| 1337 | |
|---|
| 1338 | |
|---|
| 1339 | default: |
|---|
| 1340 | glDisable(GL_POLYGON_OFFSET_FILL); |
|---|
| 1341 | glPopAttrib(); |
|---|
| 1342 | } |
|---|
| 1343 | #else |
|---|
| 1344 | OSG_NOTICE<<"Warning: Text::drawImplementation() fillMode FILLEDBOUNDINGBOX not supported"<<std::endl; |
|---|
| 1345 | #endif |
|---|
| 1346 | } |
|---|
| 1347 | } |
|---|
| 1348 | |
|---|
| 1349 | #if defined(OSG_GL_FIXED_FUNCTION_AVAILABLE) |
|---|
| 1350 | state.applyTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::ON); |
|---|
| 1351 | state.applyTextureAttribute(0,getActiveFont()->getTexEnv()); |
|---|
| 1352 | #endif |
|---|
| 1353 | |
|---|
| 1354 | if (_drawMode & TEXT) |
|---|
| 1355 | { |
|---|
| 1356 | |
|---|
| 1357 | state.disableAllVertexArrays(); |
|---|
| 1358 | |
|---|
| 1359 | |
|---|
| 1360 | |
|---|
| 1361 | |
|---|
| 1362 | |
|---|
| 1363 | |
|---|
| 1364 | if(_backdropType != NONE && _backdropImplementation != DELAYED_DEPTH_WRITES) |
|---|
| 1365 | { |
|---|
| 1366 | switch(_backdropImplementation) |
|---|
| 1367 | { |
|---|
| 1368 | case POLYGON_OFFSET: |
|---|
| 1369 | renderWithPolygonOffset(state,colorMultiplier); |
|---|
| 1370 | break; |
|---|
| 1371 | case NO_DEPTH_BUFFER: |
|---|
| 1372 | renderWithNoDepthBuffer(state,colorMultiplier); |
|---|
| 1373 | break; |
|---|
| 1374 | case DEPTH_RANGE: |
|---|
| 1375 | renderWithDepthRange(state,colorMultiplier); |
|---|
| 1376 | break; |
|---|
| 1377 | case STENCIL_BUFFER: |
|---|
| 1378 | renderWithStencilBuffer(state,colorMultiplier); |
|---|
| 1379 | break; |
|---|
| 1380 | default: |
|---|
| 1381 | renderWithPolygonOffset(state,colorMultiplier); |
|---|
| 1382 | } |
|---|
| 1383 | } |
|---|
| 1384 | else |
|---|
| 1385 | { |
|---|
| 1386 | renderWithDelayedDepthWrites(state,colorMultiplier); |
|---|
| 1387 | } |
|---|
| 1388 | } |
|---|
| 1389 | |
|---|
| 1390 | if (_drawMode & BOUNDINGBOX) |
|---|
| 1391 | { |
|---|
| 1392 | |
|---|
| 1393 | if (_textBB.valid()) |
|---|
| 1394 | { |
|---|
| 1395 | state.applyTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OFF); |
|---|
| 1396 | |
|---|
| 1397 | const osg::Matrix& matrix = _autoTransformCache[contextID]._matrix; |
|---|
| 1398 | |
|---|
| 1399 | osg::Vec3 c00(osg::Vec3(_textBB.xMin(),_textBB.yMin(),_textBB.zMin())*matrix); |
|---|
| 1400 | osg::Vec3 c10(osg::Vec3(_textBB.xMax(),_textBB.yMin(),_textBB.zMin())*matrix); |
|---|
| 1401 | osg::Vec3 c11(osg::Vec3(_textBB.xMax(),_textBB.yMax(),_textBB.zMin())*matrix); |
|---|
| 1402 | osg::Vec3 c01(osg::Vec3(_textBB.xMin(),_textBB.yMax(),_textBB.zMin())*matrix); |
|---|
| 1403 | |
|---|
| 1404 | |
|---|
| 1405 | gl.Color4f(colorMultiplier.r()*_textBBColor.r(),colorMultiplier.g()*_textBBColor.g(),colorMultiplier.b()*_textBBColor.b(),colorMultiplier.a()*_textBBColor.a()); |
|---|
| 1406 | gl.Begin(GL_LINE_LOOP); |
|---|
| 1407 | gl.Vertex3fv(c00.ptr()); |
|---|
| 1408 | gl.Vertex3fv(c10.ptr()); |
|---|
| 1409 | gl.Vertex3fv(c11.ptr()); |
|---|
| 1410 | gl.Vertex3fv(c01.ptr()); |
|---|
| 1411 | gl.End(); |
|---|
| 1412 | } |
|---|
| 1413 | } |
|---|
| 1414 | |
|---|
| 1415 | if (_drawMode & ALIGNMENT) |
|---|
| 1416 | { |
|---|
| 1417 | gl.Color4fv(colorMultiplier.ptr()); |
|---|
| 1418 | |
|---|
| 1419 | float cursorsize = _characterHeight*0.5f; |
|---|
| 1420 | |
|---|
| 1421 | const osg::Matrix& matrix = _autoTransformCache[contextID]._matrix; |
|---|
| 1422 | |
|---|
| 1423 | osg::Vec3 hl(osg::Vec3(_offset.x()-cursorsize,_offset.y(),_offset.z())*matrix); |
|---|
| 1424 | osg::Vec3 hr(osg::Vec3(_offset.x()+cursorsize,_offset.y(),_offset.z())*matrix); |
|---|
| 1425 | osg::Vec3 vt(osg::Vec3(_offset.x(),_offset.y()-cursorsize,_offset.z())*matrix); |
|---|
| 1426 | osg::Vec3 vb(osg::Vec3(_offset.x(),_offset.y()+cursorsize,_offset.z())*matrix); |
|---|
| 1427 | |
|---|
| 1428 | state.applyTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OFF); |
|---|
| 1429 | |
|---|
| 1430 | gl.Begin(GL_LINES); |
|---|
| 1431 | gl.Vertex3fv(hl.ptr()); |
|---|
| 1432 | gl.Vertex3fv(hr.ptr()); |
|---|
| 1433 | gl.Vertex3fv(vt.ptr()); |
|---|
| 1434 | gl.Vertex3fv(vb.ptr()); |
|---|
| 1435 | gl.End(); |
|---|
| 1436 | |
|---|
| 1437 | } |
|---|
| 1438 | } |
|---|
| 1439 | |
|---|
| 1440 | void Text::accept(osg::Drawable::ConstAttributeFunctor& af) const |
|---|
| 1441 | { |
|---|
| 1442 | for(TextureGlyphQuadMap::const_iterator titr=_textureGlyphQuadMap.begin(); |
|---|
| 1443 | titr!=_textureGlyphQuadMap.end(); |
|---|
| 1444 | ++titr) |
|---|
| 1445 | { |
|---|
| 1446 | const GlyphQuads& glyphquad = titr->second; |
|---|
| 1447 | af.apply(osg::Drawable::VERTICES,glyphquad._transformedCoords[0].size(),&(glyphquad._transformedCoords[0].front())); |
|---|
| 1448 | af.apply(osg::Drawable::TEXTURE_COORDS_0,glyphquad._texcoords.size(),&(glyphquad._texcoords.front())); |
|---|
| 1449 | } |
|---|
| 1450 | } |
|---|
| 1451 | |
|---|
| 1452 | void Text::accept(osg::PrimitiveFunctor& pf) const |
|---|
| 1453 | { |
|---|
| 1454 | for(TextureGlyphQuadMap::const_iterator titr=_textureGlyphQuadMap.begin(); |
|---|
| 1455 | titr!=_textureGlyphQuadMap.end(); |
|---|
| 1456 | ++titr) |
|---|
| 1457 | { |
|---|
| 1458 | const GlyphQuads& glyphquad = titr->second; |
|---|
| 1459 | |
|---|
| 1460 | pf.setVertexArray(glyphquad._transformedCoords[0].size(),&(glyphquad._transformedCoords[0].front())); |
|---|
| 1461 | pf.drawArrays(GL_QUADS,0,glyphquad._transformedCoords[0].size()); |
|---|
| 1462 | |
|---|
| 1463 | } |
|---|
| 1464 | |
|---|
| 1465 | } |
|---|
| 1466 | |
|---|
| 1467 | |
|---|
| 1468 | void Text::setThreadSafeRefUnref(bool threadSafe) |
|---|
| 1469 | { |
|---|
| 1470 | TextBase::setThreadSafeRefUnref(threadSafe); |
|---|
| 1471 | |
|---|
| 1472 | getActiveFont()->setThreadSafeRefUnref(threadSafe); |
|---|
| 1473 | } |
|---|
| 1474 | |
|---|
| 1475 | void Text::resizeGLObjectBuffers(unsigned int maxSize) |
|---|
| 1476 | { |
|---|
| 1477 | TextBase::resizeGLObjectBuffers(maxSize); |
|---|
| 1478 | |
|---|
| 1479 | getActiveFont()->resizeGLObjectBuffers(maxSize); |
|---|
| 1480 | } |
|---|
| 1481 | |
|---|
| 1482 | |
|---|
| 1483 | void Text::releaseGLObjects(osg::State* state) const |
|---|
| 1484 | { |
|---|
| 1485 | TextBase::releaseGLObjects(state); |
|---|
| 1486 | getActiveFont()->releaseGLObjects(state); |
|---|
| 1487 | } |
|---|
| 1488 | |
|---|
| 1489 | |
|---|
| 1490 | void Text::setBackdropType(BackdropType type) |
|---|
| 1491 | { |
|---|
| 1492 | if (_backdropType==type) return; |
|---|
| 1493 | |
|---|
| 1494 | _backdropType = type; |
|---|
| 1495 | computeGlyphRepresentation(); |
|---|
| 1496 | } |
|---|
| 1497 | |
|---|
| 1498 | void Text::setBackdropImplementation(BackdropImplementation implementation) |
|---|
| 1499 | { |
|---|
| 1500 | if (_backdropImplementation==implementation) return; |
|---|
| 1501 | |
|---|
| 1502 | _backdropImplementation = implementation; |
|---|
| 1503 | computeGlyphRepresentation(); |
|---|
| 1504 | } |
|---|
| 1505 | |
|---|
| 1506 | |
|---|
| 1507 | void Text::setBackdropOffset(float offset) |
|---|
| 1508 | { |
|---|
| 1509 | _backdropHorizontalOffset = offset; |
|---|
| 1510 | _backdropVerticalOffset = offset; |
|---|
| 1511 | computeGlyphRepresentation(); |
|---|
| 1512 | } |
|---|
| 1513 | |
|---|
| 1514 | void Text::setBackdropOffset(float horizontal, float vertical) |
|---|
| 1515 | { |
|---|
| 1516 | _backdropHorizontalOffset = horizontal; |
|---|
| 1517 | _backdropVerticalOffset = vertical; |
|---|
| 1518 | computeGlyphRepresentation(); |
|---|
| 1519 | } |
|---|
| 1520 | |
|---|
| 1521 | void Text::setBackdropColor(const osg::Vec4& color) |
|---|
| 1522 | { |
|---|
| 1523 | _backdropColor = color; |
|---|
| 1524 | } |
|---|
| 1525 | |
|---|
| 1526 | void Text::setColorGradientMode(ColorGradientMode mode) |
|---|
| 1527 | { |
|---|
| 1528 | if (_colorGradientMode==mode) return; |
|---|
| 1529 | |
|---|
| 1530 | _colorGradientMode = mode; |
|---|
| 1531 | computeGlyphRepresentation(); |
|---|
| 1532 | } |
|---|
| 1533 | |
|---|
| 1534 | void Text::setColorGradientCorners(const osg::Vec4& topLeft, const osg::Vec4& bottomLeft, const osg::Vec4& bottomRight, const osg::Vec4& topRight) |
|---|
| 1535 | { |
|---|
| 1536 | _colorGradientTopLeft = topLeft; |
|---|
| 1537 | _colorGradientBottomLeft = bottomLeft; |
|---|
| 1538 | _colorGradientBottomRight = bottomRight; |
|---|
| 1539 | _colorGradientTopRight = topRight; |
|---|
| 1540 | computeGlyphRepresentation(); |
|---|
| 1541 | } |
|---|
| 1542 | |
|---|
| 1543 | |
|---|
| 1544 | float Text::bilinearInterpolate(float x1, float x2, float y1, float y2, float x, float y, float q11, float q12, float q21, float q22) const |
|---|
| 1545 | { |
|---|
| 1546 | return ( |
|---|
| 1547 | ((q11 / ((x2-x1)*(y2-y1))) * (x2-x)*(y2-y)) |
|---|
| 1548 | + ((q21 / ((x2-x1)*(y2-y1))) * (x-x1)*(y2-y)) |
|---|
| 1549 | + ((q12 / ((x2-x1)*(y2-y1))) * (x2-x)*(y-y1)) |
|---|
| 1550 | + ((q22 / ((x2-x1)*(y2-y1))) * (x-x1)*(y-y1)) |
|---|
| 1551 | ); |
|---|
| 1552 | } |
|---|
| 1553 | |
|---|
| 1554 | void Text::drawForegroundText(osg::State& state, const GlyphQuads& glyphquad, const osg::Vec4& colorMultiplier) const |
|---|
| 1555 | { |
|---|
| 1556 | unsigned int contextID = state.getContextID(); |
|---|
| 1557 | |
|---|
| 1558 | const GlyphQuads::Coords3& transformedCoords = glyphquad._transformedCoords[contextID]; |
|---|
| 1559 | if (!transformedCoords.empty()) |
|---|
| 1560 | { |
|---|
| 1561 | state.setVertexPointer( 3, GL_FLOAT, 0, &(transformedCoords.front())); |
|---|
| 1562 | state.setTexCoordPointer( 0, 2, GL_FLOAT, 0, &(glyphquad._texcoords.front())); |
|---|
| 1563 | |
|---|
| 1564 | if(_colorGradientMode == SOLID) |
|---|
| 1565 | { |
|---|
| 1566 | state.disableColorPointer(); |
|---|
| 1567 | state.Color(colorMultiplier.r()*_color.r(),colorMultiplier.g()*_color.g(),colorMultiplier.b()*_color.b(),colorMultiplier.a()*_color.a()); |
|---|
| 1568 | } |
|---|
| 1569 | else |
|---|
| 1570 | { |
|---|
| 1571 | state.setColorPointer( 4, GL_FLOAT, 0, &(glyphquad._colorCoords.front())); |
|---|
| 1572 | } |
|---|
| 1573 | |
|---|
| 1574 | state.drawQuads(0,transformedCoords.size()); |
|---|
| 1575 | |
|---|
| 1576 | } |
|---|
| 1577 | } |
|---|
| 1578 | |
|---|
| 1579 | void Text::renderOnlyForegroundText(osg::State& state, const osg::Vec4& colorMultiplier) const |
|---|
| 1580 | { |
|---|
| 1581 | for(TextureGlyphQuadMap::iterator titr=_textureGlyphQuadMap.begin(); |
|---|
| 1582 | titr!=_textureGlyphQuadMap.end(); |
|---|
| 1583 | ++titr) |
|---|
| 1584 | { |
|---|
| 1585 | |
|---|
| 1586 | state.applyTextureAttribute(0,titr->first.get()); |
|---|
| 1587 | |
|---|
| 1588 | const GlyphQuads& glyphquad = titr->second; |
|---|
| 1589 | |
|---|
| 1590 | drawForegroundText(state, glyphquad, colorMultiplier); |
|---|
| 1591 | } |
|---|
| 1592 | } |
|---|
| 1593 | |
|---|
| 1594 | void Text::renderWithDelayedDepthWrites(osg::State& state, const osg::Vec4& colorMultiplier) const |
|---|
| 1595 | { |
|---|
| 1596 | |
|---|
| 1597 | if( !state.getLastAppliedMode(GL_DEPTH_TEST) ) { |
|---|
| 1598 | drawTextWithBackdrop(state,colorMultiplier); |
|---|
| 1599 | return; |
|---|
| 1600 | } |
|---|
| 1601 | |
|---|
| 1602 | |
|---|
| 1603 | |
|---|
| 1604 | glDepthMask(GL_FALSE); |
|---|
| 1605 | drawTextWithBackdrop(state,colorMultiplier); |
|---|
| 1606 | |
|---|
| 1607 | |
|---|
| 1608 | if( _enableDepthWrites ) |
|---|
| 1609 | { |
|---|
| 1610 | glDepthMask(GL_TRUE); |
|---|
| 1611 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
|---|
| 1612 | drawTextWithBackdrop(state,colorMultiplier); |
|---|
| 1613 | } |
|---|
| 1614 | |
|---|
| 1615 | state.haveAppliedAttribute(osg::StateAttribute::DEPTH); |
|---|
| 1616 | state.haveAppliedAttribute(osg::StateAttribute::COLORMASK); |
|---|
| 1617 | |
|---|
| 1618 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
|---|
| 1619 | |
|---|
| 1620 | } |
|---|
| 1621 | |
|---|
| 1622 | void Text::drawTextWithBackdrop(osg::State& state, const osg::Vec4& colorMultiplier) const |
|---|
| 1623 | { |
|---|
| 1624 | unsigned int contextID = state.getContextID(); |
|---|
| 1625 | |
|---|
| 1626 | for(TextureGlyphQuadMap::iterator titr=_textureGlyphQuadMap.begin(); |
|---|
| 1627 | titr!=_textureGlyphQuadMap.end(); |
|---|
| 1628 | ++titr) |
|---|
| 1629 | { |
|---|
| 1630 | |
|---|
| 1631 | state.applyTextureAttribute(0,titr->first.get()); |
|---|
| 1632 | |
|---|
| 1633 | const GlyphQuads& glyphquad = titr->second; |
|---|
| 1634 | |
|---|
| 1635 | if(_backdropType != NONE) |
|---|
| 1636 | { |
|---|
| 1637 | unsigned int backdrop_index; |
|---|
| 1638 | unsigned int max_backdrop_index; |
|---|
| 1639 | if(_backdropType == OUTLINE) |
|---|
| 1640 | { |
|---|
| 1641 | backdrop_index = 0; |
|---|
| 1642 | max_backdrop_index = 8; |
|---|
| 1643 | } |
|---|
| 1644 | else |
|---|
| 1645 | { |
|---|
| 1646 | backdrop_index = _backdropType; |
|---|
| 1647 | max_backdrop_index = _backdropType+1; |
|---|
| 1648 | } |
|---|
| 1649 | |
|---|
| 1650 | state.setTexCoordPointer( 0, 2, GL_FLOAT, 0, &(glyphquad._texcoords.front())); |
|---|
| 1651 | state.disableColorPointer(); |
|---|
| 1652 | state.Color(_backdropColor.r(),_backdropColor.g(),_backdropColor.b(),_backdropColor.a()); |
|---|
| 1653 | |
|---|
| 1654 | for( ; backdrop_index < max_backdrop_index; backdrop_index++) |
|---|
| 1655 | { |
|---|
| 1656 | const GlyphQuads::Coords3& transformedBackdropCoords = glyphquad._transformedBackdropCoords[backdrop_index][contextID]; |
|---|
| 1657 | if (!transformedBackdropCoords.empty()) |
|---|
| 1658 | { |
|---|
| 1659 | state.setVertexPointer( 3, GL_FLOAT, 0, &(transformedBackdropCoords.front())); |
|---|
| 1660 | state.drawQuads(0,transformedBackdropCoords.size()); |
|---|
| 1661 | } |
|---|
| 1662 | } |
|---|
| 1663 | } |
|---|
| 1664 | |
|---|
| 1665 | drawForegroundText(state, glyphquad, colorMultiplier); |
|---|
| 1666 | } |
|---|
| 1667 | } |
|---|
| 1668 | |
|---|
| 1669 | |
|---|
| 1670 | void Text::renderWithPolygonOffset(osg::State& state, const osg::Vec4& colorMultiplier) const |
|---|
| 1671 | { |
|---|
| 1672 | #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) && !defined(OSG_GL3_AVAILABLE) |
|---|
| 1673 | unsigned int contextID = state.getContextID(); |
|---|
| 1674 | |
|---|
| 1675 | if (!osg::PolygonOffset::areFactorAndUnitsMultipliersSet()) |
|---|
| 1676 | { |
|---|
| 1677 | osg::PolygonOffset::setFactorAndUnitsMultipliersUsingBestGuessForDriver(); |
|---|
| 1678 | } |
|---|
| 1679 | |
|---|
| 1680 | |
|---|
| 1681 | glPushAttrib(GL_POLYGON_OFFSET_FILL); |
|---|
| 1682 | glEnable(GL_POLYGON_OFFSET_FILL); |
|---|
| 1683 | |
|---|
| 1684 | for(TextureGlyphQuadMap::iterator titr=_textureGlyphQuadMap.begin(); |
|---|
| 1685 | titr!=_textureGlyphQuadMap.end(); |
|---|
| 1686 | ++titr) |
|---|
| 1687 | { |
|---|
| 1688 | |
|---|
| 1689 | state.applyTextureAttribute(0,titr->first.get()); |
|---|
| 1690 | |
|---|
| 1691 | const GlyphQuads& glyphquad = titr->second; |
|---|
| 1692 | |
|---|
| 1693 | unsigned int backdrop_index; |
|---|
| 1694 | unsigned int max_backdrop_index; |
|---|
| 1695 | if(_backdropType == OUTLINE) |
|---|
| 1696 | { |
|---|
| 1697 | backdrop_index = 0; |
|---|
| 1698 | max_backdrop_index = 8; |
|---|
| 1699 | } |
|---|
| 1700 | else |
|---|
| 1701 | { |
|---|
| 1702 | backdrop_index = _backdropType; |
|---|
| 1703 | max_backdrop_index = _backdropType+1; |
|---|
| 1704 | } |
|---|
| 1705 | |
|---|
| 1706 | state.setTexCoordPointer( 0, 2, GL_FLOAT, 0, &(glyphquad._texcoords.front())); |
|---|
| 1707 | state.disableColorPointer(); |
|---|
| 1708 | state.Color(_backdropColor.r(),_backdropColor.g(),_backdropColor.b(),_backdropColor.a()); |
|---|
| 1709 | |
|---|
| 1710 | for( ; backdrop_index < max_backdrop_index; backdrop_index++) |
|---|
| 1711 | { |
|---|
| 1712 | const GlyphQuads::Coords3& transformedBackdropCoords = glyphquad._transformedBackdropCoords[backdrop_index][contextID]; |
|---|
| 1713 | if (!transformedBackdropCoords.empty()) |
|---|
| 1714 | { |
|---|
| 1715 | state.setVertexPointer( 3, GL_FLOAT, 0, &(transformedBackdropCoords.front())); |
|---|
| 1716 | glPolygonOffset(0.1f * osg::PolygonOffset::getFactorMultiplier(), |
|---|
| 1717 | osg::PolygonOffset::getUnitsMultiplier() * (max_backdrop_index-backdrop_index) ); |
|---|
| 1718 | state.drawQuads(0,transformedBackdropCoords.size()); |
|---|
| 1719 | } |
|---|
| 1720 | } |
|---|
| 1721 | |
|---|
| 1722 | |
|---|
| 1723 | glPolygonOffset(0.0f,0.0f); |
|---|
| 1724 | |
|---|
| 1725 | drawForegroundText(state, glyphquad, colorMultiplier); |
|---|
| 1726 | } |
|---|
| 1727 | |
|---|
| 1728 | glPopAttrib(); |
|---|
| 1729 | #else |
|---|
| 1730 | OSG_NOTICE<<"Warning: Text::renderWithPolygonOffset(..) not implemented."<<std::endl; |
|---|
| 1731 | #endif |
|---|
| 1732 | } |
|---|
| 1733 | |
|---|
| 1734 | |
|---|
| 1735 | void Text::renderWithNoDepthBuffer(osg::State& state, const osg::Vec4& colorMultiplier) const |
|---|
| 1736 | { |
|---|
| 1737 | #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) && !defined(OSG_GL3_AVAILABLE) |
|---|
| 1738 | unsigned int contextID = state.getContextID(); |
|---|
| 1739 | |
|---|
| 1740 | glPushAttrib(GL_DEPTH_BUFFER_BIT); |
|---|
| 1741 | glDisable(GL_DEPTH_TEST); |
|---|
| 1742 | |
|---|
| 1743 | for(TextureGlyphQuadMap::iterator titr=_textureGlyphQuadMap.begin(); |
|---|
| 1744 | titr!=_textureGlyphQuadMap.end(); |
|---|
| 1745 | ++titr) |
|---|
| 1746 | { |
|---|
| 1747 | |
|---|
| 1748 | state.applyTextureAttribute(0,titr->first.get()); |
|---|
| 1749 | |
|---|
| 1750 | const GlyphQuads& glyphquad = titr->second; |
|---|
| 1751 | |
|---|
| 1752 | unsigned int backdrop_index; |
|---|
| 1753 | unsigned int max_backdrop_index; |
|---|
| 1754 | if(_backdropType == OUTLINE) |
|---|
| 1755 | { |
|---|
| 1756 | backdrop_index = 0; |
|---|
| 1757 | max_backdrop_index = 8; |
|---|
| 1758 | } |
|---|
| 1759 | else |
|---|
| 1760 | { |
|---|
| 1761 | backdrop_index = _backdropType; |
|---|
| 1762 | max_backdrop_index = _backdropType+1; |
|---|
| 1763 | } |
|---|
| 1764 | |
|---|
| 1765 | state.setTexCoordPointer( 0, 2, GL_FLOAT, 0, &(glyphquad._texcoords.front())); |
|---|
| 1766 | state.disableColorPointer(); |
|---|
| 1767 | state.Color(_backdropColor.r(),_backdropColor.g(),_backdropColor.b(),_backdropColor.a()); |
|---|
| 1768 | |
|---|
| 1769 | for( ; backdrop_index < max_backdrop_index; backdrop_index++) |
|---|
| 1770 | { |
|---|
| 1771 | const GlyphQuads::Coords3& transformedBackdropCoords = glyphquad._transformedBackdropCoords[backdrop_index][contextID]; |
|---|
| 1772 | if (!transformedBackdropCoords.empty()) |
|---|
| 1773 | { |
|---|
| 1774 | state.setVertexPointer( 3, GL_FLOAT, 0, &(transformedBackdropCoords.front())); |
|---|
| 1775 | state.drawQuads(0,transformedBackdropCoords.size()); |
|---|
| 1776 | } |
|---|
| 1777 | } |
|---|
| 1778 | |
|---|
| 1779 | drawForegroundText(state, glyphquad, colorMultiplier); |
|---|
| 1780 | } |
|---|
| 1781 | |
|---|
| 1782 | glPopAttrib(); |
|---|
| 1783 | #else |
|---|
| 1784 | OSG_NOTICE<<"Warning: Text::renderWithNoDepthBuffer(..) not implemented."<<std::endl; |
|---|
| 1785 | #endif |
|---|
| 1786 | } |
|---|
| 1787 | |
|---|
| 1788 | |
|---|
| 1789 | void Text::renderWithDepthRange(osg::State& state, const osg::Vec4& colorMultiplier) const |
|---|
| 1790 | { |
|---|
| 1791 | #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) && !defined(OSG_GL3_AVAILABLE) |
|---|
| 1792 | unsigned int contextID = state.getContextID(); |
|---|
| 1793 | |
|---|
| 1794 | |
|---|
| 1795 | |
|---|
| 1796 | |
|---|
| 1797 | glPushAttrib(GL_DEPTH_BUFFER_BIT); |
|---|
| 1798 | |
|---|
| 1799 | for(TextureGlyphQuadMap::iterator titr=_textureGlyphQuadMap.begin(); |
|---|
| 1800 | titr!=_textureGlyphQuadMap.end(); |
|---|
| 1801 | ++titr) |
|---|
| 1802 | { |
|---|
| 1803 | |
|---|
| 1804 | state.applyTextureAttribute(0,titr->first.get()); |
|---|
| 1805 | |
|---|
| 1806 | const GlyphQuads& glyphquad = titr->second; |
|---|
| 1807 | |
|---|
| 1808 | unsigned int backdrop_index; |
|---|
| 1809 | unsigned int max_backdrop_index; |
|---|
| 1810 | if(_backdropType == OUTLINE) |
|---|
| 1811 | { |
|---|
| 1812 | backdrop_index = 0; |
|---|
| 1813 | max_backdrop_index = 8; |
|---|
| 1814 | } |
|---|
| 1815 | else |
|---|
| 1816 | { |
|---|
| 1817 | backdrop_index = _backdropType; |
|---|
| 1818 | max_backdrop_index = _backdropType+1; |
|---|
| 1819 | } |
|---|
| 1820 | |
|---|
| 1821 | state.setTexCoordPointer( 0, 2, GL_FLOAT, 0, &(glyphquad._texcoords.front())); |
|---|
| 1822 | state.disableColorPointer(); |
|---|
| 1823 | state.Color(_backdropColor.r(),_backdropColor.g(),_backdropColor.b(),_backdropColor.a()); |
|---|
| 1824 | |
|---|
| 1825 | for( ; backdrop_index < max_backdrop_index; backdrop_index++) |
|---|
| 1826 | { |
|---|
| 1827 | const GlyphQuads::Coords3& transformedBackdropCoords = glyphquad._transformedBackdropCoords[backdrop_index][contextID]; |
|---|
| 1828 | if (!transformedBackdropCoords.empty()) |
|---|
| 1829 | { |
|---|
| 1830 | state.setVertexPointer( 3, GL_FLOAT, 0, &(transformedBackdropCoords.front())); |
|---|
| 1831 | double offset = double(max_backdrop_index-backdrop_index)*0.0001; |
|---|
| 1832 | glDepthRange( offset, 1.0+offset); |
|---|
| 1833 | |
|---|
| 1834 | state.drawQuads(0,transformedBackdropCoords.size()); |
|---|
| 1835 | } |
|---|
| 1836 | } |
|---|
| 1837 | |
|---|
| 1838 | glDepthRange(0.0, 1.0); |
|---|
| 1839 | |
|---|
| 1840 | drawForegroundText(state, glyphquad, colorMultiplier); |
|---|
| 1841 | } |
|---|
| 1842 | |
|---|
| 1843 | glPopAttrib(); |
|---|
| 1844 | #else |
|---|
| 1845 | OSG_NOTICE<<"Warning: Text::renderWithDepthRange(..) not implemented."<<std::endl; |
|---|
| 1846 | #endif |
|---|
| 1847 | } |
|---|
| 1848 | |
|---|
| 1849 | void Text::renderWithStencilBuffer(osg::State& state, const osg::Vec4& colorMultiplier) const |
|---|
| 1850 | { |
|---|
| 1851 | #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) && !defined(OSG_GL3_AVAILABLE) |
|---|
| 1852 | |
|---|
| 1853 | |
|---|
| 1854 | |
|---|
| 1855 | |
|---|
| 1856 | |
|---|
| 1857 | |
|---|
| 1858 | |
|---|
| 1859 | |
|---|
| 1860 | |
|---|
| 1861 | |
|---|
| 1862 | |
|---|
| 1863 | |
|---|
| 1864 | |
|---|
| 1865 | unsigned int contextID = state.getContextID(); |
|---|
| 1866 | TextureGlyphQuadMap::iterator titr; |
|---|
| 1867 | |
|---|
| 1868 | glPushAttrib(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_STENCIL_TEST); |
|---|
| 1869 | |
|---|
| 1870 | |
|---|
| 1871 | |
|---|
| 1872 | |
|---|
| 1873 | |
|---|
| 1874 | glEnable(GL_STENCIL_TEST); |
|---|
| 1875 | |
|---|
| 1876 | |
|---|
| 1877 | glStencilFunc(GL_ALWAYS, 1, 1); |
|---|
| 1878 | |
|---|
| 1879 | |
|---|
| 1880 | glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); |
|---|
| 1881 | |
|---|
| 1882 | |
|---|
| 1883 | |
|---|
| 1884 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
|---|
| 1885 | |
|---|
| 1886 | |
|---|
| 1887 | |
|---|
| 1888 | |
|---|
| 1889 | |
|---|
| 1890 | |
|---|
| 1891 | |
|---|
| 1892 | glDepthMask(GL_FALSE); |
|---|
| 1893 | |
|---|
| 1894 | |
|---|
| 1895 | |
|---|
| 1896 | |
|---|
| 1897 | |
|---|
| 1898 | for(titr=_textureGlyphQuadMap.begin(); |
|---|
| 1899 | titr!=_textureGlyphQuadMap.end(); |
|---|
| 1900 | ++titr) |
|---|
| 1901 | { |
|---|
| 1902 | |
|---|
| 1903 | state.applyTextureAttribute(0,titr->first.get()); |
|---|
| 1904 | |
|---|
| 1905 | const GlyphQuads& glyphquad = titr->second; |
|---|
| 1906 | |
|---|
| 1907 | unsigned int backdrop_index; |
|---|
| 1908 | unsigned int max_backdrop_index; |
|---|
| 1909 | if(_backdropType == OUTLINE) |
|---|
| 1910 | { |
|---|
| 1911 | backdrop_index = 0; |
|---|
| 1912 | max_backdrop_index = 8; |
|---|
| 1913 | } |
|---|
| 1914 | else |
|---|
| 1915 | { |
|---|
| 1916 | backdrop_index = _backdropType; |
|---|
| 1917 | max_backdrop_index = _backdropType+1; |
|---|
| 1918 | } |
|---|
| 1919 | |
|---|
| 1920 | state.setTexCoordPointer( 0, 2, GL_FLOAT, 0, &(glyphquad._texcoords.front())); |
|---|
| 1921 | state.disableColorPointer(); |
|---|
| 1922 | |
|---|
| 1923 | for( ; backdrop_index < max_backdrop_index; backdrop_index++) |
|---|
| 1924 | { |
|---|
| 1925 | const GlyphQuads::Coords3& transformedBackdropCoords = glyphquad._transformedBackdropCoords[backdrop_index][contextID]; |
|---|
| 1926 | if (!transformedBackdropCoords.empty()) |
|---|
| 1927 | { |
|---|
| 1928 | state.setVertexPointer( 3, GL_FLOAT, 0, &(transformedBackdropCoords.front())); |
|---|
| 1929 | state.drawQuads(0,transformedBackdropCoords.size()); |
|---|
| 1930 | } |
|---|
| 1931 | } |
|---|
| 1932 | |
|---|
| 1933 | |
|---|
| 1934 | const GlyphQuads::Coords3& transformedCoords = glyphquad._transformedCoords[contextID]; |
|---|
| 1935 | if (!transformedCoords.empty()) |
|---|
| 1936 | { |
|---|
| 1937 | state.setVertexPointer( 3, GL_FLOAT, 0, &(transformedCoords.front())); |
|---|
| 1938 | state.setTexCoordPointer( 0, 2, GL_FLOAT, 0, &(glyphquad._texcoords.front())); |
|---|
| 1939 | state.drawQuads(0,transformedCoords.size()); |
|---|
| 1940 | } |
|---|
| 1941 | } |
|---|
| 1942 | |
|---|
| 1943 | |
|---|
| 1944 | |
|---|
| 1945 | |
|---|
| 1946 | |
|---|
| 1947 | |
|---|
| 1948 | |
|---|
| 1949 | |
|---|
| 1950 | |
|---|
| 1951 | |
|---|
| 1952 | |
|---|
| 1953 | glStencilFunc(GL_EQUAL, 1, 1); |
|---|
| 1954 | |
|---|
| 1955 | |
|---|
| 1956 | glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); |
|---|
| 1957 | glStencilMask(GL_FALSE); |
|---|
| 1958 | |
|---|
| 1959 | |
|---|
| 1960 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
|---|
| 1961 | |
|---|
| 1962 | |
|---|
| 1963 | |
|---|
| 1964 | |
|---|
| 1965 | for(titr=_textureGlyphQuadMap.begin(); |
|---|
| 1966 | titr!=_textureGlyphQuadMap.end(); |
|---|
| 1967 | ++titr) |
|---|
| 1968 | { |
|---|
| 1969 | |
|---|
| 1970 | state.applyTextureAttribute(0,titr->first.get()); |
|---|
| 1971 | |
|---|
| 1972 | const GlyphQuads& glyphquad = titr->second; |
|---|
| 1973 | |
|---|
| 1974 | unsigned int backdrop_index; |
|---|
| 1975 | unsigned int max_backdrop_index; |
|---|
| 1976 | if(_backdropType == OUTLINE) |
|---|
| 1977 | { |
|---|
| 1978 | backdrop_index = 0; |
|---|
| 1979 | max_backdrop_index = 8; |
|---|
| 1980 | } |
|---|
| 1981 | else |
|---|
| 1982 | { |
|---|
| 1983 | backdrop_index = _backdropType; |
|---|
| 1984 | max_backdrop_index = _backdropType+1; |
|---|
| 1985 | } |
|---|
| 1986 | |
|---|
| 1987 | state.setTexCoordPointer( 0, 2, GL_FLOAT, 0, &(glyphquad._texcoords.front())); |
|---|
| 1988 | state.disableColorPointer(); |
|---|
| 1989 | state.Color(_backdropColor.r(),_backdropColor.g(),_backdropColor.b(),_backdropColor.a()); |
|---|
| 1990 | |
|---|
| 1991 | for( ; backdrop_index < max_backdrop_index; backdrop_index++) |
|---|
| 1992 | { |
|---|
| 1993 | const GlyphQuads::Coords3& transformedBackdropCoords = glyphquad._transformedBackdropCoords[backdrop_index][contextID]; |
|---|
| 1994 | if (!transformedBackdropCoords.empty()) |
|---|
| 1995 | { |
|---|
| 1996 | state.setVertexPointer( 3, GL_FLOAT, 0, &(transformedBackdropCoords.front())); |
|---|
| 1997 | state.drawQuads(0,transformedBackdropCoords.size()); |
|---|
| 1998 | } |
|---|
| 1999 | } |
|---|
| 2000 | |
|---|
| 2001 | drawForegroundText(state, glyphquad, colorMultiplier); |
|---|
| 2002 | } |
|---|
| 2003 | |
|---|
| 2004 | glPopAttrib(); |
|---|
| 2005 | #else |
|---|
| 2006 | OSG_NOTICE<<"Warning: Text::renderWithStencilBuffer(..) not implemented."<<std::endl; |
|---|
| 2007 | #endif |
|---|
| 2008 | } |
|---|