updated files from meshfrac_alt
[meshfrac] / main.c
diff --git a/main.c b/main.c
index 07e67c3..258cbd5 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,7 +6,9 @@
 #include <cgmath/cgmath.h>
 #include "cmesh.h"
 #include "meshgen.h"
+#include "frac.h"
 #include "sdr.h"
+#include "dynarr.h"
 
 static const char *vsdr_src =
        "varying vec3 v_norm, v_ldir, v_vdir;\n"
@@ -50,9 +52,16 @@ 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, show_planes, show_shell;
+static int cur_cell;
+
+
 int main(int argc, char **argv)
 {
        glutInit(&argc, argv);
@@ -106,10 +115,22 @@ 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);
+       //gen_torus(mesh, 2, 0.8, 24, 12, 1, 1);
+       //gen_sphere(mesh, 2, 12, 6, 1, 1);
+       gen_box(mesh, 2, 2, 2, 0, 0);
+
+       if(frac_init(&frac) == -1) {
+               return -1;
+       }
+       frac_mesh(&frac, mesh);
+       frac.cell_gap = 0.1;
+
+       //frac_gen_points(&frac, 16);
+       frac_point(&frac, -5, 0, 0);
+       frac_point(&frac, 5, 0, 0);
+       show_points = 1;
+       cur = 1;
 
        return 0;
 }
@@ -120,8 +141,64 @@ static void cleanup(void)
        free_program(sdr);
 }
 
+static void update(void)
+{
+       if(pending > cur) {
+               switch(cur) {
+               case 0:
+                       printf("Generate points...\n");
+                       if(frac_gen_points(&frac, 2) != -1) {
+                               cur++;
+                       } else {
+                               pending = cur;
+                       }
+                       show_points = 1;
+                       break;
+
+               case 1:
+                       printf("Build cells...\n");
+                       if(frac_build_cells(&frac) != -1) {
+                               cur++;
+                       } else {
+                               pending = cur;
+                       }
+                       show_planes = 1;
+                       break;
+
+               case 2:
+                       printf("Construct shell...\n");
+                       if(frac_build_shell(&frac) != -1) {
+                               cur++;
+                       } else {
+                               pending = cur;
+                       }
+                       show_orig = 0;
+                       show_shell = 1;
+                       break;
+
+               case 3:
+                       printf("Construct walls...\n");
+                       if(frac_build_walls(&frac) != -1) {
+                               cur++;
+                       } else {
+                               pending = cur;
+                       }
+                       break;
+
+               default:
+                       break;
+               }
+               if(pending > cur) glutPostRedisplay();
+       }
+}
+
 static void display(void)
 {
+       int i, j, num;
+       struct poly *poly;
+
+       update();
+
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
        cgm_mtranslation(view_mat, 0, 0, -view_dist);
@@ -131,7 +208,61 @@ static void display(void)
        glMatrixMode(GL_MODELVIEW);
        glLoadMatrixf(view_mat);
 
-       cmesh_draw(mesh);
+       bind_program(0);
+       glShadeModel(GL_FLAT);
+
+       if(show_orig) {
+               //bind_program(sdr);
+               cmesh_draw(mesh);
+               bind_program(0);
+       }
+       if(show_points) {
+               num = frac_num_cells(&frac);
+
+               glPushAttrib(GL_ENABLE_BIT);
+               glDisable(GL_LIGHTING);
+               glDisable(GL_DEPTH_TEST);
+               glEnable(GL_BLEND);
+               glBlendFunc(GL_ONE, GL_ONE);
+
+               glPointSize(2.0f);
+               glBegin(GL_POINTS);
+               for(i=0; i<num; i++) {
+                       if(cur_cell == i) {
+                               glColor3f(0.8, 1, 0.1);
+                       } else {
+                               glColor3f(0.1, 0.8, 0.1);
+                       }
+                       glVertex3fv(&frac.cells[i].pt.x);
+               }
+               glEnd();
+
+               glPopAttrib();
+       }
+
+       if(show_planes) {
+               glPushAttrib(GL_ENABLE_BIT);
+               glDisable(GL_LIGHTING);
+
+               poly = frac.cells[cur_cell].polys;
+               for(i=0; i<frac.cells[cur_cell].num_polys; i++) {
+                       glBegin(GL_LINE_LOOP);
+                       glColor3f(0.5, 0.5, 0);
+                       for(j=0; j<dynarr_size(poly->verts); j++) {
+                               glVertex3fv(&poly->verts[j].pos.x);
+                       }
+                       glEnd();
+                       poly++;
+               }
+
+               glPopAttrib();
+       }
+
+       if(show_shell) {
+               //bind_program(sdr);
+               cmesh_draw(frac.cells[cur_cell].mesh);
+               bind_program(0);
+       }
 
        assert(glGetError() == GL_NO_ERROR);
        glutSwapBuffers();
@@ -150,9 +281,49 @@ 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;
+
+       case 'P':
+               show_planes ^= 1;
+               glutPostRedisplay();
+               break;
+
+       case ']':
+               cur_cell = (cur_cell + 1) % frac_num_cells(&frac);
+               printf("current cell: %d\n", cur_cell);
+               glutPostRedisplay();
+               break;
+
+       case '[':
+               if(--cur_cell < 0) {
+                       cur_cell = frac_num_cells(&frac) - 1;
+                       printf("current cell: %d\n", cur_cell);
+                       glutPostRedisplay();
+               }
+               break;
+
+       default:
+               if(key >= '1' && key <= '4') {
+                       n = key - '0';
+                       if(cur < n) {
+                               pending = n;
+                               glutPostRedisplay();
+                       }
+               }
        }
 }