0024192: Adding support for shaders to OCCT visualization toolkit
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_Export.cxx
CommitLineData
b311480e 1// Copyright (c) 1999-2012 OPEN CASCADE SAS
2//
3// The content of this file is subject to the Open CASCADE Technology Public
4// License Version 6.5 (the "License"). You may not use the content of this file
5// except in compliance with the License. Please obtain a copy of the License
6// at http://www.opencascade.org and read it completely before using this file.
7//
8// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10//
11// The Original Code and all software distributed under the License is
12// distributed on an "AS IS" basis, without warranty of any kind, and the
13// Initial Developer hereby disclaims all such warranties, including without
14// limitation, any warranties of merchantability, fitness for a particular
15// purpose or non-infringement. Please see the License for the specific terms
16// and conditions governing the rights and limitations under the License.
17
2166f0fa 18#include <OpenGl_GraphicDriver.hxx>
86fa64d9 19#include <OpenGl_Context.hxx>
20#include <OpenGl_CView.hxx>
91322f44 21
22#include <Standard_CLocaleSentry.hxx>
7fd59977 23
498ce577 24#ifdef HAVE_CONFIG_H
25# include <config.h>
26#endif
27
7fd59977 28#ifdef HAVE_GL2PS
29#include <gl2ps.h>
30#endif
31
7fd59977 32/************************************************************************/
33/* Print Methods */
34/************************************************************************/
35
35e08fe8 36#ifdef HAVE_GL2PS
5cedc27f
K
37Standard_Boolean OpenGl_GraphicDriver::Export (const Standard_CString theFileName,
38 const Graphic3d_ExportFormat theFormat,
39 const Graphic3d_SortType theSortType,
40 const Standard_Integer theWidth,
41 const Standard_Integer theHeight,
42 const Graphic3d_CView& theView,
43 const Aspect_CLayer2d& theLayerUnder,
44 const Aspect_CLayer2d& theLayerOver,
105aae76 45 const Standard_Real /*thePrecision*/,
46 const Standard_Address /*theProgressBarFunc*/,
47 const Standard_Address /*theProgressObject*/)
7fd59977 48{
86fa64d9 49 // gl2psBeginPage() will call OpenGL functions
50 // so we should activate correct GL context before redraw scene call
51 const OpenGl_CView* aCView = (const OpenGl_CView* )theView.ptrView;
52 if (aCView == NULL || !aCView->WS->GetGlContext()->MakeCurrent())
53 {
54 return Standard_False;
55 }
56
5cedc27f
K
57 Standard_Integer aFormat = -1;
58 Standard_Integer aSortType = Graphic3d_ST_BSP_Tree;
59 switch (theFormat)
7fd59977 60 {
5cedc27f
K
61 case Graphic3d_EF_PostScript:
62 aFormat = GL2PS_PS;
63 break;
64 case Graphic3d_EF_EnhPostScript:
65 aFormat = GL2PS_EPS;
66 break;
67 case Graphic3d_EF_TEX:
68 aFormat = GL2PS_TEX;
69 break;
70 case Graphic3d_EF_PDF:
71 aFormat = GL2PS_PDF;
72 break;
73 case Graphic3d_EF_SVG:
74 aFormat = GL2PS_SVG;
75 break;
76 case Graphic3d_EF_PGF:
77 aFormat = GL2PS_PGF;
78 break;
79 case Graphic3d_EF_EMF:
80 //aFormat = GL2PS_EMF;
81 aFormat = GL2PS_PGF + 1; // 6
82 break;
83 default:
84 // unsupported format
85 return Standard_False;
7fd59977 86 }
87
5cedc27f 88 switch (theSortType)
7fd59977 89 {
5cedc27f
K
90 case Graphic3d_ST_Simple:
91 aSortType = GL2PS_SIMPLE_SORT;
92 break;
93 case Graphic3d_ST_BSP_Tree:
94 aSortType = GL2PS_BSP_SORT;
95 break;
96 }
97
98 GLint aViewport[4];
99 aViewport[0] = 0;
100 aViewport[1] = 0;
101 aViewport[2] = theWidth;
102 aViewport[3] = theHeight;
7fd59977 103
5cedc27f
K
104 GLint aBufferSize = 1024 * 1024;
105 GLint anErrCode = GL2PS_SUCCESS;
7fd59977 106
5cedc27f 107 // gl2ps uses standard write functions and do not check locale
91322f44 108 Standard_CLocaleSentry aLocaleSentry;
7fd59977 109
5cedc27f 110 while (aBufferSize > 0)
7fd59977 111 {
5cedc27f
K
112 // current patch for EMF support in gl2ps uses WinAPI functions to create file
113 FILE* aFileH = (theFormat != Graphic3d_EF_EMF) ? fopen (theFileName, "wb") : NULL;
114 anErrCode = gl2psBeginPage ("", "", aViewport, aFormat, aSortType,
7fd59977 115 GL2PS_DRAW_BACKGROUND | GL2PS_OCCLUSION_CULL | GL2PS_BEST_ROOT/* | GL2PS_SIMPLE_LINE_OFFSET*/,
116 GL_RGBA, 0, NULL,
5cedc27f
K
117 0, 0, 0, aBufferSize, aFileH, theFileName);
118 if (anErrCode != GL2PS_SUCCESS)
119 {
120 // initialization failed
121 if (aFileH != NULL)
122 fclose (aFileH);
123 break;
124 }
125 Redraw (theView, theLayerUnder, theLayerOver);
126
127 anErrCode = gl2psEndPage();
128 if (aFileH != NULL)
129 fclose (aFileH);
130
131 if (anErrCode == GL2PS_OVERFLOW)
132 aBufferSize *= 2;
133 else
134 break;
7fd59977 135 }
5cedc27f 136 return anErrCode == GL2PS_SUCCESS;
35e08fe8 137}
5cedc27f 138#else
35e08fe8 139Standard_Boolean OpenGl_GraphicDriver::Export (const Standard_CString /*theFileName*/,
140 const Graphic3d_ExportFormat /*theFormat*/,
141 const Graphic3d_SortType /*theSortType*/,
142 const Standard_Integer /*theWidth*/,
143 const Standard_Integer /*theHeight*/,
144 const Graphic3d_CView& /*theView*/,
145 const Aspect_CLayer2d& /*theLayerUnder*/,
146 const Aspect_CLayer2d& /*theLayerOver*/,
147 const Standard_Real /*thePrecision*/,
148 const Standard_Address /*theProgressBarFunc*/,
149 const Standard_Address /*theProgressObject*/)
150{
151 return Standard_False;
7fd59977 152}
35e08fe8 153#endif