| 1 | // Created on: 2013-01-29 |
| 2 | // Created by: Kirill GAVRILOV |
| 3 | // Copyright (c) 2013-2014 OPEN CASCADE SAS |
| 4 | // |
| 5 | // This file is part of Open CASCADE Technology software library. |
| 6 | // |
| 7 | // This library is free software; you can redistribute it and/or modify it under |
| 8 | // the terms of the GNU Lesser General Public License version 2.1 as published |
| 9 | // by the Free Software Foundation, with special exception defined in the file |
| 10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
| 11 | // distribution for complete text of the license and disclaimer of any warranty. |
| 12 | // |
| 13 | // Alternatively, this file may be used under the terms of Open CASCADE |
| 14 | // commercial license or contractual agreement. |
| 15 | |
| 16 | #include <OpenGl_Font.hxx> |
| 17 | |
| 18 | #include <OpenGl_Context.hxx> |
| 19 | #include <Font_FTFont.hxx> |
| 20 | #include <Graphic3d_TextureParams.hxx> |
| 21 | #include <Standard_Assert.hxx> |
| 22 | #include <TCollection_ExtendedString.hxx> |
| 23 | |
| 24 | |
| 25 | IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Font,OpenGl_Resource) |
| 26 | |
| 27 | // ======================================================================= |
| 28 | // function : OpenGl_Font |
| 29 | // purpose : |
| 30 | // ======================================================================= |
| 31 | OpenGl_Font::OpenGl_Font (const Handle(Font_FTFont)& theFont, |
| 32 | const TCollection_AsciiString& theKey) |
| 33 | : myKey (theKey), |
| 34 | myFont (theFont), |
| 35 | myAscender (0.0f), |
| 36 | myDescender (0.0f), |
| 37 | myLineSpacing (0.0f), |
| 38 | myTileSizeX (0), |
| 39 | myTileSizeY (0), |
| 40 | myLastTileId (-1), |
| 41 | myTextureFormat (GL_ALPHA) |
| 42 | { |
| 43 | memset (&myLastTilePx, 0, sizeof(myLastTilePx)); |
| 44 | } |
| 45 | |
| 46 | // ======================================================================= |
| 47 | // function : ~OpenGl_Font |
| 48 | // purpose : |
| 49 | // ======================================================================= |
| 50 | OpenGl_Font::~OpenGl_Font() |
| 51 | { |
| 52 | Release (NULL); |
| 53 | } |
| 54 | |
| 55 | // ======================================================================= |
| 56 | // function : Release |
| 57 | // purpose : |
| 58 | // ======================================================================= |
| 59 | void OpenGl_Font::Release (OpenGl_Context* theCtx) |
| 60 | { |
| 61 | if (myTextures.IsEmpty()) |
| 62 | { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter) |
| 67 | { |
| 68 | Handle(OpenGl_Texture)& aTexture = myTextures.ChangeValue (anIter); |
| 69 | if (aTexture->IsValid()) |
| 70 | { |
| 71 | // application can not handle this case by exception - this is bug in code |
| 72 | Standard_ASSERT_RETURN (theCtx != NULL, |
| 73 | "OpenGl_Font destroyed without GL context! Possible GPU memory leakage...",); |
| 74 | } |
| 75 | |
| 76 | aTexture->Release (theCtx); |
| 77 | aTexture.Nullify(); |
| 78 | } |
| 79 | myTextures.Clear(); |
| 80 | } |
| 81 | |
| 82 | // ======================================================================= |
| 83 | // function : EstimatedDataSize |
| 84 | // purpose : |
| 85 | // ======================================================================= |
| 86 | Standard_Size OpenGl_Font::EstimatedDataSize() const |
| 87 | { |
| 88 | Standard_Size aSize = 0; |
| 89 | for (NCollection_Vector<Handle(OpenGl_Texture)>::Iterator aTexIter (myTextures); aTexIter.More(); aTexIter.Next()) |
| 90 | { |
| 91 | aSize += aTexIter.Value()->EstimatedDataSize(); |
| 92 | } |
| 93 | return aSize; |
| 94 | } |
| 95 | |
| 96 | // ======================================================================= |
| 97 | // function : Init |
| 98 | // purpose : |
| 99 | // ======================================================================= |
| 100 | bool OpenGl_Font::Init (const Handle(OpenGl_Context)& theCtx) |
| 101 | { |
| 102 | Release (theCtx.operator->()); |
| 103 | if (myFont.IsNull() || !myFont->IsValid()) |
| 104 | { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | myAscender = myFont->Ascender(); |
| 109 | myDescender = myFont->Descender(); |
| 110 | myLineSpacing = myFont->LineSpacing(); |
| 111 | myTileSizeX = myFont->GlyphMaxSizeX(); |
| 112 | myTileSizeY = myFont->GlyphMaxSizeY(); |
| 113 | |
| 114 | myLastTileId = -1; |
| 115 | if (!createTexture (theCtx)) |
| 116 | { |
| 117 | Release (theCtx.operator->()); |
| 118 | return false; |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | // ======================================================================= |
| 124 | // function : createTexture |
| 125 | // purpose : |
| 126 | // ======================================================================= |
| 127 | bool OpenGl_Font::createTexture (const Handle(OpenGl_Context)& theCtx) |
| 128 | { |
| 129 | const Standard_Integer aMaxSize = theCtx->MaxTextureSize(); |
| 130 | |
| 131 | Standard_Integer aGlyphsNb = myFont->GlyphsNumber() - myLastTileId + 1; |
| 132 | |
| 133 | const Standard_Integer aTextureSizeX = OpenGl_Context::GetPowerOfTwo (aGlyphsNb * myTileSizeX, aMaxSize); |
| 134 | const Standard_Integer aTilesPerRow = aTextureSizeX / myTileSizeX; |
| 135 | const Standard_Integer aTextureSizeY = OpenGl_Context::GetPowerOfTwo (GLint((aGlyphsNb / aTilesPerRow) + 1) * myTileSizeY, aMaxSize); |
| 136 | |
| 137 | memset (&myLastTilePx, 0, sizeof(myLastTilePx)); |
| 138 | myLastTilePx.Bottom = myTileSizeY; |
| 139 | |
| 140 | Handle(Graphic3d_TextureParams) aParams = new Graphic3d_TextureParams(); |
| 141 | aParams->SetModulate (Standard_False); |
| 142 | aParams->SetRepeat (Standard_False); |
| 143 | aParams->SetFilter (Graphic3d_TOTF_BILINEAR); |
| 144 | aParams->SetAnisoFilter (Graphic3d_LOTA_OFF); |
| 145 | |
| 146 | myTextures.Append (new OpenGl_Texture (myKey + "_texture" + myTextures.Size(), aParams)); |
| 147 | Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast(); |
| 148 | |
| 149 | Image_PixMap aBlackImg; |
| 150 | if (!aBlackImg.InitZero (Image_Format_Alpha, Standard_Size(aTextureSizeX), Standard_Size(aTextureSizeY)) |
| 151 | || !aTexture->Init (theCtx, aBlackImg, Graphic3d_TOT_2D)) // myTextureFormat |
| 152 | { |
| 153 | TCollection_ExtendedString aMsg; |
| 154 | aMsg += "New texture initialization of size "; |
| 155 | aMsg += aTextureSizeX; |
| 156 | aMsg += "x"; |
| 157 | aMsg += aTextureSizeY; |
| 158 | aMsg += " for textured font has failed."; |
| 159 | theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg); |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | // ======================================================================= |
| 167 | // function : renderGlyph |
| 168 | // purpose : |
| 169 | // ======================================================================= |
| 170 | bool OpenGl_Font::renderGlyph (const Handle(OpenGl_Context)& theCtx, |
| 171 | const Standard_Utf32Char theChar) |
| 172 | { |
| 173 | if (!myFont->RenderGlyph (theChar)) |
| 174 | { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast(); |
| 179 | if (aTexture.IsNull() |
| 180 | || !aTexture->IsValid()) |
| 181 | { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | const Image_PixMap& anImg = myFont->GlyphImage(); |
| 186 | const Standard_Integer aTileId = myLastTileId + 1; |
| 187 | myLastTilePx.Left = myLastTilePx.Right + 3; |
| 188 | myLastTilePx.Right = myLastTilePx.Left + (Standard_Integer )anImg.SizeX(); |
| 189 | if (myLastTilePx.Right >= aTexture->SizeX()) |
| 190 | { |
| 191 | myLastTilePx.Left = 0; |
| 192 | myLastTilePx.Right = (Standard_Integer )anImg.SizeX(); |
| 193 | myLastTilePx.Top += myTileSizeY; |
| 194 | myLastTilePx.Bottom += myTileSizeY; |
| 195 | |
| 196 | if (myLastTilePx.Bottom >= aTexture->SizeY()) |
| 197 | { |
| 198 | if (!createTexture (theCtx)) |
| 199 | { |
| 200 | return false; |
| 201 | } |
| 202 | return renderGlyph (theCtx, theChar); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | aTexture->Bind (theCtx); |
| 207 | #if !defined(GL_ES_VERSION_2_0) |
| 208 | glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE); |
| 209 | glPixelStorei (GL_UNPACK_ROW_LENGTH, 0); |
| 210 | #endif |
| 211 | glPixelStorei (GL_UNPACK_ALIGNMENT, 1); |
| 212 | |
| 213 | glTexSubImage2D (GL_TEXTURE_2D, 0, |
| 214 | myLastTilePx.Left, myLastTilePx.Top, (GLsizei )anImg.SizeX(), (GLsizei )anImg.SizeY(), |
| 215 | aTexture->GetFormat(), GL_UNSIGNED_BYTE, anImg.Data()); |
| 216 | |
| 217 | OpenGl_Font::Tile aTile; |
| 218 | aTile.uv.Left = GLfloat(myLastTilePx.Left) / GLfloat(aTexture->SizeX()); |
| 219 | aTile.uv.Right = GLfloat(myLastTilePx.Right) / GLfloat(aTexture->SizeX()); |
| 220 | aTile.uv.Top = GLfloat(myLastTilePx.Top) / GLfloat(aTexture->SizeY()); |
| 221 | aTile.uv.Bottom = GLfloat(myLastTilePx.Top + anImg.SizeY()) / GLfloat(aTexture->SizeY()); |
| 222 | aTile.texture = aTexture->TextureId(); |
| 223 | myFont->GlyphRect (aTile.px); |
| 224 | |
| 225 | myLastTileId = aTileId; |
| 226 | myTiles.Append (aTile); |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | // ======================================================================= |
| 231 | // function : RenderGlyph |
| 232 | // purpose : |
| 233 | // ======================================================================= |
| 234 | bool OpenGl_Font::RenderGlyph (const Handle(OpenGl_Context)& theCtx, |
| 235 | const Standard_Utf32Char theUChar, |
| 236 | Tile& theGlyph) |
| 237 | { |
| 238 | Standard_Integer aTileId = 0; |
| 239 | if (!myGlyphMap.Find (theUChar,aTileId)) |
| 240 | { |
| 241 | if (renderGlyph (theCtx, theUChar)) |
| 242 | { |
| 243 | aTileId = myLastTileId; |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | myGlyphMap.Bind (theUChar, aTileId); |
| 251 | } |
| 252 | |
| 253 | const OpenGl_Font::Tile& aTile = myTiles.Value (aTileId); |
| 254 | theGlyph.px = aTile.px; |
| 255 | theGlyph.uv = aTile.uv; |
| 256 | theGlyph.texture = aTile.texture; |
| 257 | |
| 258 | return true; |
| 259 | } |