Search Projects

Wednesday, July 6, 2011

Fluids Demonstration

Description:
USES:
Project Code:

#include <stdlib.h>
#include <GL/glut.h>
#include<time.h>
#include<stdio.h>
#include<math.h>


void display(void);
void fluids();




/* for cuboid*/
GLfloat vertices[][3] = {{-1.0,-1.8,-1.0},{1.0,-1.8,-1.0},
{1.0,1.8,-1.0}, {-1.0,1.8,-1.0}, {-1.0,-1.8,1.0},
{1.0,-1.8,1.0}, {1.0,1.8,1.0}, {-1.0,1.8,1.0}};


GLfloat normals[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},
{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0},
{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};


GLfloat colors[][3] = {{0.137225,0.556863,0.137255},{0.137225,0.556863,0.137255},{0.137225,0.556863,0.137255},{0.137225,0.556863,0.137255},
{0.137225,0.556863,0.137255},{0.137225,0.556863,0.137255},{0.137225,0.556863,0.137255},{0.137225,0.556863,0.137255}};




/* for loop */
GLfloat vertice[][3]={{ 0.5,1.0,0.25},{2.5,1.0,0.25},{2.5,0.25,0.25},{2.0,0.25,0.25},{2.0,0.0,0.25},{2.5,0.0,0.25},
{2.5,-0.75,0.25},{0.5,-0.75,0.25},{0.5,0.0,0.25},{1.25,0.0,0.25},{1.25,0.25,0.25},{0.5,0.25,0.25},
{0.75,0.75,0.25},{2.25,0.75,0.25},{2.25,0.5,0.25},{1.75,0.5,0.25},{1.75,-0.25,0.25},{2.25,-0.25,0.25},{2.25,-0.5,0.25},
{0.75,-0.5,0.25},{0.75,-0.25,0.25},{1.5,-0.25,0.25},{1.5,0.5,0.25},{0.75,0.5,0.25},




{0.5,1.0,-0.25},{2.5,1.0,-0.25},{2.5,0.25,-0.25},{2.0,0.25,-0.25},{2.0,0.0,-0.25},{2.5,0.0,-0.25},
{2.5,-0.75,-0.25},{0.5,-0.75,-0.25},{0.5,0.0,-0.25},{1.25,0.0,-0.25},{1.25,0.25,-0.25},{0.5,0.25,-0.25},
{0.75,0.75,-0.25},{2.25,0.75,-0.25},{2.25,0.5,-0.25},{1.75,0.5,-0.25},{1.75,-0.25,-0.25},{2.25,-0.25,-0.25},{2.25,-0.5,-0.25},
{0.75,-0.5,-0.25},{0.75,-0.25,-0.25},{1.5,-0.25,-0.25},{1.5,0.5,-0.25},{0.75,0.5,-0.25}};


GLfloat normal[][3]={{ 0.5,1.0,0.25},{2.5,1.0,0.25},{2.5,0.25,0.25},{2.0,0.25,0.25},{2.0,0.0,0.25},{2.5,0.0,0.25},
{2.5,-0.75,0.25},{0.5,-0.75,0.25},{0.5,0.0,0.25},{1.25,0.0,0.25},{1.25,0.25,0.25},{0.5,0.25,0.25},
{0.75,0.75,0.25},{2.25,0.75,0.25},{2.25,0.5,0.25},{1.75,0.5,0.25},{1.75,-0.25,0.25},{2.25,-0.25,0.25},{2.25,-0.5,0.25},
{0.75,-0.5,0.25},{0.75,-0.25,0.25},{1.5,-0.25,0.25},{1.5,0.5,0.25},{0.75,0.5,0.25},




{0.5,1.0,-0.25},{2.5,1.0,-0.25},{2.5,0.25,-0.25},{2.0,0.25,-0.25},{2.0,0.0,-0.25},{2.5,0.0,-0.25},
{2.5,-0.75,-0.25},{0.5,-0.75,-0.25},{0.5,0.0,-0.25},{1.25,0.0,-0.25},{1.25,0.25,-0.25},{0.5,0.25,-0.25},
{0.75,0.75,-0.25},{2.25,0.75,-0.25},{2.25,0.5,-0.25},{1.75,0.5,-0.25},{1.75,-0.25,-0.25},{2.25,-0.25,-0.25},{2.25,-0.5,-0.25},
{0.75,-0.5,-0.25},{0.75,-0.25,-0.25},{1.5,-0.25,-0.25},{1.5,0.5,-0.25},{0.75,0.5,-0.25}};






