Advertisement
Soham_K

wheel.cpp

Jun 29th, 2022
688
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4.  
  5. #include <windows.h>
  6. #include <GL/glut.h>
  7.  
  8. #define pi (2*acos(0.0))
  9.  
  10. double cameraHeight;
  11. double cameraAngle;
  12. int drawgrid;
  13. int drawaxes;
  14. double angle;
  15. double dis;
  16. double rotangle;
  17. double centerotangle;
  18. double wheelradius;
  19.  
  20. struct point
  21. {
  22.     double x,y,z;
  23. };
  24.  
  25. void assignVal(struct point *p, double x, double y, double z)
  26. {
  27.     p->x = x;
  28.     p->y = y;
  29.     p->z = z;
  30. }
  31.  
  32. struct point pos;
  33.  
  34. void drawAxes()
  35. {
  36.     if(drawaxes==1)
  37.     {
  38.         glColor3f(1.0, 1.0, 1.0);
  39.         glBegin(GL_LINES);{
  40.             glVertex3f( 100,0,0);
  41.             glVertex3f(-100,0,0);
  42.  
  43.             glVertex3f(0,-100,0);
  44.             glVertex3f(0, 100,0);
  45.  
  46.             glVertex3f(0,0, 100);
  47.             glVertex3f(0,0,-100);
  48.         }glEnd();
  49.     }
  50. }
  51.  
  52.  
  53. void drawGrid()
  54. {
  55.     int i;
  56.     if(drawgrid==1)
  57.     {
  58.         glColor3f(0.6, 0.6, 0.6);   //grey
  59.         glBegin(GL_LINES);{
  60.             for(i=-20;i<=20;i++){
  61.  
  62.                 //if(i==0)
  63.                     //continue; //SKIP the MAIN axes
  64.  
  65.                 //lines parallel to Y-axis
  66.                 glVertex3f(i*10, -200, 0);
  67.                 glVertex3f(i*10,  200, 0);
  68.  
  69.                 //lines parallel to X-axis
  70.                 glVertex3f(-200, i*10, 0);
  71.                 glVertex3f( 200, i*10, 0);
  72.             }
  73.         }glEnd();
  74.     }
  75. }
  76.  
  77. void drawSquare(double a)
  78. {
  79.     //glColor3f(1.0,0.0,0.0);
  80.     glBegin(GL_QUADS);{
  81.         glVertex3f( a, a,2);
  82.         glVertex3f( a,-a,2);
  83.         glVertex3f(-a,-a,2);
  84.         glVertex3f(-a, a,2);
  85.     }glEnd();
  86. }
  87.  
  88.  
  89. void drawCircle(double radius,int segments)
  90. {
  91.     int i;
  92.     struct point points[100];
  93.     glColor3f(0.7,0.7,0.7);
  94.     //generate points
  95.     for(i=0;i<=segments;i++)
  96.     {
  97.         points[i].x=radius*cos(((double)i/(double)segments)*2*pi);
  98.         points[i].y=radius*sin(((double)i/(double)segments)*2*pi);
  99.     }
  100.     //draw segments using generated points
  101.     for(i=0;i<segments;i++)
  102.     {
  103.         glBegin(GL_LINES);
  104.         {
  105.             glVertex3f(points[i].x,points[i].y,0);
  106.             glVertex3f(points[i+1].x,points[i+1].y,0);
  107.         }
  108.         glEnd();
  109.     }
  110. }
  111.  
  112. void drawCone(double radius,double height,int segments)
  113. {
  114.     int i;
  115.     double shade;
  116.     struct point points[100];
  117.     //generate points
  118.     for(i=0;i<=segments;i++)
  119.     {
  120.         points[i].x=radius*cos(((double)i/(double)segments)*2*pi);
  121.         points[i].y=radius*sin(((double)i/(double)segments)*2*pi);
  122.     }
  123.     //draw triangles using generated points
  124.     for(i=0;i<segments;i++)
  125.     {
  126.         //create shading effect
  127.         if(i<segments/2)shade=2*(double)i/(double)segments;
  128.         else shade=2*(1.0-(double)i/(double)segments);
  129.         glColor3f(shade,shade,shade);
  130.  
  131.         glBegin(GL_TRIANGLES);
  132.         {
  133.             glVertex3f(0,0,height);
  134.             glVertex3f(points[i].x,points[i].y,0);
  135.             glVertex3f(points[i+1].x,points[i+1].y,0);
  136.         }
  137.         glEnd();
  138.     }
  139. }
  140.  
  141.  
  142. void drawSphere(double radius,int slices,int stacks)
  143. {
  144.     struct point points[100][100];
  145.     int i,j;
  146.     double h,r;
  147.     //generate points
  148.     for(i=0;i<=stacks;i++)
  149.     {
  150.         h=radius*sin(((double)i/(double)stacks)*(pi/2));
  151.         r=radius*cos(((double)i/(double)stacks)*(pi/2));
  152.         for(j=0;j<=slices;j++)
  153.         {
  154.             points[i][j].x=r*cos(((double)j/(double)slices)*2*pi);
  155.             points[i][j].y=r*sin(((double)j/(double)slices)*2*pi);
  156.             points[i][j].z=h;
  157.         }
  158.     }
  159.     //draw quads using generated points
  160.     for(i=0;i<stacks;i++)
  161.     {
  162.         glColor3f((double)i/(double)stacks,(double)i/(double)stacks,(double)i/(double)stacks);
  163.         for(j=0;j<slices;j++)
  164.         {
  165.             glBegin(GL_QUADS);{
  166.                 //upper hemisphere
  167.                 glVertex3f(points[i][j].x,points[i][j].y,points[i][j].z);
  168.                 glVertex3f(points[i][j+1].x,points[i][j+1].y,points[i][j+1].z);
  169.                 glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,points[i+1][j+1].z);
  170.                 glVertex3f(points[i+1][j].x,points[i+1][j].y,points[i+1][j].z);
  171.                 //lower hemisphere
  172.                 glVertex3f(points[i][j].x,points[i][j].y,-points[i][j].z);
  173.                 glVertex3f(points[i][j+1].x,points[i][j+1].y,-points[i][j+1].z);
  174.                 glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,-points[i+1][j+1].z);
  175.                 glVertex3f(points[i+1][j].x,points[i+1][j].y,-points[i+1][j].z);
  176.             }glEnd();
  177.         }
  178.     }
  179. }
  180.  
  181.  
  182. void drawRectangle(double p)
  183. {
  184.     glColor3f(160/(double)255, 160/(double)255, 160/(double)255);
  185.     glBegin(GL_QUADS);
  186.     {
  187.         glVertex3f(0, p, wheelradius);
  188.         glVertex3f(0, -p, wheelradius);
  189.         glVertex3f(0, -p, -wheelradius);
  190.         glVertex3f(0, p, -wheelradius);
  191.     }
  192.     glEnd();
  193. }
  194.  
  195.  
  196. void drawRim(double width)
  197. {
  198.     struct point points[500];
  199.     int segment = 499;
  200.     double h = width;
  201.     for(int i=0; i<=segment; i++) {
  202.         points[i].x = wheelradius*cos((double)i/(double)segment * (pi/2));
  203.         points[i].y = h;
  204.         points[i].z = wheelradius*sin((double)i/(double)segment * (pi/2));
  205.     }
  206.     for(int j=0; j<segment; j++)
  207.     {
  208.         glColor3f(j/(double)segment, j/(double)segment, j/(double)segment);
  209.         glBegin(GL_QUADS);{
  210.             //1st quadrant
  211.             glVertex3f(points[j].x, -points[j].y, points[j].z);
  212.             glVertex3f(points[j+1].x, -points[j+1].y, points[j+1].z);
  213.             glVertex3f(points[j+1].x, points[j+1].y, points[j+1].z);
  214.             glVertex3f(points[j].x, points[j].y, points[j].z);
  215.  
  216.             //2nd
  217.             glVertex3f(-points[j].x, -points[j].y, points[j].z);
  218.             glVertex3f(-points[j+1].x, -points[j+1].y, points[j+1].z);
  219.             glVertex3f(-points[j+1].x, points[j+1].y, points[j+1].z);
  220.             glVertex3f(-points[j].x, points[j].y, points[j].z);
  221.  
  222.             //3rd
  223.             glVertex3f(-points[j].x, -points[j].y, -points[j].z);
  224.             glVertex3f(-points[j+1].x, -points[j+1].y, -points[j+1].z);
  225.             glVertex3f(-points[j+1].x, points[j+1].y, -points[j+1].z);
  226.             glVertex3f(-points[j].x, points[j].y, -points[j].z);
  227.  
  228.             //4th
  229.             glVertex3f(points[j].x, -points[j].y, -points[j].z);
  230.             glVertex3f(points[j+1].x, -points[j+1].y, -points[j+1].z);
  231.             glVertex3f(points[j+1].x, points[j+1].y, -points[j+1].z);
  232.             glVertex3f(points[j].x, points[j].y, -points[j].z);
  233.         }glEnd();
  234.     }
  235. }
  236.  
  237. void drawWheel(double halfwidth)
  238. {
  239.     glPushMatrix();
  240.     {
  241.         drawRim(halfwidth);
  242.     }
  243.     glPopMatrix();
  244.  
  245.     glPushMatrix();
  246.     {
  247.         drawRectangle(halfwidth);
  248.     }
  249.     glPopMatrix();
  250.  
  251.  
  252.     glPushMatrix();
  253.     {
  254.         glRotatef(-90, 0, 1, 0);
  255.         drawRectangle(halfwidth);
  256.     }
  257.     glPopMatrix();
  258. }
  259.  
  260. void moveforward()
  261. {
  262.     pos.x = pos.x - dis*cos((rotangle)*pi/(double)180);
  263.     pos.y = pos.y - dis*sin((rotangle)*pi/(double)180);
  264. }
  265.  
  266. void movebackward()
  267. {
  268.     pos.x = pos.x + dis*cos((rotangle)*pi/(double)180);
  269.     pos.y = pos.y + dis*sin((rotangle)*pi/(double)180);
  270. }
  271.  
  272. void wheelmovement(double halfwidth)
  273. {
  274.     glPushMatrix();
  275.     {
  276.         glTranslatef(0, 0, wheelradius);    // positioning the wheel on the xy axis
  277.         glTranslatef(pos.x, pos.y, pos.z);  // forward/backward movement
  278.         glRotatef(rotangle, 0, 0, 1);       // wheel rotation a/d
  279.         glRotatef(centerotangle, 0, 1, 0); // wheel rotation while moving forward or backward
  280.         drawWheel(halfwidth);
  281.     }
  282.     glPopMatrix();
  283. }
  284.  
  285. void drawSS()
  286. {
  287.     glColor3f(1,0,0);
  288.     drawSquare(20);
  289.  
  290.     glRotatef(angle,0,0,1);
  291.     glTranslatef(110,0,0);
  292.     glRotatef(2*angle,0,0,1);
  293.     glColor3f(0,1,0);
  294.     drawSquare(15);
  295.  
  296.     glPushMatrix();
  297.     {
  298.         glRotatef(angle,0,0,1);
  299.         glTranslatef(60,0,0);
  300.         glRotatef(2*angle,0,0,1);
  301.         glColor3f(0,0,1);
  302.         drawSquare(10);
  303.     }
  304.     glPopMatrix();
  305.  
  306.     glRotatef(3*angle,0,0,1);
  307.     glTranslatef(40,0,0);
  308.     glRotatef(4*angle,0,0,1);
  309.     glColor3f(1,1,0);
  310.     drawSquare(5);
  311. }
  312.  
  313. void keyboardListener(unsigned char key, int x,int y){
  314.     switch(key){
  315.  
  316.         case 'w':
  317.             moveforward();
  318.             centerotangle -= dis/(2*pi*wheelradius) * 360;
  319.             break;
  320.         case 's':
  321.             movebackward();
  322.             centerotangle += dis/(2*pi*wheelradius) * 360;
  323.             break;
  324.         case 'a':
  325.             rotangle+=2;
  326.             break;
  327.         case 'd':
  328.             rotangle-=2;
  329.             break;
  330.         default:
  331.             break;
  332.     }
  333. }
  334.  
  335.  
  336. void specialKeyListener(int key, int x,int y){
  337.     switch(key){
  338.         case GLUT_KEY_DOWN:     //down arrow key
  339.             cameraHeight -= 3.0;
  340.             break;
  341.         case GLUT_KEY_UP:       // up arrow key
  342.             cameraHeight += 3.0;
  343.             break;
  344.  
  345.         case GLUT_KEY_RIGHT:
  346.             cameraAngle += 0.03;
  347.             break;
  348.         case GLUT_KEY_LEFT:
  349.             cameraAngle -= 0.03;
  350.             break;
  351.  
  352.         case GLUT_KEY_PAGE_UP:
  353.             break;
  354.         case GLUT_KEY_PAGE_DOWN:
  355.             break;
  356.  
  357.         case GLUT_KEY_INSERT:
  358.             break;
  359.  
  360.         case GLUT_KEY_HOME:
  361.             break;
  362.         case GLUT_KEY_END:
  363.             break;
  364.  
  365.         default:
  366.             break;
  367.     }
  368. }
  369.  
  370.  
  371. void mouseListener(int button, int state, int x, int y){    //x, y is the x-y of the screen (2D)
  372.     switch(button){
  373.         case GLUT_LEFT_BUTTON:
  374.             if(state == GLUT_DOWN){     // 2 times?? in ONE click? -- solution is checking DOWN or UP
  375.                 drawaxes=1-drawaxes;
  376.             }
  377.             break;
  378.  
  379.         case GLUT_RIGHT_BUTTON:
  380.             //........
  381.             break;
  382.  
  383.         case GLUT_MIDDLE_BUTTON:
  384.             //........
  385.             break;
  386.  
  387.         default:
  388.             break;
  389.     }
  390. }
  391.  
  392.  
  393.  
  394. void display(){
  395.  
  396.     //clear the display
  397.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  398.     glClearColor(0,0,0,0);  //color black
  399.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  400.  
  401.     /********************
  402.     / set-up camera here
  403.     ********************/
  404.     //load the correct matrix -- MODEL-VIEW matrix
  405.     glMatrixMode(GL_MODELVIEW);
  406.  
  407.     //initialize the matrix
  408.     glLoadIdentity();
  409.  
  410.     //now give three info
  411.     //1. where is the camera (viewer)?
  412.     //2. where is the camera looking?
  413.     //3. Which direction is the camera's UP direction?
  414.  
  415.     //gluLookAt(100,100,100,    0,0,0,  0,0,1);
  416.     gluLookAt(200*cos(cameraAngle), 200*sin(cameraAngle), cameraHeight,     0,0,0,      0,0,1);
  417.     //gluLookAt(0,0,200,    0,0,0,  0,1,0);
  418.  
  419.  
  420.     //again select MODEL-VIEW
  421.     glMatrixMode(GL_MODELVIEW);
  422.  
  423.  
  424.     /****************************
  425.     / Add your objects from here
  426.     ****************************/
  427.     //add objects
  428.  
  429.     drawAxes();
  430.     drawGrid();
  431.  
  432.     //glColor3f(1,0,0);
  433.     //drawSquare(10);
  434.  
  435.     wheelmovement(5);
  436.     //drawSS();
  437.  
  438.     //drawCircle(30,24);
  439.  
  440.     //drawCone(20,50,24);
  441.  
  442.     //drawSphere(30,24,20);
  443.  
  444.  
  445.  
  446.  
  447.     //ADD this line in the end --- if you use double buffer (i.e. GL_DOUBLE)
  448.     glutSwapBuffers();
  449. }
  450.  
  451.  
  452. void animate(){
  453.     angle+=0.05;
  454.     //codes for any changes in Models, Camera
  455.     glutPostRedisplay();
  456. }
  457.  
  458. void init(){
  459.     //codes for initialization
  460.     drawgrid=1;
  461.     drawaxes=0;
  462.     cameraHeight=150.0;
  463.     cameraAngle=1.0;
  464.     angle=0;
  465.  
  466.     rotangle=0;
  467.     dis=2;
  468.     centerotangle=0;
  469.     wheelradius=30;
  470.  
  471.     assignVal(&pos, 0, 0, 0);
  472.  
  473.     //clear the screen
  474.     glClearColor(0,0,0,0);
  475.  
  476.     /************************
  477.     / set-up projection here
  478.     ************************/
  479.     //load the PROJECTION matrix
  480.     glMatrixMode(GL_PROJECTION);
  481.  
  482.     //initialize the matrix
  483.     glLoadIdentity();
  484.  
  485.     //give PERSPECTIVE parameters
  486.     gluPerspective(80111000.0);
  487.     //field of view in the Y (vertically)
  488.     //aspect ratio that determines the field of view in the X direction (horizontally)
  489.     //near distance
  490.     //far distance
  491. }
  492.  
  493. int main(int argc, char **argv){
  494.     glutInit(&argc,argv);
  495.     glutInitWindowSize(600, 600);
  496.     glutInitWindowPosition(0, 0);
  497.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);   //Depth, Double buffer, RGB color
  498.  
  499.     glutCreateWindow("My OpenGL Program");
  500.  
  501.     init();
  502.  
  503.     glEnable(GL_DEPTH_TEST);    //enable Depth Testing
  504.  
  505.     glutDisplayFunc(display);   //display callback function
  506.     glutIdleFunc(animate);      //what you want to do in the idle time (when no drawing is occuring)
  507.  
  508.     glutKeyboardFunc(keyboardListener);
  509.     glutSpecialFunc(specialKeyListener);
  510.     glutMouseFunc(mouseListener);
  511.  
  512.     glutMainLoop();     //The main loop of OpenGL
  513.  
  514.     return 0;
  515. }
  516.  
Advertisement
RAW Paste Data Copied
Advertisement