0026128: Visualization, TKOpenGl - fix misprint in external GLX context initialization
[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 TEL_COLOUR myDefaultColor = {{ 1.0F, 1.0F, 1.0F, 1.0F }};
27   static const TCollection_AsciiString THE_EMPTY_KEY;
28 };
29
30 // =======================================================================
31 // function : OpenGl_AspectLine
32 // purpose  :
33 // =======================================================================
34 OpenGl_AspectLine::OpenGl_AspectLine ()
35  : myColor(myDefaultColor),
36    myType(Aspect_TOL_SOLID),
37    myWidth(1.0F)
38 {}
39
40 // =======================================================================
41 // function : OpenGl_AspectLine
42 // purpose  :
43 // =======================================================================
44 OpenGl_AspectLine::OpenGl_AspectLine (const OpenGl_AspectLine &AnOther)
45  : myColor(AnOther.myColor),
46    myType(AnOther.myType),
47    myWidth(AnOther.myWidth)
48 {}
49
50 // =======================================================================
51 // function : SetAspect
52 // purpose  :
53 // =======================================================================
54 void OpenGl_AspectLine::SetAspect (const CALL_DEF_CONTEXTLINE &theAspect)
55 {
56   myColor.rgb[0] = (float) theAspect.Color.r;
57   myColor.rgb[1] = (float) theAspect.Color.g;
58   myColor.rgb[2] = (float) theAspect.Color.b;
59   myColor.rgb[3] = 1.0f;
60   myType = (Aspect_TypeOfLine) theAspect.LineType;
61   myWidth = (float) theAspect.Width;
62
63   // update resource bindings
64   myShaderProgram = theAspect.ShaderProgram;
65
66   const TCollection_AsciiString& aShaderKey = myShaderProgram.IsNull() ? THE_EMPTY_KEY : myShaderProgram->GetId();
67   if (aShaderKey.IsEmpty() || myResources.ShaderProgramId != aShaderKey)
68   {
69     myResources.ResetShaderReadiness();
70   }
71 }
72
73 // =======================================================================
74 // function : Render
75 // purpose  :
76 // =======================================================================
77 void OpenGl_AspectLine::Render (const Handle(OpenGl_Workspace) &theWorkspace) const
78 {
79   theWorkspace->SetAspectLine (this);
80 }
81
82 // =======================================================================
83 // function : Release
84 // purpose  :
85 // =======================================================================
86 void OpenGl_AspectLine::Release (OpenGl_Context* theContext)
87 {
88   if (!myResources.ShaderProgram.IsNull()
89    && theContext)
90   {
91     theContext->ShaderManager()->Unregister (myResources.ShaderProgramId,
92                                              myResources.ShaderProgram);
93   }
94   myResources.ShaderProgramId.Clear();
95   myResources.ResetShaderReadiness();
96 }
97
98 // =======================================================================
99 // function : BuildShader
100 // purpose  :
101 // =======================================================================
102 void OpenGl_AspectLine::Resources::BuildShader (const Handle(OpenGl_Context)&          theCtx,
103                                                 const Handle(Graphic3d_ShaderProgram)& theShader)
104 {
105   if (theCtx->core20fwd == NULL)
106   {
107     return;
108   }
109
110   // release old shader program resources
111   if (!ShaderProgram.IsNull())
112   {
113     theCtx->ShaderManager()->Unregister (ShaderProgramId, ShaderProgram);
114     ShaderProgramId.Clear();
115     ShaderProgram.Nullify();
116   }
117   if (theShader.IsNull())
118   {
119     return;
120   }
121
122   theCtx->ShaderManager()->Create (theShader, ShaderProgramId, ShaderProgram);
123 }