1 /*
2  * GL2PS, an OpenGL to PostScript Printing Library
3  * Copyright (C) 1999-2017 Christophe Geuzaine <geuz@geuz.org>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of either:
7  *
8  * a) the GNU Library General Public License as published by the Free
9  * Software Foundation, either version 2 of the License, or (at your
10  * option) any later version; or
11  *
12  * b) the GL2PS License as published by Christophe Geuzaine, either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either
18  * the GNU Library General Public License or the GL2PS License for
19  * more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library in the file named "COPYING.LGPL";
23  * if not, write to the Free Software Foundation, Inc., 51 Franklin
24  * Street, Fifth Floor, Boston, MA 02110-1301, USA.
25  *
26  * You should have received a copy of the GL2PS License with this
27  * library in the file named "COPYING.GL2PS"; if not, I will be glad
28  * to provide one.
29  *
30  * For the latest info about gl2ps and a full list of contributors,
31  * see http://www.geuz.org/gl2ps/.
32  *
33  * Please report all bugs and problems to <gl2ps@geuz.org>.
34  */
35 
36 /*
37   To compile on Linux:
38   gcc gl2psTestSimple.c gl2ps.c -lglut -lGL -lGLU -lX11 -lm
39 
40   To compile on MacOSX:
41   gcc gl2psTestSimple.c gl2ps.c -framework OpenGL -framework GLUT -framework Cocoa
42 */
43 
44 #ifdef __APPLE__
45 #  include <GLUT/glut.h>
46 #else
47 #  include <GL/glut.h>
48 #endif
49 
50 #include <string.h>
51 #include "gl2ps.h"
52 
display(void)53 static void display(void)
54 {
55   unsigned int i;
56   unsigned int N = 50;
57   const char *help = "Press 's' to save image or 'q' to quit";
58 
59   glClearColor(0.3, 0.5, 0.8, 0.);
60   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
61 
62   /* draw a smooth-shaded torus */
63   glPushMatrix();
64   glRotatef(-60., 2., 0., 1.);
65   glEnable(GL_LIGHTING);
66   glutSolidTorus(0.3, 0.6, 30, 30);
67   glDisable(GL_LIGHTING);
68   glPopMatrix();
69 
70 
71   /* draw three triangles on the same zplane and use polygon offset
72      to order the layers: the grey triangle should be drawn on the
73      middle layer */
74   glEnable(GL_POLYGON_OFFSET_FILL);
75   glPolygonOffset(1.0, 1.0);
76   gl2psEnable(GL2PS_POLYGON_OFFSET_FILL);
77   glColor3f(0.,0.,0.);
78   glBegin(GL_TRIANGLES);
79   glVertex3f(0.6, 0.8, 0);
80   glVertex3f(0.8, 0.8, 0);
81   glVertex3f(0.7, 0.92, 0);
82   glEnd();
83 
84   glPolygonOffset(2.0, 2.0);
85   gl2psEnable(GL2PS_POLYGON_OFFSET_FILL);
86   glColor3f(1.,1.,1.);
87   glBegin(GL_TRIANGLES);
88   glVertex3f(0.7, 0.8, 0);
89   glVertex3f(0.9, 0.8, 0);
90   glVertex3f(0.8, 0.92, 0);
91   glEnd();
92 
93   glPolygonOffset(1.5, 1.5);
94   gl2psEnable(GL2PS_POLYGON_OFFSET_FILL);
95   glColor3f(0.5,0.5,0.5);
96   glBegin(GL_TRIANGLES);
97   glVertex3f(0.65, 0.86, 0);
98   glVertex3f(0.85, 0.86, 0);
99   glVertex3f(0.75, 0.98, 0);
100   glEnd();
101 
102   glDisable(GL_POLYGON_OFFSET_FILL);
103   gl2psDisable(GL2PS_POLYGON_OFFSET_FILL);
104 
105   glColor3f(0.1,0.1,0.1);
106 
107   /* Draw 3 broken lines to show line cap an line join features (which have
108      no opengl counterpart) */
109   glLineWidth(6.);
110   gl2psLineWidth (6.);
111 
112   gl2psLineCap (GL2PS_LINE_CAP_BUTT);
113   gl2psLineJoin (GL2PS_LINE_JOIN_MITER);
114   glBegin(GL_LINE_STRIP);
115   glVertex3f(-0.9, 0.8, 0);
116   glVertex3f(-0.75, 0.98, 0);
117   glVertex3f(-0.6, 0.8, 0);
118   glEnd();
119 
120   gl2psLineCap (GL2PS_LINE_CAP_ROUND);
121   gl2psLineJoin (GL2PS_LINE_JOIN_ROUND);
122   glBegin(GL_LINE_STRIP);
123   glVertex3f(-0.5, 0.8, 0);
124   glVertex3f(-0.35, 0.98, 0);
125   glVertex3f(-0.2, 0.8, 0);
126   glEnd();
127 
128   gl2psLineCap (GL2PS_LINE_CAP_SQUARE);
129   gl2psLineJoin (GL2PS_LINE_JOIN_BEVEL);
130   glBegin(GL_LINE_STRIP);
131   glVertex3f(0.2, 0.8, 0);
132   glVertex3f(0.35, 0.98, 0);
133   glVertex3f(0.5, 0.8, 0);
134   glEnd();
135 
136   /* draw a stippled line with many small segments (this tests the
137      ability of gl2ps to render lines using as few strokes as
138      possible) */
139   glLineWidth(1.);
140   gl2psLineWidth(1.);
141   glEnable(GL_LINE_STIPPLE);
142   glLineStipple(1, 0x087F);
143   gl2psEnable(GL2PS_LINE_STIPPLE);
144   glBegin(GL_LINE_STRIP);
145   for(i = 0; i < N; i++)
146     glVertex3f(-0.75 + 1.5 * (double)i/(double)(N - 1), 0.75, -0.9);
147   glEnd();
148   glDisable(GL_LINE_STIPPLE);
149   gl2psDisable(GL2PS_LINE_STIPPLE);
150 
151   /* draw a text string */
152   glRasterPos2d(-0.9,-0.9);
153   gl2psText(help, "Times-Roman", 24);
154   for (i = 0; i < strlen(help); i++)
155     glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, help[i]);
156 
157   glFlush();
158 }
159 
keyboard(unsigned char key,int x,int y)160 static void keyboard(unsigned char key, int x, int y)
161 {
162   FILE *fp;
163   int state = GL2PS_OVERFLOW, buffsize = 0;
164 
165   (void) x; (void) y;  /* not used */
166   switch(key){
167   case 'q':
168     exit(0);
169     break;
170   case 's':
171     fp = fopen("out.eps", "wb");
172     printf("Writing 'out.eps'... ");
173     while(state == GL2PS_OVERFLOW){
174       buffsize += 1024*1024;
175       gl2psBeginPage("test", "gl2psTestSimple", NULL, GL2PS_EPS, GL2PS_SIMPLE_SORT,
176                      GL2PS_DRAW_BACKGROUND | GL2PS_USE_CURRENT_VIEWPORT,
177                      GL_RGBA, 0, NULL, 0, 0, 0, buffsize, fp, "out.eps");
178       display();
179       state = gl2psEndPage();
180     }
181     fclose(fp);
182     printf("Done!\n");
183     break;
184   }
185 }
186 
main(int argc,char ** argv)187 int main(int argc, char **argv)
188 {
189   GLfloat pos[4] = {1., 1., -1., 0.};
190 
191   glutInit(&argc, argv);
192   glutInitDisplayMode(GLUT_DEPTH);
193   glutInitWindowSize(400, 400);
194   glutInitWindowPosition(100, 100);
195   glutCreateWindow(argv[0]);
196 
197   glEnable(GL_DEPTH_TEST);
198   glDepthFunc(GL_LESS);
199   glShadeModel(GL_SMOOTH);
200   glEnable(GL_LIGHT0);
201   glLightfv(GL_LIGHT0, GL_POSITION, pos);
202 
203   glutDisplayFunc(display);
204   glutKeyboardFunc(keyboard);
205   glutMainLoop();
206   return 0;
207 }
208