Advertisement
naimularif

Graphics backup

Apr 26th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.60 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #include<windows.h>
  5. #include<GL/glut.h>
  6. #include<iostream>
  7. #include<vector>
  8. using namespace std;
  9.  
  10. #define pb push_back
  11. #define PI 2*acos(0)
  12. #define pi 2*acos(0)
  13.  
  14. double cameraHeight;
  15. double cameraAngle;
  16. int drawgrid;
  17. int drawaxes;
  18. double angle;
  19.  
  20. ///prototypes
  21. void drawsphere(float radius,int slices,int stacks);
  22. void drawCircularStrip();
  23. void drawCylinder(float height, float radius, int stacks, int slices);
  24. ///
  25.  
  26. struct point
  27. {
  28.     double x,y,z;
  29. //  point(double a , double b, double c)
  30. //  {
  31. //      x=a;
  32. //      y=b;
  33. //      z=c;
  34. //  }
  35. };
  36.  
  37.  
  38. void drawAxes()
  39. {
  40.     if(drawaxes==1)
  41.     {
  42.         glColor3f(1.0, 1.0, 1.0);
  43.         glBegin(GL_LINES);{
  44.             glVertex3f( 1000,0,0);
  45.             glVertex3f(-1000,0,0);
  46.             glVertex3f(0,-1000,0);
  47.             glVertex3f(0, 1000,0);
  48.             glVertex3f(0,0, 1000);
  49.             glVertex3f(0,0,-1000);
  50.         }glEnd();
  51.     }
  52. }
  53.  
  54. void drawCurvedCylinder(float length, float angle, float radius)
  55. {
  56.     float ang=0;
  57.     for(int i=0;i<1000;i++)
  58.     {
  59.         ang=ang+angle/1000;
  60.         glPushMatrix();{
  61.             glTranslatef(-radius*cos(pi*ang/180),0,radius*sin(ang*pi/180));
  62.             glRotatef(ang,0,1,0);
  63.             drawCylinder(length/900,radius,10,10);
  64.         }glPopMatrix();
  65.     }
  66. }
  67.  
  68. void drawGrid()
  69. {
  70.     int i;
  71.     if(drawgrid==1)
  72.     {
  73.         glColor3f(0.6, 0.6, 0.6);   //grey
  74.         glBegin(GL_LINES);{
  75.             for(i=-8;i<=8;i++){
  76.  
  77.                 if(i==0)
  78.                     continue;   //SKIP the MAIN axes
  79.  
  80.                 //lines parallel to Y-axis
  81.                 glVertex3f(i*10, -90, 0);
  82.                 glVertex3f(i*10,  90, 0);
  83.  
  84.                 //lines parallel to X-axis
  85.                 glVertex3f(-90, i*10, 0);
  86.                 glVertex3f( 90, i*10, 0);
  87.             }
  88.         }glEnd();
  89.     }
  90. }
  91.  
  92.  
  93. void drawSquare(float a)
  94. {
  95.     glBegin(GL_QUADS);{
  96.         glVertex3f( a, a,2);
  97.         glVertex3f( a,-a,2);
  98.         glVertex3f(-a,-a,2);
  99.         glVertex3f(-a, a,2);
  100.     }glEnd();
  101. }
  102.  
  103. void triangularBasic(float first, float third, float width )
  104. {
  105.     glBegin(GL_QUADS);{
  106.         glVertex3f(third/2,0,width);
  107.         glVertex3f(0,sqrt(first*first-third*third/4),width);
  108.         glVertex3f(0,sqrt(first*first-third*third/4),width);
  109.         glVertex3f(-third/2,0,width);
  110.     }glEnd();
  111.     glBegin(GL_QUADS);{
  112.         glVertex3f(third/2,0,width);
  113.         glVertex3f(third/2,0,0);
  114.         glVertex3f(0,sqrt(first*first-third*third/4),0);
  115.         glVertex3f(0,sqrt(first*first-third*third/4),width);
  116.     }glEnd();
  117.     glBegin(GL_QUADS);{
  118.         glVertex3f(-third/2,0,width);
  119.         glVertex3f(-third/2,0,0);
  120.         glVertex3f(0,sqrt(first*first-third*third/4),0);
  121.         glVertex3f(0,sqrt(first*first-third*third/4),width);
  122.     }glEnd();
  123. glBegin(GL_QUADS);{
  124.         glVertex3f(third/2,0,0);
  125.         glVertex3f(0,sqrt(first*first-third*third/4),0);
  126.         glVertex3f(0,sqrt(first*first-third*third/4),0);
  127.         glVertex3f(-third/2,0,0);
  128.     }glEnd();
  129.     glBegin(GL_QUADS);{
  130.         glVertex3f(third/2, 0,width);
  131.         glVertex3f(third/2, 0,0);
  132.         glVertex3f(-third/2, 0,0);
  133.         glVertex3f(-third/2, 0,width);
  134.     }glEnd();
  135. }
  136. void triangular(float first, float third, float width, int clip )
  137. {
  138.     glPushMatrix();{
  139.             double equ[4];
  140.  
  141.             equ[0] = 0; //0.x
  142.             equ[1] = -1;    //0.y
  143.             equ[2] = 0;//-1.z
  144.             equ[3] = clip;//30
  145.             glClipPlane(GL_CLIP_PLANE0,equ);
  146.  
  147.             glEnable(GL_CLIP_PLANE0);{
  148.                 triangularBasic(1000,70,10);
  149.             }glDisable(GL_CLIP_PLANE0);
  150.         }glPopMatrix();
  151.  
  152.     triangularBasic( first,  third, width );
  153. }
  154.  
  155. void drawHexCap(float height, float side)
  156. {
  157.     glColor3f(200,200,200);
  158.     glBegin(GL_QUADS);{
  159.         glVertex3f(side, 0,0);
  160.         glVertex3f(side*cos(pi/3),side*cos(pi/6),0);
  161.         glVertex3f(0,0,height);
  162.         glVertex3f(0,0,height);
  163.     }glEnd();;
  164.  
  165.     glBegin(GL_QUADS);{
  166.         glVertex3f(side*cos(pi/3),side*cos(pi/6),0);
  167.         glVertex3f(-side*cos(pi/3),side*cos(pi/6),0);
  168.         glVertex3f(0,0,height);
  169.         glVertex3f(0,0,height);
  170.     }glEnd();
  171.  
  172.     glBegin(GL_QUADS);{
  173.         glVertex3f(-side*cos(pi/3),side*cos(pi/6),0);
  174.         glVertex3f(-side,0,0);
  175.         glVertex3f(0,0,height);
  176.         glVertex3f(0,0,height);
  177.     }glEnd();
  178.  
  179.     glBegin(GL_QUADS);{
  180.         glVertex3f(-side,0,0);
  181.         glVertex3f(-side*cos(pi/3),-side*cos(pi/6),0);
  182.         glVertex3f(0,0,height);
  183.         glVertex3f(0,0,height);
  184.     }glEnd();
  185.  
  186.     glBegin(GL_QUADS);{
  187.         glVertex3f(-side*cos(pi/3),-side*cos(pi/6),0);
  188.         glVertex3f(side*cos(pi/3),-side*cos(pi/6),0);
  189.         glVertex3f(0,0,height);
  190.         glVertex3f(0,0,height);
  191.     }glEnd();
  192.  
  193.  
  194.     glBegin(GL_QUADS);{
  195.         glVertex3f(side*cos(pi/3),-side*cos(pi/6),0);
  196.         glVertex3f(side,0,0);
  197.         glVertex3f(0,0,height);
  198.         glVertex3f(0,0,height);
  199.     }glEnd();
  200.  
  201. }
  202.  
  203. void drawRectBarBasic(float length, float side)
  204. {
  205.     glBegin(GL_QUADS);{
  206.         glColor3f((float)240/256,(float)248/256,(float)255/256);
  207.         glVertex3f(length/2,-side/2,side/2);
  208.         glVertex3f(length/2,-side/2,-side/2);
  209.         glVertex3f(-length/2,-side/2,-side/2);
  210.         glVertex3f(-length/2,-side/2,side/2);
  211.     }glEnd();
  212.  
  213. }
  214.  
  215. void drawRectBar(float length, float side)
  216. {
  217.     drawRectBarBasic(length,side);
  218.  
  219.     glPushMatrix();
  220.     {
  221.         glTranslatef(0,side,0);
  222.         drawRectBarBasic(length,side);
  223.     }glPopMatrix();
  224.     glPushMatrix();
  225.     {
  226.         glRotatef(90,1,0,0);
  227.         glColor3f(1,0,0);
  228.         drawRectBarBasic(length,side);
  229.     }glPopMatrix();
  230.     glPushMatrix();
  231.     {
  232.         glRotatef(-90,1,0,0);
  233.         glColor3f(1,0,0);
  234.         drawRectBarBasic(length,side);
  235.     }glPopMatrix();
  236. }
  237.  
  238. void HexColumnBasic(float length, float side)
  239. {
  240.     glBegin(GL_QUADS);{
  241.         glVertex3f(0,0,0);
  242.         glVertex3f(side,0,0);
  243.         glVertex3f(side,0,length);
  244.         glVertex3f(0,0,length);
  245.     }glEnd();;
  246. }
  247. void drawHexColumn(float length, float side)
  248. {
  249.     glColor3f(200,200,200);
  250.     glPushMatrix();{
  251.         glTranslated((float)side*cos(pi/3),-(float)side*cos(pi/6),0);
  252.  
  253.         //glTranslated(side,0,0);
  254.         glRotated(60,0,0,1);
  255.         HexColumnBasic(length,side);
  256.     }glPopMatrix();
  257.     glColor3f(200,200,200);
  258.     glPushMatrix();{
  259.         glTranslated(-side,0,0);
  260.         glRotated(60,0,0,1);
  261.         HexColumnBasic(length,side);
  262.     }glPopMatrix();
  263.     glColor3f(200,200,200);
  264.     glPushMatrix();{
  265.         glTranslated(-side/2,(float)side*cos(pi/6),0);
  266.         //glRotated(60,0,0,1);
  267.         HexColumnBasic(length,side);
  268.     }glPopMatrix();
  269.     glColor3f(200,200,200);
  270.     glPushMatrix();{
  271.         glTranslated(-side/2,-(float)side*cos(pi/6),0);
  272.         //glRotated(60,0,0,1);
  273.         HexColumnBasic(length,side);
  274.     }glPopMatrix();
  275.     glColor3f(200,200,200);
  276.     glPushMatrix();{
  277.         glTranslated(-side,0,0);
  278.  
  279.         //glTranslated(side,0,0);
  280.         glRotated(300,0,0,1);
  281.         HexColumnBasic(length,side);
  282.     }glPopMatrix();
  283.     glColor3f(200,200,200);
  284.     glPushMatrix();{
  285.         glTranslated(side/2,(float)side*cos(pi/6),0);
  286.  
  287.         //glTranslated(side,0,0);
  288.         glRotated(300,0,0,1);
  289.         HexColumnBasic(length,side);
  290.     }glPopMatrix();
  291. }
  292.  
  293. void drawCylinder(float height, float radius, int stacks, int slices)
  294. {
  295.  
  296.     struct point points[100][100];
  297.     int i,j;
  298.     double h,r;
  299.     for(i=0;i<=stacks;i++)
  300.     {
  301.         h=height*(double)i/(double)stacks/2;
  302.         r=radius;
  303.         for(j=0;j<=slices;j++)
  304.         {
  305.             points[i][j].x=r*cos(((double)j/(double)slices)*2*pi);
  306.             points[i][j].y=r*sin(((double)j/(double)slices)*2*pi);
  307.             points[i][j].z=h; ///this creates the upper half
  308.         }
  309.  
  310.     }
  311.     for(i=0;i<stacks;i++)
  312.     {
  313.         for(j=0;j<slices;j++)
  314.         {
  315.             glColor3f(200,200,200);
  316.             glBegin(GL_QUADS);{
  317.                 glVertex3f(points[i][j].x,points[i][j].y,points[i][j].z);
  318.                 glVertex3f(points[i][j+1].x,points[i][j+1].y,points[i][j+1].z);
  319.                 glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,points[i+1][j+1].z);
  320.                 glVertex3f(points[i+1][j].x,points[i+1][j].y,points[i+1][j].z);
  321.  
  322.             }glEnd();
  323.         }
  324.  
  325.     }
  326.  
  327.     for(i=0;i<=stacks;i++)
  328.     {
  329.         h=height*(double)i/(double)stacks/2;
  330.         r=radius;
  331.         for(j=0;j<=slices;j++)
  332.         {
  333.             points[i][j].x=r*cos(((double)j/(double)slices)*2*pi);
  334.             points[i][j].y=r*sin(((double)j/(double)slices)*2*pi);
  335.             points[i][j].z=-h; ///this creates the lower half
  336.         }
  337.  
  338.     }
  339.     for(i=0;i<stacks;i++)
  340.     {
  341.         for(j=0;j<slices;j++)
  342.         {
  343.             glColor3f(200,200,200);
  344.             glBegin(GL_QUADS);{
  345.                 glVertex3f(points[i][j].x,points[i][j].y,points[i][j].z);
  346.                 glVertex3f(points[i][j+1].x,points[i][j+1].y,points[i][j+1].z);
  347.                 glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,points[i+1][j+1].z);
  348.                 glVertex3f(points[i+1][j].x,points[i+1][j].y,points[i+1][j].z);
  349.  
  350.             }glEnd();
  351.         }
  352.  
  353.     }
  354. }
  355.  
  356. void twoColumn()
  357. {
  358.     float length=1014.73;
  359.     glPushMatrix();{
  360.         glTranslated(-105,0,0);
  361.         glRotated(6,0,1,0);
  362.         drawHexColumn(length,33.13);
  363.     }glPopMatrix();
  364.  
  365.     glPushMatrix();{
  366.         glTranslated(105,0,0);
  367.         glRotated(-6,0,1,0);
  368.         drawHexColumn(length,33.13);
  369.     }glPopMatrix();
  370.  
  371.     glPushMatrix(); ///strip
  372.     {
  373.         glTranslatef(0,0,100);
  374.         glRotatef(4,1,0,0);
  375.         float x,y,z,xa,ya,za;
  376.         x=150;
  377.         y=300;
  378.         z=600;
  379.         xa=90;
  380.         za=30;
  381.         glPushMatrix();{
  382.             glTranslatef(-x,y,z);
  383.             glRotatef(za,0,0,1);
  384.             glRotatef(90,1,0,0);
  385.             drawCircularStrip();
  386.         }glPopMatrix();
  387.         glPushMatrix();{
  388.             glTranslatef(x,y,z);
  389.             glRotatef(-za,0,0,1);
  390.             glRotatef(90,1,0,0);
  391.             drawCircularStrip();
  392.         }glPopMatrix();
  393.     }glPopMatrix();
  394.  
  395.     glPushMatrix();{
  396.         glTranslatef(0,0,250);
  397.         drawRectBar(200,20);
  398.     }glPopMatrix();
  399.     glPushMatrix();{
  400.         glTranslatef(0,0,400);
  401.         drawRectBar(150,20);
  402.     }glPopMatrix();
  403.     glPushMatrix();{
  404.         glTranslatef(0,0,550);
  405.         drawRectBar(100,20);
  406.     }glPopMatrix();
  407.     glPushMatrix();{
  408.         glTranslatef(0,0,650);
  409.         drawRectBar(100,20);
  410.     }glPopMatrix();
  411.  
  412.  
  413.     //drawCurvedCylinder(1000, 10, 10);
  414.     glPushMatrix();
  415.     {
  416. //        glTranslatef(-100,170,0);///////////
  417. //        glRotatef(30,1.9,1,1);
  418. //        glRotatef(120,0,0,1);
  419. //        glRotatef(90,1,0,0);
  420.         triangular(1000,70,10,30);//////////////
  421.     }glPopMatrix();
  422.  
  423. }
  424.  
  425. void drawCircularStrip()
  426. {
  427.     vector<point> v1,v2,v3,v4;
  428.     double radius1,radius2,width;
  429.     radius1=200;
  430.     radius2=190;
  431.     width=10;
  432.     for(int i=-45;i<50;i++)
  433.     {
  434.         float scale=.45;
  435.         point a;
  436.         a.x=-width/2;
  437.         a.y=scale*6*radius1*sin(pi*i/180);
  438.         a.z=scale*5*radius1*cos(pi*i/180);
  439.         v1.push_back(a);
  440.         a.x=width/2;
  441.         a.y=scale*6*radius1*sin(pi*i/180);
  442.         a.z=scale*5*radius1*cos(pi*i/180);
  443.         v2.push_back(a);
  444.  
  445.         a.x=-width/2;
  446.         a.y=scale*5.5*radius2*sin(pi*i/180);
  447.         a.z=scale*5*radius2*cos(pi*i/180);
  448.         v3.push_back(a);
  449.         a.x=width/2;
  450.         a.y=scale*5.5*radius2*sin(pi*i/180);
  451.         a.z=scale*5*radius2*cos(pi*i/180);
  452.         v4.push_back(a);
  453.     }
  454.     for(int i=0;i<v1.size()-1;i++)
  455.     {
  456.         glBegin(GL_QUADS);{
  457.             glColor3d(200,200,200);
  458.             glVertex3f(v1[i].x,v1[i].y,v1[i].z);
  459.             glVertex3f(v1[i+1].x,v1[i+1].y,v1[i+1].z);
  460.             glVertex3f(v2[i+1].x,v2[i+1].y,v2[i+1].z);
  461.             glVertex3f(v2[i].x,v2[i].y,v2[i].z);
  462.         }glEnd();
  463.  
  464.         glBegin(GL_QUADS);{
  465.             glColor3d(200,200,200);
  466.             glVertex3f(v3[i].x,v3[i].y,v3[i].z);
  467.             glVertex3f(v3[i+1].x,v3[i+1].y,v3[i+1].z);
  468.             glVertex3f(v4[i+1].x,v4[i+1].y,v4[i+1].z);
  469.             glVertex3f(v4[i].x,v4[i].y,v4[i].z);
  470.         }glEnd();
  471.             ///right side
  472.             glBegin(GL_QUADS);{
  473.             glColor3d(200,200,200);
  474.             glVertex3f(v1[i].x,v1[i].y,v1[i].z);
  475.             glVertex3f(v1[i+1].x,v1[i+1].y,v1[i+1].z);
  476.             glVertex3f(v3[i+1].x,v3[i+1].y,v3[i+1].z);
  477.             glVertex3f(v3[i].x,v3[i].y,v3[i].z);
  478.         }glEnd();
  479.             ///left side
  480.             glBegin(GL_QUADS);{
  481.             glColor3d(200,200,200);
  482.             glVertex3f(v2[i+1].x,v2[i+1].y,v2[i+1].z);
  483.             glVertex3f(v2[i].x,v2[i].y,v2[i].z);
  484.             glVertex3f(v4[i].x,v4[i].y,v4[i].z);
  485.             glVertex3f(v4[i+1].x,v4[i+1].y,v4[i+1].z);
  486.         }glEnd();
  487.     }
  488. }
  489.  
  490. //void drawColumnStrip(double width=1.25,double radius=22){
  491. //  vector<point> v1,v2;
  492. //  vector<point> v3,v4;
  493. //  double radius2 = radius-1;int i;
  494. //  //repi(i,-77,45)
  495. //  for(i=-77;i<=45;i++){
  496. //      point a,b,c,d;
  497. //      a.x=-width/2;
  498. //      a.y=1.4*radius*sin(PI*i/180);
  499. //      a.z=radius*cos(PI*i/180);
  500. //      v1.pb(a);
  501. ////        v1.pb(point(-width/2,1.4*radius*sin(PI*i/180.),radius*cos(PI*i/180)));
  502. //        b.x=width/2;
  503. //        b.y=1.4*radius*sin(PI*i/180);
  504. //        b.z=radius*cos(PI*i/180);
  505. ////        v2.pb(point(width/2,1.4*radius*sin(PI*i/180.),radius*cos(PI*i/180)));
  506. //        v2.pb(b);
  507. //        c.x=-width/2;
  508. //        c.y=1.32*radius2*sin(PI*i/180);
  509. //        c.z=radius2*cos(PI*i/180);
  510. //        v3.pb(c);
  511. //      //v3.pb(point(-width/2,1.32*radius2*sin(PI*i/180.),radius2*cos(PI*i/180)));
  512. //      d.x=width/2;
  513. //      d.y=1.32*radius2*sin(PI*i/180.);
  514. //      d.z=radius2*cos(PI*i/180);
  515. ////        v4.pb(point(width/2,1.32*radius2*sin(PI*i/180.),radius2*cos(PI*i/180)));
  516. //      //~ cout<<v1.back().x<<" "<<v1.back().y<<" "<<v1.back().z<<endl;
  517. //  }
  518. //  //~ exit(0);
  519. //
  520. //  glColor3d(1,0,0);
  521. //  glRotated(-90,1,0,0);
  522. //  glBegin(GL_QUADS);{
  523. //      //rep(i,len(v1)-1)
  524. //      for(i=0;i<v1.size()-2;i++){
  525. //          glColor3d(0,1,0);
  526. //          ///outer strip
  527. //          glVertex3f(v1[i].x,v1[i].y,v1[i].z);
  528. //          glVertex3f(v1[i+1].x,v1[i+1].y,v1[i+1].z);
  529. //          glVertex3f(v2[i+1].x,v2[i+1].y,v2[i+1].z);
  530. //          glVertex3f(v2[i].x,v2[i].y,v2[i].z);
  531. //
  532. //          ///inner strip
  533. //          glVertex3f(v3[i].x,v3[i].y,v3[i].z);
  534. //          glVertex3f(v3[i+1].x,v3[i+1].y,v3[i+1].z);
  535. //          glVertex3f(v4[i+1].x,v4[i+1].y,v4[i+1].z);
  536. //          glVertex3f(v4[i].x,v4[i].y,v4[i].z);
  537. //
  538. //          ///right side
  539. //          glColor3d(1,0,0);
  540. //          glVertex3f(v1[i].x,v1[i].y,v1[i].z);
  541. //          glVertex3f(v1[i+1].x,v1[i+1].y,v1[i+1].z);
  542. //          glVertex3f(v3[i+1].x,v3[i+1].y,v3[i+1].z);
  543. //          glVertex3f(v3[i].x,v3[i].y,v3[i].z);
  544. //
  545. //          ///left side
  546. //          glVertex3f(v2[i+1].x,v2[i+1].y,v2[i+1].z);
  547. //          glVertex3f(v2[i].x,v2[i].y,v2[i].z);
  548. //          glVertex3f(v4[i].x,v4[i].y,v4[i].z);
  549. //          glVertex3f(v4[i+1].x,v4[i+1].y,v4[i+1].z);
  550. //      }
  551. //  }glEnd();
  552. //}
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559. void drawCurveColumns(){
  560.     double ang = 50;
  561.     glPushMatrix();{
  562.         glTranslated(0.5,0,0);
  563.         glRotated(-ang,0,0,1);
  564.         glPushMatrix();{
  565.             glTranslated(0,-12,13.5);
  566.             glRotated(-37,1,0,0);
  567.             //drawRectBar(1.25,0.4,1.25,15,55);
  568.         };glPopMatrix();
  569.  
  570.  
  571.         glTranslated(0,2,82);
  572.         glRotated(-18,1,0,0);
  573.         glTranslated(0,-4.5,-37);
  574. //      drawColumnStrip(1.25,22);
  575.     }glPopMatrix();
  576.  
  577. }
  578.  
  579. void drawss()
  580. {
  581.     glPushMatrix();{
  582.         glTranslated(900,0,0);
  583.     drawsphere(20,20,20);}glPopMatrix();
  584.  
  585.     twoColumn();
  586.     glPushMatrix();{
  587.         glTranslatef(0,0,1014);
  588.         drawHexCap(300,33.13);
  589.     }glPopMatrix();
  590.  
  591.  
  592. }
  593.  
  594. //draws half sphere
  595. void drawsphere(float radius,int slices,int stacks)
  596. {
  597.     struct point points[100][100];
  598.     int i,j;
  599.     double h,r;
  600.     for(i=0;i<=stacks;i++)
  601.     {
  602.         h=radius*sin(((double)i/(double)stacks)*(pi/2));
  603.         r=sqrt(radius*radius-h*h);
  604.         for(j=0;j<=slices;j++)
  605.         {
  606.             points[i][j].x=r*cos(((double)j/(double)slices)*2*pi);
  607.             points[i][j].y=r*sin(((double)j/(double)slices)*2*pi);
  608.             points[i][j].z=h; ///this creates the upper half
  609.         }
  610.  
  611.     }
  612.     for(i=0;i<stacks;i++)
  613.     {
  614.         for(j=0;j<slices;j++)
  615.         {
  616.             glColor3f((double)i/(double)stacks,(double)i/(double)stacks,(double)i/(double)stacks);
  617.             glBegin(GL_QUADS);{
  618.                 glVertex3f(points[i][j].x,points[i][j].y,points[i][j].z);
  619.                 glVertex3f(points[i][j+1].x,points[i][j+1].y,points[i][j+1].z);
  620.                 glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,points[i+1][j+1].z);
  621.                 glVertex3f(points[i+1][j].x,points[i+1][j].y,points[i+1][j].z);
  622.  
  623.             }glEnd();
  624.         }
  625.  
  626.     }
  627.  
  628.         for(i=0;i<=stacks;i++)
  629.     {
  630.         h=radius*sin(((double)i/(double)stacks)*(pi/2));
  631.         r=sqrt(radius*radius-h*h);
  632.         for(j=0;j<=slices;j++)
  633.         {
  634.             points[i][j].x=r*cos(((double)j/(double)slices)*2*pi);
  635.             points[i][j].y=r*sin(((double)j/(double)slices)*2*pi);
  636.             points[i][j].z=-h; ///this creates the lower half
  637.         }
  638.  
  639.     }
  640.     for(i=0;i<stacks;i++)
  641.     {
  642.         for(j=0;j<slices;j++)
  643.         {
  644.             glColor3f((double)i/(double)stacks,(double)i/(double)stacks,(double)i/(double)stacks);
  645.             glBegin(GL_QUADS);{
  646.                 glVertex3f(points[i][j].x,points[i][j].y,points[i][j].z);
  647.                 glVertex3f(points[i][j+1].x,points[i][j+1].y,points[i][j+1].z);
  648.                 glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,points[i+1][j+1].z);
  649.                 glVertex3f(points[i+1][j].x,points[i+1][j].y,points[i+1][j].z);
  650.  
  651.             }glEnd();
  652.         }
  653.  
  654.     }
  655. }
  656.  
  657.  
  658. void keyboardListener(unsigned char key, int x,int y){
  659.     switch(key){
  660.  
  661.         case '1':
  662.             drawgrid=1-drawgrid;
  663.             break;
  664.  
  665.         default:
  666.             break;
  667.     }
  668. }
  669.  
  670.  
  671. void specialKeyListener(int key, int x,int y){
  672.     switch(key){
  673.         case GLUT_KEY_DOWN:     //down arrow key
  674.             cameraHeight -= 30.0;
  675.             break;
  676.         case GLUT_KEY_UP:       // up arrow key
  677.             cameraHeight += 30.0;
  678.             break;
  679.  
  680.         case GLUT_KEY_RIGHT:
  681.             cameraAngle += 0.03;
  682.             break;
  683.         case GLUT_KEY_LEFT:
  684.             cameraAngle -= 0.03;
  685.             break;
  686.  
  687.         case GLUT_KEY_PAGE_UP:
  688.             break;
  689.         case GLUT_KEY_PAGE_DOWN:
  690.             break;
  691.         case GLUT_KEY_INSERT:
  692.             break;
  693.  
  694.         case GLUT_KEY_HOME:
  695.             break;
  696.         case GLUT_KEY_END:
  697.             break;
  698.  
  699.         default:
  700.             break;
  701.     }
  702. }
  703.  
  704.  
  705. void mouseListener(int button, int state, int x, int y){    //x, y is the x-y of the screen (2D)
  706.     switch(button){
  707.         case GLUT_LEFT_BUTTON:
  708.             if(state == GLUT_DOWN){     // 2 times?? in ONE click? -- solution is checking DOWN or UP
  709.                 drawaxes=1-drawaxes;
  710.             }
  711.             break;
  712.  
  713.         case GLUT_RIGHT_BUTTON:
  714.             //........
  715.             break;
  716.  
  717.         case GLUT_MIDDLE_BUTTON:
  718.             //........
  719.             break;
  720.  
  721.         default:
  722.             break;
  723.     }
  724. }
  725.  
  726.  
  727.  
  728. void display(){
  729.  
  730.     //clear the display
  731.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  732.     glClearColor(0,0,0,0);  //color black
  733.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  734.  
  735.     /********************
  736.     / set-up camera here
  737.     ********************/
  738.     //load the correct matrix -- MODEL-VIEW matrix
  739.     glMatrixMode(GL_MODELVIEW);
  740.  
  741.     //initialize the matrix
  742.     glLoadIdentity();
  743.  
  744.     //now give three info
  745.     //1. where is the camera (viewer)?
  746.     //2. where is the camera looking?
  747.     //3. Which direction is the camera's UP direction?
  748.  
  749.     //gluLookAt(100,100,100,    0,0,0,  0,0,1);
  750.     gluLookAt(500*cos(cameraAngle), 500*sin(cameraAngle), cameraHeight,     0,0,0,      0,0,1);
  751.     //gluLookAt(0,-1,150,   0,0,0,  0,0,1);
  752.  
  753.  
  754.     //again select MODEL-VIEW
  755.     glMatrixMode(GL_MODELVIEW);
  756.  
  757.  
  758.     /****************************
  759.     / Add your objects from here
  760.     ****************************/
  761.     //add objects
  762.  
  763.     drawAxes();
  764.     drawGrid();
  765.  
  766.     drawss();
  767.  
  768.     //ADD this line in the end --- if you use double buffer (i.e. GL_DOUBLE)
  769.     glutSwapBuffers();
  770. }
  771.  
  772. void animate(){
  773.     angle+=0.05;
  774.     //codes for any changes in Models, Camera
  775.     glutPostRedisplay();
  776. }
  777.  
  778. void init(){
  779.     //codes for initialization
  780.     drawgrid=0;
  781.     drawaxes=1;
  782.     cameraHeight=1000.0;
  783.     cameraAngle=1.0;
  784.     angle=0;
  785.  
  786.     //clear the screen
  787.     glClearColor(0,0,0,0);
  788.  
  789.     /************************
  790.     / set-up projection here
  791.     ************************/
  792.     //load the PROJECTION matrix
  793.     glMatrixMode(GL_PROJECTION);
  794.  
  795.     //initialize the matrix
  796.     glLoadIdentity();
  797.  
  798.     //give PERSPECTIVE parameters
  799.     gluPerspective(801110000.0);
  800.     //field of view in the Y (vertically)
  801.     //aspect ratio that determines the field of view in the X direction (horizontally)
  802.     //near distance
  803.     //far distance
  804. }
  805.  
  806. int main(int argc, char **argv){
  807.     glutInit(&argc,argv);
  808.     glutInitWindowSize(500, 500);
  809.     glutInitWindowPosition(0, 0);
  810.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);   //Depth, Double buffer, RGB color
  811.  
  812.     glutCreateWindow("Mega structure, 0905004");
  813.  
  814.     init();
  815.  
  816.     glEnable(GL_DEPTH_TEST);    //enable Depth Testing
  817.  
  818.     glutDisplayFunc(display);   //display callback function
  819.     glutIdleFunc(animate);      //what you want to do in the idle time (when no drawing is occuring)
  820.  
  821.     glutKeyboardFunc(keyboardListener);
  822.     glutSpecialFunc(specialKeyListener);
  823.     glutMouseFunc(mouseListener);
  824.  
  825.     glutMainLoop();     //The main loop of OpenGL
  826.  
  827.     return 0;
  828. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement