Advertisement
naimularif

Graphics backup 2

Apr 27th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 27.61 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. void rods(void);
  25. ///
  26.  
  27. struct point
  28. {
  29.     double x,y,z;
  30. //  point(double a , double b, double c)
  31. //  {
  32. //      x=a;
  33. //      y=b;
  34. //      z=c;
  35. //  }
  36. };
  37.  
  38. void drawBase(float scale)
  39. {
  40.     float a=200;
  41.     glBegin(GL_POLYGON);
  42.     {glColor3f((float)248/255,(float)205/255,(float)156/255);
  43.         glVertex3f(-scale*a/2,0,0);
  44.         glVertex3f(-scale*a*1.5*cos(pi/6),scale*a*1.5*sin(pi/6),0);
  45.         glVertex3f(-scale*a*1.5*cos(pi/6)+scale*a/2*cos(pi*55/180),
  46.                     scale*a*1.5*sin(pi/6)+scale*a/2*sin(pi*55/180),0);
  47.         glVertex3f(-scale*a*1.5*cos(pi/6)+scale*a/2*cos(pi*55/180)+scale*a*cos(pi*70/180),
  48.                    scale*a*1.5*sin(pi/6)+scale*a/2*sin(pi*55/180)+scale*a*sin(pi*70/180),0);
  49.         glVertex3f(-(-scale*a*1.5*cos(pi/6)+scale*a/2*cos(pi*55/180)+scale*a*cos(pi*70/180)),
  50.                    scale*a*1.5*sin(pi/6)+scale*a/2*sin(pi*55/180)+scale*a*sin(pi*70/180),0);
  51.         glVertex3f(-(-scale*a*1.5*cos(pi/6)+scale*a/2*cos(pi*55/180)),
  52.                     scale*a*1.5*sin(pi/6)+scale*a/2*sin(pi*55/180),0);
  53.         glVertex3f(scale*a*1.5*cos(pi/6),scale*a*1.5*sin(pi/6),0);
  54.         glVertex3f(scale*a/2,0,0);
  55.  
  56.     }glEnd();
  57. }
  58.  
  59. void drawAxes()
  60. {
  61.     if(drawaxes==1)
  62.     {
  63.         glColor3f(1.0, 1.0, 1.0);
  64.         glBegin(GL_LINES);{
  65.             glVertex3f( 1000,0,0);
  66.             glVertex3f(-1000,0,0);
  67.             glVertex3f(0,-1000,0);
  68.             glVertex3f(0, 1000,0);
  69.             glVertex3f(0,0, 1000);
  70.             glVertex3f(0,0,-1000);
  71.         }glEnd();
  72.     }
  73. }
  74.  
  75. void drawCurvedCylinderBasic(float length, float angle, float radius)
  76. {
  77.  
  78.     float bigradius=length/sin(angle*pi/2/180);
  79.     float ang=-angle/2;
  80.     float height=length;
  81.     float stacks=50;
  82.     float slices=50;
  83.     struct point points[100][100];
  84.     int i,j;
  85.     double h,r;
  86.     for(i=0;i<=stacks;i++)
  87.     {
  88.         h=height*(double)i/(double)stacks;
  89.         r=radius;
  90.         for(j=0;j<=slices;j++)
  91.         {
  92.             points[i][j].x=r*cos(((double)j/(double)slices)*2*pi)
  93.                 +bigradius*cos(ang*pi/180)-bigradius;
  94.             points[i][j].y=r*sin(((double)j/(double)slices)*2*pi);
  95.             points[i][j].z=h; ///this creates the upper half
  96.         }
  97.         ang=ang+angle/stacks;
  98.     }
  99.     for(i=0;i<stacks;i++)
  100.     {
  101.         for(j=0;j<slices;j++)
  102.         {
  103.             glColor3f(200,200,200);
  104.             glBegin(GL_QUADS);{
  105.                 glVertex3f(points[i][j].x,points[i][j].y,points[i][j].z);
  106.                 glVertex3f(points[i][j+1].x,points[i][j+1].y,points[i][j+1].z);
  107.                 glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,points[i+1][j+1].z);
  108.                 glVertex3f(points[i+1][j].x,points[i+1][j].y,points[i+1][j].z);
  109.  
  110.             }glEnd();
  111.         }
  112.  
  113.     }
  114.  
  115. }
  116. void drawCurvedCylinder(float length, float angle, float radius)
  117. {
  118.     glPushMatrix();{
  119.         glRotatef(-90,1,0,0);
  120.         glRotatef(90,0,1,0);
  121.         glTranslatef(0,0,-length/2);
  122.         drawCurvedCylinderBasic(length,angle,radius);
  123.     }glPopMatrix();
  124. }
  125.  
  126.  
  127. void drawGrid()
  128. {
  129.     int i;
  130.     if(drawgrid==1)
  131.     {
  132.         glColor3f(0.6, 0.6, 0.6);   //grey
  133.         glBegin(GL_LINES);{
  134.             for(i=-8;i<=8;i++){
  135.  
  136.                 if(i==0)
  137.                     continue;   //SKIP the MAIN axes
  138.  
  139.                 //lines parallel to Y-axis
  140.                 glVertex3f(i*10, -90, 0);
  141.                 glVertex3f(i*10,  90, 0);
  142.  
  143.                 //lines parallel to X-axis
  144.                 glVertex3f(-90, i*10, 0);
  145.                 glVertex3f( 90, i*10, 0);
  146.             }
  147.         }glEnd();
  148.     }
  149. }
  150.  
  151.  
  152. void drawSquare(float a)
  153. {
  154.     glBegin(GL_QUADS);{
  155.         glVertex3f( a, a,2);
  156.         glVertex3f( a,-a,2);
  157.         glVertex3f(-a,-a,2);
  158.         glVertex3f(-a, a,2);
  159.     }glEnd();
  160. }
  161.  
  162. void drawBaseCircle(float angle, float radius, float thickness)
  163. {
  164.     point a;
  165.     vector<point> v1,v2;
  166.     for(float i=-angle/2;i<=angle/2;i++)
  167.     {
  168.         a.x=radius*sin(i*pi/180);
  169.         a.y=-radius*cos(i*pi/180);
  170.         a.z=0;
  171.         v1.push_back(a);
  172.     }
  173.     for(int i=0;i<v1.size()-1;i++)
  174.     {
  175.         glBegin(GL_QUADS);{
  176.             glVertex3f(v1[i].x,v1[i].y,v1[i].z);
  177.             glVertex3f(v1[i].x,v1[i].y,v1[i].z-thickness);
  178.             glVertex3f(v1[i+1].x,v1[i+1].y,v1[i+1].z-thickness);
  179.             glVertex3f(v1[i+1].x,v1[i+1].y,v1[i+1].z);
  180.         }glEnd();
  181.         glBegin(GL_QUADS);{
  182.             glVertex3f(0,0,0);
  183.             glVertex3f(v1[i].x,v1[i].y,v1[i].z);
  184.             glVertex3f(v1[i+1].x,v1[i+1].y,v1[i+1].z);
  185.             glVertex3f(0,0,0);
  186.         }glEnd();
  187.         glBegin(GL_QUADS);{
  188.             glVertex3f(0,0,0);
  189.             glVertex3f(v1[i].x,v1[i].y,v1[i].z-thickness);
  190.             glVertex3f(v1[i+1].x,v1[i+1].y,v1[i+1].z-thickness);
  191.             glVertex3f(0,0,0);
  192.         }glEnd();
  193.     }
  194.     glBegin(GL_QUADS);{
  195.         glVertex3f(0,0,0);
  196.         glVertex3f(radius*sin(angle/2*pi/180),-radius*cos(angle/2*pi/180),0);
  197.         glVertex3f(radius*sin(angle/2*pi/180),-radius*cos(angle/2*pi/180),-thickness);
  198.         glVertex3f(0,0,-thickness);
  199.     }glEnd();
  200.     glBegin(GL_QUADS);{
  201.         glVertex3f(0,0,0);
  202.         glVertex3f(radius*sin(-angle/2*pi/180),-radius*cos(-angle/2*pi/180),0);
  203.         glVertex3f(radius*sin(-angle/2*pi/180),-radius*cos(-angle/2*pi/180),-thickness);
  204.         glVertex3f(0,0,-thickness);
  205.     }glEnd();
  206. }
  207.  
  208. void triangularBasic(float first, float third, float width )
  209. {
  210.     glBegin(GL_QUADS);{
  211.         glColor3f(200,200,200);
  212.         glVertex3f(third/2,0,width);
  213.         glVertex3f(0,sqrt(first*first-third*third/4),width);
  214.         glVertex3f(0,sqrt(first*first-third*third/4),width);
  215.         glVertex3f(-third/2,0,width);
  216.     }glEnd();
  217.     glBegin(GL_QUADS);{glColor3f(200,200,200);
  218.         glVertex3f(third/2,0,width);
  219.         glVertex3f(third/2,0,0);
  220.         glVertex3f(0,sqrt(first*first-third*third/4),0);
  221.         glVertex3f(0,sqrt(first*first-third*third/4),width);
  222.     }glEnd();
  223.     glBegin(GL_QUADS);{glColor3f(200,200,200);
  224.         glVertex3f(-third/2,0,width);
  225.         glVertex3f(-third/2,0,0);
  226.         glVertex3f(0,sqrt(first*first-third*third/4),0);
  227.         glVertex3f(0,sqrt(first*first-third*third/4),width);
  228.     }glEnd();
  229. glBegin(GL_QUADS);{glColor3f(200,200,200);
  230.         glVertex3f(third/2,0,0);
  231.         glVertex3f(0,sqrt(first*first-third*third/4),0);
  232.         glVertex3f(0,sqrt(first*first-third*third/4),0);
  233.         glVertex3f(-third/2,0,0);
  234.     }glEnd();
  235.     glBegin(GL_QUADS);{glColor3f(200,200,200);
  236.         glVertex3f(third/2, 0,width);
  237.         glVertex3f(third/2, 0,0);
  238.         glVertex3f(-third/2, 0,0);
  239.         glVertex3f(-third/2, 0,width);
  240.     }glEnd();
  241. }
  242. void triangular(float first, float third, float width, float clip )
  243. {
  244.     glPushMatrix();{
  245.             double equ[4];
  246.  
  247.             equ[0] = 0; //0.x
  248.             equ[1] = -1;    //0.y
  249.             equ[2] = 0;//-1.z
  250.             equ[3] = clip;//30
  251.             glClipPlane(GL_CLIP_PLANE0,equ);
  252.  
  253.             glEnable(GL_CLIP_PLANE0);{
  254.                 triangularBasic(1000,70,10);
  255.             }glDisable(GL_CLIP_PLANE0);
  256.         }glPopMatrix();
  257.  
  258.     //triangularBasic( first,  third, width );
  259. }
  260.  
  261. void drawHexCap(float height, float side)
  262. {
  263.     glColor3f(200,200,200);
  264.     glBegin(GL_QUADS);{
  265.         glVertex3f(side, 0,0);
  266.         glVertex3f(side*cos(pi/3),side*cos(pi/6),0);
  267.         glVertex3f(0,0,height);
  268.         glVertex3f(0,0,height);
  269.     }glEnd();;
  270.  
  271.     glBegin(GL_QUADS);{
  272.         glVertex3f(side*cos(pi/3),side*cos(pi/6),0);
  273.         glVertex3f(-side*cos(pi/3),side*cos(pi/6),0);
  274.         glVertex3f(0,0,height);
  275.         glVertex3f(0,0,height);
  276.     }glEnd();
  277.  
  278.     glBegin(GL_QUADS);{
  279.         glVertex3f(-side*cos(pi/3),side*cos(pi/6),0);
  280.         glVertex3f(-side,0,0);
  281.         glVertex3f(0,0,height);
  282.         glVertex3f(0,0,height);
  283.     }glEnd();
  284.  
  285.     glBegin(GL_QUADS);{
  286.         glVertex3f(-side,0,0);
  287.         glVertex3f(-side*cos(pi/3),-side*cos(pi/6),0);
  288.         glVertex3f(0,0,height);
  289.         glVertex3f(0,0,height);
  290.     }glEnd();
  291.  
  292.     glBegin(GL_QUADS);{
  293.         glVertex3f(-side*cos(pi/3),-side*cos(pi/6),0);
  294.         glVertex3f(side*cos(pi/3),-side*cos(pi/6),0);
  295.         glVertex3f(0,0,height);
  296.         glVertex3f(0,0,height);
  297.     }glEnd();
  298.  
  299.  
  300.     glBegin(GL_QUADS);{
  301.         glVertex3f(side*cos(pi/3),-side*cos(pi/6),0);
  302.         glVertex3f(side,0,0);
  303.         glVertex3f(0,0,height);
  304.         glVertex3f(0,0,height);
  305.     }glEnd();
  306.  
  307. }
  308.  
  309. void drawRectBarBasic(float length, float side)
  310. {
  311.     glBegin(GL_QUADS);{
  312.         glColor3f((float)240/256,(float)248/256,(float)255/256);
  313.         glVertex3f(length/2,-side/2,side/2);
  314.         glVertex3f(length/2,-side/2,-side/2);
  315.         glVertex3f(-length/2,-side/2,-side/2);
  316.         glVertex3f(-length/2,-side/2,side/2);
  317.     }glEnd();
  318.  
  319. }
  320.  
  321. void drawRectBar(float length, float side)
  322. {
  323.     drawRectBarBasic(length,side);
  324.  
  325.     glPushMatrix();
  326.     {
  327.         glTranslatef(0,side,0);
  328.         drawRectBarBasic(length,side);
  329.     }glPopMatrix();
  330.     glPushMatrix();
  331.     {
  332.         glRotatef(90,1,0,0);
  333.         glColor3f(1,0,0);
  334.         drawRectBarBasic(length,side);
  335.     }glPopMatrix();
  336.     glPushMatrix();
  337.     {
  338.         glRotatef(-90,1,0,0);
  339.         glColor3f(1,0,0);
  340.         drawRectBarBasic(length,side);
  341.     }glPopMatrix();
  342. }
  343.  
  344. void HexColumnBasic(float length, float side)
  345. {
  346.     glBegin(GL_QUADS);{
  347.         glVertex3f(0,0,0);
  348.         glVertex3f(side,0,0);
  349.         glVertex3f(side,0,length);
  350.         glVertex3f(0,0,length);
  351.     }glEnd();;
  352. }
  353. void drawHexColumn(float length, float side)
  354. {
  355.     glColor3f(200,200,200);
  356.     glPushMatrix();{
  357.         glTranslated((float)side*cos(pi/3),-(float)side*cos(pi/6),0);
  358.  
  359.         //glTranslated(side,0,0);
  360.         glRotated(60,0,0,1);
  361.         HexColumnBasic(length,side);
  362.     }glPopMatrix();
  363.     glColor3f(200,200,200);
  364.     glPushMatrix();{
  365.         glTranslated(-side,0,0);
  366.         glRotated(60,0,0,1);
  367.         HexColumnBasic(length,side);
  368.     }glPopMatrix();
  369.     glColor3f(200,200,200);
  370.     glPushMatrix();{
  371.         glTranslated(-side/2,(float)side*cos(pi/6),0);
  372.         //glRotated(60,0,0,1);
  373.         HexColumnBasic(length,side);
  374.     }glPopMatrix();
  375.     glColor3f(200,200,200);
  376.     glPushMatrix();{
  377.         glTranslated(-side/2,-(float)side*cos(pi/6),0);
  378.         //glRotated(60,0,0,1);
  379.         HexColumnBasic(length,side);
  380.     }glPopMatrix();
  381.     glColor3f(200,200,200);
  382.     glPushMatrix();{
  383.         glTranslated(-side,0,0);
  384.  
  385.         //glTranslated(side,0,0);
  386.         glRotated(300,0,0,1);
  387.         HexColumnBasic(length,side);
  388.     }glPopMatrix();
  389.     glColor3f(200,200,200);
  390.     glPushMatrix();{
  391.         glTranslated(side/2,(float)side*cos(pi/6),0);
  392.  
  393.         //glTranslated(side,0,0);
  394.         glRotated(300,0,0,1);
  395.         HexColumnBasic(length,side);
  396.     }glPopMatrix();
  397. }
  398.  
  399. void drawCylinder(float height, float radius, int stacks, int slices)
  400. {
  401.  
  402.     struct point points[100][100];
  403.     int i,j;
  404.     double h,r;
  405.     for(i=0;i<=stacks;i++)
  406.     {
  407.         h=height*(double)i/(double)stacks/2;
  408.         r=radius;
  409.         for(j=0;j<=slices;j++)
  410.         {
  411.             points[i][j].x=r*cos(((double)j/(double)slices)*2*pi);
  412.             points[i][j].y=r*sin(((double)j/(double)slices)*2*pi);
  413.             points[i][j].z=h; ///this creates the upper half
  414.         }
  415.  
  416.     }
  417.     for(i=0;i<stacks;i++)
  418.     {
  419.         for(j=0;j<slices;j++)
  420.         {
  421.             glColor3f(200,200,200);
  422.             glBegin(GL_QUADS);{
  423.                 glVertex3f(points[i][j].x,points[i][j].y,points[i][j].z);
  424.                 glVertex3f(points[i][j+1].x,points[i][j+1].y,points[i][j+1].z);
  425.                 glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,points[i+1][j+1].z);
  426.                 glVertex3f(points[i+1][j].x,points[i+1][j].y,points[i+1][j].z);
  427.  
  428.             }glEnd();
  429.         }
  430.  
  431.     }
  432.  
  433.     for(i=0;i<=stacks;i++)
  434.     {
  435.         h=height*(double)i/(double)stacks/2;
  436.         r=radius;
  437.         for(j=0;j<=slices;j++)
  438.         {
  439.             points[i][j].x=r*cos(((double)j/(double)slices)*2*pi);
  440.             points[i][j].y=r*sin(((double)j/(double)slices)*2*pi);
  441.             points[i][j].z=-h; ///this creates the lower half
  442.         }
  443.  
  444.     }
  445.     for(i=0;i<stacks;i++)
  446.     {
  447.         for(j=0;j<slices;j++)
  448.         {
  449.             glColor3f(200,200,200);
  450.             glBegin(GL_QUADS);{
  451.                 glVertex3f(points[i][j].x,points[i][j].y,points[i][j].z);
  452.                 glVertex3f(points[i][j+1].x,points[i][j+1].y,points[i][j+1].z);
  453.                 glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,points[i+1][j+1].z);
  454.                 glVertex3f(points[i+1][j].x,points[i+1][j].y,points[i+1][j].z);
  455.  
  456.             }glEnd();
  457.         }
  458.  
  459.     }
  460. }
  461.  
  462. void twoColumn()
  463. {
  464.     float length=1014.73;
  465.     glPushMatrix();{
  466.         glTranslated(-105,0,0);
  467.         glRotated(6,0,1,0);
  468.         drawHexColumn(length,33.13);
  469.     }glPopMatrix();
  470.  
  471.     glPushMatrix();{
  472.         glTranslated(105,0,0);
  473.         glRotated(-6,0,1,0);
  474.         drawHexColumn(length,33.13);
  475.     }glPopMatrix();
  476.  
  477.     glPushMatrix(); ///strip
  478.     {
  479.         glTranslatef(0,0,100);
  480.         glRotatef(4,1,0,0);
  481.         float x,y,z,xa,ya,za;
  482.         x=150;
  483.         y=300;
  484.         z=600;
  485.         xa=90;
  486.         za=30;
  487.         glPushMatrix();{
  488.             glTranslatef(-x,y,z);
  489.             glRotatef(za,0,0,1);
  490.             glRotatef(90,1,0,0);
  491.             drawCircularStrip();
  492.         }glPopMatrix();
  493.         glPushMatrix();{
  494.             glTranslatef(x,y,z);
  495.             glRotatef(-za,0,0,1);
  496.             glRotatef(90,1,0,0);
  497.             drawCircularStrip();
  498.         }glPopMatrix();
  499.     }glPopMatrix();
  500.  
  501.     glPushMatrix();{
  502.         glTranslatef(0,0,250);
  503.         drawRectBar(200,20);
  504.     }glPopMatrix();
  505.     glPushMatrix();{
  506.         glTranslatef(0,0,400);
  507.         drawRectBar(150,20);
  508.     }glPopMatrix();
  509.     glPushMatrix();{
  510.         glTranslatef(0,0,550);
  511.         drawRectBar(100,20);
  512.     }glPopMatrix();
  513.     glPushMatrix();{
  514.         glTranslatef(0,0,650);
  515.         drawRectBar(100,20);
  516.     }glPopMatrix();
  517.  
  518.     rods();
  519.     //drawCurvedCylinder(1000, 10, 10);
  520.     glPushMatrix();///extension of strips
  521.     {
  522.         glTranslatef(-100,170,-20);///////////
  523.         glRotatef(30,1.9,1,.47);
  524.         glRotatef(120,0,0,1);
  525.         glRotatef(90,1,0,0);
  526.         triangular(1000,70,10,500);//////////////
  527.     }glPopMatrix();
  528.  
  529.   //}glPopMatrix();
  530.  
  531.     glPushMatrix();
  532.     {
  533.         glScalef(-1, 1, 1);
  534.         glPushMatrix();
  535.         {
  536.             glTranslatef(-100,170,-20);///////////
  537.             glRotatef(30,1.9,1,.47);
  538.             glRotatef(120,0,0,1);
  539.             glRotatef(90,1,0,0);
  540.             triangular(1000,70,10,500);//////////////
  541.         }glPopMatrix();
  542.     }glPopMatrix();
  543.  
  544.     //drawBase();
  545.  
  546. }
  547.  
  548. void drawCircularStrip()
  549. {
  550.     vector<point> v1,v2,v3,v4;
  551.     double radius1,radius2,width;
  552.     radius1=200;
  553.     radius2=190;
  554.     width=10;
  555.     for(int i=-45;i<52;i++)
  556.     {
  557.         float scale=.45;
  558.         point a;
  559.         a.x=-width/2;
  560.         a.y=scale*6*radius1*sin(pi*i/180);
  561.         a.z=scale*5*radius1*cos(pi*i/180);
  562.         v1.push_back(a);
  563.         a.x=width/2;
  564.         a.y=scale*6*radius1*sin(pi*i/180);
  565.         a.z=scale*5*radius1*cos(pi*i/180);
  566.         v2.push_back(a);
  567.  
  568.         a.x=-width/2;
  569.         a.y=scale*5.5*radius2*sin(pi*i/180);
  570.         a.z=scale*5*radius2*cos(pi*i/180);
  571.         v3.push_back(a);
  572.         a.x=width/2;
  573.         a.y=scale*5.5*radius2*sin(pi*i/180);
  574.         a.z=scale*5*radius2*cos(pi*i/180);
  575.         v4.push_back(a);
  576.     }
  577.     for(int i=0;i<v1.size()-1;i++)
  578.     {
  579.         glBegin(GL_QUADS);{
  580.             glColor3d((float)200/256,(float)200/256,(float)200/256);
  581.             glVertex3f(v1[i].x,v1[i].y,v1[i].z);
  582.             glVertex3f(v1[i+1].x,v1[i+1].y,v1[i+1].z);
  583.             glVertex3f(v2[i+1].x,v2[i+1].y,v2[i+1].z);
  584.             glVertex3f(v2[i].x,v2[i].y,v2[i].z);
  585.         }glEnd();
  586.  
  587.         glBegin(GL_QUADS);{
  588.             glColor3d((float)200/256,(float)200/256,(float)200/256);
  589.             glVertex3f(v3[i].x,v3[i].y,v3[i].z);
  590.             glVertex3f(v3[i+1].x,v3[i+1].y,v3[i+1].z);
  591.             glVertex3f(v4[i+1].x,v4[i+1].y,v4[i+1].z);
  592.             glVertex3f(v4[i].x,v4[i].y,v4[i].z);
  593.         }glEnd();
  594.             ///right side
  595.             glBegin(GL_QUADS);{
  596.             glColor3d((float)200/256,(float)200/256,(float)200/256);
  597.             glVertex3f(v1[i].x,v1[i].y,v1[i].z);
  598.             glVertex3f(v1[i+1].x,v1[i+1].y,v1[i+1].z);
  599.             glVertex3f(v3[i+1].x,v3[i+1].y,v3[i+1].z);
  600.             glVertex3f(v3[i].x,v3[i].y,v3[i].z);
  601.         }glEnd();
  602.             ///left side
  603.             glBegin(GL_QUADS);{
  604.             glColor3d((float)200/256,(float)200/256,(float)200/256);
  605.             glVertex3f(v2[i+1].x,v2[i+1].y,v2[i+1].z);
  606.             glVertex3f(v2[i].x,v2[i].y,v2[i].z);
  607.             glVertex3f(v4[i].x,v4[i].y,v4[i].z);
  608.             glVertex3f(v4[i+1].x,v4[i+1].y,v4[i+1].z);
  609.         }glEnd();
  610.     }
  611. }
  612.  
  613. void rods()
  614. {///curved rods
  615.     glPushMatrix();{
  616.         glTranslatef(0,-47,400);
  617.         drawCurvedCylinder(60,50,5);
  618.     }glPopMatrix();
  619.  
  620.  
  621.     glPushMatrix();{
  622.         glTranslatef(0,-99,490);
  623.         drawCurvedCylinder(89,50,5);
  624.     }glPopMatrix();
  625.  
  626.  
  627.     glPushMatrix();{
  628.         glTranslatef(0,-135,550);
  629.         drawCurvedCylinder(128,50,5);
  630.     }glPopMatrix();
  631.  
  632.  
  633.     glPushMatrix();{
  634.         glTranslatef(0,-145,610);
  635.         drawCurvedCylinder(145,50,5);
  636.     }glPopMatrix();
  637.  
  638.  
  639.     glPushMatrix();{
  640.         glTranslatef(0,-145,660);
  641.         drawCurvedCylinder(145,50,5);
  642.     }glPopMatrix();
  643.  
  644.  
  645.     glPushMatrix();{
  646.         glTranslatef(0,-155,710);
  647.         drawCurvedCylinder(140,50,5);
  648.     }glPopMatrix();
  649.  
  650.  
  651.     glPushMatrix();{
  652.         glTranslatef(0,-155,760);
  653.         drawCurvedCylinder(140,50,5);
  654.     }glPopMatrix();
  655.  
  656.  
  657.     glPushMatrix();{
  658.         glTranslatef(0,-155,810);
  659.         drawCurvedCylinder(140,50,5);
  660.     }glPopMatrix();
  661.  
  662.     glPushMatrix();{
  663.         glTranslatef(0,-140,860);
  664.         drawCurvedCylinder(130,50,5);
  665.     }glPopMatrix();
  666.  
  667.  
  668.     glPushMatrix();{
  669.         glTranslatef(0,-125,910);
  670.         drawCurvedCylinder(110,50,5);
  671.     }glPopMatrix();
  672.  
  673.     glPushMatrix();{
  674.         glTranslatef(0,-110,950);
  675.         drawCurvedCylinder(95,50,5);
  676.     }glPopMatrix();
  677.  
  678.  
  679.     glPushMatrix();{
  680.         glTranslatef(0,-95,995);
  681.         drawCurvedCylinder(70,50,5);
  682.     }glPopMatrix();
  683.  
  684.     glPushMatrix();{
  685.         glTranslatef(0,-75,1019);
  686.         drawCurvedCylinder(50,50,5);
  687.     }glPopMatrix();
  688.  
  689.     ///straight rods
  690.     glPushMatrix();{
  691.         glTranslatef(45,-30,490);
  692.         glRotatef(90,1,0,0);
  693.         drawCylinder(100,5,20,20);
  694.     }glPopMatrix();
  695.     glPushMatrix();{
  696.         glTranslatef(-45,-30,490);
  697.         glRotatef(90,1,0,0);
  698.         drawCylinder(100,5,20,20);
  699.     }glPopMatrix();
  700.  
  701.     glPushMatrix();{
  702.         glTranslatef(64,-30,550);
  703.         glRotatef(90,1,0,0);
  704.         drawCylinder(110,5,20,20);
  705.     }glPopMatrix();
  706.     glPushMatrix();{
  707.         glTranslatef(-64,-30,550);
  708.         glRotatef(90,1,0,0);
  709.         drawCylinder(110,5,20,20);
  710.     }glPopMatrix();
  711.  
  712.     glPushMatrix();{
  713.         glTranslatef(60,-50,610);
  714.         glRotatef(10,0,0,1);
  715.         glRotatef(90,1,0,0);
  716.         drawCylinder(110,5,20,20);
  717.     }glPopMatrix();
  718.     glPushMatrix();{
  719.         glTranslatef(-60,-50,610);
  720.         glRotatef(-10,0,0,1);
  721.         glRotatef(90,1,0,0);
  722.         drawCylinder(110,5,20,20);
  723.     }glPopMatrix();
  724.  
  725.     glPushMatrix();{
  726.         glTranslatef(60,-50,660);
  727.         glRotatef(10,0,0,1);
  728.         glRotatef(90,1,0,0);
  729.         drawCylinder(120,5,20,20);
  730.     }glPopMatrix();
  731.     glPushMatrix();{
  732.         glTranslatef(-60,-50,660);
  733.         glRotatef(-10,0,0,1);
  734.         glRotatef(90,1,0,0);
  735.         drawCylinder(120,5,20,20);
  736.     }glPopMatrix();
  737.  
  738.     glPushMatrix();{
  739.         glTranslatef(60,-50,710);
  740.         glRotatef(10,0,0,1);
  741.         glRotatef(90,1,0,0);
  742.         drawCylinder(125,5,20,20);
  743.     }glPopMatrix();
  744.     glPushMatrix();{
  745.         glTranslatef(-60,-50,710);
  746.         glRotatef(-10,0,0,1);
  747.         glRotatef(90,1,0,0);
  748.         drawCylinder(125,5,20,20);
  749.     }glPopMatrix();
  750.  
  751.     glPushMatrix();{
  752.         glTranslatef(60,-50,760);
  753.         glRotatef(10,0,0,1);
  754.         glRotatef(90,1,0,0);
  755.         drawCylinder(125,5,20,20);
  756.     }glPopMatrix();
  757.     glPushMatrix();{
  758.         glTranslatef(-60,-50,760);
  759.         glRotatef(-10,0,0,1);
  760.         glRotatef(90,1,0,0);
  761.         drawCylinder(125,5,20,20);
  762.     }glPopMatrix();
  763.  
  764.     glPushMatrix();{
  765.         glTranslatef(53,-50,810);
  766.         glRotatef(10,0,0,1);
  767.         glRotatef(90,1,0,0);
  768.         drawCylinder(125,5,20,20);
  769.     }glPopMatrix();
  770.     glPushMatrix();{
  771.         glTranslatef(-53,-50,810);
  772.         glRotatef(-10,0,0,1);
  773.         glRotatef(90,1,0,0);
  774.         drawCylinder(125,5,20,20);
  775.     }glPopMatrix();
  776.  
  777.     glPushMatrix();{
  778.         glTranslatef(50,-50,860);
  779.         glRotatef(12,0,0,1);
  780.         glRotatef(90,1,0,0);
  781.         drawCylinder(128,5,20,20);
  782.     }glPopMatrix();
  783.     glPushMatrix();{
  784.         glTranslatef(-50,-50,860);
  785.         glRotatef(-12,0,0,1);
  786.         glRotatef(90,1,0,0);
  787.         drawCylinder(128,5,20,20);
  788.     }glPopMatrix();
  789.  
  790.     glPushMatrix();{
  791.         glTranslatef(40,-50,910);
  792.         glRotatef(12,0,0,1);
  793.         glRotatef(90,1,0,0);
  794.         drawCylinder(120,5,20,20);
  795.     }glPopMatrix();
  796.     glPushMatrix();{
  797.         glTranslatef(-40,-50,910);
  798.         glRotatef(-12,0,0,1);
  799.         glRotatef(90,1,0,0);
  800.         drawCylinder(120,5,20,20);
  801.     }glPopMatrix();
  802.  
  803.     glPushMatrix();{
  804.         glTranslatef(37,-50,950);
  805.         glRotatef(12,0,0,1);
  806.         glRotatef(90,1,0,0);
  807.         drawCylinder(102,5,20,20);
  808.     }glPopMatrix();
  809.     glPushMatrix();{
  810.         glTranslatef(-37,-50,950);
  811.         glRotatef(-12,0,0,1);
  812.         glRotatef(90,1,0,0);
  813.         drawCylinder(102,5,20,20);
  814.     }glPopMatrix();
  815.  
  816.     glPushMatrix();{
  817.         glTranslatef(30,-50,995);
  818.         glRotatef(12,0,0,1);
  819.         glRotatef(90,1,0,0);
  820.         drawCylinder(70,5,20,20);
  821.     }glPopMatrix();
  822.     glPushMatrix();{
  823.         glTranslatef(-30,-50,995);
  824.         glRotatef(-12,0,0,1);
  825.         glRotatef(90,1,0,0);
  826.         drawCylinder(70,5,20,20);
  827.     }glPopMatrix();
  828.  
  829.     glPushMatrix();{
  830.         glTranslatef(20,-50,1019);
  831.         glRotatef(12,0,0,1);
  832.         glRotatef(90,1,0,0);
  833.         drawCylinder(50,5,20,20);
  834.     }glPopMatrix();
  835.     glPushMatrix();{
  836.         glTranslatef(-20,-50,1019);
  837.         glRotatef(-12,0,0,1);
  838.         glRotatef(90,1,0,0);
  839.         drawCylinder(50,5,20,20);
  840.     }glPopMatrix();
  841. }
  842.  
  843. void drawCurveColumns(){
  844.     double ang = 50;
  845.     glPushMatrix();{
  846.         glTranslated(0.5,0,0);
  847.         glRotated(-ang,0,0,1);
  848.         glPushMatrix();{
  849.             glTranslated(0,-12,13.5);
  850.             glRotated(-37,1,0,0);
  851.             //drawRectBar(1.25,0.4,1.25,15,55);
  852.         };glPopMatrix();
  853.  
  854.  
  855.         glTranslated(0,2,82);
  856.         glRotated(-18,1,0,0);
  857.         glTranslated(0,-4.5,-37);
  858. //      drawColumnStrip(1.25,22);
  859.     }glPopMatrix();
  860.  
  861. }
  862.  
  863. void drawss()
  864. {
  865.     glPushMatrix();{
  866.         glTranslated(900,0,0);
  867.     drawsphere(20,20,20);}glPopMatrix();
  868.  
  869.     twoColumn();
  870.     glPushMatrix();{
  871.         glTranslatef(0,0,1014);
  872.         drawHexCap(300,33.13);
  873.     }glPopMatrix();
  874.  
  875.     glPushMatrix();{
  876.         glTranslatef(0,-150,0);
  877.         drawBase(.75);
  878.     }glPopMatrix();
  879.     glPushMatrix();{
  880.         glTranslatef(0,-150,-20);
  881.         drawBase(.75);
  882.     }glPopMatrix();
  883.  
  884.     glPushMatrix();{
  885.         glRotatef(180,0,0,1);
  886.         drawBaseCircle(58, 300, 20);
  887.     }glPopMatrix();
  888.  
  889.     //drawCurvedCylinder(1000,50,2);
  890. }
  891.  
  892. //draws half sphere
  893. void drawsphere(float radius,int slices,int stacks)
  894. {
  895.     struct point points[100][100];
  896.     int i,j;
  897.     double h,r;
  898.     for(i=0;i<=stacks;i++)
  899.     {
  900.         h=radius*sin(((double)i/(double)stacks)*(pi/2));
  901.         r=sqrt(radius*radius-h*h);
  902.         for(j=0;j<=slices;j++)
  903.         {
  904.             points[i][j].x=r*cos(((double)j/(double)slices)*2*pi);
  905.             points[i][j].y=r*sin(((double)j/(double)slices)*2*pi);
  906.             points[i][j].z=h; ///this creates the upper half
  907.         }
  908.  
  909.     }
  910.     for(i=0;i<stacks;i++)
  911.     {
  912.         for(j=0;j<slices;j++)
  913.         {
  914.             glColor3f((double)i/(double)stacks,(double)i/(double)stacks,(double)i/(double)stacks);
  915.             glBegin(GL_QUADS);{
  916.                 glVertex3f(points[i][j].x,points[i][j].y,points[i][j].z);
  917.                 glVertex3f(points[i][j+1].x,points[i][j+1].y,points[i][j+1].z);
  918.                 glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,points[i+1][j+1].z);
  919.                 glVertex3f(points[i+1][j].x,points[i+1][j].y,points[i+1][j].z);
  920.  
  921.             }glEnd();
  922.         }
  923.  
  924.     }
  925.  
  926.         for(i=0;i<=stacks;i++)
  927.     {
  928.         h=radius*sin(((double)i/(double)stacks)*(pi/2));
  929.         r=sqrt(radius*radius-h*h);
  930.         for(j=0;j<=slices;j++)
  931.         {
  932.             points[i][j].x=r*cos(((double)j/(double)slices)*2*pi);
  933.             points[i][j].y=r*sin(((double)j/(double)slices)*2*pi);
  934.             points[i][j].z=-h; ///this creates the lower half
  935.         }
  936.  
  937.     }
  938.     for(i=0;i<stacks;i++)
  939.     {
  940.         for(j=0;j<slices;j++)
  941.         {
  942.             glColor3f((double)i/(double)stacks,(double)i/(double)stacks,(double)i/(double)stacks);
  943.             glBegin(GL_QUADS);{
  944.                 glVertex3f(points[i][j].x,points[i][j].y,points[i][j].z);
  945.                 glVertex3f(points[i][j+1].x,points[i][j+1].y,points[i][j+1].z);
  946.                 glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,points[i+1][j+1].z);
  947.                 glVertex3f(points[i+1][j].x,points[i+1][j].y,points[i+1][j].z);
  948.  
  949.             }glEnd();
  950.         }
  951.  
  952.     }
  953. }
  954.  
  955.  
  956. void keyboardListener(unsigned char key, int x,int y){
  957.     switch(key){
  958.  
  959.         case '1':
  960.             drawgrid=1-drawgrid;
  961.             break;
  962.  
  963.         default:
  964.             break;
  965.     }
  966. }
  967.  
  968.  
  969. void specialKeyListener(int key, int x,int y){
  970.     switch(key){
  971.         case GLUT_KEY_DOWN:     //down arrow key
  972.             cameraHeight -= 30.0;
  973.             break;
  974.         case GLUT_KEY_UP:       // up arrow key
  975.             cameraHeight += 30.0;
  976.             break;
  977.  
  978.         case GLUT_KEY_RIGHT:
  979.             cameraAngle += 0.03;
  980.             break;
  981.         case GLUT_KEY_LEFT:
  982.             cameraAngle -= 0.03;
  983.             break;
  984.  
  985.         case GLUT_KEY_PAGE_UP:
  986.             break;
  987.         case GLUT_KEY_PAGE_DOWN:
  988.             break;
  989.         case GLUT_KEY_INSERT:
  990.             break;
  991.  
  992.         case GLUT_KEY_HOME:
  993.             break;
  994.         case GLUT_KEY_END:
  995.             break;
  996.  
  997.         default:
  998.             break;
  999.     }
  1000. }
  1001.  
  1002.  
  1003. void mouseListener(int button, int state, int x, int y){    //x, y is the x-y of the screen (2D)
  1004.     switch(button){
  1005.         case GLUT_LEFT_BUTTON:
  1006.             if(state == GLUT_DOWN){     // 2 times?? in ONE click? -- solution is checking DOWN or UP
  1007.                 drawaxes=1-drawaxes;
  1008.             }
  1009.             break;
  1010.  
  1011.         case GLUT_RIGHT_BUTTON:
  1012.             //........
  1013.             break;
  1014.  
  1015.         case GLUT_MIDDLE_BUTTON:
  1016.             //........
  1017.             break;
  1018.  
  1019.         default:
  1020.             break;
  1021.     }
  1022. }
  1023.  
  1024.  
  1025.  
  1026. void display(){
  1027.  
  1028.     //clear the display
  1029.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1030.     glClearColor(0,0,0,0);  //color black
  1031.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1032.  
  1033.     /********************
  1034.     / set-up camera here
  1035.     ********************/
  1036.     //load the correct matrix -- MODEL-VIEW matrix
  1037.     glMatrixMode(GL_MODELVIEW);
  1038.  
  1039.     //initialize the matrix
  1040.     glLoadIdentity();
  1041.  
  1042.     //now give three info
  1043.     //1. where is the camera (viewer)?
  1044.     //2. where is the camera looking?
  1045.     //3. Which direction is the camera's UP direction?
  1046.  
  1047.     //gluLookAt(100,100,100,    0,0,0,  0,0,1);
  1048.     gluLookAt(500*cos(cameraAngle), 500*sin(cameraAngle), cameraHeight,     0,0,0,      0,0,1);
  1049.     //gluLookAt(0,-1,150,   0,0,0,  0,0,1);
  1050.  
  1051.  
  1052.     //again select MODEL-VIEW
  1053.     glMatrixMode(GL_MODELVIEW);
  1054.  
  1055.  
  1056.     /****************************
  1057.     / Add your objects from here
  1058.     ****************************/
  1059.     //add objects
  1060.  
  1061.     drawAxes();
  1062.     drawGrid();
  1063.  
  1064.     drawss();
  1065.  
  1066.     //ADD this line in the end --- if you use double buffer (i.e. GL_DOUBLE)
  1067.     glutSwapBuffers();
  1068. }
  1069.  
  1070. void animate(){
  1071.     angle+=0.05;
  1072.     //codes for any changes in Models, Camera
  1073.     glutPostRedisplay();
  1074. }
  1075.  
  1076. void init(){
  1077.     //codes for initialization
  1078.     drawgrid=0;
  1079.     drawaxes=1;
  1080.     cameraHeight=1000.0;
  1081.     cameraAngle=1.0;
  1082.     angle=0;
  1083.  
  1084.     //clear the screen
  1085.     glClearColor(0,0,0,0);
  1086.  
  1087.     /************************
  1088.     / set-up projection here
  1089.     ************************/
  1090.     //load the PROJECTION matrix
  1091.     glMatrixMode(GL_PROJECTION);
  1092.  
  1093.     //initialize the matrix
  1094.     glLoadIdentity();
  1095.  
  1096.     //give PERSPECTIVE parameters
  1097.     gluPerspective(801110000.0);
  1098.     //field of view in the Y (vertically)
  1099.     //aspect ratio that determines the field of view in the X direction (horizontally)
  1100.     //near distance
  1101.     //far distance
  1102. }
  1103.  
  1104. int main(int argc, char **argv){
  1105.     glutInit(&argc,argv);
  1106.     glutInitWindowSize(500, 500);
  1107.     glutInitWindowPosition(0, 0);
  1108.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);   //Depth, Double buffer, RGB color
  1109.  
  1110.     glutCreateWindow("Mega structure, 0905004");
  1111.  
  1112.     init();
  1113.  
  1114.     glEnable(GL_DEPTH_TEST);    //enable Depth Testing
  1115.  
  1116.     glutDisplayFunc(display);   //display callback function
  1117.     glutIdleFunc(animate);      //what you want to do in the idle time (when no drawing is occuring)
  1118.  
  1119.     glutKeyboardFunc(keyboardListener);
  1120.     glutSpecialFunc(specialKeyListener);
  1121.     glutMouseFunc(mouseListener);
  1122.  
  1123.     glutMainLoop();     //The main loop of OpenGL
  1124.  
  1125.     return 0;
  1126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement