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);
121 if(frac_init(&frac) == -1) {
124 frac_mesh(&frac, mesh);
129 static void cleanup(void)
135 static void update(void)
140 printf("Generate points...\n");
141 if(frac_gen_points(&frac, 16) != -1) {
150 printf("Build cells...\n");
151 if(frac_build_cells(&frac) != -1) {
160 printf("Construct shell...\n");
161 if(frac_build_shell(&frac) != -1) {
171 printf("Construct walls...\n");
172 if(frac_build_walls(&frac) != -1) {
182 if(pending > cur) glutPostRedisplay();
186 static void display(void)
193 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
195 cgm_mtranslation(view_mat, 0, 0, -view_dist);
196 cgm_mprerotate_x(view_mat, view_phi);
197 cgm_mprerotate_y(view_mat, view_theta);
198 cgm_mpretranslate(view_mat, -view_pos.x, -view_pos.y, -view_pos.z);
199 glMatrixMode(GL_MODELVIEW);
200 glLoadMatrixf(view_mat);
207 num = frac_num_cells(&frac);
210 glPushAttrib(GL_ENABLE_BIT);
211 glDisable(GL_LIGHTING);
212 glDisable(GL_DEPTH_TEST);
214 glBlendFunc(GL_ONE, GL_ONE);
218 for(i=0; i<num; i++) {
220 glColor3f(0.8, 1, 0.1);
222 glColor3f(0.1, 0.8, 0.1);
224 glVertex3fv(&frac.cells[i].pt.x);
234 glPushAttrib(GL_ENABLE_BIT);
235 glDisable(GL_LIGHTING);
237 poly = frac.cells[cur_cell].polys;
238 for(i=0; i<frac.cells[cur_cell].num_polys; i++) {
239 glBegin(GL_LINE_LOOP);
240 glColor3f(0.5, 0.5, 0);
241 for(j=0; j<dynarr_size(poly->verts); j++) {
242 glVertex3fv(&poly->verts[j].pos.x);
253 cmesh_draw(frac.cells[cur_cell].mesh);
256 assert(glGetError() == GL_NO_ERROR);
260 static void reshape(int x, int y)
264 glViewport(0, 0, x, y);
266 cgm_mperspective(proj_mat, cgm_deg_to_rad(50), (float)x / y, 0.5, 500.0);
267 glMatrixMode(GL_PROJECTION);
268 glLoadMatrixf(proj_mat);
271 static void keydown(unsigned char key, int x, int y)
295 cur_cell = (cur_cell + 1) % frac_num_cells(&frac);
296 printf("current cell: %d\n", cur_cell);
302 cur_cell = frac_num_cells(&frac) - 1;
303 printf("current cell: %d\n", cur_cell);
309 if(key >= '1' && key <= '4') {
319 static void mouse(int bn, int st, int x, int y)
321 int bidx = bn - GLUT_LEFT_BUTTON;
323 bnstate[bidx] = st == GLUT_DOWN;
329 static void motion(int x, int y)
336 if(!(dx | dy)) return;
339 view_theta += cgm_deg_to_rad(dx * 0.5);
340 view_phi += cgm_deg_to_rad(dy * 0.5);
341 if(view_phi < -M_PI / 2) view_phi = -M_PI / 2;
342 if(view_phi > M_PI / 2) view_phi = M_PI / 2;
348 up.x = -sin(view_theta) * sin(view_phi);
349 up.y = -cos(view_phi);
350 up.z = cos(view_theta) * sin(view_phi);
351 right.x = cos(view_theta);
353 right.z = sin(view_theta);
355 view_pos.x -= (right.x * dx + up.x * dy) * 0.01;
356 view_pos.y -= up.y * dy * 0.01;
357 view_pos.z -= (right.z * dx + up.z * dy) * 0.01;
361 view_dist += dy * 0.1;
362 if(view_dist < 0) view_dist = 0;