VRender.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the VRender library.
3  Copyright (C) 2005 Cyril Soler (Cyril.Soler@imag.fr)
4  Version 1.0.0, released on June 27, 2005.
5 
6  http://artis.imag.fr/Members/Cyril.Soler/VRender
7 
8  VRender is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  VRender is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with VRender; if not, write to the Free Software Foundation, Inc.,
20  51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22 
23 /****************************************************************************
24 
25  Copyright (C) 2002-2014 Gilles Debunne. All rights reserved.
26 
27  This file is part of the QGLViewer library version 2.6.3.
28 
29  http://www.libqglviewer.com - contact@libqglviewer.com
30 
31  This file may be used under the terms of the GNU General Public License
32  versions 2.0 or 3.0 as published by the Free Software Foundation and
33  appearing in the LICENSE file included in the packaging of this file.
34  In addition, as a special exception, Gilles Debunne gives you certain
35  additional rights, described in the file GPL_EXCEPTION in this package.
36 
37  libQGLViewer uses dual licensing. Commercial/proprietary software must
38  purchase a libQGLViewer Commercial License.
39 
40  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
41  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
42 
43 *****************************************************************************/
44 
45 #ifdef WIN32
46 # include <windows.h>
47 #endif
48 
49 #ifdef __APPLE__
50 # include <OpenGL/gl.h>
51 #else
52 # include <GL/gl.h>
53 #endif
54 
55 #include <stdio.h>
56 #include <vector>
57 #include <stdlib.h>
58 #include <string.h>
59 
60 #include "VRender.h"
61 #include "ParserGL.h"
62 #include "Exporter.h"
63 #include "SortMethod.h"
64 #include "Optimizer.h"
65 
66 using namespace vrender ;
67 using namespace std ;
68 
69 void vrender::VectorialRender(RenderCB render_callback, void *callback_params, VRenderParams& vparams)
70 {
71  GLfloat *feedbackBuffer = NULL ;
72  SortMethod *sort_method = NULL ;
73  Exporter *exporter = NULL ;
74 
75  try
76  {
77  GLint returned = -1 ;
78 
79  vparams.error() = 0 ;
80 
81  int nb_renders = 0 ;
82 
83  vparams.progress(0.0, QGLViewer::tr("Rendering...")) ;
84 
85  while(returned < 0)
86  {
87  if(feedbackBuffer != NULL)
88  delete[] feedbackBuffer ;
89 
90  feedbackBuffer = new GLfloat[vparams.size()] ;
91 
92  if(feedbackBuffer == NULL)
93  throw std::runtime_error("Out of memory during feedback buffer allocation.") ;
94 
95  glFeedbackBuffer(vparams.size(), GL_3D_COLOR, feedbackBuffer);
96  glRenderMode(GL_FEEDBACK);
97  render_callback(callback_params);
98  returned = glRenderMode(GL_RENDER);
99 
100  nb_renders++ ;
101 
102  if(returned < 0)
103  vparams.size() *= 2 ;
104  }
105 
106 #ifdef A_VOIR
107  if(SortMethod != EPS_DONT_SORT)
108  {
109  GLint depth_bits ;
110  glGetIntegerv(GL_DEPTH_BITS, &depth_bits) ;
111 
112  EGALITY_EPS = 2.0/(1 << depth_bits) ;
113  LINE_EGALITY_EPS = 2.0/(1 << depth_bits) ;
114  }
115 #endif
116  if (returned > vparams.size())
117  vparams.size() = returned;
118 #ifdef _VRENDER_DEBUG
119  cout << "Size = " << vparams.size() << ", returned=" << returned << endl ;
120 #endif
121 
122  // On a un beau feedback buffer tout plein de saloperies. Faut aller
123  // defricher tout ca. Ouaiiiis !
124 
125  vector<PtrPrimitive> primitive_tab ;
126 
127  ParserGL parserGL ;
128  parserGL.parseFeedbackBuffer(feedbackBuffer,returned,primitive_tab,vparams) ;
129 
130  if(feedbackBuffer != NULL)
131  {
132  delete[] feedbackBuffer ;
133  feedbackBuffer = NULL ;
134  }
135 
137  {
139  bfopt.optimize(primitive_tab,vparams) ;
140  }
141 
142  // Lance la methode de sorting
143 
144  switch(vparams.sortMethod())
145  {
150  sort_method = tsm ;
151  }
152  break ;
153 
154  case VRenderParams::BSPSort: sort_method = new BSPSortMethod() ;
155  break ;
156 
157  case VRenderParams::NoSorting: sort_method = new DontSortMethod() ;
158  break ;
159  default:
160  throw std::runtime_error("Unknown sorting method.") ;
161  }
162 
163  sort_method->sortPrimitives(primitive_tab,vparams) ;
164 
165  // Lance les optimisations. L'ordre est important.
166 
168  {
169  VisibilityOptimizer vopt ;
170  vopt.optimize(primitive_tab,vparams) ;
171  }
172 
173 #ifdef A_FAIRE
174  if(vparams.isEnabled(VRenderParams::OptimizePrimitiveSplit))
175  {
177  psopt.optimize(primitive_tab) ;
178  }
179 #endif
180  // Ecrit le fichier
181 
182  switch(vparams.format())
183  {
184  case VRenderParams::EPS: exporter = new EPSExporter() ;
185  break ;
186  case VRenderParams::PS: exporter = new PSExporter() ;
187  break ;
188  case VRenderParams::XFIG:exporter = new FIGExporter() ;
189  break ;
190 #ifdef A_FAIRE
191  case VRenderParams::SVG: exporter = new SVGExporter() ;
192  break ;
193 #endif
194  default:
195  throw std::runtime_error("Sorry, this output format is not handled now. Only EPS and PS are currently supported.") ;
196  }
197 
198  // sets background and black & white options
199 
200  GLfloat viewport[4],clearColor[4],lineWidth,pointSize ;
201 
202  glGetFloatv(GL_COLOR_CLEAR_VALUE, clearColor);
203  glGetFloatv(GL_LINE_WIDTH, &lineWidth);
204  glGetFloatv(GL_POINT_SIZE, &pointSize);
205  glGetFloatv(GL_VIEWPORT, viewport);
206 
207  lineWidth /= (float)max(viewport[2] - viewport[0],viewport[3]-viewport[1]) ;
208 
209  // Sets which bounding box to use.
210 
212  exporter->setBoundingBox(parserGL.xmin(),parserGL.ymin(),parserGL.xmax(),parserGL.ymax()) ;
213  else
214  exporter->setBoundingBox(viewport[0],viewport[1],viewport[0]+viewport[2],viewport[1]+viewport[3]) ;
215 
218  exporter->setClearColor(clearColor[0],clearColor[1],clearColor[2]) ;
219 
220  exporter->exportToFile(vparams.filename(),primitive_tab,vparams) ;
221 
222  // deletes primitives
223 
224  for(unsigned int i=0;i<primitive_tab.size();++i)
225  delete primitive_tab[i] ;
226 
227  if(exporter != NULL) delete exporter ;
228  if(sort_method != NULL) delete sort_method ;
229  }
230  catch(exception& e)
231  {
232  cout << "Render aborted: " << e.what() << endl ;
233 
234  if(exporter != NULL) delete exporter ;
235  if(sort_method != NULL) delete sort_method ;
236  if(feedbackBuffer != NULL) delete[] feedbackBuffer ;
237 
238  throw e ;
239  }
240 }
241 
243 {
244  _options = 0 ;
245  _format = EPS ;
246  _filename = "" ;
247  _progress_function = NULL ;
248  _sortMethod = BSPSort ;
249 }
250 
252 {}
253 
254 
255 void VRenderParams::progress(float f, const QString& progress_string)
256 {
257  _progress_function(f,progress_string) ;
258 }
259 
260 void VRenderParams::setFilename(const QString& filename)
261 {
262  _filename = filename;
263 }
264 
266 {
267  if(b)
268  _options |= opt ;
269  else
270  _options &= ~opt ;
271 }
272 
274 {
275  return (_options & opt) > 0 ;
276 }
void setClearColor(float r, float g, float b)
Definition: Exporter.cpp:105
void setOption(VRenderOption, bool)
Definition: VRender.cpp:265
GLfloat xmin() const
Definition: ParserGL.h:65
virtual void optimize(std::vector< PtrPrimitive > &, VRenderParams &)
void setClearBackground(bool b)
Definition: Exporter.cpp:106
GLfloat xmax() const
Definition: ParserGL.h:68
void setBoundingBox(float xmin, float ymin, float xmax, float ymax)
Definition: Exporter.cpp:97
const QString filename()
Definition: VRender.h:83
void setBlackAndWhite(bool b)
Definition: Exporter.cpp:107
void(* RenderCB)(void *)
Definition: VRender.h:57
void progress(float, const QString &)
Definition: VRender.cpp:255
double EGALITY_EPS
virtual void optimize(std::vector< PtrPrimitive > &, VRenderParams &)
Definition: Optimizer.h:79
void parseFeedbackBuffer(GLfloat *, int size, std::vector< PtrPrimitive > &primitive_tab, VRenderParams &vparams)
Definition: ParserGL.cpp:86
double LINE_EGALITY_EPS
void VectorialRender(RenderCB DrawFunc, void *callback_params, VRenderParams &render_params)
Definition: VRender.cpp:69
GLfloat ymin() const
Definition: ParserGL.h:66
virtual void sortPrimitives(std::vector< PtrPrimitive > &, VRenderParams &)=0
void setFilename(const QString &filename)
Definition: VRender.cpp:260
virtual void exportToFile(const QString &filename, const std::vector< PtrPrimitive > &, VRenderParams &)
Definition: Exporter.cpp:61
bool isEnabled(VRenderOption)
Definition: VRender.cpp:273
virtual void optimize(std::vector< PtrPrimitive > &, VRenderParams &)
GLfloat ymax() const
Definition: ParserGL.h:69


octovis
Author(s): Kai M. Wurm , Armin Hornung
autogenerated on Wed Jun 5 2019 19:26:39