Advertisement
rakoczyn

[OpenGL] Template

Apr 19th, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #include <windows.h>
  2. #include <GL/glut.h>
  3. #include <cmath>
  4. #define M_PI 3.14159265358979323846
  5.  
  6. float w=600,h=600;
  7. float alfa=0,beta=0;
  8. float x,y,z;
  9. float r=5;
  10. float poz1=0,poz2=0;
  11.  
  12. const GLdouble left = -5.0;
  13. const GLdouble right = 5.0;
  14. const GLdouble bottom = -5.0;
  15. const GLdouble top = 5.0;
  16. const GLdouble nnear = -5.0;
  17. const GLdouble ffar = 5.0;
  18.  
  19. int right_button_state = GLUT_UP ;
  20. int middle_button_state = GLUT_UP ;
  21. int left_button_state = GLUT_UP ;
  22.  
  23. int l_button_x, l_button_y, m_button_x, m_button_y, mr_button;
  24.  
  25.  
  26. void Keyboard( unsigned char key, int x, int y )
  27. {
  28.     if ( key == 27 )
  29.         exit( EXIT_SUCCESS );
  30.         switch(key)
  31.         {
  32.         case 'w':
  33.                 alfa-=5;
  34.                 break;
  35.         case 's':
  36.                 alfa+=5;
  37.                 break;
  38.         case 'a':
  39.                 beta-=5;
  40.                 break;
  41.         case 'd':
  42.                 beta+=5;
  43.                 break;
  44.         case 'z':
  45.                 r+=0.1;
  46.                 break;
  47.         case 'x':
  48.                 r-=0.1;
  49.                 break;
  50.         case 'e':
  51.                 poz1+=0.1;
  52.                 break;
  53.         case 'q':
  54.                 poz1-=0.1;
  55.                 break;
  56.         }
  57. }
  58. void SpecialKeys(int key, int x, int y)
  59. {
  60.         switch(key)
  61.         {
  62.         case GLUT_KEY_UP:
  63.                 alfa+=1;
  64.                 break;
  65.         }
  66. }
  67.  
  68. void Init()
  69. {
  70.     glClearColor( 0.1f, 0.1f, 0.1f, 1.0f );
  71.     glMatrixMode( GL_PROJECTION ) ;
  72.     glLoadIdentity();
  73.         gluPerspective(60,w/h,0.1,100);
  74.         glMatrixMode(GL_MODELVIEW);
  75.         glLoadIdentity();
  76.         glEnable(GL_DEPTH_TEST);
  77.        
  78.     glutKeyboardFunc( Keyboard );
  79. }
  80.  
  81. void Camera()
  82. {
  83.         x=-r*cos(alfa*M_PI/180)*sin(beta*M_PI/180);
  84.         y=r*sin(alfa*M_PI/180);
  85.         z=r*cos(alfa*M_PI/180)*cos(beta*M_PI/180);
  86. }
  87.  
  88. void MouseButton( int button, int state, int x, int y)
  89. {
  90.         if (button == GLUT_LEFT_BUTTON)
  91.     {
  92.         left_button_state = state;
  93.         if (state == GLUT_DOWN)
  94.         {
  95.             l_button_x = x;
  96.                         l_button_y = y;
  97.         }
  98.     }
  99.  
  100.  
  101.     if (button == GLUT_MIDDLE_BUTTON)
  102.     {
  103.         middle_button_state = state;
  104.         if (state == GLUT_DOWN)
  105.         {
  106.                         mr_button = y;
  107.         }
  108.     }
  109.  
  110.         if (button == GLUT_RIGHT_BUTTON)
  111.     {
  112.                 right_button_state = state;
  113.         if (state == GLUT_DOWN)
  114.         {
  115.             m_button_x = x; // by nie wracal do punktu wyjscia
  116.                         m_button_y = y;
  117.         }
  118.     }
  119.  
  120. }
  121.  
  122. void MouseMotion( int x, int y)//te zmiennie ile razy obroci sie kosta przy przejechaniu mysza przez pole obiektu.
  123. {
  124.     if (left_button_state == GLUT_DOWN)  // zmiana kata
  125.     {
  126.         beta += 50 *(top - bottom) / glutGet( GLUT_WINDOW_WIDTH ) *( x - l_button_x );
  127.                 l_button_x = x;
  128.                 alfa += 50 *(right - left) / glutGet( GLUT_WINDOW_HEIGHT) *( y - l_button_y );  //normalizacja okna, zachowanie proporcji
  129.                 l_button_y = y;
  130.  
  131.                 glutPostRedisplay();
  132.     }
  133.  
  134.         if (middle_button_state == GLUT_DOWN)
  135.     {
  136.         r += 0.01 * (ffar - nnear) * (y - mr_button); // zmiana promienia
  137.         mr_button = y;
  138.                 if (r<1)
  139.                         r=1;
  140.         glutPostRedisplay();
  141.     }
  142.  
  143.         if (right_button_state == GLUT_DOWN)
  144.     {
  145.         poz1 += 2 *(right - left) / glutGet( GLUT_WINDOW_WIDTH ) *( x - m_button_x );
  146.                 m_button_x = x;
  147.                 poz2 += 2 *(top - bottom) / glutGet( GLUT_WINDOW_HEIGHT ) *( m_button_y - y );
  148.                 m_button_y = y;
  149.  
  150.                 glutPostRedisplay();
  151.     }
  152. }
  153.  
  154. void Render()
  155. {
  156.        
  157. glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  158.  
  159. Camera();
  160. gluLookAt(x+poz1,y,z,poz1,poz2,0,0,cos(alfa*M_PI/180),0); //zmiana polozenia kamer
  161.        
  162. // CODE HERE
  163.  
  164. glLoadIdentity();
  165. glutSwapBuffers();
  166. }
  167.  
  168. void Idle()
  169. {
  170.     Render();
  171. }
  172.  
  173. int main( int argc, char **argv )
  174. {
  175.         glutInitDisplayMode( GLUT_DOUBLE);
  176.         glutInitWindowSize(w,h);
  177.         glutInitWindowPosition(50,50);
  178.         glutCreateWindow( "OpenGL" );
  179.     Init();
  180.         glutIdleFunc( Idle);
  181.     glutDisplayFunc( Render );
  182.         glutMouseFunc(MouseButton);
  183.         glutMotionFunc(MouseMotion);
  184.         glutSpecialFunc(SpecialKeys);
  185.     glutMainLoop();
  186.     return EXIT_SUCCESS;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement