nice logo
[censuslogo] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <assert.h>
5 #include <GL/glut.h>
6 #include "logo.h"
7
8 #define MSAA
9
10 int init(void);
11 void display(void);
12 void reshape(int x, int y);
13 void keyb(unsigned char key, int x, int y);
14
15 int nverts = 256;
16
17 int main(int argc, char **argv)
18 {
19         unsigned int flags = GLUT_RGB | GLUT_DOUBLE;
20 #ifdef MSAA
21         flags |= GLUT_MULTISAMPLE;
22 #endif
23
24         glutInit(&argc, argv);
25         glutInitWindowSize(800, 600);
26         glutInitDisplayMode(flags);
27         glutCreateWindow("census");
28
29         glutDisplayFunc(display);
30         glutReshapeFunc(reshape);
31         glutKeyboardFunc(keyb);
32
33         if(init() == -1) {
34                 return 1;
35         }
36
37         glutMainLoop();
38         return 0;
39 }
40
41 int init(void)
42 {
43         if(init_logo("data/census.curves") == -1) {
44                 return -1;
45         }
46
47 #ifdef MSAA
48         glEnable(GL_MULTISAMPLE);
49 #endif
50
51         return 0;
52 }
53
54 static void draw_disc(float x, float y, float rad, int sub)
55 {
56         int i;
57         glBegin(GL_TRIANGLE_FAN);
58         glVertex2f(x, y);
59         for(i=0; i<sub; i++) {
60                 float t = (float)i / (float)(sub - 1);
61                 float theta = t * M_PI * 2.0;
62                 glVertex2f(cos(theta) * rad + x, sin(theta) * rad + y);
63         }
64         glEnd();
65 }
66
67 static void draw_line(float x0, float y0, float x1, float y1, float rad)
68 {
69         float dx, dy, rx, ry, len;
70
71         dx = x1 - x0;
72         dy = y1 - y0;
73         len = sqrt(dx * dx + dy * dy);
74
75         rx = rad * dy / len;
76         ry = -rad * dx / len;
77
78         draw_disc(x0, y0, rad, 8);
79         draw_disc(x1, y1, rad, 8);
80
81         glBegin(GL_QUADS);
82         glVertex2f(x0 + rx, y0 + ry);
83         glVertex2f(x1 + rx, y1 + ry);
84         glVertex2f(x1 - rx, y1 - ry);
85         glVertex2f(x0 - rx, y0 - ry);
86         glEnd();
87 }
88
89 void display(void)
90 {
91         int i;
92         float a[2], b[2], dt = 1.0f / (float)(nverts - 1);
93
94         glClear(GL_COLOR_BUFFER_BIT);
95
96         glLineWidth(5.0);
97
98         glColor3f(1, 1, 1);
99         for(i=0; i<nverts-1; i++) {
100                 float t0 = (float)i * dt;
101                 float t1 = (float)(i + 1) * dt;
102                 eval_logo(a, t0);
103                 eval_logo(b, t1);
104                 draw_line(a[0], a[1], b[0], b[1], 0.02);
105         }
106
107         eval_logo(a, 0);
108         eval_logo(b, 1);
109         draw_disc(a[0], a[1], 0.05, 18);
110         draw_disc(b[0], b[1], 0.05, 18);
111
112         glColor3f(0.8, 0, 0);
113         draw_disc(0, 0, 0.14, 24);
114
115         glutSwapBuffers();
116         assert(glGetError() == GL_NO_ERROR);
117 }
118
119 void reshape(int x, int y)
120 {
121         float aspect = (float)x / (float)y;
122
123         glViewport(0, 0, x, y);
124         glMatrixMode(GL_PROJECTION);
125         glLoadIdentity();
126         glScalef(1.0f / aspect, 1.0f, 1.0f);
127 }
128
129 void keyb(unsigned char key, int x, int y)
130 {
131         switch(key) {
132         case 27:
133                 exit(0);
134
135         case '-':
136                 nverts -= 8;
137                 printf("nverts: %d\n", nverts);
138                 glutPostRedisplay();
139                 break;
140
141         case '=':
142                 nverts += 8;
143                 printf("nverts: %d\n", nverts);
144                 glutPostRedisplay();
145                 break;
146         }
147 }