Don't like ads? PRO users don't see any ads ;-)
Guest

untitled

By: a guest on Apr 24th, 2012  |  syntax: C++  |  size: 6.83 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #pragma comment(lib, "opengl32.lib")
  2. #pragma comment(lib, "glu32.lib")
  3. #pragma comment(lib, "glut32.lib")
  4.  
  5. #include <ctime>
  6. #include <windows.h>
  7. #include <gl/gl.h>
  8. #include <gl/glut.h>
  9. #include <gl\GLU.h>
  10. #include <math.h>
  11.  
  12. /**
  13. Sets the logical coordinate system we will use to specify
  14. our drawings.
  15. */
  16. //////////////////////////////////////////////////////////
  17.  
  18. int xPositionDownMallet = 0 , yPositionDownMallet = -70;
  19. void processMouse( int x , int y)
  20. {
  21.        
  22.                 xPositionDownMallet =  x  % 85 ;
  23.                 yPositionDownMallet = -1 * y  % 85;
  24.                         glutPostRedisplay();
  25.        
  26.  
  27. }
  28.  
  29. void SetTransformations() {
  30.         //set up the logical coordinate system of the window: [-100, 100] x [-100, 100]
  31.         glMatrixMode(GL_PROJECTION);
  32.         glLoadIdentity();
  33.         gluOrtho2D(-100, 100, -100, 100);
  34. }
  35.  
  36.  
  37.  
  38. /**
  39. Handles the paint event. This event is triggered whenever
  40. our displayed graphics are lost or out-of-date.
  41. ALL rendering code should be written here.
  42. */
  43. void OnDisplay() {
  44.         //set the background color to white
  45.         glClearColor( 0.6 , 1 , 1 , 0);
  46.         //fill the whole color buffer with the clear color
  47.         glClear(GL_COLOR_BUFFER_BIT);
  48.         SetTransformations();
  49. //////////////////////////////////////////////////////////////
  50.                        // playground
  51.         glColor3d(1 , 1 , 1);
  52.         glBegin(GL_QUADS);
  53.         glVertex3d(85 , 90 ,0);
  54.         glVertex3d(-85 , 90 ,0);
  55.         glVertex3d(-85 , -90 ,0);
  56.         glVertex3d(85 , -90 ,0);
  57.         glEnd();
  58.         glRectd(-85 , -90 , 85 , 90);
  59. /////////////////////////////////////////////////////////      
  60.                 // outer frame of the playground
  61.         glColor3d(0 , 0 , 0);
  62.         glBegin(GL_LINE_LOOP);
  63.         glVertex3f( 0 , -90 , 0);
  64.         glVertex3f( 85 , -90 , 0);
  65.         glVertex3f( 85 , 90 , 0);
  66.         glVertex3f( 0 , 90 , 0);
  67.         glVertex3f(-85 , 90 , 0);
  68.         glVertex3f( -85 , -90 , 0);
  69.         glEnd();
  70. ///////////////////////////////////////////////////////
  71.                 // line at the middle
  72.         glBegin(GL_LINES);
  73.                 glColor3f(1, 0, 0);
  74.                 glVertex3f(-85,0, 0);
  75.                 glVertex3f(85, 0, 0);
  76.                 glEnd( );
  77.  
  78. ////////////////////////////////////////////////////////
  79.                 // Goals
  80.         glColor3d( 0 , 0 , 0);
  81.         glRectd(-20 , 90 , 20 , 100);
  82.         glRectd(-20 , -90 , 20 , -100);
  83.  
  84. ///////////////////////////////////////////////////////
  85.  
  86.                 // circle at the center of the playground
  87.         glColor3d( 0.2 , 0 , 0.6);
  88.         glBegin(GL_LINE_LOOP);
  89.        
  90.         for( double i = 45; i <= 360 ; i += 1)
  91.         {
  92.            glVertex3f(20*sin(i) , 20* cos(i) , 0);
  93.         }
  94.         glEnd();
  95.  
  96. /////////////////////////////////////////////////////////////////
  97.                 glBegin(GL_LINES);
  98.                 glColor3f(1, 0, 0);
  99.                 glVertex3f(-85,70, 0);
  100.                 glVertex3f(85, 70, 0);
  101.                 glEnd( );
  102. //////////////////////////////////////////////////////////////////
  103.                 // the first position of the mallets
  104.         glColor3d( 1 , 0 , 0.2);
  105.         glBegin(GL_LINE_LOOP);
  106.        
  107.         for( int i = 45; i <= 360 ; i += 1)
  108.         {
  109.            glVertex3f(12*sin(double(i)) , 12* cos(double(i))+70 ,0);
  110.         }
  111.         glEnd();
  112. /////////////////////////////////////////////////////////////////////////////
  113. /////////////////////////////////////////////////////////////////////////////`
  114.                 glBegin(GL_LINES);
  115.                 glColor3f(1, 0, 0);
  116.                 glVertex3f(-85,-70, 0);
  117.                 glVertex3f(85, -70, 0);
  118.                 glEnd( );
  119. //////////////////////////////////////////////////////////////////
  120.                 // the second position of the mallets
  121.         glBegin(GL_LINE_LOOP);
  122.        
  123.         for( int i = 45 ; i <= 360 ; i += 1)
  124.         {
  125.             glVertex3f(12*sin(double(i))  , 12* cos(double(i))-70 ,0);
  126.         }
  127.         glEnd();
  128. ////////////////////////////////////////////////////////////////////
  129.                 // the two mallets
  130.                 // the down mallet
  131.  
  132.                 glColor3d( 0 , 0.6 , 0.6);
  133.                 glBegin(GL_TRIANGLE_FAN);
  134.         glVertex3f(xPositionDownMallet , yPositionDownMallet , 0 );
  135.         for( int i = 0; i <= 360 ; i += 1)
  136.         {
  137.              glVertex3f(9*sin(double(i))+xPositionDownMallet  , 9* cos(double(i))+yPositionDownMallet , 0);
  138.         }
  139.         glEnd();
  140.                 glColor3d( 0.1 , 0 , 0);
  141.                 glBegin(GL_TRIANGLE_FAN);
  142.         glVertex3f(xPositionDownMallet , yPositionDownMallet , 0);
  143.         for( int i = 0; i <= 360 ; i += 1)
  144.         {
  145.              glVertex3f(4*sin(double(i))+xPositionDownMallet , 4* cos(double(i))+yPositionDownMallet , 0);
  146.         }
  147.         glEnd();
  148.                 ///////////////////////////////////////////////
  149.                 // the upper mallet
  150.                 glColor3d( 0 , 0.6 , 0.6);
  151.                 glBegin(GL_TRIANGLE_FAN);
  152.         glVertex3f(0 , 70 ,-1);
  153.         for( int i = 0; i <= 360 ; i += 1)
  154.         {
  155.              glVertex3f(9*sin(double(i))  , 9* cos(double(i))+70 , 0 );
  156.         }
  157.         glEnd();
  158.                 glColor3d( 0.1 , 0 , 0);
  159.                 glBegin(GL_TRIANGLE_FAN);
  160.         glVertex3f(0 , 70 ,-1);
  161.         for( int i = 0; i <= 360 ; i += 1)
  162.         {
  163.              glVertex3f(4*sin(double(i))  , 4* cos(double(i))+70 , 0 );
  164.         }
  165.         glEnd();
  166. ////////////////////////////////////////////////////////////////////
  167.                 // the puck
  168.      
  169.                 glColor3d( 0.5 , 0 , 0);
  170.         for( int i = 0; i <= 360; i++)
  171.                 {
  172.                 glRotatef(i, 0.0, 0.0, 1.0);
  173.                 }
  174.                 glBegin(GL_TRIANGLE_FAN);
  175.         glVertex3f(0 , 0 ,-1);
  176.         for( int i = 0; i <= 360 ; i += 1)
  177.         {
  178.             glVertex3f(7*sin(double(i)) , 7* cos(double(i)) ,0);
  179.         }
  180.         glEnd();
  181.                
  182.                
  183. //////////////////////////////////////////////////////////////////            
  184.        
  185.         //force previously issued OpenGL commands to begin
  186.         //execution
  187.         glFlush();
  188.                
  189. }
  190.  
  191.  
  192.  
  193. /**
  194. Creates the main window, registers event handlers, and
  195. initializes OpenGL stuff.
  196. */
  197. void InitGraphics(int argc, char *argv[]) {
  198.        
  199.         glutInit(&argc, argv);
  200.         glutInitDisplayMode(GLUT_RGBA);
  201.         //Create an 800x600 window with its top-left corner at pixel (100, 100)
  202.         glutInitWindowPosition(0, 0); //pass (-1, -1) for Window-Manager defaults
  203.         glutInitWindowSize(600, 800);
  204.         glutCreateWindow("OpenGL Lab");
  205.         //OnDisplay will handle the paint event
  206.         glutDisplayFunc(OnDisplay);
  207.                
  208.                 glutMotionFunc(processMouse);
  209.                 glutPassiveMotionFunc(processMouse);
  210.                        
  211.                         glutMainLoop();
  212.                
  213. }
  214.  
  215. int main(int argc, char* argv[])
  216. {
  217.         //glutInit(&argc, argv); // Initialize graphics
  218.         InitGraphics(argc, argv);
  219.                 return 0;
  220. }