X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=dosdemo;a=blobdiff_plain;f=tools%2Fropesim%2Fsrc%2Fmain.c;h=6037509d2b779307b2d87d8cf19027129bc72e21;hp=bb5e5c593373bb7b8e12f166135c2cc303be0a23;hb=900d333da3db0d117e815dc181b5d73c31efa455;hpb=59c564d8a2077ce90e7c83fdb276b80c9bfa238c diff --git a/tools/ropesim/src/main.c b/tools/ropesim/src/main.c index bb5e5c5..6037509 100644 --- a/tools/ropesim/src/main.c +++ b/tools/ropesim/src/main.c @@ -3,6 +3,7 @@ #include #include "cmesh.h" #include "cgmath/cgmath.h" +#include "ropesim.h" int init(void); void cleanup(void); @@ -19,10 +20,22 @@ void sball_button(int bn, int st); float cam_theta, cam_phi, cam_dist = 10; int prev_mx, prev_my; int bnstate[8]; +int modkeys; + +long start_msec; struct cmesh *scn; struct cmesh *mesh_gout, *mesh_gin, *mesh_suz; -cgm_vec3 ganchor[4]; + +cgm_vec3 gmove; +/*cgm_quat grot = {0, 0, 0, 1};*/ +float grot_theta, grot_phi; +float ginner_xform[16], gouter_xform[16]; +cgm_vec3 ganchor[4], manchor[4]; + +cgm_vec3 dbgvec[4]; + +struct rsim_world rsim; int main(int argc, char **argv) { @@ -46,17 +59,24 @@ int main(int argc, char **argv) } atexit(cleanup); + start_msec = glutGet(GLUT_ELAPSED_TIME); glutMainLoop(); return 0; } +#define ROPE_MASSES 10 +#define ROPE_SPRINGS (ROPE_MASSES - 1) +#define ROPE_LEN 0.8f +#define ROPE_MASSES_MASS 0.01f +#define ROPE_K 180.0f int init(void) { static const char *meshnames[] = {"suzanne", "gimbal_outer", "gimbal_inner"}; static struct cmesh **meshes[] = {&mesh_suz, &mesh_gout, &mesh_gin}; static const float amb[] = {0.05, 0.05, 0.08, 1}; - int i; + int i, j; + struct rsim_rope *rope; glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); @@ -85,6 +105,44 @@ int init(void) cmesh_remove_submesh(scn, idx); } + rsim_init(&rsim); + rsim.damping = 0.3; + + if(!(rope = rsim_alloc_rope(ROPE_MASSES * 4))) { + fprintf(stderr, "failed to allocate rope\n"); + return -1; + } + rsim_add_rope(&rsim, rope); + + /* anchor points on the inner gimbal */ + for(i=0; i<4; i++) { + ganchor[i].x = (float)(((i & 1) << 1) - 1) * 1.5f; + ganchor[i].y = (float)((i & 2) - 1) * 1.5f; + ganchor[i].z = 0; + + manchor[i] = ganchor[i]; + cgm_vscale(manchor + i, 0.32); + + + + manchor[i].y += 0.15; + + for(j=0; jmasses + midx; + + float t = (float)j / (float)(ROPE_MASSES - 1.0f); + cgm_vlerp(&mass->p, ganchor + i, manchor + i, t); + mass->m = ROPE_MASSES_MASS; + + if(j == 0) { + rsim_freeze_rope_mass(rope, rope->masses + i * ROPE_MASSES); /* freeze first mass */ + } else { + rsim_set_rope_spring(rope, midx, midx - 1, ROPE_K, RSIM_RLEN_DEFAULT); + } + } + } + return 0; } @@ -94,6 +152,38 @@ void cleanup(void) cmesh_free(mesh_gout); cmesh_free(mesh_gin); cmesh_free(scn); + + rsim_destroy(&rsim); +} + +void update(long tmsec, float dt) +{ + int i; + cgm_vec3 apt0, apt1; + float theta, phi, brot; + struct rsim_rope *rope; + + /* + cgm_mrotation_quat(ginner_xform, &grot); + cgm_mtranslate(ginner_xform, gmove.x, gmove.y, gmove.z); + */ + + theta = cgm_deg_to_rad(grot_theta); + phi = cgm_deg_to_rad(grot_phi); + + cgm_mrotation_euler(ginner_xform, phi, theta, 0, CGM_EULER_XYZ); + cgm_mrotation_euler(gouter_xform, phi, 0, 0, CGM_EULER_XYZ); + + rope = rsim.ropes; + for(i=0; i<4; i++) { + apt0 = ganchor[i]; + cgm_vmul_m4v3(&apt0, ginner_xform); + + dbgvec[i] = apt0; + rope->masses[i * ROPE_MASSES].p = apt0; + } + + rsim_step(&rsim, dt); } void display(void) @@ -108,7 +198,13 @@ void display(void) {0.5, 0.3, 0.2, 1}, {0.2, 0.3, 0.2, 1} }; - int i; + int i, j, count; + long tmsec = glutGet(GLUT_ELAPSED_TIME) - start_msec; + static long prev_tmsec; + struct rsim_rope *rope; + + update(tmsec, (float)(tmsec - prev_tmsec) / 1000.0f); + prev_tmsec = tmsec; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -123,7 +219,58 @@ void display(void) glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]); } - cmesh_draw(scn); + count = cmesh_submesh_count(scn); + for(i=0; inum_masses; i++) { + for(j=i+1; jnum_masses; j++) { + if(rsim_have_spring(rope, i, j)) { + glVertex3f(rope->masses[i].p.x, rope->masses[i].p.y, rope->masses[i].p.z); + glVertex3f(rope->masses[j].p.x, rope->masses[j].p.y, rope->masses[j].p.z); + } + } + } + glEnd(); + + glBegin(GL_POINTS); + glColor3f(1, 0.2, 0.2); + for(i=0; inum_masses; i++) { + glVertex3f(rope->masses[i].p.x, rope->masses[i].p.y, rope->masses[i].p.z); + } + glEnd(); + rope = rope->next; + } + glPopAttrib(); glutSwapBuffers(); } @@ -153,6 +300,7 @@ void mouse(int bn, int st, int x, int y) prev_mx = x; prev_my = y; bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN; + modkeys = glutGetModifiers(); } void motion(int x, int y) @@ -164,27 +312,51 @@ void motion(int x, int y) if(!(dx | dy)) return; - if(bnstate[0]) { - cam_theta += dx * 0.5; - cam_phi += dy * 0.5; - if(cam_phi < -90) cam_phi = -90; - if(cam_phi > 90) cam_phi = 90; - } + if(modkeys) { + if(bnstate[0]) { + grot_theta += dx * 0.5; + grot_phi += dy * 0.5; + } + } else { + if(bnstate[0]) { + cam_theta += dx * 0.5; + cam_phi += dy * 0.5; + if(cam_phi < -90) cam_phi = -90; + if(cam_phi > 90) cam_phi = 90; + } - if(bnstate[2]) { - cam_dist += dy * 0.1; - if(cam_dist < 0.0f) cam_dist = 0.0f; + if(bnstate[2]) { + cam_dist += dy * 0.1; + if(cam_dist < 0.0f) cam_dist = 0.0f; + } } } void sball_motion(int x, int y, int z) { + gmove.x += x * 0.001f; + gmove.y += y * 0.001f; + gmove.z -= z * 0.001f; } -void sball_rotate(int rx, int ry, int rz) +void sball_rotate(int x, int y, int z) { + /* + float axis_len, s; + axis_len = (float)sqrt(x * x + y * y + z * z); + s = axis_len == 0.0f ? 1.0f : 1.0f / axis_len; + cgm_qrotate(&grot, axis_len * 0.001f, -x * s, -y * s, z * s); + */ + + grot_theta += y * 0.03f; + grot_phi += x * 0.03f; } void sball_button(int bn, int st) { + if(st == GLUT_DOWN) { + /*cgm_qcons(&grot, 0, 0, 0, 1);*/ + grot_theta = grot_phi = 0.0f; + cgm_vcons(&gmove, 0, 0, 0); + } }