Advertisement
FaisalAhemdBijoy

Bottle CURVED 3D Utsha Sir

Jun 6th, 2021
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.43 KB | None | 0 0
  1. /*
  2.  * GLUT Shapes Demo
  3.  *
  4.  * Written by Nigel Stewart November 2003
  5.  *
  6.  * This program is test harness for the sphere, cone
  7.  * and torus shapes in GLUT.
  8.  *
  9.  * Spinning wireframe and smooth shaded shapes are
  10.  * displayed until the ESC or q key is pressed.  The
  11.  * number of geometry stacks and slices can be adjusted
  12.  * using the + and - keys.
  13.  */
  14. #include <GL/gl.h>
  15. #ifdef __APPLE__
  16. #include <GLUT/glut.h>
  17. #else
  18. #include <GL/glut.h>
  19. #endif
  20.  
  21.  
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <math.h>
  25. #include <stdlib.h>
  26. #include <vector>
  27. #include <iostream>
  28.  
  29.  
  30. using namespace std;
  31.  
  32. const double PI = 3.14159265389;
  33.  
  34.  
  35. /* GLUT callback Handlers */
  36.  
  37.  
  38. int anglex= 0, angley = 0, anglez = 0;          //rotation angles
  39. int window;
  40. int wired=0;
  41. int shcpt=1;
  42. int animat = 0;
  43. const int L=20;
  44. const int dgre=3;
  45. int ncpt=L+1;
  46. int clikd=0;
  47. const int nt = 40;              //number of slices along x-direction
  48. const int ntheta = 20;
  49.  
  50.  
  51. GLfloat ctrlpoints[L+1][3] =
  52. {
  53.     { 0.0, 0.0, 0.0}, { -0.3, 0.5, 0.0},
  54.     { 0.1, 1.7, 0.0},{ 0.5, 1.5, 0.0},
  55.     {1.0, 1.5, 0.0}, {1.4, 1.4, 0.0},
  56.     {1.8, 0.4, 0.0},{2.2, 0.4, 0.0},
  57.     {2.6, 1.5, 0.0}, {3.0, 1.4, 0.0},
  58.     {3.4, 1.4, 0.0},{3.8, 1.4, 0.0},
  59.     {4.2, 1.0, 0.0},{4.6, 1.0, 0.0},
  60.     {5.0, 1.0, 0.0},{5.4, 1.0, 0.0},
  61.     {5.8, 0.5, 0.0},{6.2, 0.5, 0.0},
  62.     {6.6, 0.5, 0.0},{7.2, 0.2, 0.0},
  63.     {6.8, 0.52, 0.0}
  64. };
  65.  
  66.  
  67. double ex=0, ey=0, ez=15, lx=0,ly=0,lz=0, hx=0,hy=1,hz=0;
  68.  
  69. float wcsClkDn[3],wcsClkUp[3];
  70. ///////////////////////////////
  71. class point1
  72. {
  73. public:
  74.     point1()
  75.     {
  76.         x=0;
  77.         y=0;
  78.     }
  79.     int x;
  80.     int y;
  81. } clkpt[2];
  82. int flag=0;
  83. GLint viewport[4]; //var to hold the viewport info
  84. GLdouble modelview[16]; //var to hold the modelview info
  85. GLdouble projection[16]; //var to hold the projection matrix info
  86.  
  87. //////////////////////////
  88. void scsToWcs(float sx,float sy, float wcsv[3] );
  89. void processMouse(int button, int state, int x, int y);
  90. void matColor(float kdr, float kdg, float kdb,  float shiny, int frnt_Back=0, float ambFactor=1.0, float specFactor=1.0);
  91. ///////////////////////////
  92.  
  93. void scsToWcs(float sx,float sy, float wcsv[3] )
  94. {
  95.  
  96.     GLfloat winX, winY, winZ; //variables to hold screen x,y,z coordinates
  97.     GLdouble worldX, worldY, worldZ; //variables to hold world x,y,z coordinates
  98.  
  99.     //glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); //get the modelview info
  100.     glGetDoublev( GL_PROJECTION_MATRIX, projection ); //get the projection matrix info
  101.     glGetIntegerv( GL_VIEWPORT, viewport ); //get the viewport info
  102.  
  103.     winX = sx;
  104.     winY = (float)viewport[3] - (float)sy;
  105.     winZ = 0;
  106.  
  107.     //get the world coordinates from the screen coordinates
  108.     gluUnProject( winX, winY, winZ, modelview, projection, viewport, &worldX, &worldY, &worldZ);
  109.     wcsv[0]=worldX;
  110.     wcsv[1]=worldY;
  111.     wcsv[2]=worldZ;
  112.  
  113.  
  114. }
  115. void processMouse(int button, int state, int x, int y)
  116. {
  117.     if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
  118.     {
  119.         if(flag!=1)
  120.         {
  121.             flag=1;
  122.             clkpt[0].x=x;
  123.             clkpt[0].y=y;
  124.         }
  125.  
  126.  
  127.         scsToWcs(clkpt[0].x,clkpt[0].y,wcsClkDn);
  128.         cout<<"\nD: "<<x<<" "<<y<<" wcs: "<<wcsClkDn[0]<<" "<<wcsClkDn[1];
  129.     }
  130.     else if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
  131.     {
  132.         if (flag==1)
  133.         {
  134.             clkpt[1].x=x;
  135.             clkpt[1].y=y;
  136.             flag=0;
  137.         }
  138.         float wcs[3];
  139.         scsToWcs(clkpt[1].x,clkpt[1].y,wcsClkUp);
  140.         cout<<"\nU: "<<x<<" "<<y<<" wcs: "<<wcsClkUp[0]<<" "<<wcsClkUp[1];
  141.  
  142.         clikd=!clikd;
  143.     }
  144. }
  145.  
  146. //control points
  147. long long nCr(int n, int r)
  148. {
  149.     if(r > n / 2) r = n - r; // because C(n, r) == C(n, n - r)
  150.     long long ans = 1;
  151.     int i;
  152.  
  153.     for(i = 1; i <= r; i++)
  154.     {
  155.         ans *= n - r + i;
  156.         ans /= i;
  157.     }
  158.  
  159.     return ans;
  160. }
  161.  
  162. //polynomial interpretation for N points
  163. void BezierCurve ( double t,  float xy[2])
  164. {
  165.     double y=0;
  166.     double x=0;
  167.     t=t>1.0?1.0:t;
  168.     for(int i=0; i<=L; i++)
  169.     {
  170.         int ncr=nCr(L,i);
  171.         double oneMinusTpow=pow(1-t,double(L-i));
  172.         double tPow=pow(t,double(i));
  173.         double coef=oneMinusTpow*tPow*ncr;
  174.         x+=coef*ctrlpoints[i][0];
  175.         y+=coef*ctrlpoints[i][1];
  176.  
  177.     }
  178.     xy[0] = float(x);
  179.     xy[1] = float(y);
  180.  
  181.     //return y;
  182. }
  183.  
  184. ///////////////////////
  185. void setNormal(GLfloat x1, GLfloat y1,GLfloat z1, GLfloat x2, GLfloat y2,GLfloat z2, GLfloat x3, GLfloat y3,GLfloat z3)
  186. {
  187.     GLfloat Ux, Uy, Uz, Vx, Vy, Vz, Nx, Ny, Nz;
  188.  
  189.     Ux = x2-x1;
  190.     Uy = y2-y1;
  191.     Uz = z2-z1;
  192.  
  193.     Vx = x3-x1;
  194.     Vy = y3-y1;
  195.     Vz = z3-z1;
  196.  
  197.     Nx = Uy*Vz - Uz*Vy;
  198.     Ny = Uz*Vx - Ux*Vz;
  199.     Nz = Ux*Vy - Uy*Vx;
  200.  
  201.     glNormal3f(-Nx,-Ny,-Nz);
  202. }
  203.  
  204. void bottleBezier()
  205. {
  206.     int i, j;
  207.     float x, y, z, r;               //current coordinates
  208.     float x1, y1, z1, r1;           //next coordinates
  209.     float theta;
  210.  
  211.     const float startx = 0, endx = ctrlpoints[L][0];
  212.     //number of angular slices
  213.     const float dx = (endx - startx) / nt;  //x step size
  214.     const float dtheta = 2*PI / ntheta;     //angular step size
  215.  
  216.     float t=0;
  217.     float dt=1.0/nt;
  218.     float xy[2];
  219.     BezierCurve( t,  xy);
  220.     x = xy[0];
  221.     r = xy[1];
  222.     //rotate about z-axis
  223.     float p1x,p1y,p1z,p2x,p2y,p2z;
  224.     for ( i = 0; i < nt; ++i )              //step through x
  225.     {
  226.         theta = 0;
  227.         t+=dt;
  228.         BezierCurve( t,  xy);
  229.         x1 = xy[0];
  230.         r1 = xy[1];
  231.  
  232.         //draw the surface composed of quadrilaterals by sweeping theta
  233.         glBegin( GL_QUAD_STRIP );
  234.         for ( j = 0; j <= ntheta; ++j )
  235.         {
  236.             theta += dtheta;
  237.             double cosa = cos( theta );
  238.             double sina = sin ( theta );
  239.             y = r * cosa;
  240.             y1 = r1 * cosa; //current and next y
  241.             z = r * sina;
  242.             z1 = r1 * sina; //current and next z
  243.  
  244.             //edge from point at x to point at next x
  245.             glVertex3f (x, y, z);
  246.  
  247.             if(j>0)
  248.             {
  249.                 setNormal(p1x,p1y,p1z,p2x,p2y,p2z,x, y, z);
  250.             }
  251.             else
  252.             {
  253.                 p1x=x;
  254.                 p1y=y;
  255.                 p1z=z;
  256.                 p2x=x1;
  257.                 p2y=y1;
  258.                 p2z=z1;
  259.  
  260.             }
  261.             glVertex3f (x1, y1, z1);
  262.  
  263.             //forms quad with next pair of points with incremented theta value
  264.         }
  265.         glEnd();
  266.         x = x1;
  267.         r = r1;
  268.     } //for i
  269.  
  270. }
  271.  
  272.  
  273. static void resize(int width, int height)
  274. {
  275.     const float ar = (float) width / (float) height;
  276.  
  277.     glViewport(0, 0, width, height);
  278.     glMatrixMode(GL_PROJECTION);
  279.     glLoadIdentity();
  280.     //glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
  281.     // gluPerspective(60,ar, 2.0, 100.0);
  282.  
  283.     glOrtho(-8.0, 8.0, -8.0*(GLfloat)height/(GLfloat)width, 8.0*(GLfloat)height/(GLfloat)width, 2.0, 25.0);
  284.     glMatrixMode(GL_MODELVIEW);
  285.     glLoadIdentity() ;
  286.     gluLookAt(ex,ey,ez,lx,ly,lz,hx,hy,hz);
  287.  
  288. }
  289.  
  290.  
  291. void showControlPoints()
  292. {
  293.     glPointSize(5.0);
  294.     glColor3f(1.0, 0.0, 1.0);
  295.     glBegin(GL_POINTS);
  296.     for (int i = 0; i <=L; i++)
  297.         glVertex3fv(&ctrlpoints[i][0]);
  298.     glEnd();
  299. }
  300. static void display(void)
  301. {
  302.     const double t = glutGet(GLUT_ELAPSED_TIME) / 5000.0;
  303.     const double a = t*90.0;
  304.  
  305.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  306.  
  307.     if(wired)
  308.     {
  309.         glPolygonMode( GL_FRONT, GL_LINE ) ;
  310.         glPolygonMode( GL_BACK, GL_LINE ) ;
  311.  
  312.     }
  313.     else
  314.     {
  315.         glPolygonMode( GL_FRONT,GL_FILL ) ;
  316.         glPolygonMode( GL_BACK, GL_FILL ) ;
  317.     }
  318.  
  319.     glPushMatrix();
  320.  
  321.     if(animat)
  322.         glRotated(a,0,0,1);
  323.  
  324.     glRotatef( anglex, 1.0, 0.0, 0.0);
  325.     glRotatef( angley, 0.0, 1.0, 0.0);          //rotate about y-axis
  326.     glRotatef( anglez, 0.0, 0.0, 1.0);
  327.  
  328.     glRotatef( 90, 0.0, 0.0, 1.0);
  329.     glTranslated(-3.5,0,0);
  330.     glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); //get the modelview info
  331.  
  332.     matColor(0.9,0.5,0.1,20);   // front face color
  333.     matColor(0.0,0.5,0.8,20,1);  // back face color
  334.  
  335.  
  336.     bottleBezier();
  337.  
  338.  
  339.     if(shcpt)
  340.     {
  341.         matColor(0.0,0.0,0.9,20);
  342.         showControlPoints();
  343.     }
  344.  
  345.     glPopMatrix();
  346.  
  347.  
  348.  
  349.     glutSwapBuffers();
  350. }
  351.  
  352.  
  353. static void key(unsigned char key, int x, int y)
  354. {
  355.     switch (key)
  356.     {
  357.     case 'A':
  358.         animat=!animat;
  359.         break;
  360.  
  361.     case 's':
  362.     case 'S':
  363.         shcpt=!shcpt;
  364.         break;
  365.  
  366.     case 'w':
  367.     case 'W':
  368.         wired=!wired;
  369.         break;
  370.  
  371.     case 'x':
  372.         anglex = ( anglex + 3 ) % 360;
  373.         break;
  374.     case 'X':
  375.         anglex = ( anglex - 3 ) % 360;
  376.         break;
  377.  
  378.     case 'y':
  379.         angley = ( angley + 3 ) % 360;
  380.         break;
  381.     case 'Y':
  382.         angley = ( angley - 3 ) % 360;
  383.         break;
  384.  
  385.     case 'z':
  386.         anglez = ( anglez + 3 ) % 360;
  387.         break;
  388.     case 'Z':
  389.         anglez = ( anglez - 3 ) % 360;
  390.         break;
  391.  
  392.  
  393.     case 'q':
  394.     case 27 :
  395.         glutDestroyWindow(window);
  396.         exit(0);
  397.         break;
  398.     }
  399.  
  400.     glutPostRedisplay();
  401. }
  402.  
  403. static void idle(void)
  404. {
  405.     glutPostRedisplay();
  406. }
  407.  
  408. const GLfloat light_ambient[]  = { 0.5f, 0.5f, 0.5f, 1.0f };
  409. const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
  410. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  411. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 1.0f };
  412.  
  413. void matColor(float kdr, float kdg, float kdb,  float shiny, int frnt_Back, float ambFactor, float specFactor)
  414. {
  415.  
  416.     const GLfloat mat_ambient[]    = { kdr*ambFactor, kdg*ambFactor, kdb*ambFactor, 1.0f };
  417.     const GLfloat mat_diffuse[]    = { kdr, kdg, kdb, 1.0f };
  418.     const GLfloat mat_specular[]   = { 1.0f*specFactor, 1.0f*specFactor, 1.0f*specFactor, 1.0f };
  419.     const GLfloat high_shininess[] = { shiny };
  420.     if(frnt_Back==0)
  421.     {
  422.         glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
  423.         glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
  424.         glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
  425.         glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  426.     }
  427.     else if(frnt_Back==1)
  428.     {
  429.         glMaterialfv(GL_BACK, GL_AMBIENT,   mat_ambient);
  430.         glMaterialfv(GL_BACK, GL_DIFFUSE,   mat_diffuse);
  431.         glMaterialfv(GL_BACK, GL_SPECULAR,  mat_specular);
  432.         glMaterialfv(GL_BACK, GL_SHININESS, high_shininess);
  433.     }
  434.     else if(frnt_Back==2)
  435.     {
  436.         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT,   mat_ambient);
  437.         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,   mat_diffuse);
  438.         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,  mat_specular);
  439.         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, high_shininess);
  440.     }
  441.  
  442. }
  443.  
  444. /* Program entry point */
  445. void myInit()
  446. {
  447.     glClearColor(.1,.1,.1,1);
  448.  
  449.     glEnable(GL_LIGHT0);
  450.     glEnable(GL_NORMALIZE);
  451.     glEnable(GL_COLOR_MATERIAL);
  452.     glEnable(GL_LIGHTING);
  453.  
  454.     glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
  455.     glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
  456.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  457.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  458. }
  459. int main(int argc, char *argv[])
  460. {
  461.     glutInit(&argc, argv);
  462.     glutInitWindowSize(640,480);
  463.     glutInitWindowPosition(10,10);
  464.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  465.  
  466.     glutCreateWindow("Curved Surface");
  467.  
  468.     myInit();
  469.  
  470.     glutReshapeFunc(resize);
  471.     glutDisplayFunc(display);
  472.     glutKeyboardFunc(key);
  473.     glutMouseFunc(processMouse);
  474.     glutIdleFunc(idle);
  475.  
  476.  
  477.     glutMainLoop();
  478.  
  479.     return EXIT_SUCCESS;
  480. }
  481.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement