Commit | Line | Data |
---|---|---|
b311480e | 1 | // Created on: 2011-07-13 |
2 | // Created by: Sergey ZERCHANINOV | |
3 | // Copyright (c) 2011-2012 OPEN CASCADE SAS | |
4 | // | |
5 | // The content of this file is subject to the Open CASCADE Technology Public | |
6 | // License Version 6.5 (the "License"). You may not use the content of this file | |
7 | // except in compliance with the License. Please obtain a copy of the License | |
8 | // at http://www.opencascade.org and read it completely before using this file. | |
9 | // | |
10 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its | |
11 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. | |
12 | // | |
13 | // The Original Code and all software distributed under the License is | |
14 | // distributed on an "AS IS" basis, without warranty of any kind, and the | |
15 | // Initial Developer hereby disclaims all such warranties, including without | |
16 | // limitation, any warranties of merchantability, fitness for a particular | |
17 | // purpose or non-infringement. Please see the License for the specific terms | |
18 | // and conditions governing the rights and limitations under the License. | |
19 | ||
30f0ad28 | 20 | #include <Graphic3d_ShaderProgram.hxx> |
21 | ||
2166f0fa | 22 | #include <OpenGl_AspectLine.hxx> |
30f0ad28 | 23 | #include <OpenGl_Context.hxx> |
24 | #include <OpenGl_ShaderManager.hxx> | |
25 | #include <OpenGl_ShaderProgram.hxx> | |
bf75be98 | 26 | #include <OpenGl_Workspace.hxx> |
2166f0fa | 27 | |
30f0ad28 | 28 | namespace |
29 | { | |
30 | static const TEL_COLOUR myDefaultColor = {{ 1.0F, 1.0F, 1.0F, 1.0F }}; | |
31 | static const TCollection_AsciiString THE_EMPTY_KEY; | |
32 | }; | |
2166f0fa | 33 | |
30f0ad28 | 34 | // ======================================================================= |
35 | // function : OpenGl_AspectLine | |
36 | // purpose : | |
37 | // ======================================================================= | |
2166f0fa SK |
38 | OpenGl_AspectLine::OpenGl_AspectLine () |
39 | : myColor(myDefaultColor), | |
40 | myType(Aspect_TOL_SOLID), | |
41 | myWidth(1.0F) | |
42 | {} | |
43 | ||
30f0ad28 | 44 | // ======================================================================= |
45 | // function : OpenGl_AspectLine | |
46 | // purpose : | |
47 | // ======================================================================= | |
2166f0fa SK |
48 | OpenGl_AspectLine::OpenGl_AspectLine (const OpenGl_AspectLine &AnOther) |
49 | : myColor(AnOther.myColor), | |
50 | myType(AnOther.myType), | |
51 | myWidth(AnOther.myWidth) | |
52 | {} | |
53 | ||
30f0ad28 | 54 | // ======================================================================= |
55 | // function : SetAspect | |
56 | // purpose : | |
57 | // ======================================================================= | |
fd4a6963 | 58 | void OpenGl_AspectLine::SetAspect (const CALL_DEF_CONTEXTLINE &theAspect) |
2166f0fa | 59 | { |
fd4a6963 | 60 | myColor.rgb[0] = (float) theAspect.Color.r; |
61 | myColor.rgb[1] = (float) theAspect.Color.g; | |
62 | myColor.rgb[2] = (float) theAspect.Color.b; | |
2166f0fa | 63 | myColor.rgb[3] = 1.0f; |
fd4a6963 | 64 | myType = (Aspect_TypeOfLine) theAspect.LineType; |
65 | myWidth = (float) theAspect.Width; | |
2166f0fa | 66 | |
30f0ad28 | 67 | // update resource bindings |
68 | myShaderProgram = theAspect.ShaderProgram; | |
69 | ||
70 | const TCollection_AsciiString& aShaderKey = myShaderProgram.IsNull() ? THE_EMPTY_KEY : myShaderProgram->GetId(); | |
71 | ||
72 | if (aShaderKey.IsEmpty() || myResources.ShaderProgramId != aShaderKey) | |
73 | { | |
74 | myResources.ResetShader(); | |
75 | } | |
76 | } | |
2166f0fa | 77 | |
30f0ad28 | 78 | // ======================================================================= |
79 | // function : Render | |
80 | // purpose : | |
81 | // ======================================================================= | |
5e27df78 | 82 | void OpenGl_AspectLine::Render (const Handle(OpenGl_Workspace) &theWorkspace) const |
2166f0fa | 83 | { |
5e27df78 | 84 | theWorkspace->SetAspectLine (this); |
2166f0fa SK |
85 | } |
86 | ||
30f0ad28 | 87 | // ======================================================================= |
88 | // function : Release | |
89 | // purpose : | |
90 | // ======================================================================= | |
91 | void OpenGl_AspectLine::Release (const Handle(OpenGl_Context)& theContext) | |
92 | { | |
93 | if (!myResources.ShaderProgram.IsNull() && !theContext.IsNull()) | |
94 | { | |
95 | theContext->ShaderManager()->Unregister (myResources.ShaderProgram); | |
96 | } | |
97 | myResources.ShaderProgramId.Clear(); | |
98 | myResources.ResetShader(); | |
99 | } | |
100 | ||
101 | // ======================================================================= | |
102 | // function : BuildShader | |
103 | // purpose : | |
104 | // ======================================================================= | |
105 | void OpenGl_AspectLine::Resources::BuildShader (const Handle(OpenGl_Workspace)& theWS, | |
106 | const Handle(Graphic3d_ShaderProgram)& theShader) | |
5e27df78 | 107 | { |
30f0ad28 | 108 | const Handle(OpenGl_Context)& aContext = theWS->GetGlContext(); |
109 | ||
110 | if (!aContext->IsGlGreaterEqual (2, 0)) | |
111 | return; | |
112 | ||
113 | // release old shader program resources | |
114 | if (!ShaderProgram.IsNull()) | |
115 | { | |
116 | aContext->ShaderManager()->Unregister (ShaderProgram); | |
117 | } | |
118 | ||
119 | ShaderProgramId = theShader.IsNull() ? THE_EMPTY_KEY : theShader->GetId(); | |
120 | ||
121 | if (!theShader.IsNull()) | |
122 | { | |
123 | if (!aContext->GetResource<Handle(OpenGl_ShaderProgram)> (ShaderProgramId, ShaderProgram)) | |
124 | { | |
125 | ShaderProgram = aContext->ShaderManager()->Create (theShader); | |
126 | if (!ShaderProgramId.IsEmpty()) | |
127 | { | |
128 | aContext->ShareResource (ShaderProgramId, ShaderProgram); | |
129 | } | |
130 | } | |
131 | } | |
132 | else | |
133 | { | |
134 | ShaderProgram.Nullify(); | |
135 | } | |
5e27df78 | 136 | } |