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