31fbe7d7e76b3d8d88df89e5cbd6d0155cbf090f
[occt.git] / src / OpenGl / OpenGl_AspectLine.cxx
1 // Created on: 2011-07-13
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 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_AspectLine.hxx>
19 #include <OpenGl_Context.hxx>
20 #include <OpenGl_ShaderManager.hxx>
21 #include <OpenGl_ShaderProgram.hxx>
22 #include <OpenGl_Workspace.hxx>
23
24 namespace
25 {
26   static const TCollection_AsciiString THE_EMPTY_KEY;
27 }
28
29 // =======================================================================
30 // function : OpenGl_AspectLine
31 // purpose  :
32 // =======================================================================
33 OpenGl_AspectLine::OpenGl_AspectLine()
34 : myAspect (new Graphic3d_AspectLine3d (Quantity_NOC_WHITE, Aspect_TOL_SOLID, 1.0))
35 {
36   //
37 }
38
39 // =======================================================================
40 // function : OpenGl_AspectLine
41 // purpose  :
42 // =======================================================================
43 OpenGl_AspectLine::OpenGl_AspectLine (const Handle(Graphic3d_AspectLine3d)& theAspect)
44 {
45   SetAspect (theAspect);
46 }
47
48 // =======================================================================
49 // function : SetAspect
50 // purpose  :
51 // =======================================================================
52 void OpenGl_AspectLine::SetAspect (const Handle(Graphic3d_AspectLine3d)& theAspect)
53 {
54   myAspect = theAspect;
55   const TCollection_AsciiString& aShaderKey = myAspect->ShaderProgram().IsNull() ? THE_EMPTY_KEY : myAspect->ShaderProgram()->GetId();
56   if (aShaderKey.IsEmpty() || myResources.ShaderProgramId != aShaderKey)
57   {
58     myResources.ResetShaderReadiness();
59   }
60 }
61
62 // =======================================================================
63 // function : Render
64 // purpose  :
65 // =======================================================================
66 void OpenGl_AspectLine::Render (const Handle(OpenGl_Workspace) &theWorkspace) const
67 {
68   theWorkspace->SetAspectLine (this);
69 }
70
71 // =======================================================================
72 // function : Release
73 // purpose  :
74 // =======================================================================
75 void OpenGl_AspectLine::Release (OpenGl_Context* theContext)
76 {
77   if (!myResources.ShaderProgram.IsNull()
78    && theContext)
79   {
80     theContext->ShaderManager()->Unregister (myResources.ShaderProgramId,
81                                              myResources.ShaderProgram);
82   }
83   myResources.ShaderProgramId.Clear();
84   myResources.ResetShaderReadiness();
85 }
86
87 // =======================================================================
88 // function : BuildShader
89 // purpose  :
90 // =======================================================================
91 void OpenGl_AspectLine::Resources::BuildShader (const Handle(OpenGl_Context)&          theCtx,
92                                                 const Handle(Graphic3d_ShaderProgram)& theShader)
93 {
94   if (theCtx->core20fwd == NULL)
95   {
96     return;
97   }
98
99   // release old shader program resources
100   if (!ShaderProgram.IsNull())
101   {
102     theCtx->ShaderManager()->Unregister (ShaderProgramId, ShaderProgram);
103     ShaderProgramId.Clear();
104     ShaderProgram.Nullify();
105   }
106   if (theShader.IsNull())
107   {
108     return;
109   }
110
111   theCtx->ShaderManager()->Create (theShader, ShaderProgramId, ShaderProgram);
112 }