0027943: Visualization - fix broken shading by positional light for object with local...
[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 TCollection_AsciiString THE_EMPTY_KEY;
27 }
28
29 // =======================================================================
30 // function : OpenGl_AspectText
31 // purpose  :
32 // =======================================================================
33 OpenGl_AspectText::OpenGl_AspectText()
34 : myAspect (new Graphic3d_AspectText3d (Quantity_Color (Quantity_NOC_WHITE), "Courier", 1.0, 0.0))
35 {
36   //
37 }
38
39 // =======================================================================
40 // function : OpenGl_AspectText
41 // purpose  :
42 // =======================================================================
43 OpenGl_AspectText::OpenGl_AspectText (const Handle(Graphic3d_AspectText3d)& theAspect)
44 {
45   SetAspect (theAspect);
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 Handle(Graphic3d_AspectText3d)& theAspect)
62 {
63   myAspect = theAspect;
64   const TCollection_AsciiString& aShaderKey = myAspect->ShaderProgram().IsNull() ? THE_EMPTY_KEY : myAspect->ShaderProgram()->GetId();
65   if (aShaderKey.IsEmpty() || myResources.ShaderProgramId != aShaderKey)
66   {
67     myResources.ResetShaderReadiness();
68   }
69 }
70
71 // =======================================================================
72 // function : Render
73 // purpose  :
74 // =======================================================================
75 void OpenGl_AspectText::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
76 {
77   theWorkspace->SetAspectText (this);
78 }
79
80 // =======================================================================
81 // function : Release
82 // purpose  :
83 // =======================================================================
84 void OpenGl_AspectText::Release (OpenGl_Context* theContext)
85 {
86   if (!myResources.ShaderProgram.IsNull()
87    && theContext)
88   {
89     theContext->ShaderManager()->Unregister (myResources.ShaderProgramId,
90                                              myResources.ShaderProgram);
91   }
92   myResources.ShaderProgramId.Clear();
93   myResources.ResetShaderReadiness();
94 }
95
96 // =======================================================================
97 // function : BuildShader
98 // purpose  :
99 // =======================================================================
100 void OpenGl_AspectText::Resources::BuildShader (const Handle(OpenGl_Context)&          theCtx,
101                                                 const Handle(Graphic3d_ShaderProgram)& theShader)
102 {
103   if (theCtx->core20fwd == NULL)
104   {
105     return;
106   }
107
108   // release old shader program resources
109   if (!ShaderProgram.IsNull())
110   {
111     theCtx->ShaderManager()->Unregister (ShaderProgramId, ShaderProgram);
112     ShaderProgramId.Clear();
113     ShaderProgram.Nullify();
114   }
115   if (theShader.IsNull())
116   {
117     return;
118   }
119
120   theCtx->ShaderManager()->Create (theShader, ShaderProgramId, ShaderProgram);
121 }