0027088: Documentation of add method of GeomConvert_CompCurveToBSplineCurve lacks...
[occt.git] / src / OpenGl / OpenGl_AspectText.cxx
1 // Created on: 2011-07-13
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2013 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 <Graphic3d_ShaderProgram.hxx>
17
18 #include <OpenGl_AspectText.hxx>
19 #include <OpenGl_Context.hxx>
20 #include <OpenGl_Workspace.hxx>
21 #include <OpenGl_ShaderManager.hxx>
22 #include <OpenGl_ShaderProgram.hxx>
23
24 namespace
25 {
26   static const TEL_COLOUR TheDefaultColor = {{ 1.0F, 1.0F, 1.0F, 1.0F }};
27   static const TCollection_AsciiString THE_EMPTY_KEY;
28 };
29
30 // =======================================================================
31 // function : OpenGl_AspectText
32 // purpose  :
33 // =======================================================================
34 OpenGl_AspectText::OpenGl_AspectText()
35 : myFont ("Courier") ,
36   myColor (TheDefaultColor),
37   mySubtitleColor (TheDefaultColor),
38   myAngle (0.0f),
39   myStyleType   (Aspect_TOST_NORMAL),
40   myDisplayType (Aspect_TODT_NORMAL),
41   myFontAspect  (Font_FA_Regular),
42   myZoomable (false),
43   myShaderProgram()
44 {
45   //
46 }
47
48 // =======================================================================
49 // function : ~OpenGl_AspectText
50 // purpose  :
51 // =======================================================================
52 OpenGl_AspectText::~OpenGl_AspectText()
53 {
54   //
55 }
56
57 // =======================================================================
58 // function : SetAspect
59 // purpose  :
60 // =======================================================================
61 void OpenGl_AspectText::SetAspect (const CALL_DEF_CONTEXTTEXT& theAspect)
62 {
63   myFont = theAspect.Font;
64
65   myColor.rgb[0] = (float )theAspect.Color.r;
66   myColor.rgb[1] = (float )theAspect.Color.g;
67   myColor.rgb[2] = (float )theAspect.Color.b;
68   myColor.rgb[3] = 1.0f;
69   mySubtitleColor.rgb[0] = (float )theAspect.ColorSubTitle.r;
70   mySubtitleColor.rgb[1] = (float )theAspect.ColorSubTitle.g;
71   mySubtitleColor.rgb[2] = (float )theAspect.ColorSubTitle.b;
72   mySubtitleColor.rgb[3] = 1.0f;
73
74   myAngle       = (float )theAspect.TextAngle;
75   myStyleType   = (Aspect_TypeOfStyleText   )theAspect.Style;
76   myDisplayType = (Aspect_TypeOfDisplayText )theAspect.DisplayType;
77   myFontAspect  = (Font_FontAspect )theAspect.TextFontAspect;
78   myZoomable    = (theAspect.TextZoomable != 0);
79
80   // update resource bindings
81   myShaderProgram = theAspect.ShaderProgram;
82
83   const TCollection_AsciiString& aShaderKey = myShaderProgram.IsNull() ? THE_EMPTY_KEY : myShaderProgram->GetId();
84
85   if (aShaderKey.IsEmpty() || myResources.ShaderProgramId != aShaderKey)
86   {
87     myResources.ResetShaderReadiness();
88   }
89 }
90
91 // =======================================================================
92 // function : Render
93 // purpose  :
94 // =======================================================================
95 void OpenGl_AspectText::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
96 {
97   theWorkspace->SetAspectText (this);
98 }
99
100 // =======================================================================
101 // function : Release
102 // purpose  :
103 // =======================================================================
104 void OpenGl_AspectText::Release (OpenGl_Context* theContext)
105 {
106   if (!myResources.ShaderProgram.IsNull()
107    && theContext)
108   {
109     theContext->ShaderManager()->Unregister (myResources.ShaderProgramId,
110                                              myResources.ShaderProgram);
111   }
112   myResources.ShaderProgramId.Clear();
113   myResources.ResetShaderReadiness();
114 }
115
116 // =======================================================================
117 // function : BuildShader
118 // purpose  :
119 // =======================================================================
120 void OpenGl_AspectText::Resources::BuildShader (const Handle(OpenGl_Context)&          theCtx,
121                                                 const Handle(Graphic3d_ShaderProgram)& theShader)
122 {
123   if (theCtx->core20fwd == NULL)
124   {
125     return;
126   }
127
128   // release old shader program resources
129   if (!ShaderProgram.IsNull())
130   {
131     theCtx->ShaderManager()->Unregister (ShaderProgramId, ShaderProgram);
132     ShaderProgramId.Clear();
133     ShaderProgram.Nullify();
134   }
135   if (theShader.IsNull())
136   {
137     return;
138   }
139
140   theCtx->ShaderManager()->Create (theShader, ShaderProgramId, ShaderProgram);
141 }