Commit | Line | Data |
---|---|---|
b311480e | 1 | // Created on: 2011-07-13 |
2 | // Created by: Sergey ZERCHANINOV | |
a174a3c5 | 3 | // Copyright (c) 2011-2013 OPEN CASCADE SAS |
b311480e | 4 | // |
973c2be1 | 5 | // This file is part of Open CASCADE Technology software library. |
b311480e | 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. | |
b311480e | 12 | // |
973c2be1 | 13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. | |
b311480e | 15 | |
2166f0fa | 16 | #include <OpenGl_AspectText.hxx> |
30f0ad28 | 17 | #include <OpenGl_GlCore11.hxx> |
a174a3c5 | 18 | #include <OpenGl_GraphicDriver.hxx> |
30f0ad28 | 19 | #include <OpenGl_ShaderManager.hxx> |
20 | #include <OpenGl_ShaderProgram.hxx> | |
21 | #include <OpenGl_ShaderStates.hxx> | |
22 | #include <OpenGl_Text.hxx> | |
bf75be98 | 23 | #include <OpenGl_Workspace.hxx> |
2166f0fa | 24 | |
a174a3c5 | 25 | #include <Font_FontMgr.hxx> |
26 | #include <TCollection_HAsciiString.hxx> | |
27 | ||
28 | #ifdef HAVE_CONFIG_H | |
29 | #include <config.h> | |
30 | #endif | |
2166f0fa | 31 | |
a174a3c5 | 32 | #ifdef HAVE_GL2PS |
33 | #include <gl2ps.h> | |
34 | #endif | |
35 | ||
36 | namespace | |
2166f0fa | 37 | { |
a174a3c5 | 38 | static const GLdouble THE_IDENTITY_MATRIX[4][4] = |
39 | { | |
40 | {1.,0.,0.,0.}, | |
41 | {0.,1.,0.,0.}, | |
42 | {0.,0.,1.,0.}, | |
43 | {0.,0.,0.,1.} | |
44 | }; | |
45 | ||
46 | #ifdef HAVE_GL2PS | |
47 | static char const* TheFamily[] = {"Helvetica", "Courier", "Times"}; | |
48 | static char const* TheItalic[] = {"Oblique", "Oblique", "Italic"}; | |
49 | static char const* TheBase[] = {"", "", "-Roman"}; | |
50 | ||
51 | //! Convert font name used for rendering to some "good" font names | |
52 | //! that produce good vector text. | |
53 | static void getGL2PSFontName (const char* theSrcFont, | |
54 | char* thePsFont) | |
55 | { | |
56 | if (strstr (theSrcFont, "Symbol")) | |
57 | { | |
58 | sprintf (thePsFont, "%s", "Symbol"); | |
59 | return; | |
60 | } | |
61 | else if (strstr (theSrcFont, "ZapfDingbats")) | |
62 | { | |
63 | sprintf (thePsFont, "%s", "WingDings"); | |
64 | return; | |
65 | } | |
2166f0fa | 66 | |
a174a3c5 | 67 | int aFontId = 0; |
68 | bool isBold = false; | |
69 | bool isItalic = false; | |
70 | if (strstr (theSrcFont, "Courier")) | |
71 | { | |
72 | aFontId = 1; | |
73 | } | |
74 | else if (strstr (theSrcFont, "Times")) | |
75 | { | |
76 | aFontId = 2; | |
77 | } | |
2166f0fa | 78 | |
a174a3c5 | 79 | if (strstr (theSrcFont, "Bold")) |
80 | { | |
81 | isBold = true; | |
82 | } | |
83 | if (strstr (theSrcFont, "Italic") | |
84 | || strstr (theSrcFont, "Oblique")) | |
85 | { | |
86 | isItalic = true; | |
87 | } | |
2166f0fa | 88 | |
a174a3c5 | 89 | if (isBold) |
90 | { | |
a174a3c5 | 91 | if (isItalic) |
92 | { | |
8b224a09 | 93 | sprintf (thePsFont, "%s-Bold%s", TheFamily[aFontId], TheItalic[aFontId]); |
94 | } | |
95 | else | |
96 | { | |
97 | sprintf (thePsFont, "%s-Bold", TheFamily[aFontId]); | |
a174a3c5 | 98 | } |
99 | } | |
100 | else if (isItalic) | |
101 | { | |
102 | sprintf (thePsFont, "%s-%s", TheFamily[aFontId], TheItalic[aFontId]); | |
103 | } | |
104 | else | |
105 | { | |
106 | sprintf (thePsFont, "%s%s", TheFamily[aFontId], TheBase[aFontId]); | |
107 | } | |
108 | } | |
2166f0fa | 109 | |
a174a3c5 | 110 | static void exportText (const NCollection_String& theText, |
111 | const Standard_Boolean theIs2d, | |
112 | const OpenGl_AspectText& theAspect, | |
113 | const Standard_Integer theHeight) | |
114 | { | |
2166f0fa | 115 | |
a174a3c5 | 116 | char aPsFont[64]; |
117 | getGL2PSFontName (theAspect.FontName().ToCString(), aPsFont); | |
2166f0fa | 118 | |
a174a3c5 | 119 | if (theIs2d) |
120 | { | |
121 | glRasterPos2f (0.0f, 0.0f); | |
122 | } | |
123 | else | |
124 | { | |
125 | glRasterPos3f (0.0f, 0.0f, 0.0f); | |
126 | } | |
127 | ||
128 | GLubyte aZero = 0; | |
129 | glBitmap (1, 1, 0, 0, 0, 0, &aZero); | |
130 | ||
131 | // Standard GL2PS's alignment isn't used, because it doesn't work correctly | |
132 | // for all formats, therefore alignment is calculated manually relative | |
133 | // to the bottom-left corner, which corresponds to the GL2PS_TEXT_BL value | |
105aae76 | 134 | gl2psTextOpt (theText.ToCString(), aPsFont, (GLshort)theHeight, GL2PS_TEXT_BL, theAspect.Angle()); |
a174a3c5 | 135 | } |
136 | #endif | |
137 | ||
138 | }; | |
139 | ||
140 | // ======================================================================= | |
141 | // function : OpenGl_Text | |
142 | // purpose : | |
143 | // ======================================================================= | |
144 | OpenGl_Text::OpenGl_Text() | |
145 | : myWinX (0.0f), | |
146 | myWinY (0.0f), | |
147 | myWinZ (0.0f), | |
148 | myScaleHeight (1.0f), | |
149 | myPoint (0.0f, 0.0f, 0.0f), | |
150 | myIs2d (false) | |
151 | { | |
152 | myParams.Height = 10; | |
153 | myParams.HAlign = Graphic3d_HTA_LEFT; | |
154 | myParams.VAlign = Graphic3d_VTA_BOTTOM; | |
2166f0fa SK |
155 | } |
156 | ||
a174a3c5 | 157 | // ======================================================================= |
158 | // function : OpenGl_Text | |
159 | // purpose : | |
160 | // ======================================================================= | |
b64d84be | 161 | OpenGl_Text::OpenGl_Text (const Standard_Utf8Char* theText, |
162 | const OpenGl_Vec3& thePoint, | |
163 | const OpenGl_TextParam& theParams) | |
a174a3c5 | 164 | : myWinX (0.0f), |
165 | myWinY (0.0f), | |
166 | myWinZ (0.0f), | |
167 | myScaleHeight (1.0f), | |
168 | myExportHeight (1.0f), | |
169 | myParams (theParams), | |
b64d84be | 170 | myString (theText), |
a174a3c5 | 171 | myPoint (thePoint), |
172 | myIs2d (false) | |
173 | { | |
174 | // | |
175 | } | |
2166f0fa | 176 | |
a174a3c5 | 177 | // ======================================================================= |
178 | // function : SetPosition | |
179 | // purpose : | |
180 | // ======================================================================= | |
181 | void OpenGl_Text::SetPosition (const OpenGl_Vec3& thePoint) | |
2166f0fa | 182 | { |
a174a3c5 | 183 | myPoint = thePoint; |
2166f0fa SK |
184 | } |
185 | ||
a174a3c5 | 186 | // ======================================================================= |
187 | // function : SetFontSize | |
188 | // purpose : | |
189 | // ======================================================================= | |
190 | void OpenGl_Text::SetFontSize (const Handle(OpenGl_Context)& theCtx, | |
191 | const Standard_Integer theFontSize) | |
192 | { | |
193 | if (myParams.Height != theFontSize) | |
194 | { | |
10b9c7df | 195 | Release (theCtx.operator->()); |
a174a3c5 | 196 | } |
197 | myParams.Height = theFontSize; | |
198 | } | |
199 | ||
200 | // ======================================================================= | |
201 | // function : Init | |
202 | // purpose : | |
203 | // ======================================================================= | |
204 | void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx, | |
205 | const Standard_Utf8Char* theText, | |
206 | const OpenGl_Vec3& thePoint) | |
207 | { | |
10b9c7df | 208 | releaseVbos (theCtx.operator->()); |
a174a3c5 | 209 | myIs2d = false; |
210 | myPoint = thePoint; | |
211 | myString.FromUnicode (theText); | |
212 | } | |
213 | ||
214 | // ======================================================================= | |
215 | // function : Init | |
216 | // purpose : | |
217 | // ======================================================================= | |
218 | void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx, | |
219 | const Standard_Utf8Char* theText, | |
220 | const OpenGl_Vec3& thePoint, | |
221 | const OpenGl_TextParam& theParams) | |
222 | { | |
223 | if (myParams.Height != theParams.Height) | |
224 | { | |
10b9c7df | 225 | Release (theCtx.operator->()); |
a174a3c5 | 226 | } |
227 | else | |
228 | { | |
10b9c7df | 229 | releaseVbos (theCtx.operator->()); |
a174a3c5 | 230 | } |
231 | myIs2d = false; | |
232 | myParams = theParams; | |
233 | myPoint = thePoint; | |
234 | myString.FromUnicode (theText); | |
235 | } | |
2166f0fa | 236 | |
a174a3c5 | 237 | // ======================================================================= |
238 | // function : Init | |
239 | // purpose : | |
240 | // ======================================================================= | |
241 | void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx, | |
242 | const TCollection_ExtendedString& theText, | |
243 | const OpenGl_Vec2& thePoint, | |
244 | const OpenGl_TextParam& theParams) | |
2166f0fa | 245 | { |
a174a3c5 | 246 | if (myParams.Height != theParams.Height) |
247 | { | |
10b9c7df | 248 | Release (theCtx.operator->()); |
a174a3c5 | 249 | } |
250 | else | |
251 | { | |
10b9c7df | 252 | releaseVbos (theCtx.operator->()); |
a174a3c5 | 253 | } |
254 | myIs2d = true; | |
255 | myParams = theParams; | |
256 | myPoint.xy() = thePoint; | |
257 | myPoint.z() = 0.0f; | |
258 | myString.FromUnicode ((Standard_Utf16Char* )theText.ToExtString()); | |
259 | } | |
260 | ||
261 | // ======================================================================= | |
262 | // function : ~OpenGl_Text | |
263 | // purpose : | |
264 | // ======================================================================= | |
265 | OpenGl_Text::~OpenGl_Text() | |
266 | { | |
267 | // | |
268 | } | |
269 | ||
270 | // ======================================================================= | |
271 | // function : releaseVbos | |
272 | // purpose : | |
273 | // ======================================================================= | |
10b9c7df | 274 | void OpenGl_Text::releaseVbos (OpenGl_Context* theCtx) |
a174a3c5 | 275 | { |
276 | for (Standard_Integer anIter = 0; anIter < myVertsVbo.Length(); ++anIter) | |
277 | { | |
278 | Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.ChangeValue (anIter); | |
279 | Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.ChangeValue (anIter); | |
280 | ||
10b9c7df | 281 | if (theCtx) |
a174a3c5 | 282 | { |
283 | theCtx->DelayedRelease (aVerts); | |
284 | theCtx->DelayedRelease (aTCrds); | |
285 | } | |
286 | aVerts.Nullify(); | |
287 | aTCrds.Nullify(); | |
288 | } | |
289 | myTextures.Clear(); | |
290 | myVertsVbo.Clear(); | |
291 | myTCrdsVbo.Clear(); | |
292 | myVertsArray.Clear(); | |
293 | myTCrdsArray.Clear(); | |
294 | } | |
295 | ||
296 | // ======================================================================= | |
297 | // function : Release | |
298 | // purpose : | |
299 | // ======================================================================= | |
10b9c7df | 300 | void OpenGl_Text::Release (OpenGl_Context* theCtx) |
a174a3c5 | 301 | { |
302 | releaseVbos (theCtx); | |
303 | if (!myFont.IsNull()) | |
304 | { | |
305 | Handle(OpenGl_Context) aCtx = theCtx; | |
306 | const TCollection_AsciiString aKey = myFont->ResourceKey(); | |
307 | myFont.Nullify(); | |
10b9c7df | 308 | if (aCtx) |
309 | aCtx->ReleaseResource (aKey, Standard_True); | |
a174a3c5 | 310 | } |
311 | } | |
312 | ||
313 | // ======================================================================= | |
314 | // function : StringSize | |
315 | // purpose : | |
316 | // ======================================================================= | |
317 | void OpenGl_Text::StringSize (const Handle(OpenGl_Context)& theCtx, | |
318 | const NCollection_String& theText, | |
319 | const OpenGl_AspectText& theTextAspect, | |
320 | const OpenGl_TextParam& theParams, | |
321 | Standard_ShortReal& theWidth, | |
322 | Standard_ShortReal& theAscent, | |
323 | Standard_ShortReal& theDescent) | |
324 | { | |
325 | theWidth = 0.0f; | |
326 | theAscent = 0.0f; | |
327 | theDescent = 0.0f; | |
328 | const TCollection_AsciiString aFontKey = FontKey (theTextAspect, theParams.Height); | |
329 | Handle(OpenGl_Font) aFont = FindFont (theCtx, theTextAspect, theParams.Height, aFontKey); | |
330 | if (aFont.IsNull() || !aFont->IsValid()) | |
331 | { | |
2166f0fa | 332 | return; |
a174a3c5 | 333 | } |
334 | ||
335 | theAscent = aFont->Ascender(); | |
336 | theDescent = aFont->Descender(); | |
337 | ||
338 | GLfloat aWidth = 0.0f; | |
339 | for (NCollection_Utf8Iter anIter = theText.Iterator(); *anIter != 0;) | |
340 | { | |
341 | const Standard_Utf32Char aCharThis = *anIter; | |
342 | const Standard_Utf32Char aCharNext = *++anIter; | |
343 | ||
344 | if (aCharThis == '\x0D' // CR (carriage return) | |
345 | || aCharThis == '\a' // BEL (alarm) | |
346 | || aCharThis == '\f' // FF (form feed) NP (new page) | |
347 | || aCharThis == '\b' // BS (backspace) | |
348 | || aCharThis == '\v') // VT (vertical tab) | |
349 | { | |
350 | continue; // skip unsupported carriage control codes | |
351 | } | |
352 | else if (aCharThis == '\x0A') // LF (line feed, new line) | |
353 | { | |
354 | theWidth = Max (theWidth, aWidth); | |
355 | aWidth = 0.0f; | |
356 | continue; | |
357 | } | |
358 | else if (aCharThis == ' ') | |
359 | { | |
360 | aWidth += aFont->AdvanceX (aCharThis, aCharNext); | |
361 | continue; | |
362 | } | |
363 | else if (aCharThis == '\t') | |
364 | { | |
365 | aWidth += aFont->AdvanceX (' ', aCharNext) * 8.0f; | |
366 | continue; | |
367 | } | |
368 | ||
369 | aWidth += aFont->AdvanceX (aCharThis, aCharNext); | |
370 | } | |
371 | theWidth = Max (theWidth, aWidth); | |
372 | ||
373 | Handle(OpenGl_Context) aCtx = theCtx; | |
374 | aFont.Nullify(); | |
375 | aCtx->ReleaseResource (aFontKey, Standard_True); | |
376 | } | |
377 | ||
378 | // ======================================================================= | |
379 | // function : Render | |
380 | // purpose : | |
381 | // ======================================================================= | |
382 | void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const | |
383 | { | |
a174a3c5 | 384 | const OpenGl_AspectText* aTextAspect = theWorkspace->AspectText (Standard_True); |
385 | const Handle(OpenGl_Texture) aPrevTexture = theWorkspace->DisableTexture(); | |
386 | ||
30f0ad28 | 387 | const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext(); |
388 | ||
389 | if (aCtx->IsGlGreaterEqual (2, 0)) | |
390 | { | |
391 | Handle(OpenGl_ShaderProgram) aProgram = aTextAspect->ShaderProgramRes (theWorkspace); | |
392 | ||
393 | if (!aProgram.IsNull()) | |
394 | { | |
395 | aProgram->BindWithVariables (aCtx); | |
396 | ||
397 | const OpenGl_MaterialState* aMaterialState = aCtx->ShaderManager()->MaterialState (aProgram); | |
5322131b | 398 | |
30f0ad28 | 399 | if (aMaterialState == NULL || aMaterialState->Aspect() != aTextAspect) |
400 | aCtx->ShaderManager()->UpdateMaterialStateTo (aProgram, aTextAspect); | |
5322131b | 401 | |
30f0ad28 | 402 | aCtx->ShaderManager()->PushState (aProgram); |
403 | } | |
404 | else | |
405 | { | |
406 | OpenGl_ShaderProgram::Unbind (aCtx); | |
407 | } | |
408 | } | |
409 | ||
a174a3c5 | 410 | // use highlight color or colors from aspect |
411 | if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) | |
412 | { | |
413 | render (theWorkspace->PrinterContext(), | |
30f0ad28 | 414 | aCtx, |
a174a3c5 | 415 | *aTextAspect, |
416 | *theWorkspace->HighlightColor, | |
417 | *theWorkspace->HighlightColor); | |
418 | } | |
419 | else | |
420 | { | |
421 | render (theWorkspace->PrinterContext(), | |
30f0ad28 | 422 | aCtx, |
a174a3c5 | 423 | *aTextAspect, |
424 | aTextAspect->Color(), | |
425 | aTextAspect->SubtitleColor()); | |
426 | } | |
2166f0fa | 427 | |
a174a3c5 | 428 | // restore aspects |
429 | if (!aPrevTexture.IsNull()) | |
430 | { | |
431 | theWorkspace->EnableTexture (aPrevTexture); | |
432 | } | |
433 | } | |
2166f0fa | 434 | |
a174a3c5 | 435 | // ======================================================================= |
436 | // function : Render | |
437 | // purpose : | |
438 | // ======================================================================= | |
439 | void OpenGl_Text::Render (const Handle(OpenGl_PrinterContext)& thePrintCtx, | |
440 | const Handle(OpenGl_Context)& theCtx, | |
441 | const OpenGl_AspectText& theTextAspect) const | |
442 | { | |
443 | render (thePrintCtx, theCtx, theTextAspect, theTextAspect.Color(), theTextAspect.SubtitleColor()); | |
444 | } | |
2166f0fa | 445 | |
a174a3c5 | 446 | // ======================================================================= |
447 | // function : setupMatrix | |
448 | // purpose : | |
449 | // ======================================================================= | |
450 | void OpenGl_Text::setupMatrix (const Handle(OpenGl_PrinterContext)& thePrintCtx, | |
35e08fe8 | 451 | const Handle(OpenGl_Context)& /*theCtx*/, |
a174a3c5 | 452 | const OpenGl_AspectText& theTextAspect, |
453 | const OpenGl_Vec3 theDVec) const | |
454 | { | |
455 | // setup matrix | |
456 | if (myIs2d) | |
bf75be98 | 457 | { |
a174a3c5 | 458 | glLoadIdentity(); |
459 | glTranslatef (myPoint.x() + theDVec.x(), myPoint.y() + theDVec.y(), 0.0f); | |
03e04ead | 460 | glScalef (1.0f, -1.0f, 1.0f); |
461 | glRotatef (theTextAspect.Angle(), 0.0, 0.0, 1.0); | |
2166f0fa SK |
462 | } |
463 | else | |
464 | { | |
a174a3c5 | 465 | // align coordinates to the nearest integer |
466 | // to avoid extra interpolation issues | |
467 | GLdouble anObjX, anObjY, anObjZ; | |
468 | gluUnProject (std::floor (myWinX + (GLdouble )theDVec.x()), | |
469 | std::floor (myWinY + (GLdouble )theDVec.y()), | |
470 | myWinZ + (GLdouble )theDVec.z(), | |
471 | (GLdouble* )THE_IDENTITY_MATRIX, myProjMatrix, myViewport, | |
472 | &anObjX, &anObjY, &anObjZ); | |
473 | ||
474 | glLoadIdentity(); | |
475 | glTranslated (anObjX, anObjY, anObjZ); | |
476 | glRotated (theTextAspect.Angle(), 0.0, 0.0, 1.0); | |
477 | if (!theTextAspect.IsZoomable()) | |
478 | { | |
479 | #ifdef _WIN32 | |
480 | // if the context has assigned printer context, use it's parameters | |
481 | if (!thePrintCtx.IsNull()) | |
482 | { | |
483 | // get printing scaling in x and y dimensions | |
484 | GLfloat aTextScalex = 1.0f, aTextScaley = 1.0f; | |
485 | thePrintCtx->GetScale (aTextScalex, aTextScaley); | |
486 | ||
487 | // text should be scaled in all directions with same | |
488 | // factor to save its proportions, so use height (y) scaling | |
489 | // as it is better for keeping text/3d graphics proportions | |
490 | glScalef (aTextScaley, aTextScaley, aTextScaley); | |
491 | } | |
492 | #endif | |
493 | glScaled (myScaleHeight, myScaleHeight, myScaleHeight); | |
494 | } | |
495 | } | |
496 | } | |
497 | ||
498 | // ======================================================================= | |
499 | // function : drawText | |
500 | // purpose : | |
501 | // ======================================================================= | |
35e08fe8 | 502 | |
503 | void OpenGl_Text::drawText (const Handle(OpenGl_PrinterContext)& , | |
a174a3c5 | 504 | const Handle(OpenGl_Context)& theCtx, |
35e08fe8 | 505 | #ifdef HAVE_GL2PS |
a174a3c5 | 506 | const OpenGl_AspectText& theTextAspect) const |
35e08fe8 | 507 | #else |
508 | const OpenGl_AspectText& ) const | |
509 | #endif | |
a174a3c5 | 510 | { |
511 | #ifdef HAVE_GL2PS | |
512 | if (theCtx->IsFeedback()) | |
513 | { | |
514 | // position of the text and alignment is calculated by transformation matrix | |
515 | exportText (myString, myIs2d, theTextAspect, (Standard_Integer )myExportHeight); | |
516 | return; | |
2166f0fa | 517 | } |
a174a3c5 | 518 | #endif |
2166f0fa | 519 | |
a174a3c5 | 520 | if (myVertsVbo.Length() == myTextures.Length()) |
2166f0fa | 521 | { |
a174a3c5 | 522 | for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter) |
523 | { | |
524 | const GLuint aTexId = myTextures.Value (anIter); | |
525 | const Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.Value (anIter); | |
526 | const Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.Value (anIter); | |
527 | aVerts->BindFixed (theCtx, GL_VERTEX_ARRAY); | |
528 | aTCrds->BindFixed (theCtx, GL_TEXTURE_COORD_ARRAY); | |
529 | glBindTexture (GL_TEXTURE_2D, aTexId); | |
530 | ||
531 | glDrawArrays (GL_TRIANGLES, 0, GLsizei(aVerts->GetElemsNb())); | |
532 | ||
533 | glBindTexture (GL_TEXTURE_2D, 0); | |
27583309 | 534 | aTCrds->UnbindFixed (theCtx, GL_TEXTURE_COORD_ARRAY); |
a174a3c5 | 535 | aVerts->UnbindFixed (theCtx, GL_VERTEX_ARRAY); |
536 | } | |
2166f0fa | 537 | } |
a174a3c5 | 538 | else if (myVertsArray.Length() == myTextures.Length()) |
539 | { | |
540 | glEnableClientState (GL_VERTEX_ARRAY); | |
541 | glEnableClientState (GL_TEXTURE_COORD_ARRAY); | |
542 | for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter) | |
543 | { | |
544 | const GLuint aTexId = myTextures.Value (anIter); | |
545 | const Handle(OpenGl_Vec2Array)& aVerts = myVertsArray.Value (anIter); | |
546 | const Handle(OpenGl_Vec2Array)& aTCrds = myTCrdsArray.Value (anIter); | |
547 | ||
548 | glVertexPointer (2, GL_FLOAT, 0, (GLfloat* )&aVerts->First()); | |
549 | glTexCoordPointer (2, GL_FLOAT, 0, (GLfloat* )&aTCrds->First()); | |
550 | glBindTexture (GL_TEXTURE_2D, aTexId); | |
2166f0fa | 551 | |
a174a3c5 | 552 | glDrawArrays (GL_TRIANGLES, 0, aVerts->Length()); |
2166f0fa | 553 | |
a174a3c5 | 554 | glBindTexture (GL_TEXTURE_2D, 0); |
555 | } | |
556 | glDisableClientState (GL_TEXTURE_COORD_ARRAY); | |
557 | glDisableClientState (GL_VERTEX_ARRAY); | |
558 | } | |
559 | } | |
2166f0fa | 560 | |
a174a3c5 | 561 | // ======================================================================= |
562 | // function : FontKey | |
563 | // purpose : | |
564 | // ======================================================================= | |
565 | TCollection_AsciiString OpenGl_Text::FontKey (const OpenGl_AspectText& theAspect, | |
566 | const Standard_Integer theHeight) | |
567 | { | |
568 | const Font_FontAspect anAspect = (theAspect.FontAspect() != Font_FA_Undefined) ? theAspect.FontAspect() : Font_FA_Regular; | |
569 | return theAspect.FontName() | |
570 | + TCollection_AsciiString(":") + Standard_Integer(anAspect) | |
571 | + TCollection_AsciiString(":") + theHeight; | |
572 | } | |
573 | ||
574 | // ======================================================================= | |
575 | // function : FindFont | |
576 | // purpose : | |
577 | // ======================================================================= | |
578 | Handle(OpenGl_Font) OpenGl_Text::FindFont (const Handle(OpenGl_Context)& theCtx, | |
579 | const OpenGl_AspectText& theAspect, | |
580 | const Standard_Integer theHeight, | |
581 | const TCollection_AsciiString theKey) | |
582 | { | |
583 | Handle(OpenGl_Font) aFont; | |
584 | if (theHeight < 2) | |
2166f0fa | 585 | { |
a174a3c5 | 586 | return aFont; // invalid parameters |
587 | } | |
2166f0fa | 588 | |
a174a3c5 | 589 | if (!theCtx->GetResource (theKey, aFont)) |
590 | { | |
591 | Handle(Font_FontMgr) aFontMgr = Font_FontMgr::GetInstance(); | |
592 | const Handle(TCollection_HAsciiString) aFontName = new TCollection_HAsciiString (theAspect.FontName()); | |
593 | const Font_FontAspect anAspect = (theAspect.FontAspect() != Font_FA_Undefined) ? theAspect.FontAspect() : Font_FA_Regular; | |
594 | Handle(Font_SystemFont) aRequestedFont = aFontMgr->FindFont (aFontName, anAspect, theHeight); | |
595 | if (aRequestedFont.IsNull()) | |
2166f0fa | 596 | { |
a174a3c5 | 597 | return aFont; |
598 | } | |
599 | ||
600 | Handle(Font_FTFont) aFontFt = new Font_FTFont (NULL); | |
601 | if (!aFontFt->Init (aRequestedFont->FontPath()->ToCString(), theHeight)) | |
602 | { | |
603 | return aFont; | |
604 | } | |
605 | ||
606 | Handle(OpenGl_Context) aCtx = theCtx; | |
607 | glPushAttrib (GL_TEXTURE_BIT); | |
608 | aFont = new OpenGl_Font (aFontFt, theKey); | |
609 | if (!aFont->Init (aCtx)) | |
610 | { | |
611 | //glPopAttrib(); | |
612 | //return aFont; // out of resources? | |
613 | } | |
614 | glPopAttrib(); // texture bit | |
615 | ||
616 | aCtx->ShareResource (theKey, aFont); | |
617 | } | |
618 | return aFont; | |
619 | } | |
620 | ||
621 | // ======================================================================= | |
622 | // function : render | |
623 | // purpose : | |
624 | // ======================================================================= | |
625 | void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx, | |
626 | const Handle(OpenGl_Context)& theCtx, | |
627 | const OpenGl_AspectText& theTextAspect, | |
628 | const TEL_COLOUR& theColorText, | |
629 | const TEL_COLOUR& theColorSubs) const | |
630 | { | |
631 | if (myString.IsEmpty()) | |
632 | { | |
633 | return; | |
634 | } | |
635 | ||
636 | const TCollection_AsciiString aFontKey = FontKey (theTextAspect, myParams.Height); | |
637 | if (!myFont.IsNull() | |
638 | && !myFont->ResourceKey().IsEqual (aFontKey)) | |
639 | { | |
640 | // font changed | |
10b9c7df | 641 | const_cast<OpenGl_Text* > (this)->Release (theCtx.operator->()); |
a174a3c5 | 642 | } |
643 | ||
644 | if (myFont.IsNull()) | |
645 | { | |
646 | myFont = FindFont (theCtx, theTextAspect, myParams.Height, aFontKey); | |
647 | if (myFont.IsNull()) | |
648 | { | |
649 | return; | |
650 | } | |
651 | } | |
652 | ||
653 | if (myTextures.IsEmpty()) | |
654 | { | |
655 | OpenGl_TextFormatter aFormatter; | |
656 | aFormatter.SetupAlignment (myParams.HAlign, myParams.VAlign); | |
657 | aFormatter.Reset(); | |
658 | aFormatter.Append (theCtx, myString, *myFont.operator->()); | |
659 | aFormatter.Format(); | |
660 | ||
58655684 | 661 | if (!theCtx->caps->vboDisable && theCtx->core15 != NULL) |
a174a3c5 | 662 | { |
663 | aFormatter.Result (theCtx, myTextures, myVertsVbo, myTCrdsVbo); | |
664 | } | |
665 | else | |
666 | { | |
667 | aFormatter.Result (theCtx, myTextures, myVertsArray, myTCrdsArray); | |
668 | } | |
669 | aFormatter.BndBox (myBndBox); | |
670 | } | |
671 | ||
672 | if (myTextures.IsEmpty()) | |
673 | { | |
674 | return; | |
675 | } | |
676 | ||
677 | myExportHeight = 1.0f; | |
678 | myScaleHeight = 1.0f; | |
679 | ||
680 | glMatrixMode (GL_MODELVIEW); | |
681 | glPushMatrix(); | |
682 | if (!myIs2d) | |
683 | { | |
684 | // retrieve active matrices for project/unproject calls | |
685 | glGetDoublev (GL_MODELVIEW_MATRIX, myModelMatrix); | |
686 | glGetDoublev (GL_PROJECTION_MATRIX, myProjMatrix); | |
687 | glGetIntegerv (GL_VIEWPORT, myViewport); | |
688 | gluProject (myPoint.x(), myPoint.y(), myPoint.z(), | |
689 | myModelMatrix, myProjMatrix, myViewport, | |
690 | &myWinX, &myWinY, &myWinZ); | |
691 | ||
692 | // compute scale factor for constant text height | |
693 | GLdouble x1, y1, z1; | |
694 | gluUnProject (myWinX, myWinY, myWinZ, | |
695 | (GLdouble* )THE_IDENTITY_MATRIX, myProjMatrix, myViewport, | |
696 | &x1, &y1, &z1); | |
697 | ||
698 | GLdouble x2, y2, z2; | |
699 | const GLdouble h = (GLdouble )myFont->FTFont()->PointSize(); | |
353474f0 | 700 | gluUnProject (myWinX, myWinY + h, myWinZ, |
a174a3c5 | 701 | (GLdouble* )THE_IDENTITY_MATRIX, myProjMatrix, myViewport, |
702 | &x2, &y2, &z2); | |
703 | ||
704 | myScaleHeight = (y2 - y1) / h; | |
705 | if (theTextAspect.IsZoomable()) | |
706 | { | |
707 | myExportHeight = (float )h; | |
708 | } | |
709 | } | |
710 | myExportHeight = (float )myFont->FTFont()->PointSize() / myExportHeight; | |
711 | ||
712 | // push enabled flags to the stack | |
713 | glPushAttrib (GL_ENABLE_BIT); | |
5322131b | 714 | glDisable (GL_LIGHTING); |
a174a3c5 | 715 | |
716 | // setup depth test | |
717 | if (!myIs2d | |
718 | && theTextAspect.StyleType() != Aspect_TOST_ANNOTATION) | |
719 | { | |
720 | glEnable (GL_DEPTH_TEST); | |
721 | } | |
722 | else | |
723 | { | |
724 | glDisable (GL_DEPTH_TEST); | |
725 | } | |
726 | ||
727 | // setup alpha test | |
728 | GLint aTexEnvParam = GL_REPLACE; | |
729 | glGetTexEnviv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &aTexEnvParam); | |
730 | if (aTexEnvParam != GL_REPLACE) | |
731 | { | |
732 | glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); | |
733 | } | |
734 | glAlphaFunc (GL_GEQUAL, 0.285f); | |
735 | glEnable (GL_ALPHA_TEST); | |
736 | ||
737 | // setup blending | |
738 | glEnable (GL_BLEND); | |
739 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE | |
740 | ||
741 | // activate texture unit | |
742 | glDisable (GL_TEXTURE_1D); | |
743 | glEnable (GL_TEXTURE_2D); | |
01ca42b2 | 744 | if (theCtx->core15fwd != NULL) |
a174a3c5 | 745 | { |
01ca42b2 | 746 | theCtx->core15fwd->glActiveTexture (GL_TEXTURE0); |
a174a3c5 | 747 | } |
748 | ||
749 | // extra drawings | |
750 | switch (theTextAspect.DisplayType()) | |
751 | { | |
2166f0fa | 752 | case Aspect_TODT_BLEND: |
a174a3c5 | 753 | { |
754 | glEnable (GL_COLOR_LOGIC_OP); | |
755 | glLogicOp (GL_XOR); | |
2166f0fa | 756 | break; |
a174a3c5 | 757 | } |
2166f0fa SK |
758 | case Aspect_TODT_SUBTITLE: |
759 | { | |
a174a3c5 | 760 | glColor3fv (theColorSubs.rgb); |
761 | setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.00001f)); | |
762 | ||
763 | glBindTexture (GL_TEXTURE_2D, 0); | |
764 | glBegin (GL_QUADS); | |
765 | glVertex2f (myBndBox.Left, myBndBox.Top); | |
766 | glVertex2f (myBndBox.Right, myBndBox.Top); | |
767 | glVertex2f (myBndBox.Right, myBndBox.Bottom); | |
768 | glVertex2f (myBndBox.Left, myBndBox.Bottom); | |
2166f0fa SK |
769 | glEnd(); |
770 | break; | |
771 | } | |
2166f0fa | 772 | case Aspect_TODT_DEKALE: |
a174a3c5 | 773 | { |
774 | glColor3fv (theColorSubs.rgb); | |
775 | setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, +1.0f, 0.00001f)); | |
776 | drawText (thePrintCtx, theCtx, theTextAspect); | |
777 | setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, -1.0f, 0.00001f)); | |
778 | drawText (thePrintCtx, theCtx, theTextAspect); | |
779 | setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, +1.0f, 0.00001f)); | |
780 | drawText (thePrintCtx, theCtx, theTextAspect); | |
781 | setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, -1.0f, 0.00001f)); | |
782 | drawText (thePrintCtx, theCtx, theTextAspect); | |
783 | break; | |
784 | } | |
a6eb515f | 785 | case Aspect_TODT_DIMENSION: |
a174a3c5 | 786 | case Aspect_TODT_NORMAL: |
787 | { | |
2166f0fa SK |
788 | break; |
789 | } | |
790 | } | |
791 | ||
a174a3c5 | 792 | // main draw call |
793 | glColor3fv (theColorText.rgb); | |
794 | setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.0f)); | |
795 | drawText (thePrintCtx, theCtx, theTextAspect); | |
2166f0fa | 796 | |
a174a3c5 | 797 | glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, aTexEnvParam); |
543f0db0 | 798 | |
799 | if (theTextAspect.DisplayType() == Aspect_TODT_DIMENSION) | |
800 | { | |
801 | setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.00001f)); | |
802 | ||
803 | glDisable (GL_BLEND); | |
804 | glDisable (GL_TEXTURE_2D); | |
805 | glDisable (GL_ALPHA_TEST); | |
806 | if (!myIs2d) | |
807 | { | |
808 | glDisable (GL_DEPTH_TEST); | |
809 | } | |
810 | glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); | |
811 | ||
812 | glClear (GL_STENCIL_BUFFER_BIT); | |
813 | glEnable (GL_STENCIL_TEST); | |
814 | glStencilFunc (GL_ALWAYS, 1, 0xFF); | |
815 | glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE); | |
816 | ||
817 | glBegin (GL_QUADS); | |
818 | glVertex2f (myBndBox.Left, myBndBox.Top); | |
819 | glVertex2f (myBndBox.Right, myBndBox.Top); | |
820 | glVertex2f (myBndBox.Right, myBndBox.Bottom); | |
821 | glVertex2f (myBndBox.Left, myBndBox.Bottom); | |
822 | glEnd(); | |
823 | ||
824 | glStencilFunc (GL_ALWAYS, 0, 0xFF); | |
825 | // glPopAttrib() will reset state for us | |
826 | //glDisable (GL_STENCIL_TEST); | |
827 | //if (!myIs2d) glEnable (GL_DEPTH_TEST); | |
828 | ||
829 | glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); | |
830 | } | |
831 | ||
832 | // revert OpenGL state | |
a174a3c5 | 833 | glPopAttrib(); // enable bit |
834 | glPopMatrix(); // model view matrix was modified | |
5e27df78 | 835 | } |