Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Based on a humanoid xD
- */
- #include <GL/glut.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #include <stdbool.h>
- #include <time.h>
- #define SPHERE_SLICES 10
- #define SPHERE_STACKS 10
- #define CUBE_SIZE 0.5
- #define SPHERE_SIZE 0.1
- float cubeAngle=0;
- float incr=0.5;
- float size=0.5;
- bool mouseDown = false;
- float xrot = 0.0f;
- float yrot = 0.0f;
- float xdiff = 0.0f;
- float ydiff = 0.0f;
- enum articulaciones {CABEZA, BRAZOSI, BRAZOII, BRAZOSD, BRAZOID, PIERNASI, PIERNAII, PIERNASD, PIERNAID, TORSO_ART};
- int articulacion=TORSO_ART;
- float angulos[10] ={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
- struct nodo {
- float m[16];
- void(*f)();
- struct nodo *hijo;
- struct nodo *hermano;
- };
- struct nodo torso, cabeza, brazoSD, brazoSI, brazoID, brazoII, piernaSI, piernaSD, piernaII, piernaID;
- typedef struct particle {
- float posX;
- float posY;
- float posZ;
- float velX;
- float velY;
- float velZ;
- float fX;
- float fY;
- float fZ;
- }particle;
- particle a,b,c,d,e,f;
- typedef struct spring
- {
- particle * particle1;
- particle * particle2;
- float length;
- float enlongation;
- }spring;
- spring resorte, resorte2, resorte3, resorte4, resorte5;
- //Por si cambia el tama?o de la ventana, para que el cubo guarde la misma proporci?n
- void reshape(int w, int h)
- {
- glViewport (0, 0, (GLsizei) w, (GLsizei) h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.0, (GLdouble)w/(GLdouble)h, 1.0, 100.0) ;
- }
- void drawBody(){
- //traverse(&torso);
- glPointSize(20);
- glBegin(GL_POINTS);
- glColor3f(1,0,1);
- glVertex3f(a.posX,a.posY,a.posZ);
- glColor3f(0,0,1);
- glVertex3f(b.posX,b.posY,b.posZ);
- glColor3f(1,0,0);
- glVertex3f(c.posX,c.posY,c.posZ);
- glColor3f(1,1,0);
- glVertex3f(d.posX,d.posY,d.posZ);
- glEnd();
- }
- void actualizaParticulas(){
- float g= 0.00000;
- a.velX += a.fX;
- a.velY += a.fY;
- a.velZ += a.fZ;
- b.fY -= g;
- c.fY -= g;
- b.velX += b.fX;
- b.velY += b.fY;
- b.velZ += b.fZ;
- printf("vel by: %f",b.velY);
- c.velX += c.fX;
- c.velY += c.fY;
- c.velZ += c.fZ;
- d.velX += d.fX;
- d.velY += d.fY;
- d.velZ += d.fZ;
- a.posX += a.velX;
- a.posY += a.velY;
- a.posZ += a.velZ;
- b.posX += b.velX;
- b.posY += b.velY;
- b.posZ += b.velZ;
- c.posX += c.velX;
- c.posY += c.velY;
- c.posZ += c.velZ;
- d.posX += d.velX;
- d.posY += d.velY;
- d.posZ += d.velZ;
- }
- void thingy(spring * res){
- particle * a;
- particle * b;
- a = res->particle1;
- b = res->particle2;
- float distancia = sqrt(pow(a->posX - b->posX,2)+pow(a->posY - b->posY,2)+pow(a->posZ - b->posZ,2));
- float centro [3] = {a->posX+(a->posX - b->posX)/2, a->posY+(a->posY - b->posY)/2, a->posZ+(a->posZ - b->posZ)/2 };
- float enlonga= distancia-res->length;
- printf("enl: %f - dist: %f \n",enlonga,distancia);
- float hookForce=0.0;// = -(0.0003)*(enlonga);
- //Hacer un metodo que nos diga de que lado esta cada particula
- float x,y,z;
- x = abs(a->posX - b->posX)/distancia;
- y = abs(a->posY - b->posY)/distancia;
- z = abs(a->posZ - b->posZ)/distancia;
- float difvel [3]= {a->velX-b->velX,a->velY-b->velY,a->velZ - b->velZ};
- float distancia_unit[3] = {x,y,z};
- float elast = .00050;
- float difvel_times_elast[3] = {difvel[0]*elast, difvel[1]*elast, difvel[2]*elast};
- 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];
- //hookForce = - 0.0003*(enlonga)*distancia-elast*difvel;
- float first_term = -(0.0000003 * (distancia - res->length)+product_difvel_distance);
- float hookForce_vector [3] = {first_term*x, first_term*y, first_term*z};
- //printf("bb: elast (%f) * difvel (%f) = %f\n",elast,difvel,elast*difvel);
- if ( a->posX > centro[0]){
- //fuerza negativa
- a->fX -= (hookForce_vector[0]) ;
- b->fX += (hookForce_vector[0]);
- }else{
- a->fX += hookForce_vector[0];
- b->fX -= hookForce_vector[0];
- }
- if ( a->posY > centro[1]){
- //fuerza negativa
- a->fY -= (hookForce_vector[1]);
- b->fY += (hookForce_vector[1]);
- }else{
- a->fY += hookForce_vector[1];
- b->fY -= hookForce_vector[1];
- }
- if ( a->posZ > centro[2]){
- //fuerza negativa
- a->fZ -= (hookForce_vector[2]);
- b->fZ += (hookForce_vector[2]);
- }else{
- a->fZ += hookForce_vector[2];
- b->fZ -= hookForce_vector[2];
- }
- }
- void resetForces(particle *p ){
- p->fX=0;
- p->fY=0;
- p->fZ=0;
- }
- void resortea(){
- resetForces(&a);
- resetForces(&b);
- resetForces(&c);
- resetForces(&d);
- thingy(&resorte);
- thingy(&resorte2);
- thingy(&resorte3);
- thingy(&resorte4);
- thingy(&resorte4);
- }
- void idle(){
- resortea();
- actualizaParticulas();
- glutPostRedisplay();
- }
- void traverse (struct nodo *node)
- {
- // guardar la matriz actual porque las transformaciones a realizarse
- // sólo deben afectarle a él y a sus hijos
- glPushMatrix();
- // transformar relativo a su padre
- glMultMatrixf(node->m);
- // dibujar el nodo
- node->f();
- // primer recorrer los hijos (si hay)
- if (node->hijo != NULL)
- traverse(node->hijo);
- glPopMatrix();
- // después recorrer los hermanos (si hay)
- if(node->hermano != NULL)
- traverse(node->hermano);
- }
- void drawTorso(){
- //Torso
- glPushMatrix();
- drawCube();
- glPopMatrix();
- }
- void drawCabeza(){
- glTranslatef(0.0,0.3,0.0);
- drawArticulacion(CABEZA);
- glRotatef(angulos[CABEZA],1,0,0);
- glTranslatef(0.0,0.15,0.0);
- glScalef(0.5,0.5,0.5);
- drawCube(1.0,0.5,0.4);
- }
- void drawBrazoSI(){
- glTranslatef(-0.25,0.2,0.0);
- drawArticulacion(BRAZOSI);
- glRotatef(angulos[BRAZOSI],1,0,0);
- glTranslatef(-0.2,-0.2,0.0);
- glScalef(0.5,1.0,0.5);
- drawCube(1.00,0.0,0.0);
- }
- void drawBrazoII(){
- glTranslatef(0.0,-0.3,0.0);
- drawArticulacion(BRAZOII);
- glRotatef(angulos[BRAZOII],1,0,0);
- glTranslatef(0.0,-0.3,0.0);
- glScalef(0.5,1.0,0.5);
- drawCube(1.00,0.0,0.0);
- }
- void drawBrazoSD(){
- glTranslatef(0.25,0.2,0.0);
- drawArticulacion(BRAZOSD);
- glRotatef(angulos[BRAZOSD],1,0,0);
- glTranslatef(0.2,-0.2,0.0);
- glScalef(0.5,1.0,0.5);
- drawCube(1.00,0.0,0.0);
- }
- void drawBrazoID(){
- glTranslatef(0.0,-0.3,0.0);
- drawArticulacion(BRAZOID);
- glRotatef(angulos[BRAZOID],1,0,0);
- glTranslatef(0.0,-0.3,0.0);
- glScalef(0.5,1.0,0.5);
- drawCube(1.00,0.0,0.0);
- }
- void drawPiernaSI(){
- glTranslatef(-0.15,-0.35,0.0);
- drawArticulacion(PIERNASI);
- glRotatef(angulos[PIERNASI],1,0,0);
- glTranslatef(0.0,-0.35,0.0);
- glScalef(0.25,1.0,0.25);
- drawCube(1.00,0.0,0.0);
- }
- void drawPiernaII(){
- glTranslatef(0.0,-0.35,0.0);
- drawArticulacion(PIERNAII);
- glRotatef(angulos[PIERNAII],1,0,0);
- glTranslatef(0.0,-0.35,0.0);
- glScalef(0.3,1.0,0.3);
- drawCube(1.00,0.0,0.0);
- }
- void drawPiernaSD(){
- glTranslatef(0.15,-0.35,0.0);
- drawArticulacion(PIERNASD);
- glRotatef(angulos[PIERNASD],1,0,0);
- glTranslatef(0.0,-0.35,0.0);
- glScalef(0.25,1.0,0.25);
- drawCube(1.00,0.0,0.0);
- }
- void drawPiernaID(){
- glTranslatef(0.0,-0.35,0.0);
- drawArticulacion(PIERNAID);
- glRotatef(angulos[PIERNAID],1,0,0);
- glTranslatef(0.0,-0.35,0.0);
- glScalef(0.3,1.0,0.3);
- drawCube(1.00,0.0,0.0);
- }
- void drawCube(float r, float g, float b){
- glColor3f(r,g,b);
- glutSolidCube(CUBE_SIZE);
- }
- void drawArticulacion(int artNum){
- if (artNum == articulacion){
- glColor3f(0.9,0.9,0.1);
- }else{
- glColor3f(1.0,0.0,1.0);
- }
- glutSolidSphere(SPHERE_SIZE, SPHERE_SLICES, SPHERE_STACKS);
- }
- //Dibujo completo especificando desde donde ve la c?mara y hacia donde ve
- void display(void)
- {
- glClearColor(1.0f, 1.0f, 1.0f, 0.0f) ; // fondo blanco
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // borrar el buffer de color y de profundidad.
- glMatrixMode(GL_MODELVIEW) ;
- glLoadIdentity() ;
- gluLookAt(0, 0, -10, 0, 0, 0, 0, 1, 0) ; // para ver de lado el cubo y desde un poco m?s arriba
- glRotatef(xrot, 1.0f, 0.0f, 0.0f);
- glRotatef(yrot, 0.0f, 1.0f, 0.0f);
- glRotatef(angulos[9], 0.0, 1.0, 0.0);
- drawBody();
- glutSwapBuffers(); // intercambiar el buffer de dibujado por el buffer de despliegue.
- }
- //Inicializaci?n general
- void init(){
- glColor3f(1.0, 1.0, 1.0);
- glEnable(GL_DEPTH_TEST);
- glGetFloatv(GL_MODELVIEW_MATRIX,torso.m);
- torso.f=drawTorso;
- torso.hijo = &cabeza;
- torso.hermano = NULL;
- glGetFloatv(GL_MODELVIEW_MATRIX,cabeza.m);
- cabeza.f=drawCabeza;
- cabeza.hijo=NULL;
- cabeza.hermano=&brazoSI;
- glGetFloatv(GL_MODELVIEW_MATRIX,brazoSI.m);
- brazoSI.f=drawBrazoSI;
- brazoSI.hijo=&brazoII;
- brazoSI.hermano=&brazoSD;
- glGetFloatv(GL_MODELVIEW_MATRIX,brazoII.m);
- brazoII.f=drawBrazoII;
- brazoII.hijo=NULL;
- brazoII.hermano=NULL;
- glGetFloatv(GL_MODELVIEW_MATRIX,brazoSD.m);
- brazoSD.f=drawBrazoSD;
- brazoSD.hijo=&brazoID;
- brazoSD.hermano=&piernaSI;
- glGetFloatv(GL_MODELVIEW_MATRIX,brazoID.m);
- brazoID.f=drawBrazoID;
- brazoID.hijo=NULL;
- brazoID.hermano=NULL;
- glGetFloatv(GL_MODELVIEW_MATRIX,piernaSI.m);
- piernaSI.f=drawPiernaSI;
- piernaSI.hijo=&piernaII;
- piernaSI.hermano=&piernaSD;
- glGetFloatv(GL_MODELVIEW_MATRIX,piernaII.m);
- piernaII.f=drawPiernaII;
- piernaII.hijo=NULL;
- piernaII.hermano=NULL;
- glGetFloatv(GL_MODELVIEW_MATRIX,piernaSD.m);
- piernaSD.f=drawPiernaSD;
- piernaSD.hijo=&piernaID;
- piernaSD.hermano=NULL;
- glGetFloatv(GL_MODELVIEW_MATRIX,piernaID.m);
- piernaID.f=drawPiernaID;
- piernaID.hijo=NULL;
- piernaID.hermano=NULL;
- a.posX = 0;
- a.posY = 0;
- a.posZ = 0;
- b.posX = 0;
- b.posY = 2.2;
- b.posZ = 0;
- c.posX = 2;
- c.posY = 0;
- c.posZ = 0;
- d.posX = 2;
- d.posY = 2;
- d.posZ = 0;
- resorte.particle1 = &a;
- resorte.particle2 = &b;
- resorte.length = 1;
- resorte.enlongation = 0;
- resorte2.particle1 = &a;
- resorte2.particle2 = &c;
- resorte2.length = 1;
- resorte2.enlongation = 0;
- resorte3.particle1 = &c;
- resorte3.particle2 = &d;
- resorte3.length = 1;
- resorte3.enlongation = 0;
- resorte4.particle1 = &b;
- resorte4.particle2 = &d;
- resorte4.length = 1;
- resorte4.enlongation = 0;
- resorte5.particle1 = &a;
- resorte5.particle2 = &d;
- resorte5.length = 1;
- resorte5.enlongation = 0;
- }
- void special(int c, int x, int y){
- if( c== GLUT_KEY_UP){
- angulos[articulacion]+=2.0;
- }
- if(c==GLUT_KEY_DOWN){
- angulos[articulacion]-=2.0;
- }
- if ( c==GLUT_KEY_PAGE_DOWN){
- }
- if ( c==GLUT_KEY_PAGE_UP){
- }
- glutPostRedisplay();
- }
- void key(unsigned char c, int x, int y){
- if(c>='0'|| c<='8'){
- articulacion = c-48;
- glutPostRedisplay();
- }
- }
- void mouse(int button, int state, int x, int y)
- {
- if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
- {
- mouseDown = true;
- xdiff = x - yrot;
- ydiff = -y + xrot;
- }
- else
- mouseDown = false;
- }
- void mouseMotion(int x, int y)
- {
- if (mouseDown)
- {
- yrot = x - xdiff;
- xrot = y + ydiff;
- glutPostRedisplay();
- }
- }
- int main(int argc, char **argv)
- {
- int mode = GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH; // Modo de despliegue: Colores RGB, Doble buffer para despliegue
- glutInitDisplayMode(mode); // Inicializar modo de despliegue.
- glutInitWindowSize(500,500); // Inicializar tama?o de la ventana
- glutInit(&argc, argv); // Inicializar GLUT
- glutCreateWindow("Humanoide"); // Crear una ventana con el t?tulo indicado.
- glutDisplayFunc(display); // Indicar la funci?n para desplegar.
- glutReshapeFunc(reshape); // Indicar la funci?n en caso de cambio de tama?o de la ventana
- glutSpecialFunc(special);
- glutKeyboardFunc(key);
- glutIdleFunc(idle);
- glutMouseFunc(mouse);
- glutMotionFunc(mouseMotion);
- init(); // Inicializaci?n general
- glutMainLoop(); // Pasar el control a GLUT.
- return 0; // Regresar 0 por cortes?a.
- }
Add Comment
Please, Sign In to add comment