rendered paste body// Julien Pilet, Sept. 2008#include <OpenCV/cv.h>#include "Meddle/elasticchainondefm.h"namespace Meddle {/* ctor */ElasticChain::ElasticChain(unsigned int npts, float x, float y, Model::Animation::Handler *animation): m_npts(npts), m_first(true), m_animation(animation){ m_elastics = new t_elastic[m_npts - 1]; m_pts = new t_point[m_npts]; memset(m_pts, 0, m_npts * sizeof(t_point)); // orientation et force de la gravite gravity[0] = 0; gravity[1] = -800; float dy = 2 / m_npts; // 2/m_npts espace entre les points du spaghetti for (int i = 0; i < m_npts; i++) { m_pts[i].mass = .002; // poids des maillons intermediaires m_pts[i].pos[0] = x; m_pts[i].pos[1] = y + i*dy; } m_pts[0].flags = 10; m_pts[m_npts - 1].mass = .02; for (int i = 0;i < m_npts - 1; i++) { m_elastics[i].p1 = m_pts + i; m_elastics[i].p2 = m_pts + i + 1; m_elastics[i].factor = 2; //2 ressort plus ou moins rigide m_elastics[i].len = dy; } m_last.start();}/* dtor */ElasticChain::~ElasticChain(){ delete[] (m_pts); delete[] (m_elastics);}voidElasticChain::draw(float x, float y){ if (m_first) { m_first = false; return; } float dtime = m_last.elapsed(); m_pts[0].pos[0] = x; m_pts[0].pos[1] = y; dtime = dtime / 32; for (int i = 0;i < 32; i++) { zero_forces(m_pts, m_npts); calc_forces(m_elastics, m_npts - 1); advance(m_pts, m_npts, dtime); } m_animation->texturize(); m_animation->texture.enable(); double width = 20; //20 LARGEUR de la chaine dessine glBegin(GL_TRIANGLES); glColor4f(1, 1, 1, 1); int ndiv = 1; int tot = (m_npts - 1) * ndiv; int k = 0; for (int i = 0;i < m_npts - 1; i++) { for (int j = 0;j < ndiv; j++, k++) { float t = j / (float)ndiv; float r[2]; float dt[2]; float nr[2], ndt[2]; splineCompute(t,2, (i>0? m_pts[i-1].pos:0), m_pts[i].pos, m_pts[i+1].pos, (i<m_npts-2? m_pts[i+2].pos:0), r, dt); splineCompute(t+1.0f/ndiv,2, (i>0? m_pts[i-1].pos:0), m_pts[i].pos, m_pts[i+1].pos, (i<m_npts-2? m_pts[i+2].pos:0), nr, ndt); float w = width /sqrt(dt[0]*dt[0]+dt[1]*dt[1]); dt[0] *= w; dt[1] *= w; w = width/sqrt(ndt[0]*ndt[0]+ndt[1]*ndt[1]); ndt[0] *= w; ndt[1] *= w; /*float a[2]= {r[0]-dt[1],r[1]+dt[0]}; float b[2]= {r[0]+dt[1],r[1]-dt[0]}; float na[2]= {nr[0]-ndt[1],nr[1]+ndt[0]}; float nb[2]= {nr[0]+ndt[1],nr[1]-ndt[0]};*/ float a[2]= {r[0]+80,r[1]-80}; float b[2]= {r[0]-80,r[1]-80}; float na[2]= {r[0]+80,r[1]+80}; float nb[2]= {r[0]-80,r[1]+80}; float tex = (float)k/(float)tot; float texn = (float)(k+1)/(float)tot; glTexCoord2f(0,tex); glVertex2fv(a); glTexCoord2f(1,tex); glVertex2fv(b); glTexCoord2f(0,texn); glVertex2fv(na); glTexCoord2f(0,texn); glVertex2fv(na); glTexCoord2f(1,tex); glVertex2fv(b); glTexCoord2f(1,texn); glVertex2fv(nb); } } glEnd(); m_animation->texture.disable(); m_last.restart();}} // namespace Meddle