1 // Created on: 2011-07-13
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and / or modify it
8 // under the terms of the GNU Lesser General Public 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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <Graphic3d_ShaderProgram.hxx>
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>
26 static const TEL_COLOUR myDefaultColor = {{ 1.0F, 1.0F, 1.0F, 1.0F }};
27 static const TCollection_AsciiString THE_EMPTY_KEY;
30 // =======================================================================
31 // function : OpenGl_AspectLine
33 // =======================================================================
34 OpenGl_AspectLine::OpenGl_AspectLine ()
35 : myColor(myDefaultColor),
36 myType(Aspect_TOL_SOLID),
40 // =======================================================================
41 // function : OpenGl_AspectLine
43 // =======================================================================
44 OpenGl_AspectLine::OpenGl_AspectLine (const OpenGl_AspectLine &AnOther)
45 : myColor(AnOther.myColor),
46 myType(AnOther.myType),
47 myWidth(AnOther.myWidth)
50 // =======================================================================
51 // function : SetAspect
53 // =======================================================================
54 void OpenGl_AspectLine::SetAspect (const CALL_DEF_CONTEXTLINE &theAspect)
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;
63 // update resource bindings
64 myShaderProgram = theAspect.ShaderProgram;
66 const TCollection_AsciiString& aShaderKey = myShaderProgram.IsNull() ? THE_EMPTY_KEY : myShaderProgram->GetId();
67 if (aShaderKey.IsEmpty() || myResources.ShaderProgramId != aShaderKey)
69 myResources.ResetShaderReadiness();
73 // =======================================================================
76 // =======================================================================
77 void OpenGl_AspectLine::Render (const Handle(OpenGl_Workspace) &theWorkspace) const
79 theWorkspace->SetAspectLine (this);
82 // =======================================================================
85 // =======================================================================
86 void OpenGl_AspectLine::Release (const Handle(OpenGl_Context)& theContext)
88 if (!myResources.ShaderProgram.IsNull()
89 && !theContext.IsNull())
91 theContext->ShaderManager()->Unregister (myResources.ShaderProgramId,
92 myResources.ShaderProgram);
94 myResources.ShaderProgramId.Clear();
95 myResources.ResetShaderReadiness();
98 // =======================================================================
99 // function : BuildShader
101 // =======================================================================
102 void OpenGl_AspectLine::Resources::BuildShader (const Handle(OpenGl_Workspace)& theWS,
103 const Handle(Graphic3d_ShaderProgram)& theShader)
105 const Handle(OpenGl_Context)& aContext = theWS->GetGlContext();
106 if (!aContext->IsGlGreaterEqual (2, 0))
111 // release old shader program resources
112 if (!ShaderProgram.IsNull())
114 aContext->ShaderManager()->Unregister (ShaderProgramId, ShaderProgram);
115 ShaderProgramId.Clear();
116 ShaderProgram.Nullify();
118 if (theShader.IsNull())
123 aContext->ShaderManager()->Create (theShader, ShaderProgramId, ShaderProgram);