nice logo
[censuslogo] / src / main.c
index 06477c1..125c2db 100644 (file)
@@ -1,23 +1,29 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <math.h>
 #include <assert.h>
 #include <GL/glut.h>
 #include "logo.h"
 
-#define BEZ_SEG        16
+#define MSAA
 
 int init(void);
 void display(void);
 void reshape(int x, int y);
 void keyb(unsigned char key, int x, int y);
 
-void draw_bezier(float *cp, int sub);
+int nverts = 256;
 
 int main(int argc, char **argv)
 {
+       unsigned int flags = GLUT_RGB | GLUT_DOUBLE;
+#ifdef MSAA
+       flags |= GLUT_MULTISAMPLE;
+#endif
+
        glutInit(&argc, argv);
        glutInitWindowSize(800, 600);
-       glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
+       glutInitDisplayMode(flags);
        glutCreateWindow("census");
 
        glutDisplayFunc(display);
@@ -34,27 +40,77 @@ int main(int argc, char **argv)
 
 int init(void)
 {
-       glEnable(GL_LINE_SMOOTH);
+       if(init_logo("data/census.curves") == -1) {
+               return -1;
+       }
+
+#ifdef MSAA
+       glEnable(GL_MULTISAMPLE);
+#endif
+
        return 0;
 }
 
+static void draw_disc(float x, float y, float rad, int sub)
+{
+       int i;
+       glBegin(GL_TRIANGLE_FAN);
+       glVertex2f(x, y);
+       for(i=0; i<sub; i++) {
+               float t = (float)i / (float)(sub - 1);
+               float theta = t * M_PI * 2.0;
+               glVertex2f(cos(theta) * rad + x, sin(theta) * rad + y);
+       }
+       glEnd();
+}
+
+static void draw_line(float x0, float y0, float x1, float y1, float rad)
+{
+       float dx, dy, rx, ry, len;
+
+       dx = x1 - x0;
+       dy = y1 - y0;
+       len = sqrt(dx * dx + dy * dy);
+
+       rx = rad * dy / len;
+       ry = -rad * dx / len;
+
+       draw_disc(x0, y0, rad, 8);
+       draw_disc(x1, y1, rad, 8);
+
+       glBegin(GL_QUADS);
+       glVertex2f(x0 + rx, y0 + ry);
+       glVertex2f(x1 + rx, y1 + ry);
+       glVertex2f(x1 - rx, y1 - ry);
+       glVertex2f(x0 - rx, y0 - ry);
+       glEnd();
+}
+
 void display(void)
 {
-       int i, nseg;
+       int i;
+       float a[2], b[2], dt = 1.0f / (float)(nverts - 1);
 
        glClear(GL_COLOR_BUFFER_BIT);
 
-       glEnable(GL_BLEND);
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-       glLineWidth(2.0);
+       glLineWidth(5.0);
 
        glColor3f(1, 1, 1);
-       nseg = LOGOCP_SIZE / 4;
-       for(i=0; i<nseg; i++) {
-               draw_bezier((float*)(logocp + i * 4), BEZ_SEG);
+       for(i=0; i<nverts-1; i++) {
+               float t0 = (float)i * dt;
+               float t1 = (float)(i + 1) * dt;
+               eval_logo(a, t0);
+               eval_logo(b, t1);
+               draw_line(a[0], a[1], b[0], b[1], 0.02);
        }
 
-       glDisable(GL_BLEND);
+       eval_logo(a, 0);
+       eval_logo(b, 1);
+       draw_disc(a[0], a[1], 0.05, 18);
+       draw_disc(b[0], b[1], 0.05, 18);
+
+       glColor3f(0.8, 0, 0);
+       draw_disc(0, 0, 0.14, 24);
 
        glutSwapBuffers();
        assert(glGetError() == GL_NO_ERROR);
@@ -75,46 +131,17 @@ void keyb(unsigned char key, int x, int y)
        switch(key) {
        case 27:
                exit(0);
-       }
-}
-
-#define LERP(a, b, t)  ((a) + ((b) - (a)) * (t))
-
-void draw_bezier(float *cp, int sub)
-{
-       int i;
-       float cx[4], cy[4], mx[3], my[3], mmx[2], mmy[2], x, y;
-
-       if(sub < 1) sub = 1;
-
-       for(i=0; i<4; i++) {
-               cx[i] = cp[i * 2] / LOGO_W - 0.5f;
-               cy[i] = 0.5f - cp[i * 2 + 1] / LOGO_H;
-       }
-
-       glBegin(GL_LINE_STRIP);
-       for(i=0; i<BEZ_SEG; i++) {
-               float t = (float)i / (float)(BEZ_SEG - 1);
-
-               mx[0] = LERP(cx[0], cx[1], t);
-               my[0] = LERP(cy[0], cy[1], t);
-
-               mx[1] = LERP(cx[1], cx[2], t);
-               my[1] = LERP(cy[1], cy[2], t);
-
-               mx[2] = LERP(cx[2], cx[3], t);
-               my[2] = LERP(cy[2], cy[3], t);
 
-               mmx[0] = LERP(mx[0], mx[1], t);
-               mmy[0] = LERP(my[0], my[1], t);
-
-               mmx[1] = LERP(mx[1], mx[2], t);
-               mmy[1] = LERP(my[1], my[2], t);
-
-               x = LERP(mmx[0], mmx[1], t);
-               y = LERP(mmy[0], mmy[1], t);
-
-               glVertex2f(x, y);
+       case '-':
+               nverts -= 8;
+               printf("nverts: %d\n", nverts);
+               glutPostRedisplay();
+               break;
+
+       case '=':
+               nverts += 8;
+               printf("nverts: %d\n", nverts);
+               glutPostRedisplay();
+               break;
        }
-       glEnd();
 }