0024166: Unable to create file with "Save" menu of voxeldemo Qt sample
[occt.git] / src / OpenGl / OpenGl_AspectLine.cxx
CommitLineData
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 28namespace
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
38OpenGl_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
48OpenGl_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 58void 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();
30f0ad28 71 if (aShaderKey.IsEmpty() || myResources.ShaderProgramId != aShaderKey)
72 {
f85399e5 73 myResources.ResetShaderReadiness();
30f0ad28 74 }
75}
2166f0fa 76
30f0ad28 77// =======================================================================
78// function : Render
79// purpose :
80// =======================================================================
5e27df78 81void OpenGl_AspectLine::Render (const Handle(OpenGl_Workspace) &theWorkspace) const
2166f0fa 82{
5e27df78 83 theWorkspace->SetAspectLine (this);
2166f0fa
SK
84}
85
30f0ad28 86// =======================================================================
87// function : Release
88// purpose :
89// =======================================================================
90void OpenGl_AspectLine::Release (const Handle(OpenGl_Context)& theContext)
91{
392ac980 92 if (!myResources.ShaderProgram.IsNull()
93 && !theContext.IsNull())
30f0ad28 94 {
392ac980 95 theContext->ShaderManager()->Unregister (myResources.ShaderProgramId,
96 myResources.ShaderProgram);
30f0ad28 97 }
98 myResources.ShaderProgramId.Clear();
f85399e5 99 myResources.ResetShaderReadiness();
30f0ad28 100}
101
102// =======================================================================
103// function : BuildShader
104// purpose :
105// =======================================================================
392ac980 106void OpenGl_AspectLine::Resources::BuildShader (const Handle(OpenGl_Workspace)& theWS,
30f0ad28 107 const Handle(Graphic3d_ShaderProgram)& theShader)
5e27df78 108{
30f0ad28 109 const Handle(OpenGl_Context)& aContext = theWS->GetGlContext();
30f0ad28 110 if (!aContext->IsGlGreaterEqual (2, 0))
392ac980 111 {
30f0ad28 112 return;
392ac980 113 }
30f0ad28 114
115 // release old shader program resources
116 if (!ShaderProgram.IsNull())
117 {
392ac980 118 aContext->ShaderManager()->Unregister (ShaderProgramId, ShaderProgram);
f85399e5 119 ShaderProgramId.Clear();
120 ShaderProgram.Nullify();
30f0ad28 121 }
392ac980 122 if (theShader.IsNull())
30f0ad28 123 {
392ac980 124 return;
30f0ad28 125 }
392ac980 126
127 aContext->ShaderManager()->Create (theShader, ShaderProgramId, ShaderProgram);
5e27df78 128}