46 #include "Primitive.h"
53 const double EPSExporter::EPS_GOURAUD_THRESHOLD = 0.05 ;
54 const char *EPSExporter::CREATOR =
"VRender library - (c) Cyril Soler 2005" ;
56 float EPSExporter::last_r = -1.0 ;
57 float EPSExporter::last_g = -1.0 ;
58 float EPSExporter::last_b = -1.0 ;
60 EPSExporter::EPSExporter()
67 void EPSExporter::writeHeader(QTextStream& out)
const
71 out <<
"%!PS-Adobe-2.0 EPSF-2.0\n";
73 out <<
"%%%%HiResBoundingBox: " << _xmin <<
" " << _ymin <<
" " << _xmax <<
" " << _ymax <<
"\n";
75 out <<
"%%%%Creator: " << CREATOR <<
" (using OpenGL feedback)\n";
76 out <<
"%%EndComments\n\ngsave\n\n";
79 out <<
"% Contributors:\n";
81 out <<
"% Frederic Delhoume (delhoume@ilog.fr):\n";
82 out <<
"% Gouraud triangle PostScript fragment\n";
84 out <<
"% Cyril Soler (csoler@imag.fr):\n";
85 out <<
"% BSP Sort,\n";
86 out <<
"% Topological and advanced topological Sort,\n";
87 out <<
"% Hidden surface removal,\n";
88 out <<
"% Xfig3.2 (and EPS) format\n";
91 out <<
"/threshold " << EPS_GOURAUD_THRESHOLD <<
" def\n";
93 for(
int i = 0; GOURAUD_TRIANGLE_EPS[i] != NULL; i++)
94 out << GOURAUD_TRIANGLE_EPS[i] <<
"\n";
96 out <<
"\n" << <<
" setlinewidth\n\n", _lineWidth;
102 out << _clearR <<
" " << _clearG <<
" " << _clearB <<
" setrgbcolor\n";
103 out << _xmin <<
" " << _ymin <<
" " << _xmax <<
" " << _ymax <<
" rectfill\n\n";
107 void EPSExporter::writeFooter(QTextStream& out)
const
109 out <<
"grestore\n\n";
111 out <<
"% uncomment next line to be able to print to a printer.\n";
112 out <<
"% showpage\n";
115 void PSExporter::writeFooter(QTextStream& out)
const
120 const char *EPSExporter::GOURAUD_TRIANGLE_EPS[] =
122 "/bd{bind def}bind def /triangle { aload pop ",
123 "setrgbcolor aload pop 5 3 roll 4 2 roll 3 2 roll exch moveto lineto ",
124 "lineto closepath fill } bd /computediff1 { 2 copy sub abs threshold ",
125 "ge {pop pop pop true} { exch 2 index sub abs threshold ge { pop pop ",
126 "true} { sub abs threshold ge } ifelse } ifelse } bd /computediff3 { 3 ",
127 "copy 0 get 3 1 roll 0 get 3 1 roll 0 get computediff1 {true} { 3 copy ",
128 "1 get 3 1 roll 1 get 3 1 roll 1 get computediff1 {true} { 3 copy 2 ",
129 "get 3 1 roll 2 get 3 1 roll 2 get computediff1 } ifelse } ifelse } ",
130 "bd /middlecolor { aload pop 4 -1 roll aload pop 4 -1 roll add 2 div 5 ",
131 "1 roll 3 -1 roll add 2 div 3 1 roll add 2 div 3 1 roll exch 3 array ",
132 "astore } bd /gdt { computediff3 { 4 -1 roll aload 7 1 roll ",
133 "6 -1 roll pop 3 -1 roll pop add 2 div 3 1 roll add 2 div exch 3 -1 roll ",
134 "aload 7 1 roll exch pop 4 -1 roll pop add 2 div 3 1 roll add 2 div ",
135 "exch 3 -1 roll aload 7 1 roll pop 3 -1 roll pop add 2 div 3 1 roll add ",
136 "2 div exch 7 3 roll 10 -3 roll dup 3 index middlecolor 4 1 roll 2 copy ",
137 "middlecolor 4 1 roll 3 copy pop middlecolor 4 1 roll 13 -1 roll aload ",
138 "pop 17 index 6 index 15 index 19 index 6 index 17 index 6 array astore ",
139 "10 index 10 index 14 index gdt 17 index 5 index 17 index ",
140 "19 index 5 index 19 index 6 array astore 10 index 9 index 13 index ",
141 "gdt 13 index 16 index 5 index 15 index 18 index 5 index 6 ",
142 "array astore 12 index 12 index 9 index gdt 17 index 16 ",
143 "index 15 index 19 index 18 index 17 index 6 array astore 10 index 12 ",
144 "index 14 index gdt 18 {pop} repeat } { aload pop 5 3 roll ",
145 "aload pop 7 3 roll aload pop 9 3 roll 8 index 6 index 4 index add add 3 ",
146 "div 10 1 roll 7 index 5 index 3 index add add 3 div 10 1 roll 6 index 4 ",
147 "index 2 index add add 3 div 10 1 roll 9 {pop} repeat 3 array astore ",
148 "triangle } ifelse } bd",
152 void EPSExporter::spewPolygone(
const Polygone *P, QTextStream& out)
155 GLfloat red, green, blue;
158 nvertices = P->nbVertices() ;
165 green = vertex.green();
166 blue = vertex.blue();
170 for(
int i=1;i < nvertices && !smooth; i++)
171 if(fabs(red - P->sommet3DColor(i).red()) > 0.01 || fabs(green - P->sommet3DColor(i).green()) > 0.01 || fabs(blue - P->sommet3DColor(i).blue()) > 0.01)
174 if(smooth && !_blackAndWhite)
179 for (
int j = 0; j < nvertices - 2; j++)
181 out <<
"[" << P->sommet3DColor(0).x() <<
" " << P->sommet3DColor(j + 1).x() <<
" " << P->sommet3DColor(j + 2).x()
182 <<
" " << P->sommet3DColor(0).y() <<
" " << P->sommet3DColor(j + 1).y() <<
" " << P->sommet3DColor(j + 2).y() <<
"]";
184 out <<
" [" << P->sommet3DColor(0 ).red() <<
" " << P->sommet3DColor(0 ).green() <<
" " << P->sommet3DColor(0 ).blue()
185 <<
"] [" << P->sommet3DColor(j + 1).red() <<
" " << P->sommet3DColor(j + 1).green() <<
" " << P->sommet3DColor(j + 1).blue()
186 <<
"] [" << P->sommet3DColor(j + 2).red() <<
" " << P->sommet3DColor(j + 2).green() <<
" " << P->sommet3DColor(j + 2).blue() <<
"] gdt\n";
188 last_r = last_g = last_b = -1.0 ;
198 setColor(out,1.0,1.0,1.0) ;
200 setColor(out,red,green,blue) ;
204 out << P->sommet3DColor(0).x() <<
" " << P->sommet3DColor(0).y() <<
" moveto\n";
206 for (
int i = 1; i < nvertices; i++)
207 out << P->sommet3DColor(i).x() <<
" " << P->sommet3DColor(i).y() <<
" lineto\n";
209 out <<
"closepath fill\n\n";
214 void EPSExporter::spewSegment(
const Segment *S, QTextStream& out)
217 GLfloat dr, dg, db, absR, absG, absB, colormax;
219 GLdouble xstep=0.0, ystep=0.0;
220 GLfloat rstep=0.0, gstep=0.0, bstep=0.0;
221 GLdouble xnext=0.0, ynext=0.0, distance=0.0;
222 GLfloat rnext=0.0, gnext=0.0, bnext=0.0;
227 dr = P2.red() - P1.red();
228 dg = P2.green() - P1.green();
229 db = P2.blue() - P1.blue();
231 if((!_blackAndWhite)&&(dr != 0 || dg != 0 || db != 0))
235 dx = P2.x() - P1.x();
236 dy = P2.y() - P1.y();
238 distance = sqrt(dx*dx + dy*dy);
244 colormax =
max(absR,
max(absG, absB));
245 steps = int(0.5f +
max(1.0, colormax * distance * EPS_SMOOTH_LINE_FACTOR));
263 xnext -= xstep / 2.0;
264 ynext -= ystep / 2.0;
265 rnext -= rstep / 2.0f;
266 gnext -= gstep / 2.0f;
267 bnext -= bstep / 2.0f;
273 setColor(out,0.0,0.0,0.0) ;
275 setColor(out,P1.red(),P1.green(),P1.blue()) ;
277 out << P1.x() <<
" " << P1.y() <<
" moveto\n";
279 for(
int i = 0;i < steps;i++)
287 out << xnext <<
" " << ynext <<
" lineto stroke\n";
288 out << rnext <<
" " << gnext <<
" " << bnext <<
" setrgbcolor\n";
289 out << xnext <<
" " << ynext <<
" moveto\n";
291 last_r = last_g = last_b = -1.0 ;
293 out << P2.x() <<
" " << P2.y() <<
" lineto stroke\n";
296 void EPSExporter::spewPoint(
const Point *P, QTextStream& out)
301 setColor(out,0.0,0.0,0.0) ;
303 setColor(out,p.red(),p.green(),p.blue()) ;
305 out << p.x() <<
" " << p.y() <<
" " << (_pointSize / 2.0) <<
" 0 360 arc fill\n\n";
308 void EPSExporter::setColor(QTextStream& out,
float red,
float green,
float blue)
310 if(last_r != red || last_g != green || last_b != blue)
311 out << red <<
" " << green <<
" " << blue <<
" setrgbcolor\n";
FARSA_UTIL_TEMPLATE const T max(const T &t1, const U &t2)