1 // Created on: 2011-07-13
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2013 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_AspectText.hxx>
19 #include <OpenGl_Context.hxx>
20 #include <OpenGl_Workspace.hxx>
21 #include <OpenGl_ShaderManager.hxx>
22 #include <OpenGl_ShaderProgram.hxx>
26 static const TEL_COLOUR TheDefaultColor = {{ 1.0F, 1.0F, 1.0F, 1.0F }};
27 static const TCollection_AsciiString THE_EMPTY_KEY;
30 // =======================================================================
31 // function : OpenGl_AspectText
33 // =======================================================================
34 OpenGl_AspectText::OpenGl_AspectText()
35 : myFont ("Courier") ,
36 myColor (TheDefaultColor),
37 mySubtitleColor (TheDefaultColor),
39 myStyleType (Aspect_TOST_NORMAL),
40 myDisplayType (Aspect_TODT_NORMAL),
41 myFontAspect (Font_FA_Regular),
48 // =======================================================================
49 // function : ~OpenGl_AspectText
51 // =======================================================================
52 OpenGl_AspectText::~OpenGl_AspectText()
57 // =======================================================================
58 // function : SetAspect
60 // =======================================================================
61 void OpenGl_AspectText::SetAspect (const CALL_DEF_CONTEXTTEXT& theAspect)
63 myFont = theAspect.Font;
65 myColor.rgb[0] = (float )theAspect.Color.r;
66 myColor.rgb[1] = (float )theAspect.Color.g;
67 myColor.rgb[2] = (float )theAspect.Color.b;
68 myColor.rgb[3] = 1.0f;
69 mySubtitleColor.rgb[0] = (float )theAspect.ColorSubTitle.r;
70 mySubtitleColor.rgb[1] = (float )theAspect.ColorSubTitle.g;
71 mySubtitleColor.rgb[2] = (float )theAspect.ColorSubTitle.b;
72 mySubtitleColor.rgb[3] = 1.0f;
74 myAngle = (float )theAspect.TextAngle;
75 myStyleType = (Aspect_TypeOfStyleText )theAspect.Style;
76 myDisplayType = (Aspect_TypeOfDisplayText )theAspect.DisplayType;
77 myFontAspect = (Font_FontAspect )theAspect.TextFontAspect;
78 myZoomable = (theAspect.TextZoomable != 0);
80 // update resource bindings
81 myShaderProgram = theAspect.ShaderProgram;
83 const TCollection_AsciiString& aShaderKey = myShaderProgram.IsNull() ? THE_EMPTY_KEY : myShaderProgram->GetId();
85 if (aShaderKey.IsEmpty() || myResources.ShaderProgramId != aShaderKey)
87 myResources.ResetShaderReadiness();
91 // =======================================================================
94 // =======================================================================
95 void OpenGl_AspectText::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
97 theWorkspace->SetAspectText (this);
100 // =======================================================================
101 // function : Release
103 // =======================================================================
104 void OpenGl_AspectText::Release (const Handle(OpenGl_Context)& theContext)
106 if (!myResources.ShaderProgram.IsNull()
107 && !theContext.IsNull())
109 theContext->ShaderManager()->Unregister (myResources.ShaderProgramId,
110 myResources.ShaderProgram);
112 myResources.ShaderProgramId.Clear();
113 myResources.ResetShaderReadiness();
116 // =======================================================================
117 // function : BuildShader
119 // =======================================================================
120 void OpenGl_AspectText::Resources::BuildShader (const Handle(OpenGl_Workspace)& theWS,
121 const Handle(Graphic3d_ShaderProgram)& theShader)
123 const Handle(OpenGl_Context)& aContext = theWS->GetGlContext();
124 if (!aContext->IsGlGreaterEqual (2, 0))
129 // release old shader program resources
130 if (!ShaderProgram.IsNull())
132 aContext->ShaderManager()->Unregister (ShaderProgramId, ShaderProgram);
133 ShaderProgramId.Clear();
134 ShaderProgram.Nullify();
136 if (theShader.IsNull())
141 aContext->ShaderManager()->Create (theShader, ShaderProgramId, ShaderProgram);