SUMO - Simulation of Urban MObility

gl2psTest.c

Go to the documentation of this file.
00001 /*
00002  * GL2PS, an OpenGL to PostScript Printing Library
00003  * Copyright (C) 1999-2011 Christophe Geuzaine <geuz@geuz.org>
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of either:
00007  *
00008  * a) the GNU Library General Public License as published by the Free
00009  * Software Foundation, either version 2 of the License, or (at your
00010  * option) any later version; or
00011  *
00012  * b) the GL2PS License as published by Christophe Geuzaine, either
00013  * version 2 of the License, or (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either
00018  * the GNU Library General Public License or the GL2PS License for
00019  * more details.
00020  *
00021  * You should have received a copy of the GNU Library General Public
00022  * License along with this library in the file named "COPYING.LGPL";
00023  * if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
00024  * Cambridge, MA 02139, USA.
00025  *
00026  * You should have received a copy of the GL2PS License with this
00027  * library in the file named "COPYING.GL2PS"; if not, I will be glad
00028  * to provide one.
00029  *
00030  * For the latest info about gl2ps and a full list of contributors,
00031  * see http://www.geuz.org/gl2ps/.
00032  *
00033  * Please report all bugs and problems to <gl2ps@geuz.org>.
00034  */
00035 
00036 /*
00037   To compile on Linux:
00038   gcc gl2psTest.c gl2ps.c -lglut -lGL -lGLU -lX11 -lm
00039 
00040   To compile on MacOSX:
00041   gcc gl2psTest.c gl2ps.c -framework OpenGL -framework GLUT -framework Cocoa
00042 
00043   (To enable file compression you must add "-DHAVE_ZLIB -lz" to the
00044   commands above.  To enable embedded bitmaps in SVG files you must
00045   add "-DHAVE_LIBPNG -DHAVE_ZLIB -lpng -lz".)
00046 */
00047 
00048 #ifdef _MSC_VER /* MSVC Compiler */
00049 #pragma comment(linker, "/entry:\"mainCRTStartup\"")
00050 #endif
00051 
00052 #include "gl2ps.h"
00053 
00054 #include <string.h>
00055 #ifdef __APPLE__
00056 #  include <GLUT/glut.h>
00057 #else
00058 #  include <GL/glut.h>
00059 #endif
00060 
00061 static const char *format_string = "Postscript";
00062 static float rotation = -58.;
00063 static GLsizei window_w = 0;
00064 static GLsizei window_h = 0;
00065 static GLboolean display_multi = GL_TRUE;
00066 static GLboolean blend = GL_FALSE;
00067 static GLboolean teapot = GL_FALSE;
00068 static const char *pixmap[] = {
00069   "****************************************************************",
00070   "*..............................................................*",
00071   "*..................++++++++++++++++++++++++++..................*",
00072   "*.............+++++++++++++++++++++++++++++++++++++............*",
00073   "*.......++++++++++++aaaaaaaaaaaaaaaaaaaaaaaa++++++++++++.......*",
00074   "*.......++++++aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa++++++.......*",
00075   "*.......++aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa++.......*",
00076   "*.......++aaaa++++aa+aaaaaaaa++++aaa++++++aaa+++++aaaa++.......*",
00077   "*.......++aaa+aaaa+a+aaaaaaa+aaaa+aa+aaaaa+a+aaaaa+aaa++.......*",
00078   "*.......++aa+aaaaaaa+aaaaaaaaaaa+aaa+aaaaa+a+aaaaaaaaa++.......*",
00079   "*.......++aa+aaa+++a+aaaaaaaaaa+aaaa++++++aaa+++++aaaa++.......*",
00080   "*.......++aa+aaaaa+a+aaaaaaaaa+aaaaa+aaaaaaaaaaaaa+aaa++.......*",
00081   "*.......++aaa+aaaa+a+aaaaaaaa+aaaaaa+aaaaaaa+aaaaa+aaa++.......*",
00082   "*.......++aaaa+++++a++++++aa++++++aa+aaaaaaaa+++++aaaa++.......*",
00083   "*.......++aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa++.......*",
00084   "*.......++aaaaaaaaa++++++++++++++++++++++++++aaaaaaaaa++.......*",
00085   "*.......++aaa+++++++@@@@@@@@@@@+++++++++++++++++++aaaa++.......*",
00086   "*.......++++++@@+@@@+@@@++@@@@@+++++++++++++++++++++++++.......*",
00087   "*.......++@@@@@+@@@+@@@+@@@@+@@+++++++++++++++++++++++++.......*",
00088   "*.......++@@@@+@@@+@@+++++++@@@+++++++++++++++++++++++++.......*",
00089   "*.......++@@@@+++++++@@@@@@++@@++#####################++.......*",
00090   "*.......++@+++++@@@@@@@@@@@@@@@++#####################++.......*",
00091   "*.......++@+++@@@@@@@@@@@@@@@@@++#####################++.......*",
00092   "*.......++@++@@@@@@@+@@+@@@@@@@++#####################++.......*",
00093   "*.......++@@@@@+@@@+@@+@@@@@@@@++#####################++.......*",
00094   "*.......++@@@@+@@@+@@+@@++++++++++++++++++++++++++++++++.......*",
00095   "*.......++@@@@+@+++++++++@++++@+@+@+++++++++++++++++++++.......*",
00096   "*.......++@@@@+++@@@@@@@+@++@+++@+@+@+@+++++++++++++++++.......*",
00097   "*.......++@++++@@@@@@@@@@+@+++@+@+@+++++++++++++++++++++.......*",
00098   "*.......++@+++@@@@@@@@@@+@@@@@@@@@@@@@@+++++++++++++++++.......*",
00099   "*.......++@++@@@@@@+@@+@+@@@+++@@@@@@@@+++++++++++++++++.......*",
00100   "*.......++@@@@@+@@+@@+@@+@@+++++@@@@@@@+##############++.......*",
00101   "*.......++@@@@+@@+@@+@@++@@+@+++@@@@@+@+##############++.......*",
00102   "*.......++@@@@+@+++++++@+@@@@+++@@@@++@+##############++.......*",
00103   "*.......++@++++++@@@@@@++@@@@+++@@@@++@+##############++.......*",
00104   "*.......++@+++@@@@@@@@@@+@@@+++++@@@+@@+##############++.......*",
00105   "*.......++@++@@@@@@@@@@@+@+++++++++++@@+##############++.......*",
00106   "*.......+++++++++++++++++@+@+++++++++@@+++++++++++++++++.......*",
00107   "*.......+++++++++++++++++@+@+@@@@++++@@+@@@@@@@@@@@@@@++.......*",
00108   "*.......+++++++++++++++++@@@+@@@@@+@+@@+@@@+@@+@@@@@@@++.......*",
00109   "*........++++++++++++++++@@@@+@@@++@++@+@@+@@+@@@+@@@++........*",
00110   "*........++++++++++++++++@@@@@@@@+@@+@@+@+@@+@@@+@@@@++........*",
00111   "*........+++++++++++++++++@@@@@@+@@+@@+@++++++++++@@@++........*",
00112   "*.........++##############+@@@@@@@@@@++++@@@@@@@@++@++.........*",
00113   "*.........++###############+@@@@@@@@++++@@@@@@@@@@@@++.........*",
00114   "*..........++###############++++++++@@@@@@@+@@+@@@@++..........*",
00115   "*..........++##################++@@@@@+@@@+@@+@@+@@++..........*",
00116   "*...........++#################++@@@@+@@@+@@+@@+@@++...........*",
00117   "*...........++#################++@@@+@@++++++++@@@++...........*",
00118   "*............++++++++++++++++++++@@@++++@@@@@@++@++............*",
00119   "*.............+++++++++++++++++++@@+++@@@@@@@@@@@+.............*",
00120   "*.............+++++++++++++++++++@+++@@@@@@@@@@@++.............*",
00121   "*..............++++++++++++++++++@++@@@@+@@@@@@++..............*",
00122   "*...............+++++++++++++++++@@@+@@+@@+@@@++...............*",
00123   "*................++++++++++++++++@@+@@+@@+@@@++................*",
00124   "*.................++###########++@@+++++++@@++.................*",
00125   "*..................++##########++@+++@@@@+@++..................*",
00126   "*...................++#########++@++@@@@@@++...................*",
00127   "*....................+++#######++@++@@@@+++....................*",
00128   "*.....................++++#####++@@@@@++++.....................*",
00129   "*.......................++++###++@@@++++.......................*",
00130   "*.........................++++#++@++++.........................*",
00131   "*...........................++++++++...........................*",
00132   "*.............................++++.............................*",
00133   "*..............................................................*",
00134   "****************************************************************"};
00135 
00136 void triangles()
00137 {
00138   /* two intersecting triangles */
00139   glBegin(GL_TRIANGLES);
00140 
00141   glColor3f(1., 0., 0.);
00142   glVertex3f(-1., 0.9, 0.);
00143   glColor4f(1., 1., 0., 0.1);
00144   glVertex3f(-1., 0., 0.);
00145   glColor4f(1., 0., 1., 1.0);
00146   glVertex3f(1., 0., 0.2);
00147 
00148   glColor3f(0., 1., 0.);
00149   glVertex3f(1., 0., 0.);
00150   glColor3f(0., 1., 1.);
00151   glVertex3f(1., 0.5, 0.);
00152   glColor3f(0., 1., 1.);
00153   glVertex3f(-1., 0.5, 0.1);
00154 
00155   glEnd();
00156 }
00157 
00158 void extras()
00159 {
00160   glColor3f(1., 0., 0.);
00161 
00162   glPointSize(1.);
00163   gl2psPointSize(1.);
00164   glBegin(GL_POINTS);
00165   glVertex3f(-1., 1.0, 0.);
00166   glEnd();
00167 
00168   glPointSize(3.);
00169   gl2psPointSize(3.);
00170   glBegin(GL_POINTS);
00171   glVertex3f(-0.8, 1.0, 0.);
00172   glEnd();
00173 
00174   glPointSize(5.);
00175   gl2psPointSize(5.);
00176   glBegin(GL_POINTS);
00177   glVertex3f(-0.6, 1.0, 0.);
00178   glEnd();
00179 
00180   glPointSize(7.);
00181   gl2psPointSize(7.);
00182   glBegin(GL_POINTS);
00183   glVertex3f(-0.4, 1.0, 0.);
00184   glEnd();
00185 
00186   glLineWidth(1.);
00187   gl2psLineWidth(1.);
00188   glBegin(GL_LINES);
00189   glVertex3f(-0.2, 1.05, 0.);
00190   glVertex3f(0.2, 1.05, 0.);
00191   glEnd();
00192 
00193   glEnable(GL_LINE_STIPPLE);
00194   glLineStipple(1, 0x087F);
00195   gl2psEnable(GL2PS_LINE_STIPPLE);
00196   glBegin(GL_LINES);
00197   glVertex3f(-0.2, 0.95, 0.);
00198   glVertex3f(0.2, 0.95, 0.);
00199   glEnd();
00200   glDisable(GL_LINE_STIPPLE);
00201   gl2psDisable(GL2PS_LINE_STIPPLE);
00202 
00203   glLineWidth(3.);
00204   gl2psLineWidth(3.);
00205   glBegin(GL_LINES);
00206   glVertex3f(0.4, 1.05, 0.);
00207   glVertex3f(0.8, 1.05, 0.);
00208   glEnd();
00209 
00210   glEnable(GL_LINE_STIPPLE);
00211   glLineStipple(2, 0x0F0F);
00212   /* glLineStipple(1, 0xAAAA); */
00213   gl2psEnable(GL2PS_LINE_STIPPLE);
00214   glBegin(GL_LINES);
00215   glVertex3f(0.4, 0.95, 0.);
00216   glVertex3f(0.8, 0.95, 0.);
00217   glEnd();
00218   glDisable(GL_LINE_STIPPLE);
00219   gl2psDisable(GL2PS_LINE_STIPPLE);
00220 
00221   glPointSize(1);
00222   gl2psPointSize(1);
00223   glLineWidth(1);
00224   gl2psLineWidth(1);
00225 }
00226 
00227 void objects()
00228 {
00229   glPushMatrix();
00230   glEnable(GL_LIGHTING);
00231   glRotatef(rotation, 2., 0., 1.);
00232   glColor4d(0.2, 0.2, 0.9, 0.2);
00233   if(teapot == GL_TRUE){
00234     glutSolidTeapot(0.7);
00235   }
00236   else{
00237     glutSolidTorus(0.3, 0.6, 20, 20);
00238   }
00239   glDisable(GL_LIGHTING);
00240   glPopMatrix();
00241 }
00242 
00243 void printstring(const char *string, float angle)
00244 {
00245   unsigned int i;
00246   const char *fonts[] =
00247     { "Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic",
00248       "Helvetica", "Helvetica-Bold", "Helvetica-Oblique", "Helvetica-BoldOblique",
00249       "Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique",
00250       "Symbol", "ZapfDingbats" };
00251 
00252   /* call gl2psText before the glut function since glutBitmapCharacter
00253      changes the raster position... */
00254   gl2psTextOpt(string, fonts[4], 12, GL2PS_TEXT_BL, angle);
00255 
00256   for (i = 0; i < strlen(string); i++)
00257     glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, string[i]);
00258 }
00259 
00260 void text()
00261 {
00262   double x = -1.25, y = -0.33, dy = 0.13;
00263 
00264   glColor3f(1., 1., 0.);
00265 
00266   glRasterPos2d(x, y);
00267   printstring("Press:", 0.);
00268   y -= dy;
00269   glRasterPos2d(x, y);
00270   printstring("  p: to change the print format (PS, EPS, PDF, ...)", 0.);
00271   y -= dy;
00272   glRasterPos2d(x, y);
00273   printstring("  s: to save the images", 0.);
00274   y -= dy;
00275   glRasterPos2d(x, y);
00276   printstring("  t: to alternate between teapot and torus", 0.);
00277   y -= dy;
00278   glRasterPos2d(x, y);
00279   printstring("  v: to alternate between single and multiple viewport modes", 0.);
00280   y -= dy;
00281   glRasterPos2d(x, y);
00282   printstring("  b: to change the blending mode (transparency)", 0.);
00283   y -= dy;
00284   glRasterPos2d(x, y);
00285   printstring("  q: to quit", 0.), 0.;
00286   y -= dy;
00287   glRasterPos2d(x, y);
00288   printstring("Click and move the mouse to rotate the objects", 0.);
00289 
00290   glRasterPos2d(0, 0.75);
00291   printstring("rotated!", 45.);
00292 
00293   glRasterPos2d(x, 1.15);
00294   printstring(format_string, 0.);
00295 
00296   gl2psSpecial(GL2PS_TEX, "% This should only be printed in LaTeX output!");
00297 }
00298 
00299 void cube()
00300 {
00301   glColor3d (0.0,1.0,0.);
00302   glBegin(GL_POLYGON);
00303   glVertex3d( 0.5,-0.5,-0.5);
00304   glColor4d (0.0,1.0,0.,0.2);
00305   glVertex3d( 0.5, 0.5,-0.5);
00306   glVertex3d(-0.5, 0.5,-0.5);
00307   glColor4d (0.0,1.0,0.,1);
00308   glVertex3d(-0.5,-0.5,-0.5);
00309   glEnd();
00310 
00311   glColor3d (1.0,0.0,0.);
00312   glBegin(GL_POLYGON);
00313   glColor4d (1.0,0.0,0.,0.1);
00314   glVertex3d( 0.5,-0.5,0.5);
00315   glColor4d (1.0,0.5,1.,0.9);
00316   glVertex3d( 0.5, 0.5,0.5);
00317   glVertex3d(-0.5, 0.5,0.5);
00318   glColor4d (1.0,0.5,1.,0.1);
00319   glVertex3d(-0.5,-0.5,0.5);
00320   glEnd();
00321   glLineWidth(4.0);
00322   glColor3d (1.0,1.0,0.);
00323   glBegin(GL_LINES);
00324   glVertex3d( 0.5,-0.5, 0.5);
00325   glVertex3d( 0.5,-0.5,-0.5);
00326   glVertex3d( 0.5, 0.5, 0.5);
00327   glVertex3d( 0.5, 0.5,-0.5);
00328   glVertex3d(-0.5, 0.5, 0.5);
00329   glVertex3d(-0.5, 0.5,-0.5);
00330   glVertex3d(-0.5,-0.5, 0.5);
00331   glVertex3d(-0.5,-0.5,-0.5);
00332   glEnd();
00333 }
00334 
00335 void image(float x, float y, GLboolean opaque)
00336 {
00337   int w = 64, h = 66, row, col, pos = 0;
00338   float *pixels, r = 0., g = 0., b = 0.;
00339 
00340   /* Fill a pixmap (each pixel contains three floats defining an RGB
00341      color) */
00342   pixels = (opaque == GL_TRUE)
00343     ? (float*)malloc(3 * w * h * sizeof(float))
00344     : (float*)malloc(4 * w * h * sizeof(float));
00345 
00346   for(row = h-1; row >= 0; row--){
00347     for(col = 0; col < w; col++){
00348       switch(pixmap[row][col]){
00349       case '.' : r = 255.; g = 255.; b = 255.; break;
00350       case '+' : r = 0.  ; g = 0.  ; b = 0.  ; break;
00351       case '@' : r = 255.; g = 209.; b = 0.  ; break;
00352       case '#' : r = 255.; g = 0.  ; b = 0.  ; break;
00353       case 'a' : r = 255.; g = 209.; b = 0.  ; break;
00354       case '*' : r = 0.;   g = 0.  ; b = 20. ; break;
00355       }
00356       r /= 255.; g /= 255.; b /= 255.;
00357       pixels[pos] = r; pos++;
00358       pixels[pos] = g; pos++;
00359       pixels[pos] = b; pos++;
00360 
00361       if(opaque)
00362         continue;
00363 
00364       switch(pixmap[row][col]){
00365       case '.' : pixels[pos] = col / (float)w ; break;
00366       case 'a' : pixels[pos] = 1 - col / ((float)w - 7)  ; break;
00367       default  : pixels[pos] = 1.  ; break;
00368       }
00369       pos++;
00370     }
00371   }
00372 
00373   glRasterPos2f(x, y);
00374 
00375   /* call gl2psDrawPixels first since glDrawPixels can change the
00376      raster position */
00377   if(opaque){
00378     gl2psDrawPixels((GLsizei)w, (GLsizei)h, 0, 0, GL_RGB, GL_FLOAT, pixels);
00379     glDrawPixels((GLsizei)w, (GLsizei)h, GL_RGB, GL_FLOAT, pixels);
00380   }
00381   else{
00382     gl2psDrawPixels((GLsizei)w, (GLsizei)h, 0, 0, GL_RGBA, GL_FLOAT, pixels);
00383     glDrawPixels((GLsizei)w, (GLsizei)h, GL_RGBA, GL_FLOAT, pixels);
00384   }
00385   free(pixels);
00386 }
00387 
00388 /* A simple drawing function, using the default viewport */
00389 void draw_single()
00390 {
00391   glScissor(0, 0, window_w, window_h);
00392   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00393   triangles();
00394   extras();
00395   objects();
00396   text();
00397   glFlush();
00398 }
00399 
00400 /* A more complex drawing function, using 2 separate viewports */
00401 void draw_multi()
00402 {
00403   GLint viewport[4];
00404 
00405   glScissor(0, 0, window_w, window_h);
00406   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00407 
00408   /* First viewport with triangles, teapot or torus, etc. */
00409   glViewport((GLint)(window_w * 0.05), (GLint)(window_h * 0.525),
00410              (GLsizei)(window_w * 0.9), (GLsizei)(window_h * 0.45));
00411   glScissor((GLint)(window_w * 0.05), (GLint)(window_h * 0.525),
00412             (GLsizei)(window_w * 0.9), (GLsizei)(window_h * 0.45));
00413   glClearColor(0.2, 0.2, 0.2, 0.);
00414   glGetIntegerv(GL_VIEWPORT, viewport);
00415 
00416   gl2psBeginViewport(viewport);
00417 
00418   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00419   glMatrixMode(GL_PROJECTION);
00420   glLoadIdentity();
00421   glOrtho(-1.3,1.3, -1.3,1.3, -1.3,1.3);
00422   glMatrixMode(GL_MODELVIEW);
00423 
00424   objects();
00425   triangles();
00426   extras();
00427   text();
00428 
00429   gl2psEndViewport();
00430 
00431   /* Second viewport with cube, image, etc. */
00432   glViewport((GLint)(window_w * 0.05), (GLint)(window_h * 0.025),
00433              (GLsizei)(window_w * 0.9), (GLsizei)(window_h * 0.45));
00434   glScissor((GLint)(window_w * 0.05), (GLint)(window_h * 0.025),
00435              (GLsizei)(window_w * 0.9), (GLsizei)(window_h * 0.45));
00436   glClearColor(0.8, 0.8, 0.8, 0.);
00437   glGetIntegerv(GL_VIEWPORT, viewport);
00438 
00439   gl2psBeginViewport(viewport);
00440 
00441   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00442   glMatrixMode(GL_PROJECTION);
00443   glLoadIdentity();
00444   glOrtho(-1.3,1.3, -1.3,1.3, -1.3,1.3);
00445   glMatrixMode(GL_MODELVIEW);
00446 
00447   glPushMatrix();
00448   glRotatef(rotation, 1., 1., 1.);
00449   image(-0.8, -0.3, GL_TRUE);
00450   cube();
00451   extras();
00452   image(-0.8, 0.4, GL_FALSE);
00453   glPopMatrix();
00454 
00455   gl2psEndViewport();
00456 
00457   glClearColor(0.5, 0.5, 0.5, 0.);
00458   glFlush();
00459 }
00460 
00461 void display()
00462 {
00463   GLfloat spec[4] = {0.6, 0.6, 0.6, 1.0};
00464   glEnable(GL_DEPTH_TEST);
00465   glDepthFunc(GL_LESS);
00466   glShadeModel(GL_SMOOTH);
00467   glEnable(GL_LIGHT0);
00468   glEnable(GL_SCISSOR_TEST);
00469   glEnable(GL_COLOR_MATERIAL);
00470   glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
00471   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
00472   glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60);
00473   if(blend){
00474     glEnable(GL_BLEND);
00475     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00476   }
00477   else{
00478     glDisable(GL_BLEND);
00479   }
00480   if(display_multi == GL_TRUE){
00481     draw_multi();
00482   }
00483   else{
00484     draw_single();
00485   }
00486 }
00487 
00488 void reshape(int w, int h)
00489 {
00490   window_w = w;
00491   window_h = h;
00492 
00493   glViewport(0, 0, (GLsizei)window_w, (GLsizei)window_h);
00494   glMatrixMode(GL_PROJECTION);
00495   glLoadIdentity();
00496   glOrtho(-1.3,1.3, -1.3,1.3, -1.3,1.3);
00497   glMatrixMode(GL_MODELVIEW);
00498 
00499   glClearColor(0.5, 0.5, 0.5, 0.);
00500   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00501 }
00502 
00503 void writefile(int format, int sort, int options, int nbcol,
00504                const char *filename, const char *extension)
00505 {
00506   FILE *fp;
00507   char file[256];
00508   int state = GL2PS_OVERFLOW, buffsize = 0;
00509   GLint viewport[4];
00510 
00511   strcpy(file, filename);
00512   strcat(file, ".");
00513   strcat(file, extension);
00514 
00515   viewport[0] = 0;
00516   viewport[1] = 0;
00517   viewport[2] = window_w;
00518   viewport[3] = window_h;
00519 
00520   fp = fopen(file, "wb");
00521 
00522   if(!fp){
00523     printf("Unable to open file %s for writing\n", file);
00524     exit(1);
00525   }
00526 
00527   printf("Saving image to file %s... ", file);
00528   fflush(stdout);
00529 
00530   while(state == GL2PS_OVERFLOW){
00531     buffsize += 1024*1024;
00532     gl2psBeginPage(file, "gl2psTest", viewport, format, sort, options,
00533                    GL_RGBA, 0, NULL, nbcol, nbcol, nbcol,
00534                    buffsize, fp, file);
00535     display();
00536     state = gl2psEndPage();
00537   }
00538 
00539   fclose(fp);
00540 
00541   printf("Done!\n");
00542   fflush(stdout);
00543 }
00544 
00545 void keyboard(unsigned char key, int x, int y)
00546 {
00547   int opt;
00548   char ext[32];
00549   static int format = GL2PS_PS;
00550 
00551   switch(key){
00552   case 27:
00553   case 'q':
00554     exit(0);
00555     break;
00556   case 't':
00557     teapot = (teapot) ? GL_FALSE : GL_TRUE;
00558     display();
00559     break;
00560   case 'p':
00561     if     (format == GL2PS_PS)  format = GL2PS_EPS;
00562     else if(format == GL2PS_EPS) format = GL2PS_TEX;
00563     else if(format == GL2PS_TEX) format = GL2PS_PDF;
00564     else if(format == GL2PS_PDF) format = GL2PS_SVG;
00565     else if(format == GL2PS_SVG) format = GL2PS_PGF;
00566     else                         format = GL2PS_PS;
00567     format_string = gl2psGetFormatDescription(format);
00568     display();
00569     break;
00570   case 'b':
00571     blend = !blend;
00572     display();
00573     break;
00574   case 'v':
00575     display_multi = display_multi ? GL_FALSE : GL_TRUE;
00576     reshape(window_w, window_h);
00577     display();
00578     break;
00579   case 's':
00580     strcpy(ext, gl2psGetFileExtension(format));
00581 
00582     opt = GL2PS_DRAW_BACKGROUND;
00583     writefile(format, GL2PS_SIMPLE_SORT, opt, 0, "outSimple", ext);
00584 
00585     opt = GL2PS_DRAW_BACKGROUND | GL2PS_OCCLUSION_CULL;
00586     writefile(format, GL2PS_SIMPLE_SORT, opt, 0, "outSimpleCulled", ext);
00587 
00588     opt = GL2PS_DRAW_BACKGROUND | GL2PS_NO_PS3_SHADING | GL2PS_TIGHT_BOUNDING_BOX;
00589     writefile(format, GL2PS_SIMPLE_SORT, opt, 2, "outSimpleShading2", ext);
00590     writefile(format, GL2PS_SIMPLE_SORT, opt, 8, "outSimpleShading8", ext);
00591     writefile(format, GL2PS_SIMPLE_SORT, opt, 16, "outSimpleShading16", ext);
00592 
00593     opt = GL2PS_NO_TEXT;
00594     writefile(format, GL2PS_SIMPLE_SORT, opt, 0, "outSimpleNoText", ext);
00595 
00596     opt = GL2PS_DRAW_BACKGROUND | GL2PS_BEST_ROOT;
00597     writefile(format, GL2PS_BSP_SORT, opt, 0, "outBsp", ext);
00598 
00599     opt = GL2PS_DRAW_BACKGROUND | GL2PS_OCCLUSION_CULL | GL2PS_BEST_ROOT;
00600     writefile(format, GL2PS_BSP_SORT, opt, 0, "outBspCulled", ext);
00601 
00602 #ifdef GL2PS_HAVE_ZLIB
00603     opt = GL2PS_DRAW_BACKGROUND | GL2PS_OCCLUSION_CULL | GL2PS_BEST_ROOT | GL2PS_COMPRESS;
00604     if(format == GL2PS_PS || format == GL2PS_EPS) strcat(ext, ".gz");
00605     else if(format == GL2PS_SVG) strcat(ext, "z");
00606     writefile(format, GL2PS_BSP_SORT, opt, 0, "outBspCulledCompressed", ext);
00607 #endif
00608 
00609     printf("GL2PS %d.%d.%d%s done with all images\n", GL2PS_MAJOR_VERSION,
00610            GL2PS_MINOR_VERSION, GL2PS_PATCH_VERSION, GL2PS_EXTRA_VERSION);
00611     break;
00612   }
00613 }
00614 
00615 void motion(int x, int y)
00616 {
00617   rotation += 10.;
00618   display();
00619 }
00620 
00621 int main(int argc, char **argv)
00622 {
00623   glutInit(&argc, argv);
00624   glutInitDisplayMode(GLUT_SINGLE | GLUT_DEPTH);
00625   glutInitWindowSize(400, 600);
00626   glutInitWindowPosition(100, 100);
00627   glutCreateWindow(argv[0]);
00628   glutDisplayFunc(display);
00629   glutReshapeFunc(reshape);
00630   glutKeyboardFunc(keyboard);
00631   glutMotionFunc(motion);
00632   glutMainLoop();
00633   return 0;
00634 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines