summaryrefslogtreecommitdiffstats
path: root/src/tests/line-sample.c
blob: d55b0768505b7f77e34a0a33b9e49c92163c81fe (plain)
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
/*
 * Draw samples of wide lines, smooth lines and stippled lines in
 * all combinations.
 *
 * Brian Paul
 * 13 Feb 2015
 */


#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <GL/glew.h> // just for GL_ALIASED_LINE_WIDTH_RANGE
#include "glut_wrap.h"


static int w, h;
static float g, lw = 3.0;

static GLboolean Blend = GL_TRUE;
static GLboolean FlatShade = GL_TRUE;


static void
draw_line_sample(GLboolean smooth, GLboolean stipple, GLfloat width)
{
   float r0 = 0.4;
   float r1 = 0.8;
   float r2 = 0.9;
   float r3 = 0.95;
   int i;

   glLineWidth(width);

   if (stipple) {
      glEnable(GL_LINE_STIPPLE);
   }
   else {
      glDisable(GL_LINE_STIPPLE);
   }

   if (smooth) {
      glEnable(GL_LINE_SMOOTH);
      if (Blend)
         glEnable(GL_BLEND);
      else
         glDisable(GL_BLEND);
    }
   else {
      glDisable(GL_LINE_SMOOTH);
      glDisable(GL_BLEND);
   }

   /* spokes */
   glBegin(GL_LINES);
   for (i = 0; i < 360; i += 10) {
      float x0 = r0 * cos(i * M_PI / 180.0);
      float y0 = r0 * sin(i * M_PI / 180.0);
      float x1 = r1 * cos(i * M_PI / 180.0);
      float y1 = r1 * sin(i * M_PI / 180.0);

      glColor3f(.5, .5, 1);
      glVertex2f(x0, y0);
      glColor3f(1, 1, 1);
      glVertex2f(x1, y1);
   }
   glEnd();

   /* circle */
   glBegin(GL_LINE_STRIP);
   for (i = 0; i <= 360; i += 10) {
      float x = r2 * cos(i * M_PI / 180.0);
      float y = r2 * sin(i * M_PI / 180.0);

      glVertex2f(x, y);
   }
   glEnd();

   /* box */
   glBegin(GL_LINE_STRIP);
   glVertex2f(-r3, -r3);
   glVertex2f( r3, -r3);
   glVertex2f( r3,  r3);
   glVertex2f(-r3,  r3);
   glVertex2f(-r3, -r3);
   glEnd();
}


static void
Init(void)
{
   GLfloat range[2];
   char title[1000];

   printf("GL_RENDERER = %s\n", (const char *) glGetString(GL_RENDERER));
   printf("GL_VERSION = %s\n", (const char *) glGetString(GL_VERSION));

   glGetFloatv(GL_ALIASED_LINE_WIDTH_RANGE, range);
   printf("Aliased line width range %.5f .. %.5f\n", range[0], range[1]);
   glGetFloatv(GL_LINE_WIDTH_RANGE, range);
   printf("Smooth line width range %.5f .. %.5f\n", range[0], range[1]);
   glGetFloatv(GL_LINE_WIDTH_GRANULARITY, &g);
   printf("line width granularity %f\n", g);
   fflush(stdout);

   glClearColor(0.0, 0.0, 0.0, 0.0);

   glBlendFunc(GL_SRC_ALPHA, GL_ONE);
   /* pattern: |- -- --- ----   | */
   glLineStipple(2, 0x1eed);

   sprintf(title, "Line Sampler (%s)", (const char *) glGetString(GL_RENDERER));
   glutSetWindowTitle(title);
}


static void
Draw(void)
{
   int i;

   glClearColor(0.2, 0.2, 0.2, 0.2);
   glClear(GL_COLOR_BUFFER_BIT);

   glShadeModel(FlatShade ? GL_FLAT : GL_SMOOTH);

   for (i = 0; i < 2; i++) {
      float width = i ? lw : 1.0;
      glViewport(0*w, i*h, w, h);
      draw_line_sample(GL_FALSE, GL_FALSE, width);
      glViewport(1*w, i*h, w, h);
      draw_line_sample(GL_TRUE, GL_FALSE, width);
      glViewport(2*w, i*h, w, h);
      draw_line_sample(GL_FALSE, GL_TRUE, width);
      glViewport(3*w, i*h, w, h);
      draw_line_sample(GL_TRUE, GL_TRUE, width);
   }

    glutSwapBuffers();
}


static void
Reshape(int width, int height)
{
   w = width / 4;
   h = height / 2;
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}


static void
Key(unsigned char key, int x, int y)
{
   if (key == 'w') {
      lw -= g;
      if (lw < g)
         lw = g;
   }
   else if (key == 'W') {
      lw += g;
   }
   else if (key == 'f') {
      FlatShade = !FlatShade;
   }
   else if (key == 'b') {
      Blend = !Blend;
   }
   if (key == 27)
      exit(0);

   printf("line width %.5f  FlatShade = %d  Blend = %d\n", lw, FlatShade, Blend);
   fflush(stdout);

   glutPostRedisplay();
}


int
main(int argc, char **argv)
{
   glutInit(&argc, argv);

   glutInitWindowPosition(0, 0);
   glutInitWindowSize(1000, 500);

   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);

   if (!glutCreateWindow("Line Sampler")) {
      exit(1);
   }

   Init();

   glutReshapeFunc(Reshape);
   glutKeyboardFunc(Key);
   glutDisplayFunc(Draw);
   glutMainLoop();

   return 0;
}