Advertisement
Soham_K

shape.cpp

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