/* for fluid to move in loop*/

GLfloat vert[][3]={{0.50,1.0,0.25},{0.50,0.75,0.25},{0.70,0.75,0.25},{0.70,1.0,0.25},
{0.50,1.0,-0.25},{0.50,0.75,-0.25},{0.70,0.75,-0.25},{0.70,1.0,-0.25}};



GLfloat norm[][3]={{0.5,1.0,0.25},{0.5,0.75,0.25},{1.0,0.75,0.25},{1.0,1.0,0.25},
{0.5,1.0,-0.25},{0.5,0.75,-0.25},{1.0,0.75,-0.25},{1.0,1.0,-0.25}};



/* for handle */

GLfloat ver[][3]={{-1.0,0.85,0.05},{-1.0,0.75,0.05},{-1.10,0.85,0.05},{-1.10,0.35,0.05},{-1.0,0.35,0.05},
{-1.0,0.45,0.05},{-1.05,0.45,0.05},{-1.05,0.75,0.05},{-1.05,0.85,0.05},{-1.05,0.35,0.05},
{-1.0,0.85,-0.05},{-1.0,0.75,-0.05},{-1.10,0.85,-0.05},{-1.10,0.35,-0.05},{-1.0,0.35,-0.05},
{-1.0,0.45,-0.05},{-1.05,0.45,-0.05},{-1.05,0.75,-0.05},{-1.05,0.85,-0.05},{-1.05,0.35,-0.05}};

GLfloat nor[][3]={{-1.0,0.75,0.15},{-1.0,0.65,0.15},{-1.10,0.75,0.15},{-1.10,0.25,0.15},{-1.0,0.25,0.15},
{-1.0,0.35,0.15},{-1.05,0.35,0.15},{-1.05,0.65,0.15},{-1.05,0.75,0.15},{-1.05,0.25,0.15},
{-1.0,0.75,-0.15},{-1.0,0.65,-0.15},{-1.10,0.75,-0.15},{-1.10,0.25,-0.15},{-1.0,0.25,-0.15},
{-1.0,0.35,-0.15},{-1.05,0.35,-0.15},{-1.05,0.65,-0.15},{-1.05,0.75,-0.15},{-1.05,0.25,-0.15}};


/*
GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0},
{1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0},
{1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}};




*/




/* for left arrow */

GLfloat v[][3]={{-0.10,0,0.25},{0.15,0,0.25},{0.15,0.15,0.25},{0.4,-0.15,0.25},{0.15,-0.40,0.25},{0.15,-0.25,0.25},{-0.10,-0.25,0.25},

{-0.1,0,-0.25},{0.15,0,-0.25},{0.15,0.15,-0.25},{0.4,-0.15,-0.25},{0.15,-0.40,-0.25},{0.15,-0.25,-0.25},{-0.10,-0.25,-0.25}};


GLfloat n[][3]={{0,0,0.25},{0.25,0,0.25},{0.25,0.15,0.25},{0.5,-0.15,0.25},{0.25,-0.40,0.25},{0.25,-0.25,0.25},{0,-0.25,0.25},

{0,0,-0.25},{0.25,0,-0.25},{0.25,0.15,-0.25},{0.5,-0.15,-0.25},{0.25,-0.40,-0.25},{0.25,-0.25,-0.25},{0,-0.25,-0.25}};


