started on the fracture code
[meshfrac] / main.c
diff --git a/main.c b/main.c
index 07e67c3..724fd70 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,6 +6,7 @@
 #include <cgmath/cgmath.h>
 #include "cmesh.h"
 #include "meshgen.h"
+#include "frac.h"
 #include "sdr.h"
 
 static const char *vsdr_src =
@@ -50,9 +51,15 @@ static float proj_mat[16], view_mat[16];
 static int bnstate[8];
 static int mx, my;
 
+static struct fracture frac;
 static struct cmesh *mesh;
 static unsigned int sdr;
 
+static int cur, pending;
+static int show_orig = 1;
+static int show_points;
+
+
 int main(int argc, char **argv)
 {
        glutInit(&argc, argv);
@@ -106,11 +113,14 @@ static int init(void)
        glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
        glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60);
 
-       bind_program(sdr);
-
        mesh = cmesh_alloc();
        gen_torus(mesh, 2, 0.8, 24, 12, 1, 1);
 
+       if(frac_init(&frac) == -1) {
+               return -1;
+       }
+       frac_mesh(&frac, mesh);
+
        return 0;
 }
 
@@ -120,8 +130,56 @@ static void cleanup(void)
        free_program(sdr);
 }
 
+static void update(void)
+{
+       if(pending > cur) {
+               switch(cur) {
+               case 0:
+                       if(frac_gen_points(&frac, 16) == -1) {
+                               cur++;
+                       } else {
+                               pending = cur;
+                       }
+                       show_points = 1;
+                       break;
+
+               case 1:
+                       if(frac_build_cells(&frac) != -1) {
+                               cur++;
+                       } else {
+                               pending = cur;
+                       }
+                       break;
+
+               case 2:
+                       if(frac_build_shell(&frac) != -1) {
+                               cur++;
+                       } else {
+                               pending = cur;
+                       }
+                       break;
+
+               case 3:
+                       if(frac_build_walls(&frac) != -1) {
+                               cur++;
+                       } else {
+                               pending = cur;
+                       }
+                       break;
+
+               default:
+                       break;
+               }
+               if(pending > cur) glutPostRedisplay();
+       }
+}
+
 static void display(void)
 {
+       int i, num;
+
+       update();
+
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
        cgm_mtranslation(view_mat, 0, 0, -view_dist);
@@ -131,7 +189,30 @@ static void display(void)
        glMatrixMode(GL_MODELVIEW);
        glLoadMatrixf(view_mat);
 
-       cmesh_draw(mesh);
+       if(show_orig) {
+               bind_program(sdr);
+               cmesh_draw(mesh);
+       }
+       if(show_points) {
+               num = frac_num_cells(&frac);
+               bind_program(0);
+
+               glPushAttrib(GL_ENABLE_BIT);
+               glDisable(GL_DEPTH_TEST);
+               glDisable(GL_LIGHTING);
+               glEnable(GL_BLEND);
+               glBlendFunc(GL_ONE, GL_ONE);
+
+               glPointSize(2.0f);
+               glBegin(GL_POINTS);
+               glColor3f(0.1, 0.8, 0.1);
+               for(i=0; i<num; i++) {
+                       glVertex3fv(&frac.cells[i].pt.x);
+               }
+               glEnd();
+
+               glPopAttrib();
+       }
 
        assert(glGetError() == GL_NO_ERROR);
        glutSwapBuffers();
@@ -150,9 +231,30 @@ static void reshape(int x, int y)
 
 static void keydown(unsigned char key, int x, int y)
 {
+       int n;
+
        switch(key) {
        case 27:
                exit(0);
+
+       case 'o':
+               show_orig ^= 1;
+               glutPostRedisplay();
+               break;
+
+       case 'p':
+               show_points ^= 1;
+               glutPostRedisplay();
+               break;
+
+       default:
+               if(key >= '1' && key <= '4') {
+                       n = key - '0';
+                       if(cur < n) {
+                               pending = n;
+                               glutPostRedisplay();
+                       }
+               }
        }
 }