083333eb5a0565b68ff43e0c745be962feeb862e
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_Export.cxx
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
18 #include <OpenGl_GraphicDriver.hxx>
19 #include <OpenGl_Context.hxx>
20 #include <OpenGl_CView.hxx>
21
22 #include <Standard_CLocaleSentry.hxx>
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #ifdef HAVE_GL2PS
29 #include <gl2ps.h>
30 #endif
31
32 /************************************************************************/
33 /* Print Methods                                                        */
34 /************************************************************************/
35
36 #ifdef HAVE_GL2PS
37 Standard_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,
45                                                const Standard_Real    thePrecision,
46                                                const Standard_Address theProgressBarFunc,
47                                                const Standard_Address theProgressObject)
48 {
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
57   Standard_Integer aFormat = -1;
58   Standard_Integer aSortType = Graphic3d_ST_BSP_Tree;
59   switch (theFormat)
60   {
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;
86   }
87
88   switch (theSortType)
89   {
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;
103
104   GLint aBufferSize = 1024 * 1024;
105   GLint anErrCode = GL2PS_SUCCESS;
106
107   // gl2ps uses standard write functions and do not check locale
108   Standard_CLocaleSentry aLocaleSentry;
109
110   while (aBufferSize > 0)
111   {
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,
115                     GL2PS_DRAW_BACKGROUND | GL2PS_OCCLUSION_CULL | GL2PS_BEST_ROOT/* | GL2PS_SIMPLE_LINE_OFFSET*/,
116                     GL_RGBA, 0, NULL,
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;
135   }
136   return anErrCode == GL2PS_SUCCESS;
137 }
138 #else
139 Standard_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;
152 }
153 #endif