7b02825090c012eb6ec205c4ef9b4227c610f41b
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_Export.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <OpenGl_GraphicDriver.hxx>
15 #include <OpenGl_Context.hxx>
16 #include <OpenGl_CView.hxx>
17
18 #include <Standard_CLocaleSentry.hxx>
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #ifdef HAVE_GL2PS
25 #include <gl2ps.h>
26 #endif
27
28 /************************************************************************/
29 /* Print Methods                                                        */
30 /************************************************************************/
31
32 #ifdef HAVE_GL2PS
33 Standard_Boolean OpenGl_GraphicDriver::Export (const Standard_CString theFileName,
34                                                const Graphic3d_ExportFormat theFormat,
35                                                const Graphic3d_SortType theSortType,
36                                                const Standard_Integer theWidth,
37                                                const Standard_Integer theHeight,
38                                                const Graphic3d_CView& theView,
39                                                const Aspect_CLayer2d& theLayerUnder,
40                                                const Aspect_CLayer2d& theLayerOver,
41                                                const Standard_Real    /*thePrecision*/,
42                                                const Standard_Address /*theProgressBarFunc*/,
43                                                const Standard_Address /*theProgressObject*/)
44 {
45   // gl2psBeginPage() will call OpenGL functions
46   // so we should activate correct GL context before redraw scene call
47   const OpenGl_CView* aCView = (const OpenGl_CView* )theView.ptrView;
48   if (aCView == NULL || !aCView->WS->GetGlContext()->MakeCurrent())
49   {
50     return Standard_False;
51   }
52
53   Standard_Integer aFormat = -1;
54   Standard_Integer aSortType = Graphic3d_ST_BSP_Tree;
55   switch (theFormat)
56   {
57     case Graphic3d_EF_PostScript:
58       aFormat = GL2PS_PS;
59       break;
60     case Graphic3d_EF_EnhPostScript:
61       aFormat = GL2PS_EPS;
62       break;
63     case Graphic3d_EF_TEX:
64       aFormat = GL2PS_TEX;
65       break;
66     case Graphic3d_EF_PDF:
67       aFormat = GL2PS_PDF;
68       break;
69     case Graphic3d_EF_SVG:
70       aFormat = GL2PS_SVG;
71       break;
72     case Graphic3d_EF_PGF:
73       aFormat = GL2PS_PGF;
74       break;
75     case Graphic3d_EF_EMF:
76       //aFormat = GL2PS_EMF;
77       aFormat = GL2PS_PGF + 1; // 6
78       break;
79     default:
80       // unsupported format
81       return Standard_False;
82   }
83
84   switch (theSortType)
85   {
86     case Graphic3d_ST_Simple:
87       aSortType = GL2PS_SIMPLE_SORT;
88       break;
89     case Graphic3d_ST_BSP_Tree:
90       aSortType = GL2PS_BSP_SORT;
91       break;
92   }
93
94   GLint aViewport[4];
95   aViewport[0] = 0;
96   aViewport[1] = 0;
97   aViewport[2] = theWidth;
98   aViewport[3] = theHeight;
99
100   GLint aBufferSize = 1024 * 1024;
101   GLint anErrCode = GL2PS_SUCCESS;
102
103   // gl2ps uses standard write functions and do not check locale
104   Standard_CLocaleSentry aLocaleSentry;
105
106   while (aBufferSize > 0)
107   {
108     // current patch for EMF support in gl2ps uses WinAPI functions to create file
109     FILE* aFileH = (theFormat != Graphic3d_EF_EMF) ? fopen (theFileName, "wb") : NULL;
110     anErrCode = gl2psBeginPage ("", "", aViewport, aFormat, aSortType,
111                     GL2PS_DRAW_BACKGROUND | GL2PS_OCCLUSION_CULL | GL2PS_BEST_ROOT/* | GL2PS_SIMPLE_LINE_OFFSET*/,
112                     GL_RGBA, 0, NULL,
113                     0, 0, 0, aBufferSize, aFileH, theFileName);
114     if (anErrCode != GL2PS_SUCCESS)
115     {
116       // initialization failed
117       if (aFileH != NULL)
118         fclose (aFileH);
119       break;
120     }
121     Redraw (theView, theLayerUnder, theLayerOver);
122
123     anErrCode = gl2psEndPage();
124     if (aFileH != NULL)
125       fclose (aFileH);
126
127     if (anErrCode == GL2PS_OVERFLOW)
128       aBufferSize *= 2;
129     else
130       break;
131   }
132   return anErrCode == GL2PS_SUCCESS;
133 }
134 #else
135 Standard_Boolean OpenGl_GraphicDriver::Export (const Standard_CString /*theFileName*/,
136                                                const Graphic3d_ExportFormat /*theFormat*/,
137                                                const Graphic3d_SortType /*theSortType*/,
138                                                const Standard_Integer /*theWidth*/,
139                                                const Standard_Integer /*theHeight*/,
140                                                const Graphic3d_CView& /*theView*/,
141                                                const Aspect_CLayer2d& /*theLayerUnder*/,
142                                                const Aspect_CLayer2d& /*theLayerOver*/,
143                                                const Standard_Real    /*thePrecision*/,
144                                                const Standard_Address /*theProgressBarFunc*/,
145                                                const Standard_Address /*theProgressObject*/)
146 {
147     return Standard_False;
148 }
149 #endif