6 #include <cgmath/cgmath.h>
13 static const char *vsdr_src =
14 "varying vec3 v_norm, v_ldir, v_vdir;\n"
17 " gl_Position = ftransform();\n"
18 " vec3 vpos = (gl_ModelViewMatrix * gl_Vertex).xyz;\n"
20 " v_norm = gl_NormalMatrix * gl_Normal;\n"
21 " v_ldir = gl_LightSource[0].position.xyz - vpos;\n"
24 static const char *psdr_src =
25 "varying vec3 v_norm, v_ldir, v_vdir;\n"
28 " vec3 n = normalize(v_norm);\n"
29 " vec3 v = normalize(v_vdir);\n"
30 " vec3 l = normalize(v_ldir);\n"
31 " vec3 h = normalize(v + l);\n"
32 " float ndotl = max(dot(n, l), 0.0);\n"
33 " float ndoth = max(dot(n, h), 0.0);\n"
34 " float spec = pow(ndoth, gl_FrontMaterial.shininess);\n"
35 " vec3 dcol = gl_FrontMaterial.diffuse.rgb * ndotl;\n"
36 " vec3 scol = gl_FrontMaterial.specular.rgb * spec;\n"
37 " gl_FragColor = vec4(dcol + scol, 1.0);\n"
40 static int init(void);
41 static void cleanup(void);
42 static void display(void);
43 static void reshape(int x, int y);
44 static void keydown(unsigned char key, int x, int y);
45 static void mouse(int bn, int st, int x, int y);
46 static void motion(int x, int y);
48 static int win_width, win_height;
49 static cgm_vec3 view_pos;
50 static float view_theta, view_phi = 0.6, view_dist = 8;
51 static float proj_mat[16], view_mat[16];
52 static int bnstate[8];
55 static struct fracture frac;
56 static struct cmesh *mesh;
57 static unsigned int sdr;
59 static int cur, pending;
60 static int show_orig = 1;
61 static int show_points, show_planes, show_shell;
65 int main(int argc, char **argv)
67 glutInit(&argc, argv);
68 glutInitWindowSize(1280, 800);
69 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
70 glutCreateWindow("meshfrac");
72 glutDisplayFunc(display);
73 glutReshapeFunc(reshape);
74 glutKeyboardFunc(keydown);
76 glutMotionFunc(motion);
89 unsigned int vsdr, psdr;
90 float diffuse[] = {0.2, 0.3, 0.8, 1};
91 float specular[] = {0.8, 0.8, 0.8, 1};
96 if(!(vsdr = create_vertex_shader(vsdr_src))) {
99 if(!(psdr = create_pixel_shader(psdr_src))) {
103 if(!(sdr = create_program_link(vsdr, psdr, 0))) {
109 glEnable(GL_CULL_FACE);
110 glEnable(GL_DEPTH_TEST);
111 glEnable(GL_LIGHTING);
114 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse);
115 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
116 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60);
118 mesh = cmesh_alloc();
119 //gen_torus(mesh, 2, 0.8, 24, 12, 1, 1);
120 //gen_sphere(mesh, 2, 12, 6, 1, 1);
121 gen_box(mesh, 2, 2, 2, 0, 0);
123 if(frac_init(&frac) == -1) {
126 frac_mesh(&frac, mesh);
129 //frac_gen_points(&frac, 16);
130 frac_point(&frac, -5, 0, 0);
131 frac_point(&frac, 5, 0, 0);
138 static void cleanup(void)
144 static void update(void)
149 printf("Generate points...\n");
150 if(frac_gen_points(&frac, 2) != -1) {
159 printf("Build cells...\n");
160 if(frac_build_cells(&frac) != -1) {
169 printf("Construct shell...\n");
170 if(frac_build_shell(&frac) != -1) {
180 printf("Construct walls...\n");
181 if(frac_build_walls(&frac) != -1) {
191 if(pending > cur) glutPostRedisplay();
195 static void display(void)
202 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
204 cgm_mtranslation(view_mat, 0, 0, -view_dist);
205 cgm_mprerotate_x(view_mat, view_phi);
206 cgm_mprerotate_y(view_mat, view_theta);
207 cgm_mpretranslate(view_mat, -view_pos.x, -view_pos.y, -view_pos.z);
208 glMatrixMode(GL_MODELVIEW);
209 glLoadMatrixf(view_mat);
212 glShadeModel(GL_FLAT);
220 num = frac_num_cells(&frac);
222 glPushAttrib(GL_ENABLE_BIT);
223 glDisable(GL_LIGHTING);
224 glDisable(GL_DEPTH_TEST);
226 glBlendFunc(GL_ONE, GL_ONE);
230 for(i=0; i<num; i++) {
232 glColor3f(0.8, 1, 0.1);
234 glColor3f(0.1, 0.8, 0.1);
236 glVertex3fv(&frac.cells[i].pt.x);
244 glPushAttrib(GL_ENABLE_BIT);
245 glDisable(GL_LIGHTING);
247 poly = frac.cells[cur_cell].polys;
248 for(i=0; i<frac.cells[cur_cell].num_polys; i++) {
249 glBegin(GL_LINE_LOOP);
250 glColor3f(0.5, 0.5, 0);
251 for(j=0; j<dynarr_size(poly->verts); j++) {
252 glVertex3fv(&poly->verts[j].pos.x);
263 cmesh_draw(frac.cells[cur_cell].mesh);
267 assert(glGetError() == GL_NO_ERROR);
271 static void reshape(int x, int y)
275 glViewport(0, 0, x, y);
277 cgm_mperspective(proj_mat, cgm_deg_to_rad(50), (float)x / y, 0.5, 500.0);
278 glMatrixMode(GL_PROJECTION);
279 glLoadMatrixf(proj_mat);
282 static void keydown(unsigned char key, int x, int y)
306 cur_cell = (cur_cell + 1) % frac_num_cells(&frac);
307 printf("current cell: %d\n", cur_cell);
313 cur_cell = frac_num_cells(&frac) - 1;
314 printf("current cell: %d\n", cur_cell);
320 if(key >= '1' && key <= '4') {
330 static void mouse(int bn, int st, int x, int y)
332 int bidx = bn - GLUT_LEFT_BUTTON;
334 bnstate[bidx] = st == GLUT_DOWN;
340 static void motion(int x, int y)
347 if(!(dx | dy)) return;
350 view_theta += cgm_deg_to_rad(dx * 0.5);
351 view_phi += cgm_deg_to_rad(dy * 0.5);
352 if(view_phi < -M_PI / 2) view_phi = -M_PI / 2;
353 if(view_phi > M_PI / 2) view_phi = M_PI / 2;
359 up.x = -sin(view_theta) * sin(view_phi);
360 up.y = -cos(view_phi);
361 up.z = cos(view_theta) * sin(view_phi);
362 right.x = cos(view_theta);
364 right.z = sin(view_theta);
366 view_pos.x -= (right.x * dx + up.x * dy) * 0.01;
367 view_pos.y -= up.y * dy * 0.01;
368 view_pos.z -= (right.z * dx + up.z * dy) * 0.01;
372 view_dist += dy * 0.1;
373 if(view_dist < 0) view_dist = 0;