Guest User

Untitled

a guest
Mar 14th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.68 KB | None | 0 0
  1. #define WINDOWS
  2.  
  3. #ifdef WINDOWS
  4. #include <Windows.h>
  5. #include <gl/GL.h>
  6. #include <gl/GLU.h>
  7. #include <gl/glut.h>
  8. #include <iostream>
  9. #endif
  10.  
  11. #ifdef LINUX
  12. #include <GL/glut.h>
  13. #endif
  14.  
  15. #ifdef MAC
  16. #include <OpenGL/gl.h>      
  17. #include <OpenGL/glu.h>    
  18. #include <GLUT/glut.h>      
  19. #endif
  20.  
  21. using namespace std;
  22.  
  23. bool done = false;
  24.  
  25. float wRot;
  26. float pRot;
  27. float sRot;
  28. float cRot, cRot2;
  29.  
  30. float wTran;
  31. float pTran;
  32. float p2Tran;
  33. float sTran;
  34. float cTran;
  35.  
  36. void drawCrank()
  37. {
  38.     // back horizontal
  39.     glPushMatrix();
  40.     glRotated(90,0,1,0);
  41.     glScaled(0.08, 0.02, 0.02);
  42.     glTranslated(.5 , 0, 0 + cTran);
  43.     //glRotated(cRot,1,0,0);
  44.     glutSolidCube(1.0);
  45.     glPopMatrix();
  46.  
  47.     // back vertical
  48.     glPushMatrix();
  49.     glRotated(90,0,1,0);
  50.     glScaled(0.02, 0.16, 0.02);
  51.     glTranslated(3.5 , .5, 0+ cTran);
  52.     //glRotated(cRot,1,0,0);
  53.     glutSolidCube(1.0);
  54.     glPopMatrix();
  55.  
  56.     //front horizontal
  57.     glPushMatrix();
  58.     glRotated(90, 0, 1, 0);
  59.     glScaled(0.08, 0.02, 0.02);
  60.     glTranslated(-.5 , 0, 0+ cTran);
  61.     //glRotated(cRot,0,0,0);
  62.     glutSolidCube(1.0);
  63.     glPopMatrix();
  64.  
  65.     // front vertical
  66.     glPushMatrix();
  67.     glRotated(90,0,1,0);
  68.     //glRotated(cRot2,1,0,0);
  69.     glScaled(0.02, 0.16, 0.02);
  70.     glTranslated(-3.5 , -.5, 0 + cTran);
  71.     //glRotated(cRot,1,0,0);
  72.     glutSolidCube(1.0);
  73.     glPopMatrix();
  74.  
  75. }
  76. void drawOneSpoke()
  77. {
  78.     glPushMatrix();
  79.     glRotated(sRot, 0, 0, 1);
  80.     glScaled(.4,0.005,0.005);
  81.     glutSolidCube(1.0);
  82.     glPopMatrix();
  83. }
  84.  
  85. void drawSpokes()
  86. {
  87.     glPushMatrix();
  88.     glTranslated(sTran, 0, 0);
  89.     drawOneSpoke();
  90.     glRotated(45,0,0,1);
  91.     drawOneSpoke();
  92.     glRotated(45,0,0,1);
  93.     drawOneSpoke();
  94.     glRotated(45,0,0,1);
  95.     drawOneSpoke();
  96.     glPopMatrix();
  97. }
  98.  
  99. void drawWheel()
  100. {
  101.     glPushMatrix();
  102.     glScaled(0.1, 0.1, 0.1);
  103.     glTranslated(wTran, 0, 0);
  104.     glRotated(wRot, 0, 0, 1);
  105.     glutSolidTorus(.3, 2.0, 25, 25);
  106.     glPopMatrix();
  107. }
  108.  
  109. void DisplaySolid()
  110. {
  111.     if (done == false)
  112.     {
  113.         wTran = 0;
  114.         sTran = 0;
  115.         pTran = 0;
  116.         p2Tran = -1.5;
  117.         cTran = 0;
  118.         cRot2 = 0;
  119.     }
  120.  
  121.  
  122.     // set properties of the surface material
  123.     GLfloat mat_ambient[]={0.7f, 0.7f, 0.7f, 1.0f}; // gray
  124.     GLfloat mat_diffuse[]={0.6f, 0.6f, 0.6f, 1.0f};
  125.     GLfloat mat_specular[]={1.0f, 1.0f, 1.0f, 1.0f};
  126.     GLfloat mat_shininess[]={50.0f};
  127.    
  128.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  129.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  130.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  131.     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  132.    
  133.     // set the light source properties
  134.     GLfloat lightIntensity[] = {0.7f, 0.7f, 0.7f, 1.0f};
  135.     GLfloat light_position[] = {2.0f, 6.0f, 3.0f, 0.0f};
  136.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  137.     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightIntensity);
  138.    
  139.     // set the world window and the camera
  140.     glMatrixMode(GL_PROJECTION);
  141.     glLoadIdentity();
  142.     double winHt = 1.0; // half-height of the window
  143.     //glOrtho(-winHt*64/48.0, winHt*64/48.0, -winHt, winHt, 0.1, 100.0);
  144.     glOrtho(-.45, winHt/2, -winHt/2, winHt/2, 0.1, 100.0);
  145.     glMatrixMode(GL_MODELVIEW);
  146.     glLoadIdentity();
  147.     //gluLookAt(2.3, 1.3, 2, 0, 0.25, 0, 0.0, 1.0, 0.0);
  148.     gluLookAt(.5, 0.7, 0.6, 0, 0, 0, 0.0, 1.0, 0.0);
  149.  
  150.     // start drawing
  151.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  152.  
  153.     // pedal
  154.     glPushMatrix();
  155.     glRotated(90, 0,1,0);
  156.     glScaled(0.08, 0.04, 0.04);
  157.     glTranslated(1.5, 4, 0 + pTran);
  158.     glutSolidCube(1.0);
  159.     glPopMatrix();
  160.  
  161.     glPushMatrix();
  162.     glRotated(90, 0,1,0);
  163.     glScaled(0.08, 0.04, 0.04);
  164.     glTranslated(-1.5, -4, 0 + pTran);
  165.     glutSolidCube(1.0);
  166.     glPopMatrix();
  167.  
  168.     //wheel
  169.  
  170.  
  171.     drawWheel();
  172.  
  173.     drawSpokes();
  174.     drawCrank();
  175.  
  176.     glFlush();
  177.     glutSwapBuffers();
  178.  
  179. }
  180.    
  181. void animate(int frame)
  182. {
  183.     if (frame < 30)
  184.     {
  185.         wTran += .1;
  186.         sTran += .01;
  187.         pTran += .25;
  188.         cTran += .5;
  189.         sRot -= 5;
  190.         wRot -= 5;
  191.         cRot -= 1;
  192.         cRot2 -= 10;
  193.     }
  194.     glutPostRedisplay();
  195.    
  196.     glutTimerFunc(33, animate, 1);
  197.  
  198. }
  199.  
  200.  
  201. void Keyboard (unsigned char key, int x, int y)
  202. {
  203.     // if ESC is pressed, exit
  204.     if (key == 'a' || key == 'A')
  205.     {
  206.         done = true;
  207.         cout << "a pressed" << endl;
  208.         glutTimerFunc(33, animate, 1);
  209.     }
  210.     if (key == 27)
  211.         exit(0);
  212. }
  213.  
  214.  
  215. int main(int argc, char **argv)
  216. {
  217.     glutInit(&argc, argv);
  218.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  219.     glutInitWindowSize(640, 480);
  220.     glutInitWindowPosition(100, 100);
  221.     glutCreateWindow("Shaded example - 3D scene");
  222.     glutDisplayFunc(DisplaySolid);
  223.    
  224.     glutKeyboardFunc(Keyboard);
  225.  
  226.     glEnable(GL_LIGHTING);  //  enable the light source
  227.     glEnable(GL_LIGHT0);
  228.     glShadeModel(GL_SMOOTH);
  229.     glEnable(GL_DEPTH_TEST); // for hidden surface removal
  230.     glEnable(GL_NORMALIZE);  // normalize vectors for proper shading
  231.     glClearColor(0.1f, 0.1f, 0.1f, 0.0f); //background is light gray
  232.     glViewport(0, 0, 640, 480);
  233.    
  234.     glutMainLoop();
  235. }
Add Comment
Please, Sign In to add comment