Samples update
[occt.git] / src / OpenGl / OpenGl_AspectText.cxx
CommitLineData
b311480e 1// Created on: 2011-07-13
2// Created by: Sergey ZERCHANINOV
a174a3c5 3// Copyright (c) 2011-2013 OPEN CASCADE SAS
b311480e 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_AspectText.hxx>
30f0ad28 23#include <OpenGl_Context.hxx>
bf75be98 24#include <OpenGl_Workspace.hxx>
30f0ad28 25#include <OpenGl_ShaderManager.hxx>
26#include <OpenGl_ShaderProgram.hxx>
2166f0fa 27
a174a3c5 28namespace
29{
30 static const TEL_COLOUR TheDefaultColor = {{ 1.0F, 1.0F, 1.0F, 1.0F }};
30f0ad28 31 static const TCollection_AsciiString THE_EMPTY_KEY;
a174a3c5 32};
2166f0fa 33
a174a3c5 34// =======================================================================
35// function : OpenGl_AspectText
36// purpose :
37// =======================================================================
38OpenGl_AspectText::OpenGl_AspectText()
39: myFont ("Courier") ,
40 myColor (TheDefaultColor),
41 mySubtitleColor (TheDefaultColor),
42 myAngle (0.0f),
43 myStyleType (Aspect_TOST_NORMAL),
44 myDisplayType (Aspect_TODT_NORMAL),
45 myFontAspect (Font_FA_Regular),
30f0ad28 46 myZoomable (false),
47 myShaderProgram()
2166f0fa 48{
a174a3c5 49 //
2166f0fa
SK
50}
51
a174a3c5 52// =======================================================================
53// function : ~OpenGl_AspectText
54// purpose :
55// =======================================================================
56OpenGl_AspectText::~OpenGl_AspectText()
2166f0fa 57{
a174a3c5 58 //
2166f0fa
SK
59}
60
a174a3c5 61// =======================================================================
fd4a6963 62// function : SetAspect
a174a3c5 63// purpose :
64// =======================================================================
fd4a6963 65void OpenGl_AspectText::SetAspect (const CALL_DEF_CONTEXTTEXT& theAspect)
2166f0fa 66{
fd4a6963 67 myFont = theAspect.Font;
30f0ad28 68
fd4a6963 69 myColor.rgb[0] = (float )theAspect.Color.r;
70 myColor.rgb[1] = (float )theAspect.Color.g;
71 myColor.rgb[2] = (float )theAspect.Color.b;
2166f0fa 72 myColor.rgb[3] = 1.0f;
fd4a6963 73 mySubtitleColor.rgb[0] = (float )theAspect.ColorSubTitle.r;
74 mySubtitleColor.rgb[1] = (float )theAspect.ColorSubTitle.g;
75 mySubtitleColor.rgb[2] = (float )theAspect.ColorSubTitle.b;
2166f0fa 76 mySubtitleColor.rgb[3] = 1.0f;
30f0ad28 77
fd4a6963 78 myAngle = (float )theAspect.TextAngle;
79 myStyleType = (Aspect_TypeOfStyleText )theAspect.Style;
80 myDisplayType = (Aspect_TypeOfDisplayText )theAspect.DisplayType;
81 myFontAspect = (Font_FontAspect )theAspect.TextFontAspect;
82 myZoomable = (theAspect.TextZoomable != 0);
30f0ad28 83
84 // update resource bindings
85 myShaderProgram = theAspect.ShaderProgram;
86
87 const TCollection_AsciiString& aShaderKey = myShaderProgram.IsNull() ? THE_EMPTY_KEY : myShaderProgram->GetId();
88
89 if (aShaderKey.IsEmpty() || myResources.ShaderProgramId != aShaderKey)
90 {
91 myResources.ResetShader();
92 }
2166f0fa
SK
93}
94
a174a3c5 95// =======================================================================
96// function : Render
97// purpose :
98// =======================================================================
5e27df78 99void OpenGl_AspectText::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
2166f0fa 100{
5e27df78 101 theWorkspace->SetAspectText (this);
102}
103
a174a3c5 104// =======================================================================
105// function : Release
106// purpose :
107// =======================================================================
30f0ad28 108void OpenGl_AspectText::Release (const Handle(OpenGl_Context)& theContext)
5e27df78 109{
30f0ad28 110 if (!myResources.ShaderProgram.IsNull() && !theContext.IsNull())
111 {
112 theContext->ShaderManager()->Unregister (myResources.ShaderProgram);
113 }
114 myResources.ShaderProgramId.Clear();
115 myResources.ResetShader();
116}
117
118// =======================================================================
119// function : BuildShader
120// purpose :
121// =======================================================================
122void OpenGl_AspectText::Resources::BuildShader (const Handle(OpenGl_Workspace)& theWS,
123 const Handle(Graphic3d_ShaderProgram)& theShader)
124{
125 const Handle(OpenGl_Context)& aContext = theWS->GetGlContext();
126
127 if (!aContext->IsGlGreaterEqual (2, 0))
128 return;
129
130 // release old shader program resources
131 if (!ShaderProgram.IsNull())
132 {
133 aContext->ShaderManager()->Unregister (ShaderProgram);
134 }
135
136 ShaderProgramId = theShader.IsNull() ? THE_EMPTY_KEY : theShader->GetId();
137
138 if (!theShader.IsNull())
139 {
140 if (!aContext->GetResource<Handle(OpenGl_ShaderProgram)> (ShaderProgramId, ShaderProgram))
141 {
142 ShaderProgram = aContext->ShaderManager()->Create (theShader);
143 if (!ShaderProgramId.IsEmpty())
144 {
145 aContext->ShareResource (ShaderProgramId, ShaderProgram);
146 }
147 }
148 }
149 else
150 {
151 ShaderProgram.Nullify();
152 }
2166f0fa 153}