1/*
2 * Draw samples of wide lines, smooth lines and stippled lines in
3 * all combinations.
4 *
5 * Brian Paul
6 * 13 Feb 2015
7 */
8
9
10#include <math.h>
11#include <stdio.h>
12#include <string.h>
13#include <stdlib.h>
14#include <GL/glew.h> // just for GL_ALIASED_LINE_WIDTH_RANGE
15#include "glut_wrap.h"
16
17
18static int w, h;
19static float g, lw = 3.0;
20
21static GLboolean Blend = GL_TRUE;
22static GLboolean FlatShade = GL_TRUE;
23
24
25static void
26draw_line_sample(GLboolean smooth, GLboolean stipple, GLfloat width)
27{
28   float r0 = 0.4;
29   float r1 = 0.8;
30   float r2 = 0.9;
31   float r3 = 0.95;
32   int i;
33
34   glLineWidth(width);
35
36   if (stipple) {
37      glEnable(GL_LINE_STIPPLE);
38   }
39   else {
40      glDisable(GL_LINE_STIPPLE);
41   }
42
43   if (smooth) {
44      glEnable(GL_LINE_SMOOTH);
45      if (Blend)
46         glEnable(GL_BLEND);
47      else
48         glDisable(GL_BLEND);
49    }
50   else {
51      glDisable(GL_LINE_SMOOTH);
52      glDisable(GL_BLEND);
53   }
54
55   /* spokes */
56   glBegin(GL_LINES);
57   for (i = 0; i < 360; i += 10) {
58      float x0 = r0 * cos(i * M_PI / 180.0);
59      float y0 = r0 * sin(i * M_PI / 180.0);
60      float x1 = r1 * cos(i * M_PI / 180.0);
61      float y1 = r1 * sin(i * M_PI / 180.0);
62
63      glColor3f(.5, .5, 1);
64      glVertex2f(x0, y0);
65      glColor3f(1, 1, 1);
66      glVertex2f(x1, y1);
67   }
68   glEnd();
69
70   /* circle */
71   glBegin(GL_LINE_STRIP);
72   for (i = 0; i <= 360; i += 10) {
73      float x = r2 * cos(i * M_PI / 180.0);
74      float y = r2 * sin(i * M_PI / 180.0);
75
76      glVertex2f(x, y);
77   }
78   glEnd();
79
80   /* box */
81   glBegin(GL_LINE_STRIP);
82   glVertex2f(-r3, -r3);
83   glVertex2f( r3, -r3);
84   glVertex2f( r3,  r3);
85   glVertex2f(-r3,  r3);
86   glVertex2f(-r3, -r3);
87   glEnd();
88}
89
90
91static void
92Init(void)
93{
94   GLfloat range[2];
95   char title[1000];
96
97   printf("GL_RENDERER = %s\n", (const char *) glGetString(GL_RENDERER));
98   printf("GL_VERSION = %s\n", (const char *) glGetString(GL_VERSION));
99
100   glGetFloatv(GL_ALIASED_LINE_WIDTH_RANGE, range);
101   printf("Aliased line width range %.5f .. %.5f\n", range[0], range[1]);
102   glGetFloatv(GL_LINE_WIDTH_RANGE, range);
103   printf("Smooth line width range %.5f .. %.5f\n", range[0], range[1]);
104   glGetFloatv(GL_LINE_WIDTH_GRANULARITY, &g);
105   printf("line width granularity %f\n", g);
106   fflush(stdout);
107
108   glClearColor(0.0, 0.0, 0.0, 0.0);
109
110   glBlendFunc(GL_SRC_ALPHA, GL_ONE);
111   /* pattern: |- -- --- ----   | */
112   glLineStipple(2, 0x1eed);
113
114   sprintf(title, "Line Sampler (%s)", (const char *) glGetString(GL_RENDERER));
115   glutSetWindowTitle(title);
116}
117
118
119static void
120Draw(void)
121{
122   int i;
123
124   glClearColor(0.2, 0.2, 0.2, 0.2);
125   glClear(GL_COLOR_BUFFER_BIT);
126
127   glShadeModel(FlatShade ? GL_FLAT : GL_SMOOTH);
128
129   for (i = 0; i < 2; i++) {
130      float width = i ? lw : 1.0;
131      glViewport(0*w, i*h, w, h);
132      draw_line_sample(GL_FALSE, GL_FALSE, width);
133      glViewport(1*w, i*h, w, h);
134      draw_line_sample(GL_TRUE, GL_FALSE, width);
135      glViewport(2*w, i*h, w, h);
136      draw_line_sample(GL_FALSE, GL_TRUE, width);
137      glViewport(3*w, i*h, w, h);
138      draw_line_sample(GL_TRUE, GL_TRUE, width);
139   }
140
141    glutSwapBuffers();
142}
143
144
145static void
146Reshape(int width, int height)
147{
148   w = width / 4;
149   h = height / 2;
150   glMatrixMode(GL_PROJECTION);
151   glLoadIdentity();
152   glMatrixMode(GL_MODELVIEW);
153   glLoadIdentity();
154}
155
156
157static void
158Key(unsigned char key, int x, int y)
159{
160   if (key == 'w') {
161      lw -= g;
162      if (lw < g)
163         lw = g;
164   }
165   else if (key == 'W') {
166      lw += g;
167   }
168   else if (key == 'f') {
169      FlatShade = !FlatShade;
170   }
171   else if (key == 'b') {
172      Blend = !Blend;
173   }
174   if (key == 27)
175      exit(0);
176
177   printf("line width %.5f  FlatShade = %d  Blend = %d\n", lw, FlatShade, Blend);
178   fflush(stdout);
179
180   glutPostRedisplay();
181}
182
183
184int
185main(int argc, char **argv)
186{
187   glutInit(&argc, argv);
188
189   glutInitWindowPosition(0, 0);
190   glutInitWindowSize(1000, 500);
191
192   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
193
194   if (!glutCreateWindow("Line Sampler")) {
195      exit(1);
196   }
197
198   Init();
199
200   glutReshapeFunc(Reshape);
201   glutKeyboardFunc(Key);
202   glutDisplayFunc(Draw);
203   glutMainLoop();
204
205   return 0;
206}
207