/*  right arrow  */

GLfloat v1[][3]={{2.60,0,0.25},{2.85,0,0.25},{2.85,0.15,0.25},{3.1,-0.15,0.25},{2.85,-0.40,0.25},{2.85,-0.25,0.25},{2.60,-0.25,0.25},

{2.60,0,-0.25},{2.85,0,-0.25},{2.85,0.15,-0.25},{3.10,-0.15,-0.25},{2.85,-0.40,-0.25},{2.85,-0.25,-0.25},{2.60,-0.25,-0.25}};




GLfloat n1[][3]={{2.60,0,0.25},{2.85,0,0.25},{2.85,0.15,0.25},{3.1,-0.15,0.25},{2.85,-0.40,0.25},{2.85,-0.25,0.25},{2.60,-0.25,0.25},

{2.60,0,-0.25},{2.85,0,-0.25},{2.85,0.15,-0.25},{3.10,-0.15,-0.25},{2.85,-0.40,-0.25},{2.85,-0.25,-0.25},{2.60,-0.25,-0.25}};


void sleep(unsigned int msec)
{
clock_t goal = msec + clock();
     while(goal>clock());
}



/* for cuboid */

void polygon(int a, int b, int c , int d)
{
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glNormal3fv(normals[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[b]);
glNormal3fv(normals[b]);
glVertex3fv(vertices[b]);
glColor3fv(colors[c]);
glNormal3fv(normals[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glNormal3fv(normals[d]);
glVertex3fv(vertices[d]);
glEnd();
}

/* for loop*/

void polygon1(int a, int b,int c, int d)
{
glBegin(GL_POLYGON);
// glColor3fv(colors[a]);
glNormal3fv(normal[a]);
glVertex3fv(vertice[a]);
// glColor3fv(colors[b]);
glNormal3fv(normal[b]);
glVertex3fv(vertice[b]);
// glColor3fv(colors[c]);
glNormal3fv(normal[c]);
glVertex3fv(vertice[c]);
// glColor3fv(colors[d]);
glNormal3fv(normal[d]);
glVertex3fv(vertice[d]);
glEnd();
}

/* for fluid */


void fluid(int a,int b, int c, int d)
{

glBegin(GL_POLYGON);
glColor3fv(colors[a]);
// glNormal3fv(norm[a]);
glVertex3fv(vert[a]);
glColor3fv(colors[b]);
//glNormal3fv(norm[b]);
glVertex3fv(vert[b]);
glColor3fv(colors[c]);
// glNormal3fv(norm[c]);
glVertex3fv(vert[c]);
glColor3fv(colors[d]);
// glNormal3fv(norm[d]);
glVertex3fv(vert[d]);
glEnd();


}

/* for handle */

void hand(int a,int b, int c, int d)
{

glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glNormal3fv(nor[a]);
glVertex3fv(ver[a]);
glColor3fv(colors[b]);
glNormal3fv(nor[b]);
glVertex3fv(ver[b]);
glColor3fv(colors[c]);
glNormal3fv(nor[c]);
glVertex3fv(ver[c]);
glColor3fv(colors[d]);
glNormal3fv(nor[d]);
glVertex3fv(ver[d]);
glEnd();


}

void leftarrow(int a, int b, int c , int d)
{
glBegin(GL_QUADS);
glColor3f(1.0,0.0,0.0);
// glColor3fv(colors[a]);
// glNormal3fv(n[a]);
glVertex3fv(v[a]);
// glColor3fv(colors[b]);
// glNormal3fv(n[b]);
glVertex3fv(v[b]);
// glColor3fv(colors[c]);
// glNormal3fv(n[c]);
glVertex3fv(v[c]);
// glColor3fv(colors[d]);
// glNormal3fv(n[d]);
glVertex3fv(v[d]);
glEnd();
}



void rightarrow(int a, int b, int c , int d)
{
glBegin(GL_POLYGON);
// glColor3fv(colors[a]);
glNormal3fv(n1[a]);
glVertex3fv(v1[a]);
// glColor3fv(colors[b]);
glNormal3fv(n1[b]);
glVertex3fv(v1[b]);
// glColor3fv(colors[c]);
glNormal3fv(n1[c]);
glVertex3fv(v1[c]);
// glColor3fv(colors[d]);
glNormal3fv(n1[d]);
glVertex3fv(v1[d]);
glEnd();
}


void colorcube()
{
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);

}
void pipe()
{
polygon1(0,1,25,24);
polygon1(0,11,35,24);
polygon1(11,10,34,35);
polygon1(10,9,33,34);
polygon1(8,9,33,32);
polygon1(8,7,31,32);
polygon1(7,6,30,31);
polygon1(5,6,30,29);
polygon1(4,5,29,28);
polygon1(3,4,28,27);
polygon1(3,2,26,27);
polygon1(1,2,26,25);
polygon1(12,13,37,36);
polygon1(13,14,38,37);
polygon1(15,14,38,39);
polygon1(15,16,40,39);
polygon1(16,17,41,40);
polygon1(17,18,42,41);
polygon1(18,19,43,42);
polygon1(20,19,43,44);
polygon1(20,21,45,44);
polygon1(22,21,45,46);
polygon1(23,22,46,47);
polygon1(12,23,47,36);
}

void fluids()
{
fluid(0,1,2,3);
fluid(0,1,5,4);
fluid(0,3,7,4);
fluid(3,2,6,7);
fluid(1,2,6,5);
fluid(4,5,6,7);
}

void arrow1()
{
    leftarrow(0,1,8,7);
leftarrow(1,2,9,8);
leftarrow(2,3,10,9);
leftarrow(3,4,11,10);
leftarrow(4,5,12,11);
leftarrow(5,6,13,12);
leftarrow(0,6,13,7);
}

void arrow2()
{
    rightarrow(0,1,8,7);
rightarrow(1,2,9,8);
rightarrow(2,3,10,9);
rightarrow(3,4,11,10);
rightarrow(4,5,12,11);
rightarrow(5,6,13,12);
rightarrow(0,6,13,7);
}




void idleMouse(void)
{
  int j;
  float i;

for(j=1;j<=12;j++)
switch(j)
{
case 1:
  for(i=0;i<1.9;i+=.1)
  {   glPushMatrix();
 glTranslatef(i,0,0);
  fluids();
glPopMatrix();
sleep(15);
 glFlush();
  }
break;

case 2:
  for(i=0;i<.6;i+=.1)
  {   glPushMatrix();
 glTranslatef(1.8,-i,0);
  fluids();
glPopMatrix();
sleep(2);
 glFlush();
  }
break;

case 3:

   for(i=-1.8;i<-1.2;i+=.1)
  {   glPushMatrix();
 glTranslatef(-i,-.5,0);
  fluids();
glPopMatrix();
sleep(2);
 glFlush();
  }

break;


case 4:

  for(i=0.5;i<1.1;i+=.1)
  {   glPushMatrix();
 glTranslatef(1.25,-i,0);
  fluids();
glPopMatrix();
sleep(5);
 glFlush();

  }

break;

case 5:


  for(i=1.25;i<1.8;i+=.1)
  {
       glPushMatrix();
 glTranslatef(i,-1,0);
  sleep(2);
 fluids();
glPopMatrix();
 glFlush();


  }
break;



case 6:

  for(i=1;i<1.6;i+=.1)
  {   glPushMatrix();
 glTranslatef(1.8,-i,0);
  fluids();
glPopMatrix();
sleep(2);
 glFlush();

  }

break;

case 7:

   for(i=-1.8;i<0;i+=.1)
  {   glPushMatrix();
 glTranslatef(-i,-1.5,0);
  fluids();
glPopMatrix();
sleep(15);
 glFlush();
  }

break;

case 8:
  for(i=-1.5;i<-.9;i+=.1)
  {
       glPushMatrix();
 glTranslatef(0,i,0);
  sleep(2);
 fluids();
glPopMatrix();
 glFlush();
  }
break;


case 9:

  for(i=0;i<.9;i+=.1)
  {   glPushMatrix();
 glTranslatef(i,-1,0);
  fluids();
glPopMatrix();
sleep(2);
 glFlush();

  }

break;
case 10:
  for(i=-.9;i<-.4;i+=.1)
  {
       glPushMatrix();
 glTranslatef(.8,i,0);
  sleep(15);
 fluids();
glPopMatrix();
 glFlush();
  }
break;


case 11:

   for(i=-.8;i<.1;i+=.1)
  {   glPushMatrix();
 glTranslatef(-i,-.5,0);
  fluids();
glPopMatrix();
sleep(5);
 glFlush();
  }

break;

case 12:
 for(i=-.5;i<0;i+=.1)
  {
       glPushMatrix();
glTranslatef(0,i,0);

 fluids();
glPopMatrix();
 glFlush();


  }
break;






}
     glFlush();



     glutPostRedisplay();
 glutSwapBuffers();
glFlush();
}


void handle()
{
hand(0,1,7,8);
hand(7,8,18,17);
hand(8,0,10,18);
hand(0,1,11,10);
hand(7,1,11,17);
hand(10,11,17,18);
hand(2,8,9,3);
hand(2,8,18,12);
hand(2,3,13,12);
hand(3,9,19,13);
hand(8,9,19,18);
hand(12,18,19,13);
hand(9,4,5,6);
hand(4,5,15,14);
hand(5,6,16,15);
hand(6,9,19,16);
hand(9,4,14,19);
hand(16,15,14,19);


}


void wire(int p,int q,int r,int s)
{
glBegin(GL_LINE_LOOP);
glColor3f(0.8,0.498039,0.196078);
glVertex3fv(vertices[p]);
glVertex3fv(vertices[q]);
glVertex3fv(vertices[r]);
glVertex3fv(vertices[s]);
    glEnd();
}

void shwire()
{
wire(0,3,2,1);
wire(2,3,7,6);
wire(0,4,7,3);
wire(1,2,6,5);
wire(4,5,6,7);
wire(0,1,5,4);
}






static GLfloat theta[] = {0.0,0.0,0.0};
static GLint axis = 2;
static GLdouble viewer[]= {0.0, 0.0, 5.0}; /* initial viewer location */


void display(void)
{

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

/* Update viewer position in modelview matrix */

glLoadIdentity();
gluLookAt(viewer[0],viewer[1],viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

/* rotate cube */

glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);



//colorcube();
shwire();
     pipe();
fluids();
handle();
     arrow1();
arrow2();
idleMouse();

 glutPostRedisplay();
 glFlush();
glutSwapBuffers();

}





void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 1;
// if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 2;
theta[axis] += 2.0;
if( theta[axis] > 360.0 ) theta[axis] -= 360.0;
display();
}



void myReshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w <= h)
        glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
            2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
    else
        glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
            2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);
    glMatrixMode(GL_MODELVIEW);
}

/*
void key(unsigned char k,int x,int y)
{
if(k=='1') glutIdleFunc(colorcube);
glutPostRedisplay();
if(k=='2') glutIdleFunc(shwire);
glutPostRedisplay();
if(k=='3') exit(0);
}

*/
void  main(int argc, char **argv)
{
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
 glutInitWindowSize(1020, 500);
 glutCreateWindow("Colorcube Viewer");
 glutReshapeFunc(myReshape);
 glutDisplayFunc(display);
glutMouseFunc(mouse);
glutIdleFunc(idleMouse);
//glutKeyboardFunc(key);
// myinit();
glEnable(GL_DEPTH_TEST);
 glutMainLoop();
}

No comments:

Post a Comment