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> |
fe3a29bc |
19 | #include <Graphic3d_TextureParams.hxx> |
a174a3c5 |
20 | #include <Standard_Assert.hxx> |
cbf18624 |
21 | #include <TCollection_ExtendedString.hxx> |
a174a3c5 |
22 | |
a174a3c5 |
23 | |
24 | // ======================================================================= |
25 | // function : OpenGl_Font |
26 | // purpose : |
27 | // ======================================================================= |
28 | OpenGl_Font::OpenGl_Font (const Handle(Font_FTFont)& theFont, |
29 | const TCollection_AsciiString& theKey) |
30 | : myKey (theKey), |
31 | myFont (theFont), |
32 | myAscender (0.0f), |
33 | myDescender (0.0f), |
34 | myLineSpacing (0.0f), |
35 | myTileSizeX (0), |
36 | myTileSizeY (0), |
37 | myLastTileId (-1), |
ca3c13d1 |
38 | myTextureFormat (GL_ALPHA) |
a174a3c5 |
39 | { |
40 | memset (&myLastTilePx, 0, sizeof(myLastTilePx)); |
41 | } |
42 | |
43 | // ======================================================================= |
44 | // function : ~OpenGl_Font |
45 | // purpose : |
46 | // ======================================================================= |
47 | OpenGl_Font::~OpenGl_Font() |
48 | { |
49 | Release (NULL); |
50 | } |
51 | |
52 | // ======================================================================= |
53 | // function : Release |
54 | // purpose : |
55 | // ======================================================================= |
10b9c7df |
56 | void OpenGl_Font::Release (OpenGl_Context* theCtx) |
a174a3c5 |
57 | { |
58 | if (myTextures.IsEmpty()) |
59 | { |
60 | return; |
61 | } |
62 | |
a174a3c5 |
63 | for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter) |
64 | { |
65 | Handle(OpenGl_Texture)& aTexture = myTextures.ChangeValue (anIter); |
c6ad5e5f |
66 | if (aTexture->IsValid()) |
67 | { |
68 | // application can not handle this case by exception - this is bug in code |
69 | Standard_ASSERT_RETURN (theCtx != NULL, |
70 | "OpenGl_Font destroyed without GL context! Possible GPU memory leakage...",); |
71 | } |
72 | |
a174a3c5 |
73 | aTexture->Release (theCtx); |
74 | aTexture.Nullify(); |
75 | } |
76 | myTextures.Clear(); |
77 | } |
78 | |
79 | // ======================================================================= |
80 | // function : Init |
81 | // purpose : |
82 | // ======================================================================= |
83 | bool OpenGl_Font::Init (const Handle(OpenGl_Context)& theCtx) |
84 | { |
85 | Release (theCtx.operator->()); |
86 | if (myFont.IsNull() || !myFont->IsValid()) |
87 | { |
88 | return false; |
89 | } |
90 | |
91 | myAscender = myFont->Ascender(); |
92 | myDescender = myFont->Descender(); |
93 | myLineSpacing = myFont->LineSpacing(); |
94 | myTileSizeX = myFont->GlyphMaxSizeX(); |
95 | myTileSizeY = myFont->GlyphMaxSizeY(); |
96 | |
97 | myLastTileId = -1; |
c6ad5e5f |
98 | if (!createTexture (theCtx)) |
99 | { |
100 | Release (theCtx.operator->()); |
101 | return false; |
102 | } |
103 | return true; |
a174a3c5 |
104 | } |
105 | |
106 | // ======================================================================= |
107 | // function : createTexture |
108 | // purpose : |
109 | // ======================================================================= |
110 | bool OpenGl_Font::createTexture (const Handle(OpenGl_Context)& theCtx) |
111 | { |
112 | const Standard_Integer aMaxSize = theCtx->MaxTextureSize(); |
113 | |
114 | Standard_Integer aGlyphsNb = myFont->GlyphsNumber() - myLastTileId + 1; |
115 | |
116 | const Standard_Integer aTextureSizeX = OpenGl_Context::GetPowerOfTwo (aGlyphsNb * myTileSizeX, aMaxSize); |
117 | const Standard_Integer aTilesPerRow = aTextureSizeX / myTileSizeX; |
118 | const Standard_Integer aTextureSizeY = OpenGl_Context::GetPowerOfTwo (GLint((aGlyphsNb / aTilesPerRow) + 1) * myTileSizeY, aMaxSize); |
119 | |
120 | memset (&myLastTilePx, 0, sizeof(myLastTilePx)); |
121 | myLastTilePx.Bottom = myTileSizeY; |
122 | |
fe3a29bc |
123 | Handle(Graphic3d_TextureParams) aParams = new Graphic3d_TextureParams(); |
124 | aParams->SetModulate (Standard_False); |
125 | aParams->SetRepeat (Standard_False); |
126 | aParams->SetFilter (Graphic3d_TOTF_BILINEAR); |
127 | aParams->SetAnisoFilter (Graphic3d_LOTA_OFF); |
128 | |
129 | myTextures.Append (new OpenGl_Texture (aParams)); |
a174a3c5 |
130 | Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast(); |
131 | |
132 | Image_PixMap aBlackImg; |
076ca35c |
133 | if (!aBlackImg.InitZero (Image_PixMap::ImgAlpha, Standard_Size(aTextureSizeX), Standard_Size(aTextureSizeY)) |
a174a3c5 |
134 | || !aTexture->Init (theCtx, aBlackImg, Graphic3d_TOT_2D)) // myTextureFormat |
135 | { |
cbf18624 |
136 | TCollection_ExtendedString aMsg; |
137 | aMsg += "New texture intialization of size "; |
138 | aMsg += aTextureSizeX; |
139 | aMsg += "x"; |
140 | aMsg += aTextureSizeY; |
141 | aMsg += " for textured font has failed."; |
142 | theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg); |
a174a3c5 |
143 | return false; |
144 | } |
145 | |
a174a3c5 |
146 | return true; |
147 | } |
148 | |
149 | // ======================================================================= |
150 | // function : renderGlyph |
151 | // purpose : |
152 | // ======================================================================= |
153 | bool OpenGl_Font::renderGlyph (const Handle(OpenGl_Context)& theCtx, |
154 | const Standard_Utf32Char theChar) |
155 | { |
156 | if (!myFont->RenderGlyph (theChar)) |
157 | { |
158 | return false; |
159 | } |
160 | |
161 | Handle(OpenGl_Texture)& aTexture = myTextures.ChangeLast(); |
c6ad5e5f |
162 | if (aTexture.IsNull() |
163 | || !aTexture->IsValid()) |
164 | { |
165 | return false; |
166 | } |
a174a3c5 |
167 | |
168 | const Image_PixMap& anImg = myFont->GlyphImage(); |
169 | const Standard_Integer aTileId = myLastTileId + 1; |
170 | myLastTilePx.Left = myLastTilePx.Right + 3; |
171 | myLastTilePx.Right = myLastTilePx.Left + (Standard_Integer )anImg.SizeX(); |
172 | if (myLastTilePx.Right >= aTexture->SizeX()) |
173 | { |
174 | myLastTilePx.Left = 0; |
175 | myLastTilePx.Right = (Standard_Integer )anImg.SizeX(); |
176 | myLastTilePx.Top += myTileSizeY; |
177 | myLastTilePx.Bottom += myTileSizeY; |
178 | |
179 | if (myLastTilePx.Bottom >= aTexture->SizeY()) |
180 | { |
181 | if (!createTexture (theCtx)) |
182 | { |
183 | return false; |
184 | } |
185 | return renderGlyph (theCtx, theChar); |
186 | } |
187 | } |
188 | |
189 | aTexture->Bind (theCtx); |
ca3c13d1 |
190 | #if !defined(GL_ES_VERSION_2_0) |
a174a3c5 |
191 | glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE); |
192 | glPixelStorei (GL_UNPACK_ROW_LENGTH, 0); |
ca3c13d1 |
193 | #endif |
a174a3c5 |
194 | glPixelStorei (GL_UNPACK_ALIGNMENT, 1); |
195 | |
196 | glTexSubImage2D (GL_TEXTURE_2D, 0, |
197 | myLastTilePx.Left, myLastTilePx.Top, (GLsizei )anImg.SizeX(), (GLsizei )anImg.SizeY(), |
c6ad5e5f |
198 | aTexture->GetFormat(), GL_UNSIGNED_BYTE, anImg.Data()); |
a174a3c5 |
199 | |
200 | OpenGl_Font::Tile aTile; |
201 | aTile.uv.Left = GLfloat(myLastTilePx.Left) / GLfloat(aTexture->SizeX()); |
202 | aTile.uv.Right = GLfloat(myLastTilePx.Right) / GLfloat(aTexture->SizeX()); |
203 | aTile.uv.Top = GLfloat(myLastTilePx.Top) / GLfloat(aTexture->SizeY()); |
204 | aTile.uv.Bottom = GLfloat(myLastTilePx.Top + anImg.SizeY()) / GLfloat(aTexture->SizeY()); |
205 | aTile.texture = aTexture->TextureId(); |
206 | myFont->GlyphRect (aTile.px); |
207 | |
208 | myLastTileId = aTileId; |
209 | myTiles.Append (aTile); |
210 | return true; |
211 | } |
212 | |
213 | // ======================================================================= |
214 | // function : RenderGlyph |
215 | // purpose : |
216 | // ======================================================================= |
317d68c9 |
217 | bool OpenGl_Font::RenderGlyph (const Handle(OpenGl_Context)& theCtx, |
a174a3c5 |
218 | const Standard_Utf32Char theUChar, |
317d68c9 |
219 | Tile& theGlyph) |
a174a3c5 |
220 | { |
221 | Standard_Integer aTileId = 0; |
317d68c9 |
222 | if (!myGlyphMap.Find (theUChar,aTileId)) |
a174a3c5 |
223 | { |
224 | if (renderGlyph (theCtx, theUChar)) |
225 | { |
226 | aTileId = myLastTileId; |
227 | } |
228 | else |
229 | { |
317d68c9 |
230 | return false; |
a174a3c5 |
231 | } |
317d68c9 |
232 | |
a174a3c5 |
233 | myGlyphMap.Bind (theUChar, aTileId); |
234 | } |
235 | |
236 | const OpenGl_Font::Tile& aTile = myTiles.Value (aTileId); |
317d68c9 |
237 | theGlyph.px = aTile.px; |
238 | theGlyph.uv = aTile.uv; |
239 | theGlyph.texture = aTile.texture; |
240 | |
241 | return true; |
a174a3c5 |
242 | } |