Guest User

Untitled

a guest
May 24th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.58 KB | None | 0 0
  1. /*
  2. * Based on a humanoid xD
  3. */
  4.  
  5. #include <GL/glut.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <math.h>
  9. #include <stdbool.h>
  10. #include <time.h>
  11.  
  12.  
  13. #define SPHERE_SLICES 10
  14. #define SPHERE_STACKS 10
  15. #define CUBE_SIZE 0.5
  16. #define SPHERE_SIZE 0.1
  17.  
  18. float cubeAngle=0;
  19. float incr=0.5;
  20. float size=0.5;
  21.  
  22. bool mouseDown = false;
  23.  
  24. float xrot = 0.0f;
  25. float yrot = 0.0f;
  26.  
  27. float xdiff = 0.0f;
  28. float ydiff = 0.0f;
  29.  
  30. enum articulaciones {CABEZA, BRAZOSI, BRAZOII, BRAZOSD, BRAZOID, PIERNASI, PIERNAII, PIERNASD, PIERNAID, TORSO_ART};
  31.  
  32. int articulacion=TORSO_ART;
  33.  
  34. float angulos[10] ={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
  35.  
  36. struct nodo {
  37. float m[16];
  38. void(*f)();
  39. struct nodo *hijo;
  40. struct nodo *hermano;
  41. };
  42.  
  43. struct nodo torso, cabeza, brazoSD, brazoSI, brazoID, brazoII, piernaSI, piernaSD, piernaII, piernaID;
  44.  
  45. typedef struct particle {
  46. float posX;
  47. float posY;
  48. float posZ;
  49. float velX;
  50. float velY;
  51. float velZ;
  52. float fX;
  53. float fY;
  54. float fZ;
  55. }particle;
  56.  
  57. particle a,b,c,d,e,f;
  58.  
  59. typedef struct spring
  60. {
  61. particle * particle1;
  62. particle * particle2;
  63.  
  64. float length;
  65. float enlongation;
  66. }spring;
  67.  
  68. spring resorte, resorte2, resorte3, resorte4, resorte5;
  69.  
  70. //Por si cambia el tama?o de la ventana, para que el cubo guarde la misma proporci?n
  71. void reshape(int w, int h)
  72. {
  73. glViewport (0, 0, (GLsizei) w, (GLsizei) h);
  74. glMatrixMode(GL_PROJECTION);
  75. glLoadIdentity();
  76. gluPerspective(45.0, (GLdouble)w/(GLdouble)h, 1.0, 100.0) ;
  77. }
  78.  
  79. void drawBody(){
  80.  
  81. //traverse(&torso);
  82. glPointSize(20);
  83.  
  84. glBegin(GL_POINTS);
  85. glColor3f(1,0,1);
  86. glVertex3f(a.posX,a.posY,a.posZ);
  87. glColor3f(0,0,1);
  88. glVertex3f(b.posX,b.posY,b.posZ);
  89. glColor3f(1,0,0);
  90. glVertex3f(c.posX,c.posY,c.posZ);
  91. glColor3f(1,1,0);
  92. glVertex3f(d.posX,d.posY,d.posZ);
  93. glEnd();
  94.  
  95. }
  96.  
  97. void actualizaParticulas(){
  98. float g= 0.00000;
  99.  
  100. a.velX += a.fX;
  101. a.velY += a.fY;
  102. a.velZ += a.fZ;
  103. b.fY -= g;
  104. c.fY -= g;
  105.  
  106. b.velX += b.fX;
  107. b.velY += b.fY;
  108. b.velZ += b.fZ;
  109. printf("vel by: %f",b.velY);
  110.  
  111. c.velX += c.fX;
  112. c.velY += c.fY;
  113. c.velZ += c.fZ;
  114.  
  115. d.velX += d.fX;
  116. d.velY += d.fY;
  117. d.velZ += d.fZ;
  118.  
  119. a.posX += a.velX;
  120. a.posY += a.velY;
  121. a.posZ += a.velZ;
  122.  
  123. b.posX += b.velX;
  124. b.posY += b.velY;
  125. b.posZ += b.velZ;
  126.  
  127. c.posX += c.velX;
  128. c.posY += c.velY;
  129. c.posZ += c.velZ;
  130.  
  131. d.posX += d.velX;
  132. d.posY += d.velY;
  133. d.posZ += d.velZ;
  134.  
  135. }
  136.  
  137. void thingy(spring * res){
  138. particle * a;
  139. particle * b;
  140.  
  141. a = res->particle1;
  142. b = res->particle2;
  143.  
  144. float distancia = sqrt(pow(a->posX - b->posX,2)+pow(a->posY - b->posY,2)+pow(a->posZ - b->posZ,2));
  145. float centro [3] = {a->posX+(a->posX - b->posX)/2, a->posY+(a->posY - b->posY)/2, a->posZ+(a->posZ - b->posZ)/2 };
  146. float enlonga= distancia-res->length;
  147. printf("enl: %f - dist: %f \n",enlonga,distancia);
  148.  
  149.  
  150. float hookForce=0.0;// = -(0.0003)*(enlonga);
  151. //Hacer un metodo que nos diga de que lado esta cada particula
  152.  
  153.  
  154. float x,y,z;
  155.  
  156. x = abs(a->posX - b->posX)/distancia;
  157. y = abs(a->posY - b->posY)/distancia;
  158. z = abs(a->posZ - b->posZ)/distancia;
  159.  
  160. float difvel [3]= {a->velX-b->velX,a->velY-b->velY,a->velZ - b->velZ};
  161. float distancia_unit[3] = {x,y,z};
  162. float elast = .00050;
  163. float difvel_times_elast[3] = {difvel[0]*elast, difvel[1]*elast, difvel[2]*elast};
  164. float product_difvel_distance = difvel_times_elast[0]*distancia_unit[0]+difvel_times_elast[1]*distancia_unit[1]+difvel_times_elast[2]*distancia_unit[2];
  165.  
  166.  
  167. //hookForce = - 0.0003*(enlonga)*distancia-elast*difvel;
  168. float first_term = -(0.0000003 * (distancia - res->length)+product_difvel_distance);
  169. float hookForce_vector [3] = {first_term*x, first_term*y, first_term*z};
  170. //printf("bb: elast (%f) * difvel (%f) = %f\n",elast,difvel,elast*difvel);
  171.  
  172.  
  173.  
  174. if ( a->posX > centro[0]){
  175. //fuerza negativa
  176. a->fX -= (hookForce_vector[0]) ;
  177. b->fX += (hookForce_vector[0]);
  178. }else{
  179. a->fX += hookForce_vector[0];
  180. b->fX -= hookForce_vector[0];
  181. }
  182.  
  183. if ( a->posY > centro[1]){
  184. //fuerza negativa
  185. a->fY -= (hookForce_vector[1]);
  186. b->fY += (hookForce_vector[1]);
  187. }else{
  188. a->fY += hookForce_vector[1];
  189. b->fY -= hookForce_vector[1];
  190. }
  191.  
  192. if ( a->posZ > centro[2]){
  193. //fuerza negativa
  194. a->fZ -= (hookForce_vector[2]);
  195. b->fZ += (hookForce_vector[2]);
  196. }else{
  197. a->fZ += hookForce_vector[2];
  198. b->fZ -= hookForce_vector[2];
  199. }
  200.  
  201.  
  202.  
  203. }
  204.  
  205. void resetForces(particle *p ){
  206. p->fX=0;
  207. p->fY=0;
  208. p->fZ=0;
  209. }
  210.  
  211. void resortea(){
  212. resetForces(&a);
  213. resetForces(&b);
  214. resetForces(&c);
  215. resetForces(&d);
  216. thingy(&resorte);
  217. thingy(&resorte2);
  218. thingy(&resorte3);
  219. thingy(&resorte4);
  220. thingy(&resorte4);
  221. }
  222.  
  223. void idle(){
  224. resortea();
  225. actualizaParticulas();
  226. glutPostRedisplay();
  227.  
  228. }
  229.  
  230.  
  231. void traverse (struct nodo *node)
  232. {
  233. // guardar la matriz actual porque las transformaciones a realizarse
  234. // sólo deben afectarle a él y a sus hijos
  235. glPushMatrix();
  236. // transformar relativo a su padre
  237. glMultMatrixf(node->m);
  238. // dibujar el nodo
  239. node->f();
  240. // primer recorrer los hijos (si hay)
  241. if (node->hijo != NULL)
  242. traverse(node->hijo);
  243.  
  244. glPopMatrix();
  245.  
  246. // después recorrer los hermanos (si hay)
  247. if(node->hermano != NULL)
  248. traverse(node->hermano);
  249.  
  250. }
  251.  
  252. void drawTorso(){
  253. //Torso
  254. glPushMatrix();
  255. drawCube();
  256. glPopMatrix();
  257. }
  258. void drawCabeza(){
  259.  
  260. glTranslatef(0.0,0.3,0.0);
  261. drawArticulacion(CABEZA);
  262. glRotatef(angulos[CABEZA],1,0,0);
  263. glTranslatef(0.0,0.15,0.0);
  264.  
  265. glScalef(0.5,0.5,0.5);
  266. drawCube(1.0,0.5,0.4);
  267.  
  268. }
  269. void drawBrazoSI(){
  270.  
  271. glTranslatef(-0.25,0.2,0.0);
  272. drawArticulacion(BRAZOSI);
  273. glRotatef(angulos[BRAZOSI],1,0,0);
  274. glTranslatef(-0.2,-0.2,0.0);
  275.  
  276. glScalef(0.5,1.0,0.5);
  277. drawCube(1.00,0.0,0.0);
  278.  
  279. }
  280. void drawBrazoII(){
  281. glTranslatef(0.0,-0.3,0.0);
  282. drawArticulacion(BRAZOII);
  283. glRotatef(angulos[BRAZOII],1,0,0);
  284. glTranslatef(0.0,-0.3,0.0);
  285.  
  286. glScalef(0.5,1.0,0.5);
  287. drawCube(1.00,0.0,0.0);
  288. }
  289.  
  290. void drawBrazoSD(){
  291. glTranslatef(0.25,0.2,0.0);
  292. drawArticulacion(BRAZOSD);
  293. glRotatef(angulos[BRAZOSD],1,0,0);
  294. glTranslatef(0.2,-0.2,0.0);
  295.  
  296. glScalef(0.5,1.0,0.5);
  297. drawCube(1.00,0.0,0.0);
  298. }
  299. void drawBrazoID(){
  300.  
  301. glTranslatef(0.0,-0.3,0.0);
  302. drawArticulacion(BRAZOID);
  303. glRotatef(angulos[BRAZOID],1,0,0);
  304. glTranslatef(0.0,-0.3,0.0);
  305.  
  306. glScalef(0.5,1.0,0.5);
  307. drawCube(1.00,0.0,0.0);
  308. }
  309.  
  310. void drawPiernaSI(){
  311.  
  312. glTranslatef(-0.15,-0.35,0.0);
  313. drawArticulacion(PIERNASI);
  314. glRotatef(angulos[PIERNASI],1,0,0);
  315. glTranslatef(0.0,-0.35,0.0);
  316.  
  317. glScalef(0.25,1.0,0.25);
  318. drawCube(1.00,0.0,0.0);
  319. }
  320. void drawPiernaII(){
  321.  
  322. glTranslatef(0.0,-0.35,0.0);
  323. drawArticulacion(PIERNAII);
  324. glRotatef(angulos[PIERNAII],1,0,0);
  325. glTranslatef(0.0,-0.35,0.0);
  326.  
  327. glScalef(0.3,1.0,0.3);
  328. drawCube(1.00,0.0,0.0);
  329. }
  330.  
  331. void drawPiernaSD(){
  332.  
  333. glTranslatef(0.15,-0.35,0.0);
  334. drawArticulacion(PIERNASD);
  335. glRotatef(angulos[PIERNASD],1,0,0);
  336. glTranslatef(0.0,-0.35,0.0);
  337.  
  338. glScalef(0.25,1.0,0.25);
  339. drawCube(1.00,0.0,0.0);
  340. }
  341. void drawPiernaID(){
  342.  
  343. glTranslatef(0.0,-0.35,0.0);
  344. drawArticulacion(PIERNAID);
  345. glRotatef(angulos[PIERNAID],1,0,0);
  346. glTranslatef(0.0,-0.35,0.0);
  347.  
  348. glScalef(0.3,1.0,0.3);
  349. drawCube(1.00,0.0,0.0);
  350. }
  351.  
  352. void drawCube(float r, float g, float b){
  353. glColor3f(r,g,b);
  354. glutSolidCube(CUBE_SIZE);
  355. }
  356.  
  357.  
  358. void drawArticulacion(int artNum){
  359. if (artNum == articulacion){
  360. glColor3f(0.9,0.9,0.1);
  361. }else{
  362. glColor3f(1.0,0.0,1.0);
  363. }
  364. glutSolidSphere(SPHERE_SIZE, SPHERE_SLICES, SPHERE_STACKS);
  365. }
  366.  
  367. //Dibujo completo especificando desde donde ve la c?mara y hacia donde ve
  368. void display(void)
  369. {
  370. glClearColor(1.0f, 1.0f, 1.0f, 0.0f) ; // fondo blanco
  371. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // borrar el buffer de color y de profundidad.
  372.  
  373. glMatrixMode(GL_MODELVIEW) ;
  374. glLoadIdentity() ;
  375. gluLookAt(0, 0, -10, 0, 0, 0, 0, 1, 0) ; // para ver de lado el cubo y desde un poco m?s arriba
  376.  
  377. glRotatef(xrot, 1.0f, 0.0f, 0.0f);
  378. glRotatef(yrot, 0.0f, 1.0f, 0.0f);
  379.  
  380.  
  381. glRotatef(angulos[9], 0.0, 1.0, 0.0);
  382. drawBody();
  383.  
  384. glutSwapBuffers(); // intercambiar el buffer de dibujado por el buffer de despliegue.
  385. }
  386.  
  387. //Inicializaci?n general
  388. void init(){
  389. glColor3f(1.0, 1.0, 1.0);
  390. glEnable(GL_DEPTH_TEST);
  391.  
  392. glGetFloatv(GL_MODELVIEW_MATRIX,torso.m);
  393. torso.f=drawTorso;
  394. torso.hijo = &cabeza;
  395. torso.hermano = NULL;
  396.  
  397. glGetFloatv(GL_MODELVIEW_MATRIX,cabeza.m);
  398. cabeza.f=drawCabeza;
  399. cabeza.hijo=NULL;
  400. cabeza.hermano=&brazoSI;
  401.  
  402. glGetFloatv(GL_MODELVIEW_MATRIX,brazoSI.m);
  403. brazoSI.f=drawBrazoSI;
  404. brazoSI.hijo=&brazoII;
  405. brazoSI.hermano=&brazoSD;
  406.  
  407. glGetFloatv(GL_MODELVIEW_MATRIX,brazoII.m);
  408. brazoII.f=drawBrazoII;
  409. brazoII.hijo=NULL;
  410. brazoII.hermano=NULL;
  411.  
  412. glGetFloatv(GL_MODELVIEW_MATRIX,brazoSD.m);
  413. brazoSD.f=drawBrazoSD;
  414. brazoSD.hijo=&brazoID;
  415. brazoSD.hermano=&piernaSI;
  416.  
  417. glGetFloatv(GL_MODELVIEW_MATRIX,brazoID.m);
  418. brazoID.f=drawBrazoID;
  419. brazoID.hijo=NULL;
  420. brazoID.hermano=NULL;
  421.  
  422. glGetFloatv(GL_MODELVIEW_MATRIX,piernaSI.m);
  423. piernaSI.f=drawPiernaSI;
  424. piernaSI.hijo=&piernaII;
  425. piernaSI.hermano=&piernaSD;
  426.  
  427. glGetFloatv(GL_MODELVIEW_MATRIX,piernaII.m);
  428. piernaII.f=drawPiernaII;
  429. piernaII.hijo=NULL;
  430. piernaII.hermano=NULL;
  431.  
  432. glGetFloatv(GL_MODELVIEW_MATRIX,piernaSD.m);
  433. piernaSD.f=drawPiernaSD;
  434. piernaSD.hijo=&piernaID;
  435. piernaSD.hermano=NULL;
  436.  
  437. glGetFloatv(GL_MODELVIEW_MATRIX,piernaID.m);
  438. piernaID.f=drawPiernaID;
  439. piernaID.hijo=NULL;
  440. piernaID.hermano=NULL;
  441.  
  442. a.posX = 0;
  443. a.posY = 0;
  444. a.posZ = 0;
  445.  
  446. b.posX = 0;
  447. b.posY = 2.2;
  448. b.posZ = 0;
  449.  
  450.  
  451. c.posX = 2;
  452. c.posY = 0;
  453. c.posZ = 0;
  454.  
  455. d.posX = 2;
  456. d.posY = 2;
  457. d.posZ = 0;
  458.  
  459. resorte.particle1 = &a;
  460. resorte.particle2 = &b;
  461. resorte.length = 1;
  462. resorte.enlongation = 0;
  463.  
  464. resorte2.particle1 = &a;
  465. resorte2.particle2 = &c;
  466. resorte2.length = 1;
  467. resorte2.enlongation = 0;
  468.  
  469. resorte3.particle1 = &c;
  470. resorte3.particle2 = &d;
  471. resorte3.length = 1;
  472. resorte3.enlongation = 0;
  473.  
  474.  
  475. resorte4.particle1 = &b;
  476. resorte4.particle2 = &d;
  477. resorte4.length = 1;
  478. resorte4.enlongation = 0;
  479.  
  480. resorte5.particle1 = &a;
  481. resorte5.particle2 = &d;
  482. resorte5.length = 1;
  483. resorte5.enlongation = 0;
  484. }
  485.  
  486. void special(int c, int x, int y){
  487.  
  488. if( c== GLUT_KEY_UP){
  489. angulos[articulacion]+=2.0;
  490.  
  491. }
  492. if(c==GLUT_KEY_DOWN){
  493. angulos[articulacion]-=2.0;
  494.  
  495. }
  496. if ( c==GLUT_KEY_PAGE_DOWN){
  497.  
  498. }
  499. if ( c==GLUT_KEY_PAGE_UP){
  500.  
  501. }
  502. glutPostRedisplay();
  503. }
  504.  
  505. void key(unsigned char c, int x, int y){
  506.  
  507. if(c>='0'|| c<='8'){
  508. articulacion = c-48;
  509. glutPostRedisplay();
  510. }
  511.  
  512. }
  513. void mouse(int button, int state, int x, int y)
  514. {
  515. if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  516. {
  517. mouseDown = true;
  518.  
  519. xdiff = x - yrot;
  520. ydiff = -y + xrot;
  521. }
  522. else
  523. mouseDown = false;
  524. }
  525.  
  526. void mouseMotion(int x, int y)
  527. {
  528. if (mouseDown)
  529. {
  530. yrot = x - xdiff;
  531. xrot = y + ydiff;
  532.  
  533. glutPostRedisplay();
  534. }
  535. }
  536.  
  537.  
  538.  
  539. int main(int argc, char **argv)
  540. {
  541. int mode = GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH; // Modo de despliegue: Colores RGB, Doble buffer para despliegue
  542. glutInitDisplayMode(mode); // Inicializar modo de despliegue.
  543. glutInitWindowSize(500,500); // Inicializar tama?o de la ventana
  544. glutInit(&argc, argv); // Inicializar GLUT
  545. glutCreateWindow("Humanoide"); // Crear una ventana con el t?tulo indicado.
  546. glutDisplayFunc(display); // Indicar la funci?n para desplegar.
  547. glutReshapeFunc(reshape); // Indicar la funci?n en caso de cambio de tama?o de la ventana
  548.  
  549. glutSpecialFunc(special);
  550. glutKeyboardFunc(key);
  551.  
  552. glutIdleFunc(idle);
  553.  
  554. glutMouseFunc(mouse);
  555. glutMotionFunc(mouseMotion);
  556.  
  557. init(); // Inicializaci?n general
  558. glutMainLoop(); // Pasar el control a GLUT.
  559. return 0; // Regresar 0 por cortes?a.
  560. }
Add Comment
Please, Sign In to add comment