0022734: Memory allocation error in OpenGl
[occt.git] / src / OpenGl / OpenGl_PrinterContext.cxx
CommitLineData
b311480e 1// Created on: 2011-05-20
2// Created by: Anton POLETAEV
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
7edf74fd
A
20
21#include <OpenGl_PrinterContext.hxx>
22
23OpenGl_PrinterContext* OpenGl_PrinterContext::g_PrinterContext = NULL;
2166f0fa 24GLCONTEXT OpenGl_PrinterContext::g_ContextId = NULL;
7edf74fd
A
25
26//=======================================================================
27//function : OpenGl_PrinterContext
28//purpose : Constructor
29//=======================================================================
30
31OpenGl_PrinterContext::OpenGl_PrinterContext (GLCONTEXT theCtx) :
32 myCtx (theCtx), myProjTransform (0, 3, 0, 3), myLayerViewportX (0),
33 myLayerViewportY (0), myScaleX (1.0f), myScaleY (1.0f)
34{
35 // assign global instance to the current object
36 if (myCtx != NULL)
37 {
38 g_PrinterContext = this;
39 g_ContextId = myCtx;
40 }
41
42 // init projection matrix
43 Standard_Real anInitValue = 0.0;
44 myProjTransform.Init (anInitValue);
45 myProjTransform (0,0) = 1.0f;
46 myProjTransform (1,1) = 1.0f;
47 myProjTransform (2,2) = 1.0f;
48 myProjTransform (3,3) = 1.0f;
49}
50
51//=======================================================================
52//function : ~OpenGl_PrinterContext
53//purpose : Destructor
54//=======================================================================
55
56OpenGl_PrinterContext::~OpenGl_PrinterContext ()
57{
58 // unassign global instance
59 if (g_PrinterContext == this)
60 {
61 g_ContextId = NULL;
62 g_PrinterContext = NULL;
63 }
64}
65
66//=======================================================================
67//function : GetProjTransformation
68//purpose : Get view projection transformation matrix.
69//=======================================================================
70
71void OpenGl_PrinterContext::GetProjTransformation (GLfloat theMatrix[16])
72{
73 for (int i = 0, k = 0; i < 4; i++)
74 for (int j = 0; j < 4; j++, k++)
75 theMatrix[k] = (GLfloat)myProjTransform (i,j);
76}
77
78//=======================================================================
79//function : SetProjTransformation
80//purpose : Set view projection transformation matrix for printing purposes.
81// theProjTransform parameter should be an 4x4 array.
82//=======================================================================
83
84bool OpenGl_PrinterContext::SetProjTransformation (TColStd_Array2OfReal& thePrj)
85{
86 if (thePrj.RowLength () != 4 || thePrj.ColLength () != 4)
87 return false;
88
89 myProjTransform = thePrj;
90
91 return true;
92}
93
94//=======================================================================
95//function : Deactivate
96//purpose : Deactivate PrinterContext object.
97// Useful when you need to redraw in usual mode the same
98// OpenGl context that you used for printing right after printing,
99// before the OpenGl_PrinterContext instance destroyed
100//=======================================================================
101
102void OpenGl_PrinterContext::Deactivate ()
103{
104 // unassign global instance
105 if (g_PrinterContext == this)
106 {
107 g_ContextId = NULL;
108 g_PrinterContext = NULL;
109 }
110}
111
112
113//=======================================================================
114//function : GetInstance
115//purpose : Get the PrinterContext instance assigned for OpenGl context.
116// Return NULL, if there is no current printing operation and
117// there is no assigned instance for "theCtx" OpenGl context.
118//=======================================================================
119
120OpenGl_PrinterContext* OpenGl_PrinterContext::GetPrinterContext (GLCONTEXT theCtx)
121{
122 if (g_ContextId == theCtx)
123 return g_PrinterContext;
124 else
125 return NULL;
126}