Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <GL/glut.h>
- #include <cmath>
- #define M_PI 3.14159265358979323846
- float w=600,h=600;
- float alfa=0,beta=0;
- float x,y,z;
- float r=5;
- float poz1=0,poz2=0;
- const GLdouble left = -5.0;
- const GLdouble right = 5.0;
- const GLdouble bottom = -5.0;
- const GLdouble top = 5.0;
- const GLdouble nnear = -5.0;
- const GLdouble ffar = 5.0;
- int right_button_state = GLUT_UP ;
- int middle_button_state = GLUT_UP ;
- int left_button_state = GLUT_UP ;
- int l_button_x, l_button_y, m_button_x, m_button_y, mr_button;
- void Keyboard( unsigned char key, int x, int y )
- {
- if ( key == 27 )
- exit( EXIT_SUCCESS );
- switch(key)
- {
- case 'w':
- alfa-=5;
- break;
- case 's':
- alfa+=5;
- break;
- case 'a':
- beta-=5;
- break;
- case 'd':
- beta+=5;
- break;
- case 'z':
- r+=0.1;
- break;
- case 'x':
- r-=0.1;
- break;
- case 'e':
- poz1+=0.1;
- break;
- case 'q':
- poz1-=0.1;
- break;
- }
- }
- void SpecialKeys(int key, int x, int y)
- {
- switch(key)
- {
- case GLUT_KEY_UP:
- alfa+=1;
- break;
- }
- }
- void Init()
- {
- glClearColor( 0.1f, 0.1f, 0.1f, 1.0f );
- glMatrixMode( GL_PROJECTION ) ;
- glLoadIdentity();
- gluPerspective(60,w/h,0.1,100);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glEnable(GL_DEPTH_TEST);
- glutKeyboardFunc( Keyboard );
- }
- void Camera()
- {
- x=-r*cos(alfa*M_PI/180)*sin(beta*M_PI/180);
- y=r*sin(alfa*M_PI/180);
- z=r*cos(alfa*M_PI/180)*cos(beta*M_PI/180);
- }
- void MouseButton( int button, int state, int x, int y)
- {
- if (button == GLUT_LEFT_BUTTON)
- {
- left_button_state = state;
- if (state == GLUT_DOWN)
- {
- l_button_x = x;
- l_button_y = y;
- }
- }
- if (button == GLUT_MIDDLE_BUTTON)
- {
- middle_button_state = state;
- if (state == GLUT_DOWN)
- {
- mr_button = y;
- }
- }
- if (button == GLUT_RIGHT_BUTTON)
- {
- right_button_state = state;
- if (state == GLUT_DOWN)
- {
- m_button_x = x; // by nie wracal do punktu wyjscia
- m_button_y = y;
- }
- }
- }
- void MouseMotion( int x, int y)//te zmiennie ile razy obroci sie kosta przy przejechaniu mysza przez pole obiektu.
- {
- if (left_button_state == GLUT_DOWN) // zmiana kata
- {
- beta += 50 *(top - bottom) / glutGet( GLUT_WINDOW_WIDTH ) *( x - l_button_x );
- l_button_x = x;
- alfa += 50 *(right - left) / glutGet( GLUT_WINDOW_HEIGHT) *( y - l_button_y ); //normalizacja okna, zachowanie proporcji
- l_button_y = y;
- glutPostRedisplay();
- }
- if (middle_button_state == GLUT_DOWN)
- {
- r += 0.01 * (ffar - nnear) * (y - mr_button); // zmiana promienia
- mr_button = y;
- if (r<1)
- r=1;
- glutPostRedisplay();
- }
- if (right_button_state == GLUT_DOWN)
- {
- poz1 += 2 *(right - left) / glutGet( GLUT_WINDOW_WIDTH ) *( x - m_button_x );
- m_button_x = x;
- poz2 += 2 *(top - bottom) / glutGet( GLUT_WINDOW_HEIGHT ) *( m_button_y - y );
- m_button_y = y;
- glutPostRedisplay();
- }
- }
- void Render()
- {
- glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- Camera();
- gluLookAt(x+poz1,y,z,poz1,poz2,0,0,cos(alfa*M_PI/180),0); //zmiana polozenia kamer
- // CODE HERE
- glLoadIdentity();
- glutSwapBuffers();
- }
- void Idle()
- {
- Render();
- }
- int main( int argc, char **argv )
- {
- glutInitDisplayMode( GLUT_DOUBLE);
- glutInitWindowSize(w,h);
- glutInitWindowPosition(50,50);
- glutCreateWindow( "OpenGL" );
- Init();
- glutIdleFunc( Idle);
- glutDisplayFunc( Render );
- glutMouseFunc(MouseButton);
- glutMotionFunc(MouseMotion);
- glutSpecialFunc(SpecialKeys);
- glutMainLoop();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement