0030595: Oriented Bounding Box seems not optimal for some shapes
[occt.git] / src / OpenGl / OpenGl_Font.cxx
CommitLineData
a174a3c5 1// Created on: 2013-01-29
2// Created by: Kirill GAVRILOV
d5f74e42 3// Copyright (c) 2013-2014 OPEN CASCADE SAS
a174a3c5 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
a174a3c5 6//
d5f74e42 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
973c2be1 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.
a174a3c5 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
a174a3c5 15
16#include <OpenGl_Font.hxx>
17
18#include <OpenGl_Context.hxx>
d2eddacc 19#include <Font_FTFont.hxx>
fe3a29bc 20#include <Graphic3d_TextureParams.hxx>
a174a3c5 21#include <Standard_Assert.hxx>
cbf18624 22#include <TCollection_ExtendedString.hxx>
a174a3c5 23
a174a3c5 24
92efcf78 25IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Font,OpenGl_Resource)
26
a174a3c5 27// =======================================================================
28// function : OpenGl_Font
29// purpose :
30// =======================================================================
31OpenGl_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),
ca3c13d1 41 myTextureFormat (GL_ALPHA)
a174a3c5 42{
43 memset (&myLastTilePx, 0, sizeof(myLastTilePx));
44}
45
46// =======================================================================
47// function : ~OpenGl_Font
48// purpose :
49// =======================================================================
50OpenGl_Font::~OpenGl_Font()
51{
52 Release (NULL);
53}
54
55// =======================================================================
56// function : Release
57// purpose :
58// =======================================================================
10b9c7df 59void OpenGl_Font::Release (OpenGl_Context* theCtx)
a174a3c5 60{
61 if (myTextures.IsEmpty())
62 {
63 return;
64 }
65
a174a3c5 66 for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter)
67 {
68 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeValue (anIter);
c6ad5e5f 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
a174a3c5 76 aTexture->Release (theCtx);
77 aTexture.Nullify();
78 }
79 myTextures.Clear();
80}
81
82// =======================================================================
15669413 83// function : EstimatedDataSize
84// purpose :
85// =======================================================================
86Standard_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// =======================================================================
a174a3c5 97// function : Init
98// purpose :
99// =======================================================================
100bool 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;
c6ad5e5f 115 if (!createTexture (theCtx))
116 {
117 Release (theCtx.operator->());
118 return false;
119 }
120 return true;
a174a3c5 121}
122
123// =======================================================================
124// function : createTexture
125// purpose :
126// =======================================================================
127bool 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
fe3a29bc 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
cc8cbabe 146 myTextures.Append (new OpenGl_Texture (myKey + "_texture" + myTextures.Size(), aParams));
a174a3c5 147 Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast();
148
149 Image_PixMap aBlackImg;
dc858f4c 150 if (!aBlackImg.InitZero (Image_Format_Alpha, Standard_Size(aTextureSizeX), Standard_Size(aTextureSizeY))
a174a3c5 151 || !aTexture->Init (theCtx, aBlackImg, Graphic3d_TOT_2D)) // myTextureFormat
152 {
cbf18624 153 TCollection_ExtendedString aMsg;
dc858f4c 154 aMsg += "New texture initialization of size ";
cbf18624 155 aMsg += aTextureSizeX;
156 aMsg += "x";
157 aMsg += aTextureSizeY;
158 aMsg += " for textured font has failed.";
3b523c4c 159 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
a174a3c5 160 return false;
161 }
162
a174a3c5 163 return true;
164}
165
166// =======================================================================
167// function : renderGlyph
168// purpose :
169// =======================================================================
170bool 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();
c6ad5e5f 179 if (aTexture.IsNull()
180 || !aTexture->IsValid())
181 {
182 return false;
183 }
a174a3c5 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);
ca3c13d1 207#if !defined(GL_ES_VERSION_2_0)
a174a3c5 208 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
209 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
ca3c13d1 210#endif
a174a3c5 211 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
212
213 glTexSubImage2D (GL_TEXTURE_2D, 0,
214 myLastTilePx.Left, myLastTilePx.Top, (GLsizei )anImg.SizeX(), (GLsizei )anImg.SizeY(),
c6ad5e5f 215 aTexture->GetFormat(), GL_UNSIGNED_BYTE, anImg.Data());
a174a3c5 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// =======================================================================
317d68c9 234bool OpenGl_Font::RenderGlyph (const Handle(OpenGl_Context)& theCtx,
a174a3c5 235 const Standard_Utf32Char theUChar,
317d68c9 236 Tile& theGlyph)
a174a3c5 237{
238 Standard_Integer aTileId = 0;
317d68c9 239 if (!myGlyphMap.Find (theUChar,aTileId))
a174a3c5 240 {
241 if (renderGlyph (theCtx, theUChar))
242 {
243 aTileId = myLastTileId;
244 }
245 else
246 {
317d68c9 247 return false;
a174a3c5 248 }
317d68c9 249
a174a3c5 250 myGlyphMap.Bind (theUChar, aTileId);
251 }
252
253 const OpenGl_Font::Tile& aTile = myTiles.Value (aTileId);
317d68c9 254 theGlyph.px = aTile.px;
255 theGlyph.uv = aTile.uv;
256 theGlyph.texture = aTile.texture;
257
258 return true;
a174a3c5 259}