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();
- // }
- //}
- //
- //float ctrlPoints[6][3]={
- // 0,-0,0,
- // -5,-2,1,
- // -7,3,-3,
- // 7,3,2,
- // 4,-1,0,
- // 0,-5,0
- // };
- //
- //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
- //
- //glMap1f(GL_MAP1_VERTEX_3, //do krzywych
- // 0, //poczatek
- // 1, //koniec
- // 3, //odl pomiedzy kolejnymi wsp punktow kontrolnych
- // 6, //punkty kontrolne brane pod uwage podczas robienia krzywej
- // &ctrlPoints[0][0]); //odwolanie
- // glEnable(GL_MAP1_VERTEX_3);
- // glMapGrid1f(20,0,1); //generowanie siatku punktow
- // glEvalMesh1(GL_LINE,0,20);
- //
- //
- //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;
- //}
- #include <windows.h>
- #include <GL/glut.h>
- //#include <math.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;
- float ctrlPoint[3][4][3]= {
- {{-2,-2,0}, //u0 //v0
- {-1,-2,0}, //u1
- {1,-2,0},
- {2,-2,0}}, //u3
- {{-2,-2,3}, //u0 //v1
- {-1,2,3}, //u1
- {1,2,3},
- {2,-2,3}}, //u3
- {{-2,-2,6}, //u0 //v2
- {-1,-2,6}, //u1
- {1,-2,6},
- {2,-2,6}} //u3
- };
- 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( 1.0f, 1.0f, 1.0f, 1.0f );
- glMatrixMode( GL_PROJECTION ) ;
- glLoadIdentity();
- gluPerspective(60,w/h,0.1,100);
- //glOrtho( -2.0, 2.0, -2.0, 2.0, -2.0, 2.0 );
- 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;
- m_button_y = y;
- }
- }
- }
- void MouseMotion( int x, int y)
- {
- if (left_button_state == GLUT_DOWN)
- {
- 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 );
- l_button_y = y;
- glutPostRedisplay();
- }
- if (middle_button_state == GLUT_DOWN)
- {
- r += 0.01 * (ffar - nnear) * (y - mr_button);
- 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);
- //glutWireCube(1);
- glColor3f(1,0,0);
- glMap2f(GL_MAP2_VERTEX_3,
- 0.0f, //początek przedziału u
- 1.0f, //koniec przedziału u
- 3, //odległość wsp. pktctrl
- 4, //max liczba pkt w danym zbiorze pkt (parametr u)
- 0.0f, //początek przedziału v
- 1.0f, //koniec przedziału v
- 12, //odległość wsp. pktctrl
- 3, //max liczba pkt w danym zbiorze pkt (parametr v)
- &ctrlPoint[0][0][0]);
- glEnable(GL_MAP2_VERTEX_3);
- glMapGrid2f(20,0,1,20,0,1);
- glEvalMesh2(GL_LINE,0,20,0,20);
- 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